editsitevars.php3 7.03 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2007, 2018 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 39 40 41
if ($PORTAL_ENABLE || $CLUSTER_PORTAL != "") {
    header("Location: ${TBBASE}/portal/sitevars.php");
    return;
}

42
#
43
# Verify page arguments.
44
#
45 46 47 48
$optargs = OptionalPageArguments(# Edit greenballs pull up an Edit subform.
				 "edit",       PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY,
				 # Edit has three submit buttons.
49 50 51
				 "defaulted",  PAGEARG_STRING,
				 "edited",     PAGEARG_STRING,
				 "canceled",   PAGEARG_STRING);
52

53 54 55 56 57
#
# Standard Testbed Header
#
PAGEHEADER("Edit Site Variables");

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
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>";
98 99 100

    $result = DBQueryFatal("SELECT name, value, defaultvalue, description ".
			   "FROM sitevariables ".
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 145 146 147 148 149
			   "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'");
150 151 152 153 154 155

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

157 158
	echo "<center>";
	echo "<form action='editsitevars.php3' method='post'>";
159 160 161 162

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

163 164 165 166 167 168 169 170 171 172 173 174
	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>";
175 176 177 178

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

179 180
	echo "</td></tr><tr><td>";
	echo "<b>New value:</b></td><td>";
181 182 183
	echo "<input size='60' type='text'
		     name=\"formfields[value]\"
		     value=\"" . $value . "\">";
184
	echo "</td></tr><tr><td>&nbsp;</td><td>";
185 186 187

	echo "<input type='submit' name='edited'
	    	     value='Change to New Value'></input>";
188
	echo "&nbsp;";
189 190 191 192

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

193 194 195 196 197 198
	echo "</td></tr></table>";
	echo "</form>";
	echo "</center>";
    }
}

199 200 201 202 203
#
# Accumulate error reports for the user, e.g.
#    $errors["Key"] = "Msg";
# Required page args may need to be checked early.
$errors  = array();
204

205 206
# Show an optional status message.
$message = "";
207

208 209 210 211 212
#
#  On first load, just display current values.
#
if (!isset($edit) && 
    !isset($edited) && !isset($defaulted) && !isset($canceled)) {
213
    SPITFORM("", $errors);
214 215 216 217
    PAGEFOOTER();
    return;
}

218 219 220 221 222 223 224 225 226 227 228
# 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;
    }
229 230
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#
# 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"];
248
    }
249 250 251 252 253 254 255 256
    $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";
}
257

258 259
if ($do_change) {
    $result = SetSiteVar($name, $args, $errors);
260 261
}

262 263
# Respit the form to see the result.
SPITFORM($message, $errors);
264 265 266 267 268

#
# Standard Testbed Footer
# 
PAGEFOOTER();
269
return;
270
			
271
?>