editsitevars.php3 8.79 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2007 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 25 26 27 28
#
include("defs.php3");

#
# Only known and logged in users can do this.
#
29 30 31
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
32 33

if (! $isadmin) {
34
    USERERROR("You do not have admin privileges to edit site variables!", 1);
35 36
}

37
#
38
# Verify page arguments.
39
#
40 41 42 43
$optargs = OptionalPageArguments(# Edit greenballs pull up an Edit subform.
				 "edit",       PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY,
				 # Edit has three submit buttons.
44 45 46
				 "defaulted",  PAGEARG_STRING,
				 "edited",     PAGEARG_STRING,
				 "canceled",   PAGEARG_STRING);
47

48 49 50 51 52
#
# Standard Testbed Header
#
PAGEHEADER("Edit Site Variables");

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
function SPIT_MSGS($message, $errors)
{
    if ($message !== "") {
	echo "<H3>$message</H3>\n";
    }

    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, $text) = each ($errors)) {
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$text</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
}

function SPITFORM($message, $errors) {
    SPIT_MSGS($message, $errors);

    echo "<table>
	  <tr>
	    <th>&nbsp;Name&nbsp;</th>
	    <th>&nbsp;Value&nbsp;</th>
	    <th><font size='-1'>Edit</font></th>
	    <th>&nbsp;Default Value&nbsp;</th>
	    <th>&nbsp;Description&nbsp;</th> 
	  </tr>";
93 94 95

    $result = DBQueryFatal("SELECT name, value, defaultvalue, description ".
			   "FROM sitevariables ".
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			   "ORDER BY name");

    while ($row = mysql_fetch_row($result)) {
	$name         = $row[0];
	$value        = $row[1];
	$defaultvalue = $row[2];
	$description  = $row[3];
	$cginame      = urlencode($name);

	echo "<tr><td>&nbsp;<b>$name</b>&nbsp;</td>\n";

	echo "<td>&nbsp;";
	if (isset($value)) {
	    if (0 != strcmp($value, "")) {
		$wrapped_value = wordwrap($value, 30, "<br />", 1);
		echo "<code>$wrapped_value</code>&nbsp;</td>";
	    } else {
		echo "<font color='#00B040'><i>empty string</i></font>&nbsp;</td>";	
	    }
	} else {
	    echo "<font color='#00B040'><i>default</i></font>&nbsp;</td>";
	}
	echo "<td align=center>";
	echo "&nbsp;<a href='editsitevars.php3?edit=$cginame'>";
	echo "<img border='0' src='greenball.gif' /></a>";
	echo "&nbsp;</td>";

	echo "<td nowrap='1'>&nbsp;";
	if (0 != strcmp($defaultvalue, "")) {
	    echo "<code>$defaultvalue</code>&nbsp;</td>";
	} else {
	    echo "<font color='#00B040'><i>empty string</i></font>&nbsp;</td>";	
	}

	echo "<td>&nbsp;$description&nbsp;</td></tr>\n";
    }

    echo "</table>";
}

# The "Edit" greenball column displays an edit subform for individual vars.
function SPIT_SUBFORM($formfields, $message, $errors)
{
    SPIT_MSGS($message, $errors);

    $name = $formfields["name"];
    $result = DBQueryFatal("SELECT name, value, defaultvalue, description ".
			   "FROM sitevariables ".
			   "WHERE name='$name'");
145 146 147 148 149 150

    if ($row = mysql_fetch_row($result)) {
	$name         = $row[0];
	$value        = $row[1];
	$defaultvalue = $row[2];
	$description  = $row[3];
151

152 153
	echo "<center>";
	echo "<form action='editsitevars.php3' method='post'>";
154 155 156 157

	echo "<input type='hidden' name=\"formfields[name]\"
		     value=\"" . $formfields["name"] . "\">";

158 159 160 161 162 163 164 165 166 167 168 169
	echo "<table><tr><th colspan=2>";
	echo "Editing site variable '<b>$name</b>':";
	echo "</th></tr><tr><td>";
	echo "<b>Description:</b></td><td>$description</font>";
	echo "</td></tr><tr><td>";
	echo "<b>Default value:</b></td><td>";
	if (0 != strcmp($defaultvalue,"")) {
	    echo "<code>$defaultvalue</code>";
	} else {
	    echo "<font color='#00B040'><i>Empty String</i></font>";	    
	}
	echo "</td></tr><tr><td>&nbsp;</td><td>";
170 171 172 173

	echo "<input type='submit' name='defaulted' ".
	     "       value='Reset to Default Value'></input>";

174 175
	echo "</td></tr><tr><td>";
	echo "<b>New value:</b></td><td>";
176 177 178
	echo "<input size='60' type='text'
		     name=\"formfields[value]\"
		     value=\"" . $value . "\">";
179
	echo "</td></tr><tr><td>&nbsp;</td><td>";
180 181 182

	echo "<input type='submit' name='edited'
	    	     value='Change to New Value'></input>";
183
	echo "&nbsp;";
184 185 186 187

	echo "<input type='submit' name='canceled'
		     value='Cancel'>";

188 189 190 191 192 193
	echo "</td></tr></table>";
	echo "</form>";
	echo "</center>";
    }
}

