template_metadata.php 11.6 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 29 30 31 32 33 34 35 36
#
# Verify page arguments
#
$reqargs = RequiredPageArguments("action",        PAGEARG_STRING);
$optargs = OptionalPageArguments("template",      PAGEARG_TEMPLATE,
				 "submit",        PAGEARG_STRING,
				 "metadata",      PAGEARG_METADATA,
				 "metadata_type", PAGEARG_STRING,
				 "formfields",    PAGEARG_ARRAY);

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

37 38 39 40 41
#
# Spit the form out using the array of data.
#
function SPITFORM($action, $formfields, $errors)
{
Kevin Atkinson's avatar
 
Kevin Atkinson committed
42
    global $template, $metadata;
43 44 45 46
    global $metadata_type;

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

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

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

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

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

117 118
    $readonly_name  = ($action == "add"    ? "" : "readonly");
    $readonly_value = ($action == "delete" ? "readonly" : "");
119 120 121 122 123 124 125 126

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

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

    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";
175 176 177 178 179 180
}

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

        #
        # Verify Permission. Need permission for the template, any version.
        #
195 196 197
	if (! isset($template)) {
	    $template = Template::Lookup($metadata->template_guid(), 1);
	}
198 199

	if (!$template ||
200
	    !$template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
201 202 203 204 205 206 207 208
	    USERERROR("You do not have permission to view metadata in ".
		      " template $template_guid!", 1);
	}

	PAGEHEADER("Show Metadata");
	$metadata->Show();
	PAGEFOOTER();
	return;
209
    }
210
    elseif ($action == "modify" || $action == "delete") {
211 212
	if (!isset($template)) {
	    PAGEARGERROR("Must provide a template guid");
213
	}
214 215
	$template_guid = $template->guid();
	$template_vers = $template->vers();
216

217 218
	if (!isset($metadata)) {
	    PAGEARGERROR("Must provide a metadata guid");
219
	}
220 221
	$metadata_guid = $metadata->guid();
	$metadata_vers = $metadata->vers();
222
	$metadata_type = $metadata->type();
223 224
    }
    else {
225 226 227 228 229
	if (!isset($template)) {
	    PAGEARGERROR("Must provide a template guid");
	}
	$template_guid = $template->guid();
	$template_vers = $template->vers();
230

231 232
	if (isset($metadata_type) && $metadata_type != "") {
	    if (!TBvalid_template_metadata_type($metadata_type)) {
233 234 235 236 237 238
		PAGEARGERROR("Invalid characters in metadata type!");
	    }
	}
	else {
	    unset($metadata_type);
	}
239
    }
240 241

    # Perm check for add/modify to the template.
242
    if (!$template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
243 244
	USERERROR("You do not have permission to $action metadata in ".
		  " template $template_guid!", 1);
245 246
    }

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

285 286
    if (!isset($metadata)) {
	PAGEARGERROR("Must provide a metadata guid");
287
    }
288 289
    $metadata_guid = $metadata->guid();
    $metadata_vers = $metadata->vers();
290
    $metadata_type = $metadata->type();
291 292
}
else {
293 294
    if (!isset($template)) {
	PAGEARGERROR("Must provide a template guid");
295
    }
296 297 298
    $template_guid = $template->guid();
    $template_vers = $template->vers();

299 300 301 302 303 304 305 306
    if (isset($metadata_type) && $metadata_type != "") {
	if (!TBvalid_template_metadata_type($metadata_type)) {
	    PAGEARGERROR("Invalid characters in metadata type!");
	}
    }
    else {
	unset($metadata_type);
    }
307 308
}

309
# Perm check for add/modify to the template.
310
if (!$template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
311 312
    USERERROR("You do not have permission to $action metadata in ".
	      " template $template_guid!", 1);
313 314 315 316 317 318
}

#
# Okay, validate form arguments.
#
$errors = array();
319
$command_opts = "";
320 321 322 323

