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

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

#
# Only known and logged in users.
#
17 18 19
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
20

21 22 23 24 25 26 27 28
#
# Verify page arguments
#
$reqargs = RequiredPageArguments("action",        PAGEARG_STRING);
$optargs = OptionalPageArguments("template",      PAGEARG_TEMPLATE,
				 "submit",        PAGEARG_STRING,
				 "metadata",      PAGEARG_METADATA,
				 "metadata_type", PAGEARG_STRING,
29
				 "referrer",      PAGEARG_STRING,
30 31 32 33 34 35 36 37
				 "formfields",    PAGEARG_ARRAY);

# Need these below.
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$unix_gid = $template->UnixGID();

38 39 40 41 42
#
# Spit the form out using the array of data.
#
function SPITFORM($action, $formfields, $errors)
{
43
    global $template, $metadata, $referrer;
44 45 46 47
    global $metadata_type;

    $template_guid = $template->guid();
    $template_vers = $template->vers();
48 49 50
    
    PAGEHEADER("Manage Template Metadata");

51 52
    if ($action == "add") {
	echo "<center>";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53 54
	echo "<font size=+1>
                  Attach metadata[<b>1</b>] to your template.</font>";
55 56 57 58 59
	echo "</center><br>\n";
    }
    elseif ($action == "delete") {
	echo "<center>";
	echo "<h3>Are you sure you want to delete this item?</h3>";
60 61 62
	echo "</center><br>\n";
    }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    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";
    }

85 86 87 88 89 90 91 92 93 94 95
    if ($action == "modify" || $action == "delete") {
	$url = CreateURL("template_metadata", $template, $metadata);
    }
    else {
	$url = CreateURL("template_metadata", $template);
    }
    if (isset($metadata_type) && $metadata_type != "") {
	$url .= "&metadata_type=$metadata_type";
    }
    
    echo "<form action='${url}&action=$action' method=post>\n";
96 97
    echo "<table align=center border=1>\n";

98 99 100 101
    if (isset($referrer) && $referrer != "") {
	echo "<input type=hidden name=referrer value='$referrer'>";
    }

102 103 104 105 106 107 108 109 110
    #
    # 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";

111
    if ($action == "modify" || $action == "delete") {
112 113 114
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
	
115 116 117 118 119 120 121
	echo "<tr>
                  <td class='pad4'>Metadata GUID:</td>
                  <td class='pad4' class=left>
                      $metadata_guid/$metadata_vers</td>\n";
	echo "</tr>\n";
    }

122 123
    $readonly_name  = ($action == "add"    ? "" : "readonly");
    $readonly_value = ($action == "delete" ? "readonly" : "");
124 125 126 127 128 129 130 131

    #
    # Name of the item
    #
    echo "<tr>
              <td>*Name:<br>
                  (something short and pithy)</td>
              <td class=pad4 class=left>
132
	          <input type=text $readonly_name
133
                         name=\"formfields[name]\"
134
                         value=\"" . $formfields["name"] . "\"
135 136 137 138 139 140 141 142 143 144 145
	                 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>
146
                  <textarea $readonly_value name=\"formfields[value]\"
147
                    rows=10 cols=80>" .
148
	            ereg_replace("\r", "", $formfields["value"]) .
149 150 151 152 153 154 155
	           "</textarea>
              </td>
          </tr>\n";

    if ($action == "modify") {
	$tag = "Modify Metadata";
    }
156 157 158
    elseif ($action == "delete") {
	$tag = "Delete Metadata";
    }
159 160 161 162 163 164 165 166 167 168 169
    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
170 171 172 173 174 175 176 177 178 179

    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";
180 181 182 183 184 185
}

#
# On first load, display virgin form and exit.
#
if (!isset($submit)) {
186 187 188 189 190
    #
    # 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") {
191 192
	if (!isset($metadata)) {
	    PAGEARGERROR("Must provide a metadata guid");
193
	}
194 195
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
196 197 198 199

        #
        # Verify Permission. Need permission for the template, any version.
        #
200 201 202
	if (! isset($template)) {
	    $template = Template::Lookup($metadata->template_guid(), 1);
	}
203 204

	if (!$template ||
205
	    !$template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
206 207 208 209 210 211 212 213
	    USERERROR("You do not have permission to view metadata in ".
		      " template $template_guid!", 1);
	}

	PAGEHEADER("Show Metadata");
	$metadata->Show();
	PAGEFOOTER();
	return;
214
    }
215
    elseif ($action == "modify" || $action == "delete") {
216 217
	if (!isset($template)) {
	    PAGEARGERROR("Must provide a template guid");
218
	}
219 220
	$template_guid = $template->guid();
	$template_vers = $template->vers();
221

222 223
	if (!isset($metadata)) {
	    PAGEARGERROR("Must provide a metadata guid");
224
	}
225 226
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
227
	$metadata_type = $metadata->type();
228 229
    }
    else {
230 231 232 233 234
	if (!isset($template)) {
	    PAGEARGERROR("Must provide a template guid");
	}
	$template_guid = $template->guid();
	$template_vers = $template->vers();
235

236 237
	if (isset($metadata_type) && $metadata_type != "") {
	    if (!TBvalid_template_metadata_type($metadata_type)) {
238 239 240 241 242 243
		PAGEARGERROR("Invalid characters in metadata type!");
	    }
	}
	else {
	    unset($metadata_type);
	}
244
    }
245 246

    # Perm check for add/modify to the template.
247
    if (!$template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
248 249
	USERERROR("You do not have permission to $action metadata in ".
		  " template $template_guid!", 1);
250 251
    }

252
    # Defaults for the form come from the DB.
253
    $defaults = array();
