template_metadata.php 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
include("template_defs.php");

#
# No PAGEHEADER since we spit out a Location header later. See below.
#

#
# Only known and logged in users.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

#
# Spit the form out using the array of data.
#
function SPITFORM($action, $formfields, $errors)
{
    global $template_guid, $template_vers;
    global $metadata_guid, $metadata_vers;
    
    PAGEHEADER("Manage Template Metadata");

31 32 33 34 35 36
    if ($action == "add") {
	echo "<center>";
	echo "<h3>Use this page to attach metadata to your template.</h3>";
	echo "</center><br>\n";
    }

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

    echo "<form action=template_metadata.php?action=$action method=post>\n";
    echo "<table align=center border=1>\n";

    #
    # Template GUID and Version. These are read-only fields.
    #
    echo "<tr>
              <td class='pad4'>Template GUID:</td>
              <td class='pad4' class=left>
                  $template_guid/$template_vers</td>\n";
    echo "</tr>\n";
    echo "<input type=hidden name=template_guid value=$template_guid>\n";
    echo "<input type=hidden name=template_vers value=$template_vers>\n";

    if ($action == "modify") {
	echo "<tr>
                  <td class='pad4'>Metadata GUID:</td>
                  <td class='pad4' class=left>
                      $metadata_guid/$metadata_vers</td>\n";
	echo "</tr>\n";
	echo "<input type=hidden name=metadata_guid value=$metadata_guid>\n";
	echo "<input type=hidden name=metadata_vers
                                 value=$metadata_vers>\n";
    }

    $readonly = ($action == "modify" ? "readonly" : "");

    #
    # Name of the item
    #
    echo "<tr>
              <td>*Name:<br>
                  (something short and pithy)</td>
              <td class=pad4 class=left>
	          <input type=text $readonly
                         name=\"formfields[name]\"
                         value=\"" . $formfields[name] . "\"
	                 size=64>
             </td>
          </tr>\n";

    echo "<tr>
              <td colspan=2>
               Value (use this area to enter the value of your metadata item).
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[value]\"
                    rows=10 cols=80>" .
	            ereg_replace("\r", "", $formfields[value]) .
	           "</textarea>
              </td>
          </tr>\n";

    if ($action == "modify") {
	$tag = "Modify Metadata";
    }
    else {
	$tag = "Add Metadata";
    }    
 
    echo "<tr>
              <td class='pad4' align=center colspan=2>
                 <b><input type=submit name=submit value='$tag'></b>
              </td>
         </tr>
        </form>
        </table>\n";
}