194 195 196 197 198
#
# Accumulate error reports for the user, e.g.
#    $errors["Key"] = "Msg";
# Required page args may need to be checked early.
$errors  = array();
199

200 201
# Show an optional status message.
$message = "";
202

203 204 205 206 207
#
#  On first load, just display current values.
#
if (!isset($edit) && 
    !isset($edited) && !isset($defaulted) && !isset($canceled)) {
208
    SPITFORM("", $errors);
209 210 211 212
    PAGEFOOTER();
    return;
}

213 214 215 216 217 218 219 220 221 222 223
# The "Edit" greenball column displays an edit subform for individual vars.
if (isset($edit)) {
    if (!TBSiteVarExists($edit)) {
	USERERROR("Couldn't find variable '$edit'!", 1);
    }
    else {
	$formfields["name"] = $edit;
	SPIT_SUBFORM($formfields, $message, $errors);
	PAGEFOOTER();
	return;
    }
224 225
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#
# Build up argument array to pass along.
#
$args = array();
$name = "";
if (isset($formfields["name"]) && $formfields["name"] != "") {
    $name = $formfields["name"];
}

# Actions for the Edit subform.
$do_change = 0;
if (isset($canceled)) {		# Action for the Cancel submit button.
    $message = "Operation canceled.";
}
elseif (isset($edited)) {	# Action for the "Change to new value" button.
    if (isset($formfields["value"]) && $formfields["value"] != "") {
	$args["value"] = $value = $formfields["value"];
243
    }
244 245 246 247 248 249 250 251
    $message = "Setting '$name' to '$value'.";
    $do_change = 1;
}
elseif (isset($defaulted)) {	# Action for "Reset to Default value" button.
    $message = "Resetting '$name' to default.";
    $do_change = 1;
    $args["reset"] = "1";
}
252

253 254
if ($do_change) {
    $result = SetSiteVar($name, $args, $errors);
255 256
}

257 258
# Respit the form to see the result.
SPITFORM($message, $errors);
259 260 261 262 263

#
# Standard Testbed Footer
# 
PAGEFOOTER();
264
return;
265
			
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
#
# Edit site variables.  (No class for them at present.)
function SetSiteVar($name, $args, &$errors) {
    global $suexec_output, $suexec_output_array;

    #
    # Generate a temporary file and write in the XML goo.
    #
    $xmlname = tempnam("/tmp", "editsitevars");
    if (! $xmlname) {
	TBERROR("Could not create temporary filename", 0);
	$errors[] = "Transient error(1); please try again later.";
	return null;
    }
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
	$errors[] = "Transient error(2); please try again later.";
	return null;
    }

    # Add these. Maybe caller should do this?
    $args["name"] = $name;

    fwrite($fp, "<sitevar>\n");
    foreach ($args as $name => $value) {
	fwrite($fp, "<attribute name=\"$name\">");
	fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	fwrite($fp, "</attribute>\n");
    }
    fwrite($fp, "</sitevar>\n");
    fclose($fp);
    chmod($xmlname, 0666);

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

    if ($retval) {
	if ($retval < 0) {
304
	    $errors[] = "Transient error(3, $retval); please try again later.";
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	}
	else {
	    # 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
321
		$errors[] = "Transient error(4, $retval); please try again later.";
322 323 324 325 326 327 328 329 330 331 332 333 334
	}
	return null;
    }

    # There are no return value(s) to parse at the end of the output.

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

?>