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

10
#
11
# Only known and logged in users.
12
#
13 14 15
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
16

17 18 19 20 21
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("experiment",   PAGEARG_EXPERIMENT,
				 "template",     PAGEARG_TEMPLATE,
22
				 "copyid",       PAGEARG_INTEGER,
23
				 "record",       PAGEARG_INTEGER,
24 25 26 27
				 "nsref",        PAGEARG_INTEGER,
				 "guid",         PAGEARG_INTEGER,
				 "nsdata",       PAGEARG_ANYTHING);

28 29 30 31 32
#
# This comes from the begin_experiment page, when cloning an experiment
# from another experiment.
#
if (isset($copyid) && $copyid != "") {
33
    if (TBvalid_integer($copyid)) {
34
	#
35
	# See if its a current experiment.
36
	#
37 38 39
	$experiment = Experiment::Lookup($copyid);
	if (!$experiment) {
	    $record = $copyid;
40 41 42
	}
    }
    else {
43
	PAGEARGERROR("Invalid copyID!");
44 45 46
    }
}

47
#
48
# Spit back an NS file to the user. 
49
#
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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;
}
73 74 75 76

#
# A template.
#
77 78
if (isset($template)) {
    if (! $template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
Leigh Stoller's avatar
Leigh Stoller committed
79
	USERERROR("You do not have permission to view template!", 1);
80 81 82 83 84 85
    }
    header("Content-Type: text/plain");

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

Leigh Stoller's avatar
Leigh Stoller committed
88 89
    for ($i = 0; $i < count($input_list); $i++) {
	echo $input_list[$i];
90 91 92 93 94
	echo "\n\n";
    }
    return;
}

95
#
96 97
# if requesting a specific pid,eid must have permission.
#
98
if (isset($experiment)) {
99 100 101
    #
    # Verify Permission.
    #
102
    if (!$experiment->AccessCheck($this_user, $TB_EXPT_READINFO)) {
103 104 105 106 107 108 109
	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.
    #
110 111 112 113 114
    if (($nsfile = $experiment->NSFile())) {
	header("Content-Type: text/plain");
	echo "$nsfile\n";
    }
    else {
Russ Fish's avatar
Russ Fish committed
115 116
	$pid = $experiment->pid();
	$eid = $experiment->eid();
117 118 119 120 121
	USERERROR("There is no NS file recorded for ".
		  "experiment $eid in project $pid!", 1);
    }
    return;
}
122 123

#
124 125 126 127 128 129 130 131
# 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.
132
#
133 134 135
if (isset($nsdata) && strcmp($nsdata, "") != 0) {
    header("Content-Type: text/plain");
    echo "$nsdata";
136 137 138 139 140 141 142 143 144
} 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;
    }
145 146 147

    if (! file_exists($nsfile)) {
	PAGEHEADER("View Generated NS File");
148 149
	USERERROR("Could not find temporary file for user/guid \"" . $id .
                  "\" with id \"$nsref\".<br>\n" . 
150
	          "You likely copy-and-pasted an URL incorrectly,<br>\n" .
151 152
 		  "or you've already used the file to create an experiment" . 
                  "(thus erasing it),<br>\n" .
153
		  "or the file has expired.\n", 1 );
154
	PAGEFOOTER();
155
    } else {
156
	$fp = fopen($nsfile, "r");
157 158 159 160 161 162
	header("Content-Type: text/plain");
	$contents = fread ($fp, filesize ($nsfile));
	fclose($fp);
	echo "$contents";
    }
} else {
163
    USERERROR("No NS file provided!",1);
164 165 166
}

?>
167 168 169 170 171 172