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

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

#
# Only known and logged in users can end experiments.
#
19 20 21
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
22 23 24 25 26

if (! $isadmin) {
    USERERROR("You do not have permission to edit node types!", 1);
}

27 28
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY,
29 30 31 32 33 34 35

				 # Send new_type=1 to create new nodetype.
				 "new_type",   PAGEARG_STRING,
				 # Optional if new_type, required if not.
				 "node_type",  PAGEARG_STRING,

				 # Attribute creation and deletion.
36 37 38 39
				 "deletes",    PAGEARG_ARRAY,
				 "attributes", PAGEARG_ARRAY,
				 "newattribute_type",  PAGEARG_STRING,
				 "newattribute_name",  PAGEARG_STRING,
40
				 "newattribute_value", PAGEARG_ANYTHING);
Russ Fish's avatar
Russ Fish committed
41 42
if (!isset($node_type)) { $node_type = ""; }
if (!isset($attributes)) { $attributes = array(); }
Russ Fish's avatar
Russ Fish committed
43
if (!isset($deletes)) { $deletes = array(); }
44

45 46 47 48 49 50
$emulab_ops = Project::LookupByPid("emulab-ops");
$freebsd_mfs = OSinfo::LookupByName($emulab_ops,"FREEBSD-MFS");
$rhl_std = OSinfo::LookupByName($emulab_ops, "RHL-STD");
$fbsd_std = OSinfo::LookupByName($emulab_ops,"FBSD-STD");
$frisbee_mfs = OSinfo::LookupByName($emulab_ops,"FRISBEE-MFS");

51 52 53 54 55
# The default image comes from a site variable to avoid hardwiring here.
$default_imagename = TBGetSiteVar("general/default_imagename");
$default_image = Image::LookupByName($emulab_ops, $default_imagename);

if ($freebsd_mfs == null || $default_image == null ||
56 57 58 59 60
    $rhl_std == null || $fbsd_std == null || $frisbee_mfs == null) {
    PAGEERROR("You must add images from Utah into your database" .
              " before adding a nodetype. See installation documentation for details!",1);
}

61 62
# This belongs elsewhere!
$initial_attributes = array(
63 64
    array("attrkey" => "adminmfs_osid", "attrvalue" => $freebsd_mfs->osid(),
	  "attrtype" => "integer"),
65 66 67 68 69 70 71 72
    array("attrkey" => "bios_waittime", "attrvalue" => "60",
	  "attrtype" => "integer"),
    array("attrkey" => "bootdisk_unit", "attrvalue" => "0",
	  "attrtype" => "integer"),
    array("attrkey" => "control_interface", "attrvalue" => "ethX",
	  "attrtype" => "string"),
    array("attrkey" => "control_network", "attrvalue" => "X",
	  "attrtype" => "integer"),
73
    array("attrkey" => "default_imageid",
74
	  "attrvalue" => $default_image->imageid(),
75
	  "attrtype" => "string"),
76 77
    array("attrkey" => "default_osid", "attrvalue" => $rhl_std->osid(),
	  "attrtype" => "integer"),
78 79
    array("attrkey" => "delay_capacity", "attrvalue" => "2",
	  "attrtype" => "integer"),
80 81 82 83
    array("attrkey" => "delay_osid", "attrvalue" => $fbsd_std->osid(),
	  "attrtype" => "integer"),
    array("attrkey" => "diskloadmfs_osid", "attrvalue" => $frisbee_mfs->osid(),
	  "attrtype" => "integer"),
84 85 86 87 88 89 90 91
    array("attrkey" => "disksize", "attrvalue" => "0.00",
	  "attrtype" => "float"),
    array("attrkey" => "disktype", "attrvalue" => "ad",
	  "attrtype" => "string"),
    array("attrkey" => "frequency", "attrvalue" => "XXX",
	  "attrtype" => "integer"),
    array("attrkey" => "imageable", "attrvalue" => "1",
	  "attrtype" => "boolean"),
92 93
    array("attrkey" => "jail_osid", "attrvalue" =>  $fbsd_std->osid(),
	  "attrtype" => "integer"),
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    array("attrkey" => "max_interfaces", "attrvalue" => "X",
	  "attrtype" => "integer"),
    array("attrkey" => "memory", "attrvalue" => "XXX",
	  "attrtype" => "integer"),
    array("attrkey" => "power_delay", "attrvalue" => "60",
	  "attrtype" => "integer"),
    array("attrkey" => "processor", "attrvalue" => "PIII",
	  "attrtype" => "string"),
    array("attrkey" => "rebootable", "attrvalue" => "1",
	  "attrtype" => "boolean"),
    array("attrkey" => "simnode_capacity", "attrvalue" => "650",
	  "attrtype" => "integer"),
    array("attrkey" => "trivlink_maxspeed", "attrvalue" => "400000",
	  "attrtype" => "integer"),
    array("attrkey" => "virtnode_capacity", "attrvalue" => "20",
	  "attrtype" => "integer"),
    );

