setnodeloc.php3 13.3 KB
Newer Older
1 2 3 4 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 32 33 34 35 36 37 38 39 40
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");

#
# Only admins.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

#
# This is a multistep process.
#
# * Initially we come in with just a nodeid. Display a page of little maps
#   and titles the user must select (a floor in a building) from. User clicks.
# * Next time through we have a building and floor defined in addition to the
#   the nodeid. We put up that big image with the current nodes on that floor.
#   We use the external floormap program to generate that image, but without
#   an imagemap. Instead, we use a form with an input type=image, which acts
#   as a big submit button; when the user clicks in the image, the browser
#   submits the form, but with the x,y coords added as form arguments. 
# * We get all of the above arguments on the final click, including x,y. Verify
#   all the aguments, and then do the insert.
# * If user goes to reset the node location, form includes an additional submit
#   button that says to use the old coords. This allows us to change the contact
#   info for a node without actually changing the location. 
#

# Only admins cat set node location
if (!$isadmin) {
    USERERROR("You do not have permission to access this page!", 1);
}

#
# Check page args. Must always supply a nodeid.
41 42 43 44 45 46 47 48 49 50 51 52 53 54
#
if (!isset($node_id) || $node_id == "") {
    PAGEARGERROR("Must supply node_id");
}
elseif (isset($isnewid) && $isnewid == 1) {
    if (!TBvalid_integer($node_id)) {
	PAGEARGERROR("Must supply new node identifier in node_id");
    }
}
elseif (!TBvalid_node_id($node_id)) {	
    PAGEARGERROR("Invalid characters in node_id");
}
elseif (!TBValidNodeName($node_id)) {
    PAGEARGERROR("Invalid node_id $node_id");
55 56 57 58 59 60 61
}

#
# This routine spits out the selectable image for getting x,y coords plus
# contact info. We use a form and an input type=image, which acts as a submit
# button; the browser sends x,y coords when the user clicks in the image. 
#
62
function SPITFORM($errors, $node_id, $isnewid, $building, $floor,
63 64
		  $room, $contact, $phone, $old_x, $old_y,
		  $delete = 0)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
    # Careful with this local variable
    unset($prefix);

    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
    
    #
    # Create a tempfile to use as a unique prefix; it is not actually used but
    # serves the same purpose (the script uses ${prefix}.png and ${prefix}.map)
    # 
    $prefix = tempnam("/tmp", "floormap");

    #
    # Get the unique part to send back.
    #
    if (!preg_match("/^\/tmp\/([-\w]+)$/", $prefix, $matches)) {
	TBERROR("Bad tempnam: $prefix", 1);
    }
    $uniqueid = $matches[1];

    $retval = SUEXEC("nobody", "nobody",
106
		     "webfloormap -t -o $prefix -f $floor $building",
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		     SUEXEC_ACTION_IGNORE);

    if ($retval) {
	SUEXECERROR(SUEXEC_ACTION_USERERROR);
	die("");
    }
    
    echo "<script language=JavaScript>
          <!--
          function foo(target, event) {
              target.href='https://www.foo.edu/bar'
              msgWindow=window.open('','msg','width=400,height=400')
	      msgWindow.document.write(target.href + '<br>')
	      msgWindow.document.write(event.pageX + '<br>')
	      return false;
          }
          //-->
          </script>\n";

    echo "<center>
127 128
          <font size=+2>Enter contact info for $node_id,
                        then click on location</font><br>
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
          <form action=setnodeloc.php3 method=get>
          <table class=nogrid
                 align=center border=0 cellpadding=6 cellspacing=0>
          <tr>
             <td>Contact Name:</td>
             <td><input type=text name=contact value=\"" . $contact . "\"
                        size=30>
              </td>
          </tr>
          <tr>
             <td>Phone Number:</td>
             <td><input type=text name=phone value=\"" . $phone . "\"
                        size=30>
              </td>
          </tr>
          <tr>
             <td>Room Number:</td>
             <td><input type=text name=room value=\"" . $room . "\"
                        size=30>
              </td>
          </tr>\n";
150
    if (isset($old_x) && isset($old_y) && $old_x && $old_y) {
151 152 153 154 155 156
	echo "<tr>
                 <td colspan=2 align=center>
                     <b><input type=submit name=submit
	                       value='Use Existing Location'></b>
                 </td>
             </tr>\n";
157 158 159 160 161 162 163 164

	echo "<tr><td colspan=2 align=center>&nbsp</td><tr>\n";
	echo "<tr>
                 <td colspan=2 align=center>
                     <b><input type=submit name=dodelete
	                       value='Delete Location'></b></font>
                 </td>
             </tr>\n";
165
    }
166 167 168
    if (isset($isnewid) && $isnewid == 1) {
	echo "<input type=hidden name=isnewid value=1>\n";
    }
