template_metadata.php 13 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24
#
include("defs.php3");
25
include_once("template_defs.php");
26 27 28 29 30 31 32 33

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

#
# Only known and logged in users.
#
34 35 36
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
37

38 39 40 41 42 43 44 45
#
# Verify page arguments
#
$reqargs = RequiredPageArguments("action",        PAGEARG_STRING);
$optargs = OptionalPageArguments("template",      PAGEARG_TEMPLATE,
				 "submit",        PAGEARG_STRING,
				 "metadata",      PAGEARG_METADATA,
				 "metadata_type", PAGEARG_STRING,
46
				 "referrer",      PAGEARG_STRING,
47 48 49 50 51 52 53
				 "formfields",    PAGEARG_ARRAY);

# Need these below.
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$unix_gid = $template->UnixGID();
54 55
$project  = $template->GetProject();
$unix_pid = $project->unix_gid();
56

57 58 59 60 61
#
# Spit the form out using the array of data.
#
function SPITFORM($action, $formfields, $errors)
{
62
    global $template, $metadata, $referrer;
63 64 65 66
    global $metadata_type;

    $template_guid = $template->guid();
    $template_vers = $template->vers();
67 68 69
    
    PAGEHEADER("Manage Template Metadata");

70 71
    if ($action == "add") {
	echo "<center>";
Leigh Stoller's avatar
Leigh Stoller committed
72 73
	echo "<font size=+1>
                  Attach metadata[<b>1</b>] to your template.</font>";
74 75 76 77 78
	echo "</center><br>\n";
    }
    elseif ($action == "delete") {
	echo "<center>";
	echo "<h3>Are you sure you want to delete this item?</h3>";
79 80 81
	echo "</center><br>\n";
    }

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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";
    }

104 105 106 107 108 109 110 111 112 113 114
    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";
115 116
    echo "<table align=center border=1>\n";

117 118 119 120
    if (isset($referrer) && $referrer != "") {
	echo "<input type=hidden name=referrer value='$referrer'>";
    }

121 122 123 124 125 126 127 128 129
    #
    # 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";

130
    if ($action == "modify" || $action == "delete") {
131 132 133
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
	
134 135 136 137 138 139 140
	echo "<tr>
                  <td class='pad4'>Metadata GUID:</td>
                  <td class='pad4' class=left>
                      $metadata_guid/$metadata_vers</td>\n";
	echo "</tr>\n";
    }

141 142
    $readonly_name  = ($action == "add"    ? "" : "readonly");
    $readonly_value = ($action == "delete" ? "readonly" : "");
143 144 145 146 147 148 149 150

    #
    # Name of the item
    #
    echo "<tr>
              <td>*Name:<br>
                  (something short and pithy)</td>
              <td class=pad4 class=left>
151
	          <input type=text $readonly_name
152
                         name=\"formfields[name]\"
153
                         value=\"" . $formfields["name"] . "\"
154 155 156 157 158 159 160 161 162 163 164
	                 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>
165
                  <textarea $readonly_value name=\"formfields[value]\"
166
                    rows=10 cols=80>" .
167
	            str_replace("\r", "", $formfields["value"]) .
168 169 170 171 172 173 174
	           "</textarea>
              </td>
          </tr>\n";

    if ($action == "modify") {
	$tag = "Modify Metadata";
    }
175 176 177
    elseif ($action == "delete") {
	$tag = "Delete Metadata";
    }
178 179 180 181 182 183 184 185 186 187 188
    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 Stoller's avatar
Leigh Stoller committed
189 190 191 192 193 194 195 196 197 198

    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";
199 200 201 202 203 204
}

#
# On first load, display virgin form and exit.
#
if (!isset($submit)) {
205 206 207 208 209
    #
    # 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") {
210 211
	if (!isset($metadata)) {
	    PAGEARGERROR("Must provide a metadata guid");
212
	}
213 214
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
215 216 217 218

        #
        # Verify Permission. Need permission for the template, any version.
        #
219 220 221
	if (! isset($template)) {
	    $template = Template::Lookup($metadata->template_guid(), 1);
	}
222 223

	if (!$template ||
224
	    !$template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
225 226 227 228 229 230 231 232
	    USERERROR("You do not have permission to view metadata in ".
		      " template $template_guid!", 1);
	}

	PAGEHEADER("Show Metadata");
	$metadata->Show();
	PAGEFOOTER();
	return;
233
    }
234
    elseif ($action == "modify" || $action == "delete") {
235 236
	if (!isset($template)) {
	    PAGEARGERROR("Must provide a template guid");
237
	}
238 239
	$template_guid = $template->guid();
	$template_vers = $template->vers();
240

241 242
	if (!isset($metadata)) {
	    PAGEARGERROR("Must provide a metadata guid");
243
	}
244 245
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
246
	$metadata_type = $metadata->type();
247 248
    }
    else {
249 250 251 252 253
	if (!isset($template)) {
	    PAGEARGERROR("Must provide a template guid");
	}
	$template_guid = $template->guid();
	$template_vers = $template->vers();
254

255 256
	if (isset($metadata_type) && $metadata_type != "") {
	    if (!TBvalid_template_metadata_type($metadata_type)) {
257 258 259 260 261 262
		PAGEARGERROR("Invalid characters in metadata type!");
	    }
	}
	else {
	    unset($metadata_type);
	}
263
    }
264 265

    # Perm check for add/modify to the template.
266
    if (!$template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
267 268
	USERERROR("You do not have permission to $action metadata in ".
		  " template $template_guid!", 1);
269 270
    }

271
    # Defaults for the form come from the DB.
272
    $defaults = array();
