template_modify.php 7.22 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2012 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
#
include("defs.php3");
25
include_once("template_defs.php");
26 27 28 29

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

34 35 36
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

37 38 39 40 41 42 43 44 45 46 47 48
#
# Verify page arguments
#
$reqargs = RequiredPageArguments("template",   PAGEARG_TEMPLATE);
$optargs = OptionalPageArguments("modify",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);

# Need these below.
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$unix_gid = $template->UnixGID();
49 50
$project  = $template->GetProject();
$unix_pid = $project->unix_gid();
51 52 53 54 55 56

if (! $template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to export in template ".
	      "$guid/$vers!", 1);
}

57 58 59 60 61
#
# Standard Testbed Header
#
PAGEHEADER("Modify Experiment Template");

62 63 64
#
# Spit the form out using the array of data.
#
65
function SPITFORM($template, $formfields, $errors)
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
{
    global $TBDB_PIDLEN, $TBDB_GIDLEN, $TBDB_EIDLEN, $TBDOCBASE;

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

91
    echo $template->PageHeader();
92 93
    echo "<br><br>\n";

94 95 96
    $url = CreateURL("template_modify", $template);

    echo "<form action='$url' method='post'>";
97 98 99 100 101 102 103 104 105 106 107
    echo "<table align=center border=1>\n";

    #
    # TID:
    #
    echo "<tr>
              <td class='pad4'>Template ID:
              <br><font size='-1'>(alphanumeric, no blanks)</font></td>
              <td class='pad4' class=left>
                  <input type=text
                         name=\"formfields[tid]\"
108
                         value=\"" . $formfields["tid"] . "\"
109 110
	                 size=$TBDB_EIDLEN
                         maxlength=$TBDB_EIDLEN>
111
              &nbsp (optionally change this to something informative).
112 113 114 115 116 117 118 119 120 121 122 123
              </td>
          </tr>\n";

    echo "<tr>
              <td colspan=2>
               Use this text area to optionally describe your template:
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
                    rows=4 cols=100>" .
124
	            str_replace("\r", "", $formfields["description"]) .
125 126 127 128 129 130 131 132
	           "</textarea>
              </td>
          </tr>\n";


    echo "<tr>
              <td colspan=2>
              <textarea name=\"formfields[nsdata]\"
133
                   cols=100 rows=40>" . $formfields["nsdata"] . "</textarea>
134 135 136 137 138 139 140 141 142 143 144 145
              </td>
          </tr>\n";

    echo "<tr>
              <td class='pad4' align=center colspan=2>
                <b><input type=submit name=modify value='Modify Template'></b>
              </td>
         </tr>
        </form>
        </table>\n";
}

146 147 148 149 150 151 152
#
# Put up the modify form on first load.
# 
if (! isset($modify)) {
    #
    # Grab NS file for the template.
    #
153
    $input_list  = $template->InputFiles();
154

155
    $defaults = array();
156
    $defaults["tid"] = $template->NextTID();
157
    $defaults["nsdata"] = $input_list[0];
158
    $defaults["description"] = $template->description();
159
    SPITFORM($template, $defaults, 0);
160 161 162
    PAGEFOOTER();
    exit();
}
163 164 165
elseif (! isset($formfields)) {
    PAGEARGERROR();
}
166 167

#
168
# Okay, validate form arguments.
169
#
170 171
$errors       = array();
$command_args = "";
172 173

#
174
# Generate a hopefully unique filename that is hard to guess. See below.
175 176 177 178 179
# 
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$foo = rand();

180 181 182
#
# TID
#
183
if (!isset($formfields["tid"]) || $formfields["tid"] == "") {
184
    #
185
    # Generate a new one.
186
    #
187
    $tid = $template->NextTID();
188
}
189
elseif (!TBvalid_eid($formfields["tid"])) {
190 191 192
    $errors["Template ID"] = TBFieldErrorString();
}
else {
193
    $tid = $formfields["tid"];
194
}
195 196

#
197 198
# Description:
# 
199
if (!isset($formfields["description"]) || $formfields["description"] == "") {
200 201
    $errors["Description"] = "Missing Field";
}
202
elseif (!TBvalid_template_description($formfields["description"])) {
203 204 205
    $errors["Description"] = TBFieldErrorString();
}
else {
206
    $command_args .= " -E " . escapeshellarg($formfields["description"]);
207 208
}

209
#
210 211
# NS File.
#
212
if (!isset($formfields["nsdata"]) || $formfields["nsdata"] == "") {
213 214 215 216
    $errors["NS File"] = "Missing Field";
}

if (count($errors)) {
217
    SPITFORM($template, $formfields, $errors);
218 219 220 221
    PAGEFOOTER();
    exit(1);
}

Leigh Stoller's avatar
Leigh Stoller committed
222 223 224 225
echo "<script type='text/javascript' language='javascript' ".
     "        src='template_sup.js'>\n";
echo "</script>\n";

226 227 228 229 230 231 232 233
#
# Generate a unique and hard to guess filename, and write NS to it.
#
$nsfile = "/tmp/$uid-$foo.nsfile";

if (! ($fp = fopen($nsfile, "w"))) {
    TBERROR("Could not create temporary file $nsfile", 1);
}
234
fwrite($fp, $formfields["nsdata"]);
235 236 237
fclose($fp);
chmod($nsfile, 0666);

238
STARTBUSY("Starting template modification!");
239 240

# And run that script!
241
$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
242
		 "webtemplate_create ".
243
		 "-m $guid/$vers $command_args $pid $tid $nsfile",
244 245
		 SUEXEC_ACTION_IGNORE);
unlink($nsfile);
Leigh Stoller's avatar
Leigh Stoller committed
246
/* Clear the various 'loading' indicators. */
247
HIDEBUSY();
248 249

if ($retval) {
250 251 252 253 254 255 256 257
    #
    # 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);
    }
    # User error. Tell user and exit.
258 259 260 261
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
}

262 263 264
#
# Parse the last line of output. Ick.
#
265
if (preg_match("/^Template\s+(\w+)\/(\w+)\s+is being/",
266 267 268 269
	       $suexec_output_array[count($suexec_output_array)-1],
	       $matches)) {
    $guid = $matches[1];
    $vers = $matches[2];
Leigh Stoller's avatar
Leigh Stoller committed
270

271 272 273 274
    $template = Template::Lookup($guid, $vers);
    if (! $template) {
	TBERROR("Could not lookup template object for $guid/$vers", 1);
	return;
275
    }
276 277 278
    echo $template->PageHeader();
    echo "<br><br>\n";
    STARTLOG($template);
279
}
280 281 282
else {
    SUEXECERROR(SUEXEC_ACTION_DIE);
}    
283 284 285 286 287 288

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>