template_metadata.php 11.6 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 27 28 29 30

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

130 131 132 133
# Objects
$metadata = NULL;
$template = NULL;

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

156 157 158 159 160
    #
    # 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") {
161 162 163 164
	$metadata_guid = $guid;
	$metadata_vers = $version;

	#
165
	# Find this metadata item.
166
	#
167 168 169
	$metadata = TemplateMetadata::Lookup($metadata_guid, $metadata_vers);
	
	if (! $metadata) {
170 171
	    USERERROR("Invalid metadata $guid/$version", 1);
	}
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

        #
        # 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;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }
    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.
	#
210 211 212 213 214 215 216 217 218 219 220
	$template = Template::Lookup($template_guid, $template_vers);

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

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

	if (!$template) {
	    USERERROR("Invalid metadat $metadata_guid/$metadata_vers", 1);
221 222 223 224 225 226 227 228 229
	}
    }
    else {
	$template_guid = $guid;
	$template_vers = $version;

        #
        # Check to make sure this is a valid template.
        #
230 231 232 233
	$template = Template::Lookup($template_guid, $template_vers);

	if (!$template) {
	    USERERROR("Invalid template $template_guid/$template_vers", 1);
234 235
	}
    }
236 237 238

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

243 244
    # Defaults from the form come from the DB.
    $defaults = array();
245
    if ($action == "modify") {
246 247
	$defaults["name"]  = $metadata->name();
	$defaults["value"] = $metadata->value();
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    }
    
    #
    # 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!");
    }
283 284 285 286 287 288 289 290 291 292 293 294 295
    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!");
    }
    
296
    #
297
    # Verify this metadata is attached to the template.
298
    #
299 300 301 302
    $template = Template::Lookup($template_guid, $template_vers);

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

305 306 307 308
    $metadata = $template->LookupMetadataByGUID($metadata_guid,$metadata_vers);

    if (!$template) {
	USERERROR("Invalid metadat $metadata_guid/$metadata_vers", 1);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    }
}
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.
    #
328 329 330 331
    $template = Template::Lookup($template_guid, $template_vers);
    
    if (!$template) {
	USERERROR("Invalid template $template_guid/$template_vers", 1);
332 333 334
    }
}

335 336 337 338
# 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);
339 340 341 342 343 344
}

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

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

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

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

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
#
# 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();
	}
    }
}

398 399 400 401 402 403 404
if (count($errors)) {
    SPITFORM($action, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
405
# Generate a temporary file and write in the data.
406
#
407 408 409
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$foo = rand();
410

411 412 413 414
$datafile = "/tmp/$uid-$foo.txt";

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

417 418 419
fwrite($fp, $formfields[value]);
fclose($fp);
chmod($datafile, 0666);
420

421
#
422
# The backend does the actual work.
423
#
424 425
$pid = $template->pid();
$gid = $template->gid();
426 427 428 429 430 431 432 433
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);
434 435

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

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

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