273
    if ($action == "modify" || $action == "delete") {
274 275
	$defaults["name"]  = $metadata->name();
	$defaults["value"] = $metadata->value();
276
    }
277 278 279
    else {
	$defaults["name"]  = "";
	$defaults["value"] = "";
280 281
    }
    $referrer = $_SERVER['HTTP_REFERER'];
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    
    #
    # 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.
#
303
if ($action == "modify" || $action == "delete") {
304 305
    if (!isset($template)) {
	PAGEARGERROR("Must provide a template guid");
306
    }
307 308
    $template_guid = $template->guid();
    $template_vers = $template->vers();
309

310 311
    if (!isset($metadata)) {
	PAGEARGERROR("Must provide a metadata guid");
312
    }
313 314
    $metadata_guid = $metadata->guid();
    $metadata_vers = $metadata->vers();
315
    $metadata_type = $metadata->type();
316 317
}
else {
318 319
    if (!isset($template)) {
	PAGEARGERROR("Must provide a template guid");
320
    }
321 322 323
    $template_guid = $template->guid();
    $template_vers = $template->vers();

324 325 326 327 328 329 330 331
    if (isset($metadata_type) && $metadata_type != "") {
	if (!TBvalid_template_metadata_type($metadata_type)) {
	    PAGEARGERROR("Invalid characters in metadata type!");
	}
    }
    else {
	unset($metadata_type);
    }
332 333
}

334
# Perm check for add/modify to the template.
335
if (!$template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
336 337
    USERERROR("You do not have permission to $action metadata in ".
	      " template $template_guid!", 1);
338 339 340 341 342 343
}

#
# Okay, validate form arguments.
#
$errors = array();
344
$command_opts = "";
345 346 347 348

#
# Name
#
349
if (!isset($formfields["name"]) || $formfields["name"] == "") {
350 351
    $errors["Metadata Name"] = "Missing Field";
}
352
elseif (!TBvalid_template_metadata_name($formfields["name"])) {
353 354 355
    $errors["Metadata Name"] = TBFieldErrorString();
}

356
if ($action == "add") {
357
    if ($template->LookupMetadataByName($formfields["name"])) {
358
	$errors["Metadata Name"] = "Name already in use";
359
    }
360
    if (isset($metadata_type)) {
361
	$command_opts .= "-t " . escapeshellarg($metadata_type) . " ";
362
    }
363
    $command_opts .= "-a add " . escapeshellarg($formfields["name"]);
364
}
365
elseif ($action == "delete") {
366
    $command_opts .= "-a delete " . escapeshellarg($formfields["name"]);
367
}
368
else {
369
    # Had to already exist above. 
370
    $command_opts .= "-a modify " . escapeshellarg($formfields["name"]);
371 372 373 374
}

#
# Value:
375
#
376 377
if ($action != "delete" && $action != "add") {
    if (!isset($formfields["value"]) || $formfields["value"] == "") {
378 379
	$errors["Metadata Value"] = "Missing Field";
    }
380
    elseif (!TBvalid_template_metadata_value($formfields["value"])) {
381 382 383
	$errors["Metadata Value"] = TBFieldErrorString();
    }
    if ($action == "modify" &&
384
	$formfields["value"] == $metadata->value()) {
385 386
	$errors["Metadata Value"] = "New value identical to old value";
    }
387 388
}

389 390 391
#
# XXX Some metadata is special ...
#
392 393 394 395 396
if (isset($metadata_type)) {
    if ($metadata_type == "tid") {
	if ($action == "delete") {
	    $errors["TID"] = "Not allowed to delete this";
	}
397
	elseif (!TBvalid_eid($formfields["value"])) {
398 399 400
	    $errors["TID"] = TBFieldErrorString();
	}
    }
401 402 403 404
    elseif ($metadata_type == "template_description") {
	if ($action == "delete") {
	    $errors["Description"] = "Not allowed to delete this";
	}
405
	elseif (!TBvalid_template_description($formfields["value"])) {
406 407 408 409
	    $errors["Description"] = TBFieldErrorString();
	}
    }
    elseif ($metadata_type == "parameter_description") {
410
	if (!TBvalid_template_parameter_description($formfields["value"])) {
411 412 413
	    $errors["Description"] = TBFieldErrorString();
	}
    }
414 415 416 417 418 419 420 421 422 423
    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();
	}
    }
424 425
}

426 427 428 429 430 431 432
if (count($errors)) {
    SPITFORM($action, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
433
# Generate a temporary file and write in the data.
434
#
435 436 437 438
if ($action != "delete") {
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
    $foo = rand();
439

440
    $datafile = "/tmp/$uid-$foo.txt";
441

442 443 444 445
    if (! ($fp = fopen($datafile, "w"))) {
	TBERROR("Could not create temporary file $datafile", 1);
    }

446
    fwrite($fp, $formfields["value"]);
447 448
    fclose($fp);
    chmod($datafile, 0666);
449

450 451
    $command_opts = " -f $datafile $command_opts";
}
452

453
#
454
# The backend does the actual work.
455
#
456 457
$pid = $template->pid();
$gid = $template->gid();
458

459
$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
460
		 "webtemplate_metadata ".
461 462 463
		 "$command_opts $template_guid/$template_vers",
		 SUEXEC_ACTION_IGNORE);

464 465 466
if ($action != "delete") {
    unlink($datafile);
}
467 468

#
469 470 471 472 473
# 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);
474 475
}

476 477 478 479
# User error. Tell user and exit.
if ($retval) {
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
Leigh Stoller's avatar
Leigh Stoller committed
480 481
}

482 483 484 485 486 487
if (isset($referrer)) {
    header("Location: $referrer");
}
else {
    header("Location: ". CreateURL("template_show", $template));
}
488 489

?>