archive_tag.php3 6.04 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006-2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11
# All rights reserved.
#
include("defs.php3");

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

#
# Verify page arguments.
18
#
19 20 21
$reqargs = RequiredPageArguments("experiment", PAGEARG_EXPERIMENT);
$optargs = OptionalPageArguments("submit", PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);
22

23 24 25 26
# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();
$gid = $experiment->gid();
27

28
# Permission
29
if (!$isadmin &&
30
    !$experiment->AccessCheck($this_user, $TB_PROJECT_READINFO)) {
31
    USERERROR("You do not have permission to view tags for ".
32
	      "archive in $pid/$eid!", 1);
33 34
}

35 36 37 38 39
#
# Standard Testbed Header
#
PAGEHEADER("Commit and Tag");

40 41
function SPITFORM($formfields, $errors)
{
42
    global $experiment, $TBDB_ARCHIVE_TAGLEN, $referrer;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

    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)) {
60 61
            # XSS prevention.
	    $message = CleanString($message);
62 63 64 65 66 67 68 69 70 71
	    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";
    }

72 73 74 75 76
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }

77
    echo "<table align=center border=1> 
78 79
          <form action='" . CreateURL("archive_tag", $experiment) . "' ".
	        "method=post>\n";
80

81
    if (isset($referrer)) {
82
	$referrer = urlencode($referrer);
83 84 85
	echo "<input type=hidden name=referrer value=$referrer>\n";
    }
    
86 87 88 89 90 91 92 93 94 95
    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]\"
96
                         value=\"" . $formfields["tag"] . "\"
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	                 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>" .
112
	            ereg_replace("\r", "", $formfields["message"]) .
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
	            "</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.
#
140
if (! isset($submit)) {
141
    $defaults = array();
142 143 144
    $defaults["tag"]     = "";
    $defaults["message"] = "";
    
145 146 147
    if (!isset($referrer))
	$referrer = $_SERVER['HTTP_REFERER'];
    
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
    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;
}

218 219
STARTBUSY("Committing and Tagging!");

220 221 222
#
# First lets make sure the tag is unique. 
#
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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);
    }
238

239 240 241
    # User error. Tell user and exit.
    if ($retval) {
	$errors["Tag"] = "Already in use; pick another";
242
    
243 244 245 246
	SPITFORM($formfields, $errors);
	PAGEFOOTER();
	return;
    }
247 248 249
}

SUEXEC($uid, "$pid,$gid",
250
       "webarchive_control $tagarg $message -u commit $pid $eid",
251 252
       SUEXEC_ACTION_DIE);

253 254
STOPBUSY();

255 256
if (!isset($referrer)) {
    $exptidx  = $experiment->idx();
257
    $referrer = "archive_view.php3/$exptidx/?exptidx=$exptidx";
258
}
259 260 261 262 263 264 265

PAGEREPLACE($referrer);

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