112 113 114
#
# Spit the form out using the array of data.
#
115
function SPITFORM($node_type, $formfields, $attributes, $deletes, $errors)
116
{
117
    global $osid_result, $imageid_result, $mfsosid_result, $new_type;
118
    global $newattribute_name, $newattribute_value, $newattribute_type;
119

120 121 122 123
    #
    # Split default_imageid
    #

124 125 126 127 128 129 130 131 132 133 134 135 136
    if (isset($attributes["default_imageid"])) {
	$default_imageids = preg_split('/,/', $attributes["default_imageid"]);
	for ($i = 0; $i != count($default_imageids); $i++) {
	    $attributes["default_imageid_$i"] = $default_imageids[$i];
	}
	$last_default_imageid_label = "default_imageid_$i";
	$attributes["default_imageid_$i"] = 0;
	unset($attributes["default_imageid"]);
	ksort($attributes);
    } else {
	$attributes["default_imageid_0"] = 0;
	$last_default_imageid_label = "default_imageid_0";
	ksort($attributes);
137 138
    }

139 140 141
    #
    # Standard Testbed Header
    #
142
    if (! isset($new_type)) {
143 144 145 146 147 148
	PAGEHEADER("Edit Node Type");

	echo "<font size=+2>Node Type <b>".
              "<a href='shownodetype.php3?node_type=$node_type'>$node_type
                 </a></b>\n".
	       "</font>\n";
149

150 151 152 153 154
	echo "<br><br>\n";
    }
    else {
	PAGEHEADER("Create Node Type");
    }
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

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

178 179

    $formargs = "node_type=$node_type";
180
    if (isset($new_type)) {
181 182 183
	$formargs .= "&new_type=1";
    }

184
    echo "<table align=center border=1>
185 186
          <form action=editnodetype.php3?$formargs method=post " .
	  " name=typeform>\n";
187 188

    echo "<tr>
189
              <td colspan=2>Type:</td>\n";
190
    if (isset($new_type)) {
191 192 193 194 195 196 197 198
        echo "<td class=left>
                 <input type=text
                        name=\"node_type\"
                        value=\"" . $node_type . "\"
	                size=10>
             </td>\n";
    } else {
         echo "<td class=left>$node_type</td>
199
          </tr>\n";
200
    }
201 202

    echo "<tr>
203
              <td colspan=2>Class:</td>\n";
204
    if (isset($new_type)) {
205 206 207 208 209 210 211
        echo "<td class=left>
                 <input type=text
                        name=\"formfields[class]\"
                        value=\"" . $formfields['class'] . "\"
	                size=10>
             </td>\n";
    } else {
212
	echo "<td class=left>" . $formfields["class"] . "</td>
213
          </tr>\n";
214
    }
215 216

    echo "<tr>
217
             <td colspan=2>isvirtnode:</td>
218 219 220
             <td class=left>
                 <input type=text
                        name=\"formfields[isvirtnode]\"
221
                        value=\"" . $formfields["isvirtnode"] . "\"
222 223 224 225
	                size=2>
             </td>
          </tr>\n";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
226
    echo "<tr>
227
             <td colspan=2>isjailed:</td>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
228 229
             <td class=left>
                 <input type=text
230
                        name=\"formfields[isjailed]\"
231
                        value=\"" . $formfields["isjailed"] . "\"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
232 233 234 235 236
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
237
             <td colspan=2>isdynamic:</td>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
238 239 240
             <td class=left>
                 <input type=text
                        name=\"formfields[isdynamic]\"
241
                        value=\"" . $formfields["isdynamic"] . "\"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
242 243 244 245
	                size=2>
             </td>
          </tr>\n";

246
    echo "<tr>
247
             <td colspan=2>isremotenode:</td>
248 249 250
             <td class=left>
                 <input type=text
                        name=\"formfields[isremotenode]\"
251
                        value=\"" . $formfields["isremotenode"] . "\"
252 253 254 255 256
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
257
             <td colspan=2>issubnode:</td>
258 259 260
             <td class=left>
                 <input type=text
                        name=\"formfields[issubnode]\"
261
                        value=\"" . $formfields["issubnode"] . "\"
262 263 264 265 266
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
267
             <td colspan=2>isplabdslice:</td>
268 269 270
             <td class=left>
                 <input type=text
                        name=\"formfields[isplabdslice]\"
271
                        value=\"" . $formfields["isplabdslice"] . "\"
272 273 274 275 276
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
277
             <td colspan=2>issimnode:</td>
278 279 280
             <td class=left>
                 <input type=text
                        name=\"formfields[issimnode]\"
281
                        value=\"" . $formfields["issimnode"] . "\"
282 283 284 285
	                size=2>
             </td>
          </tr>\n";

286 287 288 289 290 291 292 293 294 295
    echo "<tr>
             <td colspan=2>isgeninode:</td>
             <td class=left>
                 <input type=text
                        name=\"formfields[isgeninode]\"
                        value=\"" . $formfields["isgeninode"] . "\"
	                size=2>
             </td>
          </tr>\n";

296 297 298 299 300 301 302 303 304 305
    echo "<tr>
             <td colspan=2>isfednode:</td>
             <td class=left>
                 <input type=text
                        name=\"formfields[isfednode]\"
                        value=\"" . $formfields["isfednode"] . "\"
	                size=2>
             </td>
          </tr>\n";

306 307 308 309 310 311 312 313 314
    #
    # Now do attributes.
    #
    echo "<tr></tr>
          <tr></tr>
           <td align=center><font size=-1>Delete?</font></td>
           <td align=center colspan=2><b>Node Attributes</b></td></tr>\n";

    while (list ($key, $val) = each ($attributes)) {
315 316 317 318
	if (!isset($deletes[$key])) {
	    # Somehow this doesn't get initialized in the Create Node case.
	    $deletes[$key] = "";
	}
319 320 321 322 323 324
	if ($key == "default_osid" ||
	    $key == "jail_osid" ||
	    $key == "delay_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $osid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
325 326 327 328 329
	elseif ($key == "adminmfs_osid" ||
		$key == "diskloadmfs_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $mfsosid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
330 331 332 333 334 335 336
	elseif ($key == $last_default_imageid_label) {
	    WRITEIMAGEIDMENU("$key<sup><b>1</b></sup>", "attributes[$key]", $imageid_result, $val,
			     "deletes[$key]", $deletes[$key]);
	}
	elseif ($key == "default_imageid" || 
    	        substr($key, 0, strlen("default_imageid_")) == "default_imageid_") {
	    WRITEIMAGEIDMENU("$key", "attributes[$key]", $imageid_result, $val,
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
			     "deletes[$key]", $deletes[$key]);
	}
	else {
	    echo "<tr>
                      <td align=center>
                            <input type=checkbox value=checked
                                    name=\"deletes[$key]\" $deletes[$key]></td>
                      <td>${key}:</td>
                      <td class=left>
                          <input type=text
                                 name=\"attributes[$key]\"
                                 value=\"" . $val . "\"></td>
                  </tr>\n";
	}
    }

    #
    # Provide area for adding a new attribute.
    # 
    echo "<tr></tr>
          <tr></tr>
           <td align=center colspan=3><b>Add New Attribute</b></td></tr>\n";

360
    echo "<tr>
361 362 363 364 365 366 367 368 369 370
           <td colspan=2>Attribute Name:</td>
           <td class=left>
               <input type=text name=newattribute_name
                      value=\"$newattribute_name\"></td>
          </tr>\n";
    echo "<tr>
           <td colspan=2>Attribute Value:</td>
           <td class=left>
               <input type=text name=newattribute_value
                      value=\"$newattribute_value\"></td>
371 372
          </tr>\n";

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    $typenames   = array();
    $typenames[] = "boolean";
    $typenames[] = "integer";
    $typenames[] = "float";
    $typenames[] = "string";
    
    echo "<tr>
           <td colspan=2>Attribute Type:</td>
           <td class=left>\n";

    foreach ($typenames as $i => $type) {
	$checked = "";

	if ($newattribute_type == $type)
	    $checked = "checked";
	
	echo "<input type=radio $checked name=newattribute_type
                     value=$type>$type\n";
    }
    echo "</td></tr>\n";

394
    echo "<tr>
395
              <td colspan=3 align=center>
396 397 398 399 400 401
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";
402 403 404 405

    echo "<blockquote>
	      <ol type=1 start=1>
		 <li> To add more than one image, add the first image and 
Grant Ayers's avatar
Grant Ayers committed
406
                      submit the form.  Then add the next, etc.</li>
407 408
              </ol>
              </blockquote>";
409 410
}

411
if (isset($new_type)) {
412
    #
413
    # Starting a new node type - give some reasonable defaults
414
    #
415 416 417
    $defaults = array("class" => "pc", "isvirtnode" => 0,
		      "isremotenode" => 0, "issubnode" => 0,
		      "isplabdslice" => 0, "isjailed" => 0, "isdynamic" => 0,
418
		      "issimnode" => 0, "isgeninode" => 0, "isfednode" => 0);
419 420 421 422 423 424 425 426 427 428 429

    $default_attributes = array();
    $attribute_types = array();
    $attribute_deletes = array();
    
    foreach ($initial_attributes as $entry) {
	$default_attributes[$entry['attrkey']] = $entry['attrvalue'];
	$attribute_types[$entry['attrkey']] = $entry['attrtype'];
	$attribute_deletes[$entry['attrkey']] = "";
    }
}
430
elseif (isset($node_type)) {
431
    #
432
    # Editing an existing type - suck the current info out of the
433 434 435 436 437 438 439
    # database.
    #
    if (!preg_match("/^[-\w]+$/", $node_type)) {
	USERERROR("$node_type contains illegal characters!", 1);    
    }
    $query_result =
	DBQueryFatal("select * from node_types where type='$node_type'");
440

441 442 443
    if (($defaults = mysql_fetch_array($query_result)) == 0) {
	USERERROR("$node_type is not a valid node type!", 1);
    }
444 445 446 447 448 449 450
    
    #
    # And the attributes ... needed below.
    #
    $default_attributes = array();
    $attribute_types = array();
    $attribute_deletes = array();
451

452 453 454 455 456 457 458 459
    $query_result =
	DBQueryFatal("select * from node_type_attributes ".
		     "where type='$node_type'");
    while ($row = mysql_fetch_array($query_result)) {
	$default_attributes[$row['attrkey']] = $row['attrvalue'];
	$attribute_types[$row['attrkey']] = $row['attrtype'];
	$attribute_deletes[$row['attrkey']] = "";
    }
460
    $attribute_types["default_imageid"] = "string";
461
}
462 463 464 465
else {
    PAGEARGERROR("Must provide one of node_type or new_type");
    return;
}
466

467
#
468
# We need lists of osids and imageids for selection.
469 470 471 472
#
$osid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path='' or path is NULL) ".
473
		 "order by pid,osname");