169 170 171 172 173 174 175 176 177 178 179
    echo "</table>
	  <input type=hidden name=node_id value=$node_id>
	  <input type=hidden name=building value=$building>
	  <input type=hidden name=floor value=$floor>
          <input type=image
                 src=\"floormap_aux.php3?prefix=$uniqueid\">
          </form>
          </center>\n";
}

#
180
# Get default values from location info or from the new_nodes table.
181
#
182 183 184 185 186 187 188 189 190 191
if (isset($isnewid) && $isnewid == 1) {
    $query_result =
	DBQueryFatal("select * from new_nodes ".
		     "where new_node_id='$node_id'");
}
else {
    $query_result =
	DBQueryFatal("select * from location_info ".
		     "where node_id='$node_id'");
}
192 193 194 195 196 197 198 199 200 201 202
if (mysql_num_rows($query_result)) {
    $row = mysql_fetch_array($query_result);
    
    if (! isset($contact))
	$contact = $row["contact"];
    if (! isset($room))
	$room = $row["room"];
    if (! isset($phone))
	$phone = $row["phone"];
    $old_x = $row["loc_x"];
    $old_y = $row["loc_y"];
203 204
    $old_building = $row["building"];
    $old_floor    = $row["floor"];
205 206 207 208 209 210 211 212 213 214
}
else {
    if (! isset($contact))
	$contact = "";
    if (! isset($room))
	$room = "";
    if (! isset($phone))
	$phone = "";
}

215 216 217 218 219 220 221 222 223 224 225 226
#
# Handle deletion.
#
if (isset($delete) && $delete == 1) {
    #
    # Skip to form with node on it, which includes delete button.
    #
    PAGEHEADER("Set Node Location");

    if (! isset($old_building)) {
	USERERROR("There is no location info for node $node_id!", 1);
    }
227 228
    SPITFORM(0, $node_id, $isnewid, $old_building, $old_floor, $room, $contact,
	     $phone, $old_x, $old_y);
229 230 231 232 233 234
    PAGEFOOTER();
    exit(0);
}
elseif (isset($dodelete) && $dodelete != "") {
    #
    # Delete the entry and zap to shownode page. 
235 236 237 238 239 240 241 242 243 244 245
    #
    if (isset($isnewid) && $isnewid == 1) {
	DBQueryFatal("update new_nodes set " .
		     "       building=NULL,floor=NULL,loc_x=0,loc_y=0 ".
		     "where new_node_id='$node_id'");
	header("Location: newnode_edit.php3?id=$node_id");
    }
    else {
	DBQueryFatal("delete from location_info where node_id='$node_id'");
	header("Location: shownode.php3?node_id=$node_id");
    }
246 247 248 249 250 251 252 253 254 255 256
	
    exit(0);
}

#
# If no building/floor specified, throw up a page of images of all the
# buildings and floors
#
if (!isset($building) || $building == "" || !isset($floor) || $floor == "") {
    PAGEHEADER("Set Node Location");

257 258 259 260
    $building_result =
	DBQueryFatal("select b.building,b.title,f.floor,f.thumb_path ".
		     "   from buildings as b ".
		     "left join floorimages as f on f.building=b.building " .
261
		     "where f.scale=1 " .
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		     "order by b.building,f.floor");

    if (! mysql_num_rows($building_result)) {
	USERERROR("There is no floormap data in the DB!", 1);
    }

    # Ha.
    $floortags = array();
    $floortags[1] = "1st floor";
    $floortags[2] = "2nd floor";   
    $floortags[3] = "3rd floor";
    $floortags[4] = "4th floor";
    $floortags[5] = "5th floor";
    $floortags[6] = "6th floor";
    $floortags[7] = "7th floor";
    $floortags[8] = "8th floor";
    $floortags[9] = "9th floor";

280 281 282
    echo "<center>\n";

    if (isset($old_building)) {
283 284
	echo "<a href='setnodeloc.php3?node_id=$node_id&delete=1".
	         "&isnewid=$isnewid'>
285 286 287 288
                 Delete</a> location info or<br>\n";
    }

    echo "<font size=+2>Pick a floor, any floor</font><br><br>\n";
289
    echo "<table>\n";
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    $maxcol = 2;
    $i = 0;
    $titles = array();

    #
    # XXX This will need to be generalized to multiple buildings at some
    # point; right now just throw up a set of thumb images in a single 
    # table. Not very pretty.
    #
    while ($row = mysql_fetch_array($building_result)) {
	$building = $row["building"];
	$title    = $row["title"];
	$floor    = $row["floor"];
	$thumb    = $row["thumb_path"];

	if ($i == $maxcol) {
	    #
	    # Start a new row. Keeps the window reasonably narrow.
	    # We have to put the titles in for the previous row before
	    # starting the next row; see below where we have to do this
	    # when the table is finished. 
	    #
	    echo "</tr><tr>\n";
	    for ($i = 0 ; $i < $maxcol; $i++) {
		echo "<td align=center>" . $titles[$i] . "</td>\n";
	    }
	    echo "</tr><tr>\n";
	    $i = 0;
	}
	
	echo "<td>
                <a href='setnodeloc.php3?node_id=$node_id".
323
	               "&building=$building&floor=$floor&isnewid=$isnewid'>
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
                   <img src=$thumb></a>
              </td>\n";
	$titles[$i] = "$title - " . $floortags[$floor];
	$i++;
    }
    # Finish out previous row.
    while ($i < $maxcol) {
	echo "<td></td>";
	$titles[$i] = "";
	$i++;
    }
    echo "</tr>";
    # Then stick in the title row.
    echo "<tr>";
    for ($i = 0 ; $i < $maxcol; $i++) {
	echo "<td align=center>" . $titles[$i] . "</td>\n";
    }
    echo "</tr>\n";

    echo "</table>\n";
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    echo "</center>\n";

    PAGEFOOTER();
    exit(0);
}