#
# Name
#
324
if (!isset($formfields["name"]) || $formfields["name"] == "") {
325 326
    $errors["Metadata Name"] = "Missing Field";
}
327
elseif (!TBvalid_template_metadata_name($formfields["name"])) {
328 329 330
    $errors["Metadata Name"] = TBFieldErrorString();
}

331
if ($action == "add") {
332
    if ($template->LookupMetadataByName($formfields["name"])) {
333
	$errors["Metadata Name"] = "Name already in use";
334
    }
335
    if (isset($metadata_type)) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
336
	$command_opts .= "-t $metadata_type ";
337
    }
338
    $command_opts .= "-a add " . escapeshellarg($formfields["name"]);
339
}
340
elseif ($action == "delete") {
341
    $command_opts .= "-a delete " . escapeshellarg($formfields["name"]);
342
}
343
else {
344
    # Had to already exist above. 
345
    $command_opts .= "-a modify " . escapeshellarg($formfields["name"]);
346 347 348 349
}

#
# Value:
350
#
351 352
if ($action != "delete" && $action != "add") {
    if (!isset($formfields["value"]) || $formfields["value"] == "") {
353 354
	$errors["Metadata Value"] = "Missing Field";
    }
355
    elseif (!TBvalid_template_metadata_value($formfields["value"])) {
356 357 358
	$errors["Metadata Value"] = TBFieldErrorString();
    }
    if ($action == "modify" &&
359
	$formfields["value"] == $metadata->value()) {
360 361
	$errors["Metadata Value"] = "New value identical to old value";
    }
362 363
}

364 365 366
#
# XXX Some metadata is special ...
#
367 368 369 370 371
if (isset($metadata_type)) {
    if ($metadata_type == "tid") {
	if ($action == "delete") {
	    $errors["TID"] = "Not allowed to delete this";
	}
372
	elseif (!TBvalid_eid($formfields["value"])) {
373 374 375
	    $errors["TID"] = TBFieldErrorString();
	}
    }
376 377 378 379
    elseif ($metadata_type == "template_description") {
	if ($action == "delete") {
	    $errors["Description"] = "Not allowed to delete this";
	}
380
	elseif (!TBvalid_template_description($formfields["value"])) {
381 382 383 384
	    $errors["Description"] = TBFieldErrorString();
	}
    }
    elseif ($metadata_type == "parameter_description") {
385
	if (!TBvalid_template_parameter_description($formfields["value"])) {
386 387 388 389 390
	    $errors["Description"] = TBFieldErrorString();
	}
    }
}

391 392 393 394 395 396 397
if (count($errors)) {
    SPITFORM($action, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
398
# Generate a temporary file and write in the data.
399
#
400 401 402 403
if ($action != "delete") {
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
    $foo = rand();
404

405
    $datafile = "/tmp/$uid-$foo.txt";
406

407 408 409 410
    if (! ($fp = fopen($datafile, "w"))) {
	TBERROR("Could not create temporary file $datafile", 1);
    }

411
    fwrite($fp, $formfields["value"]);
412 413
    fclose($fp);
    chmod($datafile, 0666);
414

415 416
    $command_opts = " -f $datafile $command_opts";
}
417

418
#
419
# The backend does the actual work.
420
#
421 422
$pid = $template->pid();
$gid = $template->gid();
423 424

$retval = SUEXEC($uid, "$pid,$unix_gid",
425
		 "webtemplate_metadata ".
426 427 428
		 "$command_opts $template_guid/$template_vers",
		 SUEXEC_ACTION_IGNORE);

429 430 431
if ($action != "delete") {
    unlink($datafile);
}
432 433

#
434 435 436 437 438
# 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);
439 440
}

441 442 443 444
# User error. Tell user and exit.
if ($retval) {
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
445 446
}

Kevin Atkinson's avatar
 
Kevin Atkinson committed
447 448
header("Location: ".
       "template_show.php?guid=$template_guid&version=$template_vers");