474

475 476 477 478 479
$mfsosid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path is not NULL and path!='') ".
		 "order by pid,osname");

480 481
$imageid_result =
    DBQueryFatal("select imageid,imagename,pid from images ".
482
		 "order by pid,imagename");
483 484 485 486 487

#
# On first load, display initial values.
#
if (! isset($submit)) {
488 489
    SPITFORM($node_type, $defaults,
	     $default_attributes, $attribute_deletes, 0);
490 491 492 493 494 495 496
    PAGEFOOTER();
    return;
}

#
# We do not allow these to be changed.
#
497
if (!isset($new_type)) {
498 499
    $formfields["class"] = $defaults["class"];
}
500 501

#
502
# Otherwise, must validate and redisplay if errors.
503 504 505
#
$errors  = array();

506 507 508 509
#
# Combine imageids into a comma seperated list
#
$default_imagesids = array();
510
$default_imagesid_once_defined = 0;
511 512 513 514 515
for ($i = 0; isset($attributes["default_imageid_$i"]); $i++) {
    if (!(isset($deletes["default_imageid_$i"]) && $deletes["default_imageid_$i"] == "checked")
        && $attributes["default_imageid_$i"] != 0)
        array_push($default_imagesids, $attributes["default_imageid_$i"]);
    unset($attributes["default_imageid_$i"]);
516 517 518 519 520 521 522
    $default_imagesid_once_defined = 1;
}
if (count($default_imagesids) > 0) {
    $attributes["default_imageid"] = join(',', $default_imagesids);
} elseif ($default_imagesid_once_defined) {
    $attributes["default_imageid"] = "0";
    $deletes["default_imageid"] = "checked";
523 524
}

