template_show.php 7.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
include("template_defs.php");

#
11
# Only known and logged in users ...
12 13 14 15 16
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
17 18 19
# Need these below, set in CheckArguments.
$pid = "";
$eid = "";
20
$gid = "";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
21

22 23
function CheckArguments($guid, $version) {
    global $TB_EXPT_READINFO;
24 25 26 27 28
    global $uid, $pid, $eid, $gid;

    #
    # Verify page arguments.
    # 
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    if (!isset($guid) ||
	strcmp($guid, "") == 0) {
	USERERROR("You must provide a template GUID.", 1);
    }
    
    if (!isset($version) ||
	strcmp($version, "") == 0) {
	USERERROR("You must provide a template version number", 1);
    }
    if (!TBvalid_guid($guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($version)) {
	PAGEARGERROR("Invalid characters in version!");
    }
    
45 46 47
    #
    # Check to make sure this is a valid template.
    #
48 49 50 51 52
    if (! TBValidExperimentTemplate($guid, $version)) {
	USERERROR("The experiment template $guid/$version is not a ".
		  "valid experiment template!", 1);
    }
    TBTemplatePidEid($guid, $version, $pid, $eid);
53 54 55 56
  
    #
    # Verify Permission.
    #
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    if (! TBExptTemplateAccessCheck($uid, $guid, $TB_EXPT_READINFO)) {
	USERERROR("You do not have permission to view experiment ".
		  "template $guid/$version!", 1);
    }
}

/**
 * Get the template versions associated with the given GUID.
 *
 * @param $guid The template GUID.
 * @return An array of template "objects".
 */
function GetTemplates($guid) {
    CheckArguments($guid, 1); /* XXX bogus version number. */

    $query_result =
	DBQueryFatal("SELECT vers,parent_vers,tid,hidden ".
		     "FROM experiment_templates ".
		     "WHERE guid='$guid' ".
		     "ORDER BY vers");

    $retval = array();
    while ($row = mysql_fetch_array($query_result)) {
	$metadata_result =
	    DBQueryFatal("SELECT name,value ".
			 "FROM experiment_template_metadata ".
			 "WHERE template_guid='$guid' and ".
			 "      template_vers='$row[vers]' ".
			 "ORDER BY created DESC");

	$metadata = array();
	while ($mrow = mysql_fetch_array($metadata_result)) {
	    $metadata[$mrow[name]] = $mrow[value];
	}

	if (count($metadata) > 0) {
	    $row[metadata] = $metadata;
	}
	
	if (TBTemplatePidEid($guid, $row[vers], &$pid, &$eid)) {
	    $parameter_result =
		DBQueryFatal("SELECT name,value FROM virt_parameters ".
			     "WHERE pid='$pid' and eid='$eid'");
	    
	    $parameters = array();
	    while ($prow = mysql_fetch_array($parameter_result)) {
		$parameters[$prow[name]] = $prow[value];
	    }
	    
	    if (count($parameters) > 0) {
		$row[parameters] = $parameters;
	    }
	}
	
	$retval[] = $row;
    }
    
    return $retval;
115 116
}

117 118
function queryFilter($v) {
    return ($v != "");
119
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

function SearchTemplates($guid, $terms) {
    /* XXX */
    CheckArguments($guid, 1);

    $matches = array();
    $retval = array();

    $terms = split(" ", $terms);
    $terms = array_filter($terms, "queryFilter");

    foreach ($terms as $term) {
	$term = "'%" . mysql_real_escape_string($term) . "%'";

	$query_result =
	    DBQueryFatal("SELECT template_vers ".
			 "FROM experiment_template_metadata ".
			 "WHERE template_guid='$guid' and ".
			 "      (name like ". $term . " or " .
			 "       value like ". $term . ") " .
			 "GROUP BY template_vers");

	while ($row = mysql_fetch_array($query_result)) {
	    if (!isset($matches[$row[template_vers]])) {
		$matches[$row[template_vers]] = 0;
	    }
	    $matches[$row[template_vers]] += 1;
	}
    }

    foreach ($matches as $key => $match) {
	if ($match == count($terms)) {
	    $retval[] = $key;
	}
    }

    return $retval;
157
}
158 159 160 161 162 163 164 165 166 167 168 169

CheckArguments($guid, $version);

if (isset($action)) {
    if ($action == "hide") {
	DBQueryFatal("UPDATE experiment_templates SET hidden=1 ".
		     "WHERE guid='$guid' and vers='$version'");
    }
    else if ($action == "show") {
	DBQueryFatal("UPDATE experiment_templates SET hidden=0 ".
		     "WHERE guid='$guid' and vers='$version'");
    }
170 171 172 173 174 175 176 177 178 179 180 181 182
    else if ($action == "showall") {
	DBQueryFatal("UPDATE experiment_templates SET hidden=0 ".
		     "WHERE guid='$guid'");
    }

    if (! TBGuid2PidGid($guid, $pid, $gid)) {
	TBERROR("Could not get template pid,gid for template $guid", 1);
    }
    # Need to update the template graph.
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

    SUEXEC($uid, "$pid,$unix_gid", "webtemplate_graph $guid",
	   SUEXEC_ACTION_DIE);
183 184 185 186 187 188 189 190
}

#
# Standard Testbed Header after argument checking.
#
PAGEHEADER("Experiment Template: $guid/$version");

SUBPAGESTART();
191

192 193 194 195 196 197 198 199
SUBMENUSTART("Template Options");

WRITESUBMENUBUTTON("Show NS File &nbsp &nbsp",
		   "spitnsdata.php3?guid=$guid&version=$version");

WRITESUBMENUBUTTON("Modify Template",
		   "template_modify.php?guid=$guid&version=$version");

200 201 202 203 204 205 206 207 208
if (TBIsExperimentTemplateHidden($guid, $version)) {
    WRITESUBMENUBUTTON("Show Template",
		       "template_show.php?guid=$guid&version=$version&action=show");
}
else {
    WRITESUBMENUBUTTON("Hide Template",
		       "template_show.php?guid=$guid&version=$version&action=hide");
}

209 210 211 212 213 214 215
WRITESUBMENUBUTTON("Instantiate Template",
		   "template_swapin.php?guid=$guid&version=$version");

WRITESUBMENUBUTTON("Add Metadata",
		   "template_metadata.php?action=add&".
		   "guid=$guid&version=$version");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
216 217 218
WRITESUBMENUBUTTON("Template Archive",
		   "archive_view.php3?pid=$pid&eid=$eid");

219 220 221 222 223 224 225 226 227 228 229 230 231 232
WRITESUBMENUBUTTON("Template History",
		   "template_history.php?guid=$guid&version=$version");

SUBMENUEND_2A();

#
# Ick.
#
$rsrcidx = TBrsrcIndex($pid, $eid);

echo "<br>
      <img border=1 alt='template visualization'
           src='showthumb.php3?idx=$rsrcidx'>";

233 234 235 236
if (TemplateInstanceCount($guid, $version)) {
    SHOWTEMPLATEINSTANCES($guid, $version);
}

237 238
SUBMENUEND_2B();

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
echo "<script type='text/javascript' src='js/wz_dragdrop.js'></script>";

SHOWTEMPLATEGRAPH($guid);

#
# Define the zoom buttons. This should go elsewhere.
#
echo "<script type=text/javascript>
      function ZoomOut() {
      }
      function ZoomIn() {
      }
      function ShowAll() {
          open('template_show.php?guid=$guid&version=$version&action=showall',
               '_self');
      }
      </script>\n";

echo "<center>\n";
echo "<button name=showall type=button onClick='ShowAll();'>";
echo " Show All Templates</button></a>&nbsp &nbsp ";
echo "<button name=zoomout type=button onClick='ZoomOut();'>";
echo " Zoom Out</button>\n";
echo "<button name=zoomin type=button onClick='ZoomIn();'>";
echo "Zoom In</button>\n";
echo "</center>\n";

266 267
SUBPAGEEND();

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
echo "<script type='text/javascript' src='js/wz_tooltip.js'></script>";

$paramcount = TemplateParameterCount($guid, $version);
$metacount  = TemplateMetadataCount($guid, $version);
$rowspan    = (($paramcount && $metacount) ? 2 : 1);

echo "<center>\n";
echo "<table border=0 bgcolor=#000 color=#000 class=stealth ".
     " cellpadding=0 cellspacing=0>\n";
echo "<tr valign=top><td rowspan=$rowspan class=stealth align=center>\n";

SHOWTEMPLATE($guid, $version);

echo "</td>\n";

if ($paramcount || $metacount) {
    echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
    echo "<td align=center class=stealth> \n";
    
    if ($paramcount && $metacount) {
	SHOWTEMPLATEPARAMETERS($guid, $version);
	echo "</td>\n";
	echo "</tr>\n";
	echo "<tr valign=top>";
	echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
	echo "<td class=stealth align=center>\n";
	SHOWTEMPLATEMETADATA($guid, $version);
    }
    elseif ($paramcount) {
	SHOWTEMPLATEPARAMETERS($guid, $version);
    }
    else {
	SHOWTEMPLATEMETADATA($guid, $version);
    }
    echo "</td>\n";
303 304
}

305 306 307
echo "</tr>\n";
echo "</table>\n";
echo "</center>\n";
308 309 310 311 312 313

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>