#
# At this point both building and floor are specified. Sanitize for shell.
#
if (!preg_match("/^[-\w]+$/", $building)) {
    PAGEARGERROR("Invalid characters in building");
}
if (!preg_match("/^[-\w]+$/", $floor)) {
    PAGEARGERROR("Invalid characters in floor");
}

360 361 362 363 364 365 366 367
#
# If no x,y yet, then we must have a building/floor and we put up that image.
# If there were existing coords, then the user got a submit box. When that is
# clicked, we use those old coords (we will not get x,y from the image submit). 
#
if (!isset($submit) && (!isset($x) || $x == "" || !isset($y) || $y == "")) {
    PAGEHEADER("Set Node Location");

368
    SPITFORM(0, $node_id, $isnewid, $building, $floor, $room, $contact, $phone,
369 370 371 372 373 374 375 376 377 378 379 380 381 382
	     $old_x, $old_y);
    PAGEFOOTER();
    exit(0);
}
elseif (isset($submit)) {
    #
    # Lets make sure that the node really does have an existing location.
    #
    if (!isset($old_x) || !isset($old_y)) {
	PAGEHEADER("Set Node Location");
	PAGEARGERROR("Need location coordinates");
    }
    $x = $old_x;
    $y = $old_y;
383 384
    $building = $old_building;
    $floor    = $old_floor;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
}

#
# Okay, make sure the x and y coords are numbers, and verify all input data.
#
$errors  = array();
$inserts = array();

# x,y
if (! TBvalid_slot($x, "location_info", "loc_x")) {
    $errors["X"] = TBFieldErrorString();
}
elseif ($x <= 0) {
    $errors["X"] = "Must be greater then zero";
}
else {
    $inserts["loc_x"] = $x;
}
if (! TBvalid_slot($y, "location_info", "loc_y")) {
    $errors["Y"] = TBFieldErrorString();
}
elseif ($x <= 0) {
    $errors["Y"] = "Must be greater then zero";
}
else {
    $inserts["loc_y"] = $y;
}

# Building
if (! TBvalid_slot($building, "location_info", "building")) {
    $errors["Building"] = TBFieldErrorString();
}
else {
    $inserts["building"] = $building;
}

# Floor
if (! TBvalid_slot($floor, "location_info", "floor")) {
    $errors["Floor"] = TBFieldErrorString();
}
else {
    $inserts["floor"] = $floor;
}

# Contact
if (isset($contact) && $contact != "" &&
    !TBvalid_slot($contact, "location_info", "contact")) {
    $errors["Contact"] = TBFieldErrorString();
}
else {
    $inserts["contact"] = $contact;
}

# Phone
if (isset($phone) && $phone != "" &&
    !TBvalid_slot($phone, "location_info", "phone")) {
    $errors["Phone"] = TBFieldErrorString();
}
else {
    $inserts["phone"] = $phone;
}

# Room
if (isset($room) && $room != "" &&
    !TBvalid_slot($room, "location_info", "room")) {
    $errors["Room"] = TBFieldErrorString();
}
else {
    $inserts["room"] = $room;
}

#
# Spit any errors now.
#
if (count($errors)) {
    PAGEHEADER("Set Node Location");
461
    SPITFORM($errors, $node_id, $isnewid, $building, $floor, $room, $contact,
462 463 464 465 466 467
	     $phone, $old_x, $old_y);
    PAGEFOOTER();
    return;
}

#
468
# Otherwise, do the inserts or updates
469 470 471 472 473
#
$insert_data = array();
foreach ($inserts as $name => $value) {
    $insert_data[] = "$name='$value'";
}
474 475 476 477 478 479 480 481 482 483

if (isset($isnewid) && $isnewid == 1) {
    DBQueryFatal("update new_nodes set " . implode(",", $insert_data) . " ".
		 "where new_node_id='$node_id'");
}
else {
    $insert_data[] = "node_id='$node_id'";

    DBQueryFatal("replace into location_info set " . implode(",", $insert_data));
}
484 485 486 487 488 489 490 491 492 493 494

#
# Zap back to floormap for this building/floor.
# 
header("Location: floormap.php3?building=$building&floor=$floor");

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