525 526 527 528 529 530 531 532 533
# Check the attributes.
while (list ($key, $val) = each ($attributes)) {
    # Skip checks if scheduled for deletion
    if (isset($deletes[$key]) && $deletes[$key] == "checked") 
	continue;

    if (!isset($attribute_types[$key])) {
	$errors[$key] = "Unknown Attribute";
	continue;
534
    }
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    
    if ($val == "") {
	$errors[$key] = "No value provided for $key";
	continue;
    }

    # Probably redundant with the XML keyfields checking...
    $attrtype = $attribute_types[$key];
    if ($attrtype == "") {	# Shouldn't happen...
	$attrtype = $attribute_types[$key] = "integer";
    }
    if (strpos(":boolean:float:integer:string:", ":$attrtype:")===FALSE) {
	$errors[$key] = "Invalid type information: $attrtype";
	continue;
    }

    # New attributes require type and value.
    if (isset($newattribute_name) && $newattribute_name != "" &&
	!(isset($newattribute_type) && $newattribute_type != "")) {
	$errors[$newattribute_name] = "Missing type";
    }
    if (isset($newattribute_name) && $newattribute_name != "" &&
	!(isset($newattribute_value) && $newattribute_value != "")) {
	$errors[$newattribute_name] = "Missing value";
559 560 561
    }
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
    PAGEFOOTER();
    return;
}