#
# On first load, display virgin form and exit.
#
if (!isset($submit)) {
    #
    # Verify page arguments.
    # 
    if (!isset($guid) ||
	strcmp($guid, "") == 0) {
139
	USERERROR("You must provide a GUID.", 1);
140 141 142
    }
    if (!isset($version) ||
	strcmp($version, "") == 0) {
143
	USERERROR("You must provide a version", 1);
144 145 146 147 148 149 150 151
    }
    if (!TBvalid_guid($guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($version)) {
	PAGEARGERROR("Invalid characters in version!");
    }

152 153 154 155 156
    #
    # In show mode, we can show any metadata entry, but it cannot be modified
    # unless its in the context of a template. That might change later?
    #
    if ($action == "show") {
157 158 159 160
	$metadata_guid = $guid;
	$metadata_vers = $version;

	#
161
	# Find template associated with this metadata item (for perm check).
162
	#
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	if (! TBMetadataTemplate($guid, $version, $template_guid)) {
	    USERERROR("Invalid metadata $guid/$version", 1);
	}
    }
    elseif ($action == "modify") {
	$template_guid = $guid;
	$template_vers = $version;

	# Must get the metadata guid and vers we want to change.
	if (!isset($metadata_guid) || $metadata_guid == "") {
	    USERERROR("You must provide a metadata GUID", 1);
	}
	if (!isset($metadata_vers) || $metadata_vers == "") {
	    USERERROR("You must provide a metadata version", 1);
	}
	if (!TBvalid_guid($metadata_guid)) {
	    PAGEARGERROR("Invalid characters in GUID!");
	}
	if (!TBvalid_integer($metadata_vers)) {
	    PAGEARGERROR("Invalid characters in metadata version!");
	}

	#
	# Verify this metadata is attached to the template.
	#
	if (! TBValidTemplateMetadata($metadata_guid, $metadata_vers,
				      $template_guid, $template_vers)) {
	    USERERROR("Invalid metadata $guid/$version", 1);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	}
    }
    else {
	$template_guid = $guid;
	$template_vers = $version;

        #
        # Check to make sure this is a valid template.
        #
	if (! TBValidExperimentTemplate($guid, $version)) {
	    USERERROR("The experiment template $guid/$version is not a valid ".
		      "experiment template!", 1);
	}
    }
    
    #
    # Verify Permission.
    #
    if (! TBExptTemplateAccessCheck($uid, $template_guid, $TB_EXPT_MODIFY)) {
210 211
	USERERROR("You do not have permission to $action metadata in ".
		  " template $template_guid!", 1);
212 213 214 215 216 217
    }

    #
    # For plain show ...
    #
    if ($action == "show") {
218 219
	PAGEHEADER("Show Metadata");
	SHOWMETADATAITEM($guid, $version);	
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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
	PAGEFOOTER();
	return;
    }

    if ($action == "modify") {
	if (! TBMetadataData($metadata_guid, $metadata_vers, $defaults)) {
	    USERERROR("Could not retrieve metadata data", 1);
	}
    }
    else {
	$defaults = array();
    }
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

    SPITFORM($action, $defaults, 0);
    PAGEFOOTER();
    return;
}
elseif (! isset($formfields)) {
    PAGEARGERROR();
}

#
# Verify page arguments, which depend on action.
#
if ($action == "modify") {
    if (!isset($metadata_guid) || $metadata_guid == "") {
	USERERROR("You must provide a Metadata GUID.", 1);
    }
    if (!isset($metadata_vers) || $metadata_vers == "") {
	USERERROR("You must provide a Metadata version", 1);
    }
    if (!TBvalid_guid($metadata_guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($metadata_vers)) {
	PAGEARGERROR("Invalid characters in version!");
    }
266 267 268 269 270 271 272 273 274 275 276 277 278
    if (!isset($template_guid) || $template_guid == "") {
	USERERROR("You must provide a Template GUID.", 1);
    }
    if (!isset($template_vers) || $template_vers == "") {
	USERERROR("You must provide a Template version", 1);
    }
    if (!TBvalid_guid($template_guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($template_vers)) {
	PAGEARGERROR("Invalid characters in version!");
    }
    
279
    #
280
    # Verify this metadata is attached to the template.
281
    #
282 283 284
    if (! TBValidTemplateMetadata($metadata_guid, $metadata_vers,
				  $template_guid, $template_vers)) {
	USERERROR("Invalid metadata $guid/$version", 1);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    }

    # Need this below.
    if (! TBMetadataData($metadata_guid, $metadata_vers, $metadata_data)) {
	USERERROR("Could not retrieve metadata data", 1);
    }
}
else {
    if (!isset($template_guid) || $template_guid == "") {
	USERERROR("You must provide a Template GUID.", 1);
    }
    if (!isset($template_vers) || $template_vers == "") {
	USERERROR("You must provide a Template version", 1);
    }
    if (!TBvalid_guid($template_guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($template_vers)) {
	PAGEARGERROR("Invalid characters in version!");
    }

    #
    # Check to make sure this is a valid template.
    #
    if (! TBValidExperimentTemplate($template_guid, $template_vers)) {
	USERERROR("The experiment template $template_guid/$template_vers ".
		  "is not a valid experiment template!", 1);
    }
}

#
# Verify Permission.
#
if (! TBExptTemplateAccessCheck($uid, $template_guid, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to modify experiment template ".
	      "$template_guid/$template_vers!", 1);
}

#
# Okay, validate form arguments.
#
$errors = array();
327
$command_opts = "";
328 329 330 331

#
# Name
#
332 333 334 335 336 337 338
if (!isset($formfields[name]) || $formfields[name] == "") {
    $errors["Metadata Name"] = "Missing Field";
}
elseif (!TBvalid_template_metadata_name($formfields[name])) {
    $errors["Metadata Name"] = TBFieldErrorString();
}

339
if ($action == "add") {
340 341 342
    if (TBTemplateMetadataLookup($template_guid, $template_vers,
				 $formfields[name], $metadata_value)) {
	$errors["Metadata Name"] = "Name already in use";
343
    }
344 345 346 347 348 349
    $command_opts .= "-a add " . escapeshellarg($formfields[name]);
}
else {
    if (!TBTemplateMetadataLookup($template_guid, $template_vers,
				  $formfields[name], $metadata_value)) {
	$errors["Metadata Name"] = "Name does not exist to modify";
350
    }
351
    $command_opts .= "-a modify " . escapeshellarg($formfields[name]);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}

#
# Value:
# 
if (!isset($formfields[value]) || $formfields[value] == "") {
    $errors["Metadata Value"] = "Missing Field";
}
elseif (!TBvalid_template_metadata_value($formfields[value])) {
    $errors["Metadata Value"] = TBFieldErrorString();
}
if ($action == "modify" &&
    $formfields[value] == $metadata_data[value]) {
    $errors["Metadata Value"] = "New value identical to old value";
}

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
#
# XXX Some metadata is special ...
#
if ($action == "modify") {
    if ($formfields[name] == "TID") {
	if (!TBvalid_eid($formfields[value])) {
	    $errors["TID"] = TBFieldErrorString();
	}
    }
    elseif ($formfields[name] == "description") {
	if (!TBvalid_template_description($formfields[value])) {
	    $errors["Description"] = TBFieldErrorString();
	}
    }
}

384 385 386 387 388 389 390
if (count($errors)) {
    SPITFORM($action, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
391
# Generate a temporary file and write in the data.
392
#
393 394 395
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$foo = rand();
396

397 398 399 400
$datafile = "/tmp/$uid-$foo.txt";

if (! ($fp = fopen($datafile, "w"))) {
    TBERROR("Could not create temporary file $datafile", 1);
401
}
402

403 404 405
fwrite($fp, $formfields[value]);
fclose($fp);
chmod($datafile, 0666);
406

407
#
408
# The backend does the actual work.
409
#
410 411
if (!TBGuid2PidGid($template_guid, $pid, $gid)) {
    TBERROR("Could not determine pid/gid of template $template_guid", 1);
412
}
413 414 415 416 417 418 419 420
TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

$retval = SUEXEC($uid, "$pid,$unix_gid",
		 "webtemplate_metadata -f $datafile ".
		 "$command_opts $template_guid/$template_vers",
		 SUEXEC_ACTION_IGNORE);

unlink($datafile);
421 422

#
423 424 425 426 427
# Fatal Error. Report to the user, even though there is not much he can
# do with the error. Also reports to tbops.
# 
if ($retval < 0) {
    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
428 429
}

430 431 432 433
# User error. Tell user and exit.
if ($retval) {
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
434 435
}

436 437
header("Location: ".
       "template_show.php?guid=$template_guid&version=$template_vers");