template_metadata.php 13.8 KB
Newer Older
1 2 3 4 5 6 7
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
8
include_once("template_defs.php");
9 10 11 12 13 14 15 16 17 18

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

#
# Only known and logged in users.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
19
$isadmin  = ISADMIN($uid);
20 21 22 23 24 25 26

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

31 32
    if ($action == "add") {
	echo "<center>";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
33 34
	echo "<font size=+1>
                  Attach metadata[<b>1</b>] to your template.</font>";
35 36 37 38 39
	echo "</center><br>\n";
    }
    elseif ($action == "delete") {
	echo "<center>";
	echo "<h3>Are you sure you want to delete this item?</h3>";
40 41 42
	echo "</center><br>\n";
    }

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
    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";

79
    if ($action == "modify" || $action == "delete") {
80 81 82 83 84 85
	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";
86 87 88 89
	echo "<input type=hidden name=metadata_vers value=$metadata_vers>\n";
    }
    if (isset($metadata_type) && $metadata_type != "") {
	echo "<input type=hidden name=metadata_type value=$metadata_type>\n";
90 91
    }

92 93
    $readonly_name  = ($action == "add"    ? "" : "readonly");
    $readonly_value = ($action == "delete" ? "readonly" : "");
94 95 96 97 98 99 100 101

    #
    # Name of the item
    #
    echo "<tr>
              <td>*Name:<br>
                  (something short and pithy)</td>
              <td class=pad4 class=left>
102
	          <input type=text $readonly_name
103 104 105 106 107 108 109 110 111 112 113 114 115
                         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>
116
                  <textarea $readonly_value name=\"formfields[value]\"
117 118 119 120 121 122 123 124 125
                    rows=10 cols=80>" .
	            ereg_replace("\r", "", $formfields[value]) .
	           "</textarea>
              </td>
          </tr>\n";

    if ($action == "modify") {
	$tag = "Modify Metadata";
    }
126 127 128
    elseif ($action == "delete") {
	$tag = "Delete Metadata";
    }
129 130 131 132 133 134 135 136 137 138 139
    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";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
140 141 142 143 144 145 146 147 148 149

    echo "<blockquote><blockquote>
          <ol>
            <li> Metadata can be any arbitrary name/value pair that you want
                 to associate with your template. The name can include
                 any printable ascii character including spaces, but
                 not newlines. The value can include any printable ascii
                 character and my be multiline.
          </ol>
          </blockquote></blockquote>\n";
150 151
}

152 153 154 155
# Objects
$metadata = NULL;
$template = NULL;

156 157 158 159 160 161 162 163 164
#
# On first load, display virgin form and exit.
#
if (!isset($submit)) {
    #
    # Verify page arguments.
    # 
    if (!isset($guid) ||
	strcmp($guid, "") == 0) {
165
	USERERROR("You must provide a GUID.", 1);
166 167 168
    }
    if (!isset($version) ||
	strcmp($version, "") == 0) {
169
	USERERROR("You must provide a version", 1);
170 171 172 173 174 175 176 177
    }
    if (!TBvalid_guid($guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($version)) {
	PAGEARGERROR("Invalid characters in version!");
    }

178 179 180 181 182
    #
    # 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") {
183 184 185 186
	$metadata_guid = $guid;
	$metadata_vers = $version;

	#
187
	# Find this metadata item.
188
	#
189 190 191
	$metadata = TemplateMetadata::Lookup($metadata_guid, $metadata_vers);
	
	if (! $metadata) {
192 193
	    USERERROR("Invalid metadata $guid/$version", 1);
	}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

        #
        # Verify Permission. Need permission for the template, any version.
        #
	$template = Template::Lookup($metadata->template_guid(), 1);

	if (!$template ||
	    !$template->AccessCheck($uid, $TB_EXPT_READINFO)) {
	    USERERROR("You do not have permission to view metadata in ".
		      " template $template_guid!", 1);
	}

	PAGEHEADER("Show Metadata");
	$metadata->Show();
	PAGEFOOTER();
	return;
210
    }
211
    elseif ($action == "modify" || $action == "delete") {
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	$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.
	#
232 233 234 235 236 237 238 239 240
	$template = Template::Lookup($template_guid, $template_vers);

	if (!$template) {
	    USERERROR("Invalid template $template_guid/$template_vers", 1);
	}

	$metadata = $template->LookupMetadataByGUID($metadata_guid,
						    $metadata_vers);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
241
	if (!$metadata) {
242
	    USERERROR("Invalid metadata $metadata_guid/$metadata_vers", 1);
243
	}
244
	$metadata_type = $metadata->type();
245 246 247 248 249
    }
    else {
	$template_guid = $guid;
	$template_vers = $version;

250 251 252 253 254 255 256 257 258 259 260
	if (isset($type) && $type != "") {
	    if (!TBvalid_template_metadata_type($type)) {
		PAGEARGERROR("Invalid characters in metadata type!");
	    }
	    $metadata_type = $type;
	}
	else {
	    unset($type);
	    unset($metadata_type);
	}

261 262 263
        #
        # Check to make sure this is a valid template.
        #
264 265 266 267
	$template = Template::Lookup($template_guid, $template_vers);

	if (!$template) {
	    USERERROR("Invalid template $template_guid/$template_vers", 1);
268 269
	}
    }
270 271 272

    # Perm check for add/modify to the template.
    if (!$template->AccessCheck($uid, $TB_EXPT_MODIFY)) {
273 274
	USERERROR("You do not have permission to $action metadata in ".
		  " template $template_guid!", 1);
275 276
    }

277
    # Defaults for the form come from the DB.
278
    $defaults = array();
279
    if ($action == "modify" || $action == "delete") {
280 281
	$defaults["name"]  = $metadata->name();
	$defaults["value"] = $metadata->value();
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    }
    
    #
    # 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.
#
304
if ($action == "modify" || $action == "delete") {
305 306 307 308 309 310 311 312 313 314 315 316
    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!");
    }
317 318 319 320 321 322 323 324 325 326 327 328 329
    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!");
    }
    