#
# Build up argument array to pass along.
#
$args = array();

# Class (only for new types.)
if (isset($new_type) &&
    isset($formfields['class']) && $formfields['class'] != "") {
    $args["new_type"] = "1";
    $args["class"] = $formfields["class"];
}

584
# isvirtnode
585
if (isset($formfields["isvirtnode"]) && $formfields["isvirtnode"] != "") {
586
    $args["isvirtnode"] = $formfields["isvirtnode"];
587 588
}

589
# isjailed
590
if (isset($formfields["isjailed"]) && $formfields["isjailed"] != "") {
591
    $args["isjailed"] = $formfields["isjailed"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
592 593 594
}

# isdynamic
595
if (isset($formfields["isdynamic"]) && $formfields["isdynamic"] != "") {
596
    $args["isdynamic"] = $formfields["isdynamic"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
597 598
}

599
# isremotenode
600
if (isset($formfields["isremotenode"]) && $formfields["isremotenode"] != "") {
601
    $args["isremotenode"] = $formfields["isremotenode"];
602 603 604
}

# issubnode
605
if (isset($formfields["issubnode"]) && $formfields["issubnode"] != "") {
606
    $args["issubnode"] = $formfields["issubnode"];
607 608 609
}

# isplabdslice
610
if (isset($formfields["isplabdslice"]) && $formfields["isplabdslice"] != "") {
611
    $args["isplabdslice"] = $formfields["isplabdslice"];
612 613 614
}

# issimnode
615
if (isset($formfields["issimnode"]) && $formfields["issimnode"] != "") {
616
    $args["issimnode"] = $formfields["issimnode"];
617 618
}

619 620 621 622 623
# isgeninode
if (isset($formfields["isgeninode"]) && $formfields["isgeninode"] != "") {
    $args["isgeninode"] = $formfields["isgeninode"];
}

624 625 626 627 628
# isfednode
if (isset($formfields["isfednode"]) && $formfields["isfednode"] != "") {
    $args["isfednode"] = $formfields["isfednode"];
}

629 630 631 632 633 634
# Existing attributes.
foreach ($attributes as $attr_key => $attr_val) {
    if (isset($deletes[$attr_key]) && $deletes[$attr_key] == "checked") 
	$args["delete_${attr_key}"] = "1";
    $attr_type = $attribute_types[$attr_key];
    $args["attr_${attr_type}_${attr_key}"] = $attr_val;
635 636 637
}

#
638
# Form allows for adding a single new attribute, but someday be more fancy.
639
#
640 641 642 643 644 645 646 647 648 649 650 651 652
if (isset($newattribute_name) && $newattribute_name != "" &&
    isset($newattribute_value) && $newattribute_value != "" &&
    isset($newattribute_type) && $newattribute_type != "") {

    $args["new_attr"] = $newattribute_name;
    # The following is matched by wildcards on the other side of XML,
    # including checking its type and value, just like existing attributes.
    $args["attr_${newattribute_type}_$newattribute_name"] = $newattribute_value;
}

if (! ($result = SetNodeType($node_type, $args, $errors))) {
    # Always respit the form so that the form fields are not lost.
    # I just hate it when that happens so lets not be guilty of it ourselves.
653
    SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
654 655 656 657
    PAGEFOOTER();
    return;
}

Russ Fish's avatar
Russ Fish committed
658
PAGEHEADER(isset($new_type) ? "Create" : "Edit" . " Node Type");
659

660
#
661 662
# Spit out a redirect so that the history does not include a post
# in it. The back button skips over the post and to the form.
663
#
664
PAGEREPLACE("editnodetype.php3?node_type=$node_type");
665

666 667 668 669
#
# Standard Testbed Footer
# 
PAGEFOOTER();
670

671
#
672
# Create or edit a nodetype.  (No class for that at present.)
673
#
674 675
function SetNodeType($node_type, $args, &$errors) {
    global $suexec_output, $suexec_output_array;
676

677 678 679 680 681 682 683 684
    #
    # Generate a temporary file and write in the XML goo.
    #
    $xmlname = tempnam("/tmp", "editnodetype");
    if (! $xmlname) {
	TBERROR("Could not create temporary filename", 0);
	$errors[] = "Transient error(1); please try again later.";
	return null;
685
    }
686 687 688 689
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
	$errors[] = "Transient error(2); please try again later.";
	return null;
690
    }
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

    # Add these. Maybe caller should do this?
    $args["node_type"] = $node_type;
    
    fwrite($fp, "<nodetype>\n");
    foreach ($args as $name => $value) {
	fwrite($fp, "<attribute name=\"$name\">");
	fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	fwrite($fp, "</attribute>\n");
    }
    fwrite($fp, "</nodetype>\n");
    fclose($fp);
    chmod($xmlname, 0666);

    $retval = SUEXEC("nobody", "nobody", "webeditnodetype $xmlname",
		     SUEXEC_ACTION_IGNORE);

    if ($retval) {
	if ($retval < 0) {
710
	    $errors[] = "Transient error(3, $retval); please try again later.";
711
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
712 713
	}
	else {
714 715 716 717 718 719 720 721 722 723 724 725 726
	    # unlink($xmlname);
	    if (count($suexec_output_array)) {
		for ($i = 0; $i < count($suexec_output_array); $i++) {
		    $line = $suexec_output_array[$i];
		    if (preg_match("/^([-\w]+):\s*(.*)$/",
				   $line, $matches)) {
			$errors[$matches[1]] = $matches[2];
		    }
		    else
			$errors[] = $line;
		}
	    }
	    else
727
		$errors[] = "Transient error(4, $retval); please try again later.";
728
	}
729
	return null;
730
    }
731
    # There are no return value(s) to parse at the end of the output.
732

733 734 735 736 737
    # Unlink this here, so that the file is left behind in case of error.
    # We can then create the nodetype by hand from the xmlfile, if desired.
    unlink($xmlname);
    return true;
}
738 739

?>