setnodeloc.php3 14.3 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2004-2012 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 admins.
#
30 31 32
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#
# 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
48 49
#   button that says to use the old coords. This allows us to change the 
#   contact info for a node without actually changing the location. 
50 51 52 53 54 55 56 57 58
#

# 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.
59
#
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
$reqargs = RequiredPageArguments("node",       PAGEARG_NODE);
$optargs = OptionalPageArguments("building",   PAGEARG_STRING,
				 "floor",      PAGEARG_STRING,
				 "x",          PAGEARG_STRING,
				 "y",          PAGEARG_STRING,
				 "contact",    PAGEARG_STRING,
				 "room",       PAGEARG_STRING,
				 "phone",      PAGEARG_STRING,
				 "submit",     PAGEARG_STRING,
				 "dodelete",   PAGEARG_STRING,
				 "isnewid",    PAGEARG_BOOLEAN,
				 "delete",     PAGEARG_BOOLEAN);

#
# Need these below.
#
$node_id = $node->node_id();
$isnewid = (isset($isnewid) ? $isnewid : 0);
78 79 80 81 82 83

#
# 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. 
#
84
function SPITFORM($errors, $node_id, $isnewid, $building, $floor,
85 86
		  $room, $contact, $phone, $old_x, $old_y,
		  $delete = 0)
87
{
Leigh Stoller's avatar
Leigh Stoller committed
88 89
    global $uid;
    
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    # 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)) {
105 106
            # XSS prevention.
	    $message = CleanString($message);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	    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];

131
    $retval = SUEXEC($uid, "nobody",
132
		     "webfloormap -t -o $prefix -f $floor $building",
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		     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>
153 154
          <font size=+2>Enter contact info for $node_id,
                        then click on location</font><br>
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
          <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";
176
    if ($old_x && $old_y) {
177 178 179 180 181 182
	echo "<tr>
                 <td colspan=2 align=center>
                     <b><input type=submit name=submit
	                       value='Use Existing Location'></b>
                 </td>
             </tr>\n";
183 184 185 186 187 188 189 190

	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";
191
    }
192 193 194
    if (isset($isnewid) && $isnewid == 1) {
	echo "<input type=hidden name=isnewid value=1>\n";
    }
195 196 197 198 199 200 201 202 203 204 205
    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";
}

#
206
# Get default values from location info or from the new_nodes table.
207
#
208 209 210 211 212 213 214 215 216 217
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'");
}
218 219 220 221 222 223 224 225 226 227 228
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"];
229 230
    $old_building = $row["building"];
    $old_floor    = $row["floor"];
231 232 233 234 235 236 237 238
}
else {
    if (! isset($contact))
	$contact = "";
    if (! isset($room))
	$room = "";
    if (! isset($phone))
	$phone = "";
239 240
    $old_x = null;
    $old_y = null;
241 242
}

243 244 245 246 247 248 249 250 251 252 253 254
#
# 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);
    }
255 256
    SPITFORM(0, $node_id, $isnewid, $old_building, $old_floor, $room, $contact,
	     $phone, $old_x, $old_y);
257 258 259 260 261 262
    PAGEFOOTER();
    exit(0);
}
elseif (isset($dodelete) && $dodelete != "") {
    #
    # Delete the entry and zap to shownode page. 
263 264 265 266 267 268 269 270 271 272 273
    #
    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");
    }
274 275 276 277 278 279 280 281 282 283 284
	
    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");

285 286 287 288
    $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 " .
289
		     "where f.scale=1 " .
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		     "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";

308 309 310
    echo "<center>\n";

    if (isset($old_building)) {
311 312
	echo "<a href='setnodeloc.php3?node_id=$node_id&delete=1".
	         "&isnewid=$isnewid'>
313 314 315 316
                 Delete</a> location info or<br>\n";
    }

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

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    $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".
351
	               "&building=$building&floor=$floor&isnewid=$isnewid'>
352
                   <img src=\"floormap/$thumb\"></a>
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
              </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";
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    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");
}

388 389 390 391 392 393 394 395
#
# 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");

396
    SPITFORM(0, $node_id, $isnewid, $building, $floor, $room, $contact, $phone,
397 398 399 400 401 402 403 404 405 406 407 408 409 410
	     $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;
411 412
    $building = $old_building;
    $floor    = $old_floor;
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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
}

#
# 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");
489
    SPITFORM($errors, $node_id, $isnewid, $building, $floor, $room, $contact,
490 491 492 493 494 495
	     $phone, $old_x, $old_y);
    PAGEFOOTER();
    return;
}

#
496
# Otherwise, do the inserts or updates
497 498 499 500 501
#
$insert_data = array();
foreach ($inserts as $name => $value) {
    $insert_data[] = "$name='$value'";
}
502 503 504 505 506 507 508 509 510 511

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));
}
512 513 514 515 516 517 518 519 520 521 522

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

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