330
    #
331
    # Verify this metadata is attached to the template.
332
    #
333 334 335 336
    $template = Template::Lookup($template_guid, $template_vers);

    if (!$template) {
	USERERROR("Invalid template $template_guid/$template_vers", 1);
337 338
    }

339 340 341
    $metadata = $template->LookupMetadataByGUID($metadata_guid,$metadata_vers);

    if (!$template) {
342
	USERERROR("Invalid metadata $metadata_guid/$metadata_vers", 1);
343
    }
344 345
    # For checks below;
    $metadata_type = $metadata->type();
346 347 348 349 350 351 352 353 354 355 356 357 358 359
}
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!");
    }
360 361 362 363 364 365 366 367
    if (isset($metadata_type) && $metadata_type != "") {
	if (!TBvalid_template_metadata_type($metadata_type)) {
	    PAGEARGERROR("Invalid characters in metadata type!");
	}
    }
    else {
	unset($metadata_type);
    }
368 369 370 371

    #
    # Check to make sure this is a valid template.
    #
372 373 374 375
    $template = Template::Lookup($template_guid, $template_vers);
    
    if (!$template) {
	USERERROR("Invalid template $template_guid/$template_vers", 1);
376 377 378
    }
}

379 380 381 382
# Perm check for add/modify to the template.
if (!$template->AccessCheck($uid, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to $action metadata in ".
	      " template $template_guid!", 1);
383 384 385 386 387 388
}

#
# Okay, validate form arguments.
#
$errors = array();
389
$command_opts = "";
390 391 392 393

#
# Name
#
394 395 396 397 398 399 400
if (!isset($formfields[name]) || $formfields[name] == "") {
    $errors["Metadata Name"] = "Missing Field";
}
elseif (!TBvalid_template_metadata_name($formfields[name])) {
    $errors["Metadata Name"] = TBFieldErrorString();
}

401
if ($action == "add") {
402
    if ($template->LookupMetadataByName($formfields[name])) {
403
	$errors["Metadata Name"] = "Name already in use";
404
    }
405 406 407
    if (isset($metadata_type)) {
	$command_opts .= "-t $metadata_type ";
    }
408 409
    $command_opts .= "-a add " . escapeshellarg($formfields[name]);
}
410 411 412
elseif ($action == "delete") {
    $command_opts .= "-a delete " . escapeshellarg($formfields[name]);
}
413
else {
414
    # Had to already exist above. 
415
    $command_opts .= "-a modify " . escapeshellarg($formfields[name]);
416 417 418 419
}

#
# Value:
420 421 422 423 424 425 426 427 428 429 430 431
#
if ($action != "delete") {
    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";
    }
432 433
}

434 435 436
#
# XXX Some metadata is special ...
#
437 438 439 440 441 442
if (isset($metadata_type)) {
    if ($metadata_type == "tid") {
	if ($action == "delete") {
	    $errors["TID"] = "Not allowed to delete this";
	}
	elseif (!TBvalid_eid($formfields[value])) {
443 444 445
	    $errors["TID"] = TBFieldErrorString();
	}
    }
446 447 448 449 450 451 452 453 454 455
    elseif ($metadata_type == "template_description") {
	if ($action == "delete") {
	    $errors["Description"] = "Not allowed to delete this";
	}
	elseif (!TBvalid_template_description($formfields[value])) {
	    $errors["Description"] = TBFieldErrorString();
	}
    }
    elseif ($metadata_type == "parameter_description") {
	if (!TBvalid_template_parameter_description($formfields[value])) {
456 457 458 459 460
	    $errors["Description"] = TBFieldErrorString();
	}
    }
}

461 462 463 464 465 466 467
if (count($errors)) {
    SPITFORM($action, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
468
# Generate a temporary file and write in the data.
469
#
470 471 472 473
if ($action != "delete") {
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
    $foo = rand();
474

475
    $datafile = "/tmp/$uid-$foo.txt";
476

477 478 479 480 481 482 483
    if (! ($fp = fopen($datafile, "w"))) {
	TBERROR("Could not create temporary file $datafile", 1);
    }

    fwrite($fp, $formfields[value]);
    fclose($fp);
    chmod($datafile, 0666);
484

485 486
    $command_opts = " -f $datafile $command_opts";
}
487

488
#
489
# The backend does the actual work.
490
#
491 492
$pid = $template->pid();
$gid = $template->gid();
493 494 495
TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

$retval = SUEXEC($uid, "$pid,$unix_gid",
496
		 "webtemplate_metadata ".
497 498 499
		 "$command_opts $template_guid/$template_vers",
		 SUEXEC_ACTION_IGNORE);

500 501 502
if ($action != "delete") {
    unlink($datafile);
}
503 504

#
505 506 507 508 509
# 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);
510 511
}

512 513 514 515
# User error. Tell user and exit.
if ($retval) {
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
516 517
}

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