spitnsdata.php3 4.5 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003, 2005-2006 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7 8
include("defs.php3");

9 10 11 12 13 14
#
# Only known and logged in users can begin experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

15 16 17 18 19 20 21 22 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 48 49 50 51 52 53 54 55 56 57 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
#
# This comes from the begin_experiment page, when cloning an experiment
# from another experiment.
#
if (isset($copyid) && $copyid != "") {
    unset($exptidx);
    unset($copypid);
    unset($copyeid);
    unset($copytag);
    
    #
    # See what kind of copyid.
    #
    if (preg_match("/^(\d+)(?::([-\w]*))?$/", $copyid, $matches)) {
	$exptidx = $matches[1];
	$copytag = $matches[2];
	    
	if (TBvalid_integer($exptidx)) {
            #
	    # See if its a current experiment.
	    #
	    $query_result =
		DBQueryFatal("select pid,eid from experiments ".
			     "where idx='$exptidx'");
		
	    if (mysql_num_rows($query_result)) {
		$row = mysql_fetch_row($query_result);

		$copypid = $row[0];
		$copyeid = $row[1];
	    }
	}
    }
    elseif (preg_match("/^([-\w]+),([-\w]+)(?::([-\w]*))?$/",
		       $copyid, $matches)) {
	$copypid = $matches[1];
	$copyeid = $matches[2];
	$copytag = $matches[3];
    }
    else {
	PAGEARGERROR("Invalid ID");
    }

    if (isset($copypid) && isset($copyeid) &&
	(!isset($copytag) || $copytag == "")) {
	$pid = $copypid;
	$eid = $copyeid;
	# Fall through to below.
    }
    elseif (isset($exptidx)) {
	#
	# By convention, this means to always go to the archive. There
	# must be a tag.
	#
	if (! isset($copytag) || $copytag == "") {
	    PAGEARGERROR("Must supply a tag");
	}
	if (! isset($copypid)) {
	    #
	    # Ick, map to pid,eid so we can generate a proper path into
	    # the archive. This is bad. 
	    #
	    $query_result =
		DBQueryFatal("select pid,eid from experiments_stats ".
			     "where exptidx='$exptidx'");
		
	    if (!mysql_num_rows($query_result)) {
		PAGEARGERROR("Invalid experiment index!", 1);
	    }
	    $row = mysql_fetch_row($query_result);
	    $copypid = $row[0];
	    $copyeid = $row[1];
	}
	header("Location: cvsweb/cvsweb.php3/$exptidx/tags/$copytag/".
	       "proj/$copypid/exp/$copyeid/${copyeid}.ns?".
	       "exptidx=$exptidx&view=markup");
    }
    else {
	PAGEARGERROR("");
    }
}

97
#
98
# Spit back an NS file to the user. 
99
#
100 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
# if requesting a specific pid,eid must have permission.
#
if (isset($pid) && isset($eid)) {
    #
    # Check to make sure this is a valid PID/EID tuple.
    #
    if (! TBValidExperiment($pid, $eid)) {
	USERERROR("Experiment $eid is not a valid experiment ".
		  "in project $pid.", 1);
    }

    #
    # Verify Permission.
    #
    if (! TBExptAccessCheck($uid, $pid, $eid, $TB_EXPT_READINFO)) {
	USERERROR("You do not have permission to view the NS file for ".
		  "experiment $eid in project $pid!", 1);
    }

    #
    # Grab the NS file from the DB.
    #
    $query_result =
	DBQueryFatal("select nsfile from nsfiles ".
		     "where pid='$pid' and eid='$eid'");
    if (mysql_num_rows($query_result) == 0) {
	USERERROR("There is no NS file recorded for ".
		  "experiment $eid in project $pid!", 1);
    }
    $row    = mysql_fetch_array($query_result);
130
    $nsfile = $row["nsfile"];
131 132 133 134 135
    
    header("Content-Type: text/plain");
    echo "$nsfile\n";
    return;
}
136 137

#
138 139 140 141 142 143 144 145
# Otherwise:
#
# See if nsdata was provided. I think nsdata is deprecated now, and
# netbuild uses the nsref variant (LBS).
#
# See if nsref was provided. This is how the current netbuild works, by 
# uploading the nsfile with nssave.php3, to a randomly generated name in 
# /tmp. Spit that file back.
146
#
147 148 149
if (isset($nsdata) && strcmp($nsdata, "") != 0) {
    header("Content-Type: text/plain");
    echo "$nsdata";
150 151 152 153 154 155 156 157 158
} elseif (isset($nsref) && strcmp($nsref,"") != 0 && 
          ereg("^[0-9]+$", $nsref)) {
    if (isset($guid) && ereg("^[0-9]+$", $guid)) {
	$nsfile = "/tmp/$guid-$nsref.nsfile";    
        $id = $guid;
    } else {
	$nsfile = "/tmp/$uid-$nsref.nsfile";    
        $id = $uid;
    }
159 160 161

    if (! file_exists($nsfile)) {
	PAGEHEADER("View Generated NS File");
162 163
	USERERROR("Could not find temporary file for user/guid \"" . $id .
                  "\" with id \"$nsref\".<br>\n" . 
164
	          "You likely copy-and-pasted an URL incorrectly,<br>\n" .
165 166
 		  "or you've already used the file to create an experiment" . 
                  "(thus erasing it),<br>\n" .
167
		  "or the file has expired.\n", 1 );
168
	PAGEFOOTER();
169
    } else {
170
	$fp = fopen($nsfile, "r");
171 172 173 174 175 176
	header("Content-Type: text/plain");
	$contents = fread ($fp, filesize ($nsfile));
	fclose($fp);
	echo "$contents";
    }
} else {
177
    USERERROR("No NS file provided!",1);
178 179 180
}

?>
181 182 183 184 185 186