toggle.php 6.24 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2008 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15
# All rights reserved.
#
include("defs.php3");

#
# This page is a generic toggle page, like adminmode.php3, but more
# generalized. There are a set of things you can toggle, and each of
# those items has a permission check and a set (pair) of valid values.
#
# Usage: toggle.php?type=swappable&value=1&pid=foo&eid=bar
# (type & value are required, others are optional and vary by type)
16 17 18 19 20
#
# No PAGEHEADER since we spit out a Location header later. See below.
#
# Only known and logged in users can do this.
#
21 22 23
$this_user = CheckLoginOrDie(CHECKLOGIN_USERSTATUS|CHECKLOGIN_WEBONLY);
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
24 25

# List of valid toggles
26
$toggles = array("adminon", "webfreeze", "cvsweb", "lockdown", "stud",
27
		 "cvsrepo_public", "workbench", "hiderun", "widearearoot");
28 29

# list of valid values for each toggle
30
$values  = array("adminon"        => array(0,1),
31 32
		 "webfreeze"      => array(0,1),
		 "cvsweb"         => array(0,1),
33
		 "stud"           => array(0,1),
34
		 "lockdown"       => array(0,1),
35
		 "cvsrepo_public" => array(0,1),
36
		 "workbench"      => array(0,1),
37
		 "widearearoot"   => array(0,1),
38
		 "hiderun"        => array(0,1));
39 40

# list of valid extra variables for the each toggle, and mandatory flag.
41
$optargs = array("adminon"        => array(),
42 43
		 "webfreeze"      => array("user" => 1),
		 "cvsweb"         => array("user" => 1),
44
		 "stud"           => array("user" => 1),
45
		 "lockdown"       => array("pid" => 1, "eid" => 1),
46
		 "cvsrepo_public" => array("pid" => 1),
47
		 "workbench"      => array("pid" => 1),
48
		 "widearearoot"   => array("user" => 1),
49
		 "hiderun"        => array("instance" => 1, "runidx" => 1));
50 51

# Mandatory page arguments.
52 53
$reqargs = RequiredPageArguments("type",  PAGEARG_STRING,
				 "value", PAGEARG_STRING);
54

55 56
# Where we zap to.
$zapurl = null;
57 58

if (! in_array($type, $toggles)) {
59
    PAGEARGERROR("There is no toggle for $type!");
60 61
}
if (! in_array($value, $values[$type])) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    PAGEARGERROR("The value '$value' is illegal for the $type toggle!");
}

# Check optional args and bind locally.
while (list ($arg, $required) = each ($optargs[$type])) {
    if (!isset($_GET[$arg])) {
	if ($required)
	    PAGEARGERROR("Toggle '$type' requires argument '$arg'");
	else
	    unset($$arg);
    }
    else {
	$$arg = addslashes($_GET[$arg]);
    }
76 77 78 79 80
}

#
# Permissions checks, and do the toggle...
#
81
if ($type == "adminon") {
82
    # must be admin
83
    # Do not check if they are admin mode (ISADMIN), check if they
84 85
    # have the power to change to admin mode!
    if (! ($CHECKLOGIN_STATUS & CHECKLOGIN_ISADMIN) ) {
86 87
	USERERROR("You do not have permission to toggle $type!", 1);
    }
88
    SETADMINMODE($value);
89
}
90 91
elseif ($type == "webfreeze") {
    # must be admin
92
    if (! $isadmin) {
93 94
	USERERROR("You do not have permission to toggle $type!", 1);
    }
95 96
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
97
    }
98
    $zapurl = CreateURL("showuser", $target_user);
99
    $target_user->SetWebFreeze($value);
100
}
101 102 103 104 105
elseif ($type == "cvsweb") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
106 107
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
108
    }
109
    $zapurl = CreateURL("showuser", $target_user);
110
    $target_user->SetCVSWeb($value);
111
}
112 113 114 115 116 117 118 119 120 121 122
elseif ($type == "stud") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
    }
    $zapurl = CreateURL("showuser", $target_user);
    $target_user->SetStudly($value);
}
123 124 125 126 127 128 129 130 131 132 133
elseif ($type == "widearearoot") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
    }
    $zapurl = CreateURL("showuser", $target_user);
    $target_user->SetWideAreaRoot($value);
}
134 135 136 137 138
elseif ($type == "lockdown") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
139
    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
140 141
	PAGEARGERROR("Experiment $pid/$eid is not a valid experiment!");
    }
142 143
    $zapurl = CreateURL("showexp", $experiment);
    $experiment->SetLockDown($value);
144
}
145 146 147 148 149
elseif ($type == "cvsrepo_public") {
    # Must validate the pid since we allow non-admins to do this.
    if (! TBvalid_pid($pid)) {
	PAGEARGERROR("Invalid characters in $pid");
    }
150
    if (! ($project = Project::Lookup($pid))) {
151 152 153 154 155 156 157
	PAGEARGERROR("Project $pid is not a valid project!");
    }
    # Must be admin or project/group root.
    if (!$isadmin &&
	! TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
158 159
    $zapurl = CreateURL("showproject", $project);
    $project->SetCVSRepoPublic($value);
160 161
    SUEXEC($uid, $pid, "webcvsrepo_ctrl $pid", SUEXEC_ACTION_DIE);
}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
elseif ($type == "workbench") {
    # Must validate the pid since we allow non-admins to do this.
    if (! TBvalid_pid($pid)) {
	PAGEARGERROR("Invalid characters in $pid");
    }
    if (! ($project = Project::Lookup($pid))) {
	PAGEARGERROR("Project $pid is not a valid project!");
    }
    # Must be admin
    if (!$isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    $zapurl = CreateURL("showproject", $project);
    $project->SetAllowWorkbench($value);
}
177 178 179 180 181 182 183 184 185
elseif ($type == "hiderun") {
    RequiredPageArguments("instance",  PAGEARG_INSTANCE,
			  "runidx",    PAGEARG_INTEGER);

    if (! $instance->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
	USERERROR("You do not have permission to modify this instance", 1);
    }
    $instance->SetRunHidden($runidx, $value);
}
186
else {
187 188 189 190 191 192
    USERERROR("Nobody has permission to toggle $type!", 1);
}
    
#
# Spit out a redirect 
#
193
if (isset($HTTP_REFERER) && $HTTP_REFERER != "" &&
194
    strpos($HTTP_REFERER,$_SERVER["SCRIPT_NAME"])===false) {
195
    # Make sure the referer is not me!
196 197
    header("Location: $HTTP_REFERER");
}
198 199 200
elseif ($zapurl) {
    header("Location: $zapurl");
}
201
else {
202
    header("Location: $TBBASE/showuser.php3");
203 204 205
}

?>