record_revise.php 5.86 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#
include("defs.php3");

#
# Only known and logged in users can look at experiments.
#
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();

#
# Verify page arguments.
#
$reqargs = RequiredPageArguments("instance",   PAGEARG_INSTANCE,
				 "runidx",     PAGEARG_INTEGER);
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
				 "referrer",   PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);

$template = $instance->GetTemplate();

# Need these below.
$pid = $instance->pid();
$eid = $instance->eid();
$gid = $instance->gid();
48 49 50 51
$project = $instance->Project();
$unix_pid = $project->unix_gid();
$group    = Group::LookupByPidGid($pid, $gid);
$unix_gid = $group->unix_gid();
52 53 54 55 56 57 58

# Permission
if (!$isadmin &&
    !$instance->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to change this record!", 1);
}

59 60 61 62 63
#
# Standard Testbed Header
#
PAGEHEADER("Revise Run Record");

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
function SPITFORM($formfields, $errors)
{
    global $instance, $runidx, $TBDB_ARCHIVE_TAGLEN, $referrer;

    $iid   = $instance->id();
    $runid = $instance->GetRunID($runidx);

    echo $instance->RunPageHeader($runidx);

    echo "<br><br><center>
          Revise Record $runid in Instance $iid </center><br>\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, $message) = each ($errors)) {
88 89
            # XSS prevention.
	    $message = CleanString($message);
90 91 92 93 94 95 96 97 98
	    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";
    }
99 100 101 102
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
103 104 105 106 107 108 109

    $url = CreateURL("record_revise", $instance, "runidx", $runidx);

    echo "<table align=center border=1> 
          <form action='$url' method=post>\n";

    if (isset($referrer)) {
110 111
	$url = urlencode($referrer);
	echo "<input type=hidden name=referrer value='$url'>\n";
112 113 114 115 116 117 118 119 120 121 122 123
    }
    
    echo "<tr>
              <td align=center>
               <b>Please enter a message to be logged
                       with the revision</b>
              </td>
          </tr>
          <tr>
              <td align=center class=left>
                  <textarea name=\"formfields[message]\"
                    rows=10 cols=70>" .
124
	            str_replace("\r", "", $formfields["message"]) .
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	            "</textarea>
              </td>
          </tr>\n";

    echo "<tr>
              <td align=center>
                 <b><input type=submit name=submit value='Revise Record'></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";

    echo "<blockquote><blockquote>
          <ul>
            <li> This operation will update the end of run archive that was
                 saved when the run originally completed. The original record
                 will not be lost, and you can update as many times as you
                 like.</li>
          </ul>
          </blockquote></blockquote>\n";
}

#
# On first load, display a virgin form and exit.
#
if (! isset($submit)) {
    $defaults = array();
    $defaults["message"] = "";
    
    if (!isset($referrer))
	$referrer = $_SERVER['HTTP_REFERER'];
    
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

# Args to shell.
$command  = "-r $runidx -i " . $instance->exptidx();
$message  = "";
$tmpfname = Null;

function CLEANUP()
{
    global $tmpfname;

    if ($tmpfname != NULL)
	unlink($tmpfname);
    
    exit();
}
register_shutdown_function("CLEANUP");

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

if (isset($formfields["message"]) && $formfields["message"] != "") {
    if (! TBvalid_archive_message($formfields["message"])) {
	$errors["Message"] = TBFieldErrorString();
    }
    else {
	#
	# Easier to stick the entire message into a temporary file and
	# send that through. 
	#
	$tmpfname = tempnam("/tmp", "archive_tag");

	$fp = fopen($tmpfname, "w");
	fwrite($fp, $formfields["message"]);
	fclose($fp);
	chmod($tmpfname, 0666);
	
	$message = "-m $tmpfname";
    }
}
else {
    $errors["Message"] = "Please provide a minimal message!";
}

#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

STARTBUSY("Revising the record");

219
SUEXEC($uid, "$unix_pid,$unix_gid",
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
       "webtemplate_revise -t ReviseRecord $message $command ",
       SUEXEC_ACTION_DIE);

STOPBUSY();

if (!isset($referrer)) {
    $referrer = CreateURL("instance_show", $instance);
}

PAGEREPLACE($referrer);

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