modnodeattributes.php3 4.15 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2013 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/>.
# 
# }}}
23 24
#
include("defs.php3");
25
include_once("node_defs.php");
26 27 28 29

#
# Only known and logged in users can do this.
#
30 31 32
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
33 34

#
35
# Only admin users may modify node attributes.
36
#
37 38
if (! $isadmin) {
    USERERROR("You do not have permission to modify node atrributes!", 1);
39
}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#
# Verify form arguments.
#
$reqargs = RequiredPageArguments("node",       PAGEARG_NODE);
$optargs = OptionalPageArguments("refer",      PAGEARG_STRING,
				 "submit",     PAGEARG_STRING,
				 "_delattrs",  PAGEARG_ARRAY,
				 "_modattrs",  PAGEARG_ARRAY,
				 "_newattrs",  PAGEARG_ARRAY,
				 "_newvals",   PAGEARG_ARRAY);

# Need these below ...
$node_id = $node->node_id();
$type    = $node->type();
55

56 57 58 59 60
#
# Standard Testbed Header
#
PAGEHEADER("Modify Node Attributes Form");

61 62 63
#
# Get current set of attributes for node - used in comparison below
#
64
$cur_node_attrs = array();
65 66 67 68
$attr_result =
    DBQueryFatal("select attrkey,attrvalue from node_attributes ".
		 "where node_id='$node_id'");
while($row = mysql_fetch_array($attr_result)) {
69
    $cur_node_attrs[$row["attrkey"]] = $row["attrvalue"];
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}

#
# Command strings are empty initially
#
$mod_command_string = "";
$add_command_string = "";
$del_command_string = "";

#
# Figure out which attributes are to be modified, added, or deleted
#

# Find attributes needing modification - make sure they are actually
# different than the current value before adding them to the command string.
85
if (isset($_modattrs)) {
86
  foreach ($_modattrs as $attrkey => $attrval) {
87 88
      if (!array_key_exists($attrkey, $cur_node_attrs) ||
	  $cur_node_attrs[$attrkey] != $attrval) {
89 90 91 92 93 94
      $mod_command_string .= "$attrkey='$attrval' ";
    }
  }
}

# Check for new attributes - make sure they are unique.
95
if (isset($_newattrs)) {
96 97 98 99 100 101 102 103 104 105 106 107
  for ($i = 0; $i < count($_newattrs); $i++) {
    if ($cur_node_attrs && 
        array_key_exists($_newattrs[$i], $cur_node_attrs)) {
      USERERROR("You cannot add a key that already exists!",1);
    }
    if ($_newattrs[$i]) {
      $add_command_string .= "$_newattrs[$i]='$_newvals[$i]' ";
    }
  }
}

# Finally, see if any attributes need to be deleted.
108
if (isset($_delattrs)) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122
  foreach ($_delattrs as $attrkey => $attrval) {
    $del_command_string .= "$attrkey ";
  }
}

#
# Pass commands off to the script. It will check the arguments.
# NB: This is how nodcontrol.php3 does it, but it may not be safe;
#     Shell command injection may be possible.  This is an admin-only
#     command though.
#

# Fire off the modify operation first
if ($mod_command_string) {
123
  SUEXEC($uid, "nobody", "webnode_attributes -m ".
124 125 126 127 128
         "$mod_command_string $node_id",
         SUEXEC_ACTION_DIE);
}
# Next, add attributes
if ($add_command_string) {
129
  SUEXEC($uid, "nobody", "webnode_attributes -a ".
130 131 132 133 134
         "$add_command_string $node_id",
         SUEXEC_ACTION_DIE);
}
# Finally, delete attributes.
if ($del_command_string) {
135
  SUEXEC($uid, "nobody", "webnode_attributes -r ".
136 137 138 139 140 141 142 143 144
         "$del_command_string $node_id",
         SUEXEC_ACTION_DIE);
}

echo "<center>
      <br>
      <h3>Node attributes successfully modified!</h3><p>
      </center>\n";

145
$node->Show(SHOWNODE_NOFLAGS);
146 147 148 149

#
# Edit option.
#
150
$url = CreateURL("modnodeattributes_form", $node);
151
echo "<br><center>
152
           <A href='$url'>
153 154 155 156 157 158 159 160
           Edit this node's attributes again?</a>
         </center>\n";

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>