archive_tag.php3 6.73 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
#
include("defs.php3");

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

#
# Verify page arguments.
35
#
36 37 38
$reqargs = RequiredPageArguments("experiment", PAGEARG_EXPERIMENT);
$optargs = OptionalPageArguments("submit", PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);
39

40 41 42 43
# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();
$gid = $experiment->gid();
44

45
# Permission
46
if (!$isadmin &&
47
    !$experiment->AccessCheck($this_user, $TB_PROJECT_READINFO)) {
48
    USERERROR("You do not have permission to view tags for ".
49
	      "archive in $pid/$eid!", 1);
50 51
}

52 53 54 55 56
#
# Standard Testbed Header
#
PAGEHEADER("Commit and Tag");

57 58
function SPITFORM($formfields, $errors)
{
59
    global $experiment, $TBDB_ARCHIVE_TAGLEN, $referrer;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    echo "<center>
          Commit/Tag Archive
          </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)) {
77 78
            # XSS prevention.
	    $message = CleanString($message);
79 80 81 82 83 84 85 86 87 88
	    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";
    }

89 90 91 92 93
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }

94
    echo "<table align=center border=1> 
95 96
          <form action='" . CreateURL("archive_tag", $experiment) . "' ".
	        "method=post>\n";
97

98
    if (isset($referrer)) {
99
	$referrer = urlencode($referrer);
100 101 102
	echo "<input type=hidden name=referrer value=$referrer>\n";
    }
    
103 104 105 106 107 108 109 110 111 112
    echo "<tr>
              <td align=center>
               <b>Please enter a tag[<b>1</b>]</b>
              </td>
          </tr>\n";

    echo "<tr>
              <td class=left>
                  <input type=text
                         name=\"formfields[tag]\"
113
                         value=\"" . $formfields["tag"] . "\"
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	                 size=$TBDB_ARCHIVE_TAGLEN
                         maxlength=$TBDB_ARCHIVE_TAGLEN>
          </tr>\n";


    echo "<tr>
              <td align=center>
               <b>Please enter an optional message to be logged
                       with the tag.</b>
              </td>
          </tr>
          <tr>
              <td align=center class=left>
                  <textarea name=\"formfields[message]\"
                    rows=10 cols=70>" .
129
	            str_replace("\r", "", $formfields["message"]) .
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
	            "</textarea>
              </td>
          </tr>\n";

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

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

    echo "<blockquote><blockquote><blockquote>
          <ol>
            <li> Optional tag must contain only alphanumeric characters,
                 starting with an alpha character. If no tag is supplied,
                 we will make one up for you, but that is probably not
                 what you want.

          </ol>
          </blockquote></blockquote></blockquote>\n";
}

#
# On first load, display a virgin form and exit.
#
157
if (! isset($submit)) {
158
    $defaults = array();
159 160 161
    $defaults["tag"]     = "";
    $defaults["message"] = "";
    
162 163 164
    if (!isset($referrer))
	$referrer = $_SERVER['HTTP_REFERER'];
    
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

# Args to shell.
$tag      = "";
$tagarg   = "";
$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();

#
# Tag
#
if (isset($formfields["tag"]) && $formfields["tag"] != "") {
    if (! TBvalid_archive_tag($formfields["tag"])) {
	$errors["Tag"] = TBFieldErrorString();
    }
    else {
	$tag = escapeshellarg($formfields["tag"]);
	$tagarg = "-t $tag" ;
    }
}

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

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

235 236
STARTBUSY("Committing and Tagging!");

237 238 239
#
# First lets make sure the tag is unique. 
#
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
if ($tag != "") {
    $retval = SUEXEC($uid, "$pid,$gid",
		     "webarchive_control checktag $pid $eid $tag",
		     SUEXEC_ACTION_IGNORE);

    /* Clear the various 'loading' indicators. */
    if ($retval) 
	CLEARBUSY();

    #
    # Fatal Error. 
    # 
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_DIE);
    }
255

256 257 258
    # User error. Tell user and exit.
    if ($retval) {
	$errors["Tag"] = "Already in use; pick another";
259
    
260 261 262 263
	SPITFORM($formfields, $errors);
	PAGEFOOTER();
	return;
    }
264 265 266
}

SUEXEC($uid, "$pid,$gid",
267
       "webarchive_control $tagarg $message -u commit $pid $eid",
268 269
       SUEXEC_ACTION_DIE);

270 271
STOPBUSY();

272 273
if (!isset($referrer)) {
    $exptidx  = $experiment->idx();
274
    $referrer = "archive_view.php3/$exptidx/?exptidx=$exptidx";
275
}
276 277 278 279 280 281 282

PAGEREPLACE($referrer);

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