spitnsdata.php3 4.81 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2
#
Mike Hibler's avatar
Mike Hibler committed
3
# Copyright (c) 2000-2010 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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24
include("defs.php3");
25
include_once("template_defs.php");
26

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

34 35 36 37 38
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("experiment",   PAGEARG_EXPERIMENT,
				 "template",     PAGEARG_TEMPLATE,
39
				 "copyid",       PAGEARG_INTEGER,
40
				 "record",       PAGEARG_INTEGER,
41 42 43 44
				 "nsref",        PAGEARG_INTEGER,
				 "guid",         PAGEARG_INTEGER,
				 "nsdata",       PAGEARG_ANYTHING);

45 46 47 48 49
#
# This comes from the begin_experiment page, when cloning an experiment
# from another experiment.
#
if (isset($copyid) && $copyid != "") {
50
    if (TBvalid_integer($copyid)) {
51
	#
52
	# See if its a current experiment.
53
	#
54 55 56
	$experiment = Experiment::Lookup($copyid);
	if (!$experiment) {
	    $record = $copyid;
57 58 59
	}
    }
    else {
60
	PAGEARGERROR("Invalid copyID!");
61 62 63
    }
}

64
#
65
# Spit back an NS file to the user. 
66
#
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
if (isset($record) && $record != "" && TBvalid_integer($record)) {
    $experiment_resources = ExperimentResources::Lookup($record);
    if (! $experiment_resources) {
	USERERROR("No such experiment resources record $record!", 1);
    }
    if (!$experiment_resources->AccessCheck($this_user, $TB_EXPT_READINFO)) {
	USERERROR("You do not have permission to view the NS file for ".
		  "experiment resource record $record!", 1);
    }
    
    #
    # Grab the NS file.
    #
    if (($nsfile = $experiment_resources->NSFile())) {
	header("Content-Type: text/plain");
	echo "$nsfile\n";
    }
    else {
	USERERROR("There is no NS file recorded for ".
		  "experiment resource record $record!", 1);
    }
    return;
}
90 91 92 93

#
# A template.
#
94 95
if (isset($template)) {
    if (! $template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
Leigh Stoller's avatar
Leigh Stoller committed
96
	USERERROR("You do not have permission to view template!", 1);
97 98 99 100 101 102
    }
    header("Content-Type: text/plain");

    #
    # Grab all of the input files. Display each one. 
    #
Leigh Stoller's avatar
Leigh Stoller committed
103
    $input_list = $template->InputFiles();
104

Leigh Stoller's avatar
Leigh Stoller committed
105 106
    for ($i = 0; $i < count($input_list); $i++) {
	echo $input_list[$i];
107 108 109 110 111
	echo "\n\n";
    }
    return;
}

112
#
113 114
# if requesting a specific pid,eid must have permission.
#
115
if (isset($experiment)) {
116 117 118
    #
    # Verify Permission.
    #
119
    if (!$experiment->AccessCheck($this_user, $TB_EXPT_READINFO)) {
120 121 122 123 124 125 126
	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.
    #
127 128 129 130 131
    if (($nsfile = $experiment->NSFile())) {
	header("Content-Type: text/plain");
	echo "$nsfile\n";
    }
    else {
Russ Fish's avatar
Russ Fish committed
132 133
	$pid = $experiment->pid();
	$eid = $experiment->eid();
134 135 136 137 138
	USERERROR("There is no NS file recorded for ".
		  "experiment $eid in project $pid!", 1);
    }
    return;
}
139 140

#
141 142 143 144 145 146 147 148
# 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.
149
#
150 151 152
if (isset($nsdata) && strcmp($nsdata, "") != 0) {
    header("Content-Type: text/plain");
    echo "$nsdata";
153
} elseif (isset($nsref) && strcmp($nsref,"") != 0 && 
Mike Hibler's avatar
Mike Hibler committed
154 155
          preg_match('/^[0-9]+$/', $nsref)) {
    if (isset($guid) && preg_match('/^[0-9]+$/', $guid)) {
156 157 158 159 160 161
	$nsfile = "/tmp/$guid-$nsref.nsfile";    
        $id = $guid;
    } else {
	$nsfile = "/tmp/$uid-$nsref.nsfile";    
        $id = $uid;
    }
162 163 164

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

?>
184 185 186 187 188 189