254
    if ($action == "modify" || $action == "delete") {
255 256
	$defaults["name"]  = $metadata->name();
	$defaults["value"] = $metadata->value();
257
    }
258 259 260
    else {
	$defaults["name"]  = "";
	$defaults["value"] = "";
261 262
    }
    $referrer = $_SERVER['HTTP_REFERER'];
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    
    #
    # 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.
#
284
if ($action == "modify" || $action == "delete") {
285 286
    if (!isset($template)) {
	PAGEARGERROR("Must provide a template guid");
287
    }
288 289
    $template_guid = $template->guid();
    $template_vers = $template->vers();
290

291 292
    if (!isset($metadata)) {
	PAGEARGERROR("Must provide a metadata guid");
293
    }
294 295
    $metadata_guid = $metadata->guid();
    $metadata_vers = $metadata->vers();
296
    $metadata_type = $metadata->type();
297 298
}
else {
299 300
    if (!isset($template)) {
	PAGEARGERROR("Must provide a template guid");
301
    }
302 303 304
    $template_guid = $template->guid();
    $template_vers = $template->vers();

305 306 307 308 309 310 311 312
    if (isset($metadata_type) && $metadata_type != "") {
	if (!TBvalid_template_metadata_type($metadata_type)) {
	    PAGEARGERROR("Invalid characters in metadata type!");
	}
    }
    else {
	unset($metadata_type);
    }
313 314
}

315
# Perm check for add/modify to the template.
316
if (!$template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
317 318
    USERERROR("You do not have permission to $action metadata in ".
	      " template $template_guid!", 1);
319 320 321 322 323 324
}

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

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

337
if ($action == "add") {
338
    if ($template->LookupMetadataByName($formfields["name"])) {
339
	$errors["Metadata Name"] = "Name already in use";
340
    }
341
    if (isset($metadata_type)) {
342
	$command_opts .= "-t " . escapeshellarg($metadata_type) . " ";
343
    }
344
    $command_opts .= "-a add " . escapeshellarg($formfields["name"]);
345
}
346
elseif ($action == "delete") {
347
    $command_opts .= "-a delete " . escapeshellarg($formfields["name"]);
348
}
349
else {
350
    # Had to already exist above. 
351
    $command_opts .= "-a modify " . escapeshellarg($formfields["name"]);
352 353 354 355
}

#
# Value:
356
#
357 358
if ($action != "delete" && $action != "add") {
    if (!isset($formfields["value"]) || $formfields["value"] == "") {
359 360
	$errors["Metadata Value"] = "Missing Field";
    }
361
    elseif (!TBvalid_template_metadata_value($formfields["value"])) {
362 363 364
	$errors["Metadata Value"] = TBFieldErrorString();
    }
    if ($action == "modify" &&
365
	$formfields["value"] == $metadata->value()) {
366 367
	$errors["Metadata Value"] = "New value identical to old value";
    }
368 369
}

370 371 372
#
# XXX Some metadata is special ...
#
373 374 375 376 377
if (isset($metadata_type)) {
    if ($metadata_type == "tid") {
	if ($action == "delete") {
	    $errors["TID"] = "Not allowed to delete this";
	}
378
	elseif (!TBvalid_eid($formfields["value"])) {
379 380 381
	    $errors["TID"] = TBFieldErrorString();
	}
    }
382 383 384 385
    elseif ($metadata_type == "template_description") {
	if ($action == "delete") {
	    $errors["Description"] = "Not allowed to delete this";
	}
386
	elseif (!TBvalid_template_description($formfields["value"])) {
387 388 389 390
	    $errors["Description"] = TBFieldErrorString();
	}
    }
    elseif ($metadata_type == "parameter_description") {
391
	if (!TBvalid_template_parameter_description($formfields["value"])) {
392 393 394
	    $errors["Description"] = TBFieldErrorString();
	}
    }
395 396 397 398 399 400 401 402 403 404
    elseif ($metadata_type == "instance_description") {
	if (!TBvalid_template_instance_description($formfields["value"])) {
	    $errors["Description"] = TBFieldErrorString();
	}
    }
    elseif ($metadata_type == "run_description") {
	if (!TBvalid_experiment_run_description($formfields["value"])) {
	    $errors["Description"] = TBFieldErrorString();
	}
    }
405 406
}

407 408 409 410 411 412 413
if (count($errors)) {
    SPITFORM($action, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
414
# Generate a temporary file and write in the data.
415
#
416 417 418 419
if ($action != "delete") {
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
    $foo = rand();
420

421
    $datafile = "/tmp/$uid-$foo.txt";
422

423 424 425 426
    if (! ($fp = fopen($datafile, "w"))) {
	TBERROR("Could not create temporary file $datafile", 1);
    }

427
    fwrite($fp, $formfields["value"]);
428 429
    fclose($fp);
    chmod($datafile, 0666);
430

431 432
    $command_opts = " -f $datafile $command_opts";
}
433

434
#
435
# The backend does the actual work.
436
#
437 438
$pid = $template->pid();
$gid = $template->gid();
439 440

$retval = SUEXEC($uid, "$pid,$unix_gid",
441
		 "webtemplate_metadata ".
442 443 444
		 "$command_opts $template_guid/$template_vers",
		 SUEXEC_ACTION_IGNORE);

445 446 447
if ($action != "delete") {
    unlink($datafile);
}
448 449

#
450 451 452 453 454
# 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);
455 456
}

457 458 459 460
# User error. Tell user and exit.
if ($retval) {
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
461 462
}

463 464 465 466 467 468
if (isset($referrer)) {
    header("Location: $referrer");
}
else {
    header("Location: ". CreateURL("template_show", $template));
}