toggle.php 4.52 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# All rights reserved.
#
include("defs.php3");

#
# No PAGEHEADER since we spit out a Location header later. See below.
# 

#
# Only known and logged in users can do this.
#
# Note different test though, since we want to allow logged in
# users with expired passwords to change them.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

#
# 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)

# List of valid toggles
32 33
$toggles = array("adminoff", "swappable", "idleswap", "autoswap",
		 "idle_ignore");
34 35

# list of valid values for each toggle
36 37
$values  = array("adminoff"    => array(0,1),
		 "swappable"   => array(0,1),
38 39
		 "idleswap"    => array(0,1),
		 "autoswap"    => array(0,1),
40
		 "idle_ignore" => array(0,1) );
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

if (! in_array($type, $toggles)) {
    USERERROR("There is no toggle for $type!", 1);
}
if (! in_array($value, $values[$type])) {
    USERERROR("The value '$value' is illegal for the $type toggle!", 1);
}

#
# Permissions checks, and do the toggle...
#
if ($type=="adminoff") {
    # must be admin
    if (! ($CHECKLOGIN_STATUS & CHECKLOGIN_ISADMIN)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    # Admins can change status for other users.
    if (!isset($target_uid)) { $target_uid = $uid; }

    DBQueryFatal("update users set adminoff=$value where uid='$target_uid'");
    
62
} elseif ($type=="swappable" || $type=="idleswap" || $type=="autoswap") {
63 64 65 66 67 68 69 70 71 72 73
    # must be admin OR must have permission to modify the expt...
    if (! ($CHECKLOGIN_STATUS & CHECKLOGIN_ISADMIN) ||
	! TBExptAccessCheck($uid, $pid, $eid, $TB_EXPT_MODIFY)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    # require pid/eid
    if (!isset($pid) || !isset($eid) ||
	!TBValidExperiment($pid, $eid)) {
	USERERROR("Experiment '$pid/$eid' is not valid!", 1);
    }
    
74
    DBQueryFatal("update experiments set $type=$value ".
75 76
		 "where pid='$pid' and eid='$eid'");

77 78
    # Send mail on these changes...
    if ($type!="autoswap" && $value=="0") {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	$q = DBQueryFatal("select * from experiments ".
			  "where pid='$pid' and eid='$eid'");
	$r = mysql_fetch_array($q);
	$s = ($r[swappable] ? "Yes" : "No");
	$sr= $r[noswap_reason];
	$i = ($r[idleswap] ? "Yes" : "No");
	$it= $r[idleswap_timeout] / 60.0;
	$ir= $r[noidleswap_reason];
	$a = ($r[autoswap] ? "Yes" : "No");
	$at= $r[autoswap_timeout] / 60.0;
	$cuid = $r[expt_head_uid];
	$suid= $r[expt_swap_uid];
	TBUserInfo($uid, $user_name, $user_email);
	TBUserInfo($cuid, $cname, $cemail);
	TBUserInfo($suid, $sname, $semail);
94 95 96
	TBMAIL($TBMAIL_OPS,"$pid/$eid swap settings changed",
	       "\nThe swap settings for $pid/$eid have changed.\n".
	       "\nThe $type bit has been cleared.\n".
97 98 99 100 101 102 103 104 105
	       "\nThe new settings are:\n".
	       "Swappable:\t$s\t('$sr')\n".
	       "Idleswap:\t$i\t(after $it hrs)\t('$ir')\n".
	       "Autoswap:\t$a\t(after $at hrs)\n".
	       "\nCreator:\t$cuid ($cname <$cemail>)\n".
	       "Swapper:\t$suid ($sname <$semail>)\n".
	       "\nIf it is necessary to change these settings, ".
	       "please reply to this message \nto notify the user, ".
	       "then change the settings here:\n\n".
106 107
	       "$TBBASE/showexp.php3?pid=$pid&eid=$eid\n\n".
	       "Thanks,\nTestbed WWW\n",
108
	       "From: $user_name <$user_email>\n".
109 110 111 112
	       "Errors-To: $TBMAIL_WWW");
    }
	
    
113 114 115 116 117 118 119 120 121 122 123 124 125 126
} elseif ($type=="idle_ignore") {
    # must be admin 
    if (! ($CHECKLOGIN_STATUS & CHECKLOGIN_ISADMIN)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    # require pid/eid
    if (!isset($pid) || !isset($eid) ||
	!TBValidExperiment($pid, $eid)) {
	USERERROR("Experiment '$pid/$eid' is not valid!", 1);
    }
    
    DBQueryFatal("update experiments set idle_ignore=$value ".
		 "where pid='$pid' and eid='$eid'");

127 128 129 130 131 132 133 134 135 136 137 138 139 140
#} elseif ($type=="foo") {
# Add more here...
#
} else {
    USERERROR("Nobody has permission to toggle $type!", 1);
}
    
#
# Spit out a redirect 
#
if (isset($HTTP_REFERER) && strcmp($HTTP_REFERER, "")) {
    header("Location: $HTTP_REFERER");
}
else {
141 142 143 144 145 146 147
    if (isset($target_uid)) {
	header("Location: $TBBASE/showuser.php3?target_uid=$target_uid");
    } elseif (isset($pid) && isset($eid)) {
	header("Location: $TBBASE/showexp.php3?pid=$pid&eid=$eid");
    } else {
	header("Location: $TBBASE/showuser.php3");
    }
148 149 150
}

?>