floormap.php3 15.8 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 25 26
#
include("defs.php3");

#
27
# Only logged in people at the moment; might open up at some point.
28
#
29 30 31
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#
# Verify page args
#
$optargs = OptionalPageArguments("experiment",    PAGEARG_EXPERIMENT,
				 "building",      PAGEARG_STRING,
				 "floor",         PAGEARG_STRING,
				 "feature",       PAGEARG_STRING,
				 "prefix",        PAGEARG_STRING,
				 "scale_0_x",     PAGEARG_INTEGER,
				 "scale_1_x",     PAGEARG_INTEGER,
				 "scale_2_x",     PAGEARG_INTEGER,
				 "scale_3_x",     PAGEARG_INTEGER,
				 "scale_4_x",     PAGEARG_INTEGER,
				 "scale_5_x",     PAGEARG_INTEGER,
				 "map_x",         PAGEARG_INTEGER,
				 "map_y",         PAGEARG_INTEGER,
				 "last_scale",    PAGEARG_INTEGER,
				 "last_x",        PAGEARG_INTEGER,
				 "last_y",        PAGEARG_INTEGER,
				 "last_x_off",    PAGEARG_INTEGER,
				 "last_y_off",    PAGEARG_INTEGER,
				 "last_notitles", PAGEARG_INTEGER, 
				 "last_ghost",    PAGEARG_INTEGER,
				 "ghost_on_x",    PAGEARG_INTEGER,
				 "ghost_off_x",   PAGEARG_INTEGER);

#
# Standard Testbed Header
#
PAGEHEADER("Wireless PC Map" .
	   ((isset($feature) && $feature != "")?" ($feature)":" (802.11)"));


66 67 68
# Careful with this local variable
unset($prefix);

69
#
70
# Allow user to optionally specify building/floor.
71 72 73 74 75 76 77 78 79 80
#
if (isset($building) && $building != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[-\w]+$/", $building)) {
	PAGEARGERROR("Invalid building argument.");
    }
    # Optional floor argument. Sanitize for the shell.
    if (isset($floor) && !preg_match("/^[-\w]+$/", $floor)) {
	PAGEARGERROR("Invalid floor argument.");
    }
81
}
82
else {
83
    $building = "MEB";
84
    unset($floor);
85 86
}

87 88 89 90 91 92 93 94 95 96
if (isset($feature) && $feature != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[-\w]+$/", $feature)) {
        PAGEARGERROR("Invalid feature argument.");
    }
}
else {
    unset($feature);
}

97
#
98
# Optional experiment. Without a building/floor, show all the nodes for the
99 100 101
# experiment in all buildings/floors. Without pid,eid show all wireless
# nodes in the specified building/floor.
#
102 103 104 105 106 107
if (isset($experiment)) {
    $pid = $experiment->pid();
    $eid = $experiment->eid();
    if (! $experiment->AccessCheck($this_user, $TB_EXPT_READINFO)) {
	USERERROR("You do not have permission to view floormaps ".
		  "for experiment $pid/$eid!", 1);
108 109 110
    }
}

111 112 113
# THIS is ugly...  Value= is not passed by IE on <input type=image>, so we wrap the
# scale value into the button name.  Hence we will see coordinates of scale_1_x
# scale_2_x, etc. when the buttons are pressed.  Decode to just a $scale value here.
114 115 116
if (isset($scale_0_x) && $scale_0_x != "") {
    $scale = 0;
}
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
if (isset($scale_1_x) && $scale_1_x != "") {
    $scale = 1;
}
elseif (isset($scale_2_x) && $scale_2_x != "") {
    $scale = 2;
}
elseif (isset($scale_3_x) && $scale_3_x != "") {
    $scale = 3;
}
elseif (isset($scale_4_x) && $scale_4_x != "") {
    $scale = 4;
}
elseif (isset($scale_5_x) && $scale_5_x != "") {
    $scale = 5;
}

133 134 135
#
# Verify numeric scale and centering arguments.
#
136
if (isset($scale) && $scale !== "") {  # "0" is a valid value for $scale.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $scale)) {
	PAGEARGERROR("Invalid scale argument.");
    }
}
else {
    unset($scale);
}
#
# map_x and map_y are the map.x and map.y coordinates from clicking on the map.
#
if (isset($map_x) && $map_x != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $map_x)) {
	PAGEARGERROR("Invalid map_x argument.");
    }
}
else {
    unset($map_x);
}
if (isset($map_y) && $map_y != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $map_y)) {
	PAGEARGERROR("Invalid map_y argument.");
    }
}
else {
    unset($map_y);
}
#
# We need the previous scale and click coords, and offsets to interpret new click coords!
#
169
if (isset($last_scale) && $last_scale !== "") {  # "0" is a valid value.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $last_scale)) {
	PAGEARGERROR("Invalid last_scale argument.");
    }
}
else {
    unset($last_scale);
}
if (isset($last_x) && $last_x != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $last_x)) {
	PAGEARGERROR("Invalid last_x argument.");
    }
}
else {
    unset($last_x);
}
if (isset($last_y) && $last_y != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $last_y)) {
	PAGEARGERROR("Invalid last_y argument.");
    }
}
else {
    unset($last_y);
}
if (isset($last_x_off) && $last_x_off != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $last_x_off)) {
	PAGEARGERROR("Invalid last_x_off argument.");
    }
}
else {
    unset($last_x_off);
}
if (isset($last_y_off) && $last_y_off != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $last_y_off)) {
	PAGEARGERROR("Invalid last_y_off argument.");
    }
}
else {
    unset($last_y_off);
}
if (isset($last_notitles) && $last_notitles != "") {
    # Sanitize for the shell.
    if (!preg_match("/^[0-9]+$/", $last_notitles)) {
	PAGEARGERROR("Invalid last_notitles argument.");
    }
}
else {
    unset($last_notitles);
}
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
if (isset($last_ghost) && $last_ghost !== "") {  # "0" is a valid value.
    # Sanitize for the shell.
    if (!preg_match("/^[01]$/", $last_ghost)) {
	PAGEARGERROR("Invalid last_ghost argument.");
    }
}
else {
    unset($last_ghost);
}

# With image checkboxes, we have two names: ghost_on and ghost_off.
if (isset($ghost_on_x)) {
    $ghost = 1;
}
elseif (isset($ghost_off_x)) {
    $ghost = 0;
}
else {
    # Default the ghosting checkbox state to checked the first time through.
    $ghost = (isset($last_ghost) ? $last_ghost : 1);	
}
244

Leigh Stoller's avatar
Leigh Stoller committed
245 246 247
#
# Figure out what channels are in use for the current building. We only
# have one building (MEB) at the moment, so this is quite easy.
248
# Also determine what the free,reserved,dead counts are. 
Leigh Stoller's avatar
Leigh Stoller committed
249
#
250 251
$channels   = array();
$nodecounts = array();
252
$nodes      = array();
253 254 255 256

$nodecounts["free"]     = 0;
$nodecounts["reserved"] = 0;
$nodecounts["dead"]     = 0;
Leigh Stoller's avatar
Leigh Stoller committed
257 258

$query_result =
259
    DBQueryFatal("select loc.*,s.capval,r.pid,r.eid from location_info as loc ".
Leigh Stoller's avatar
Leigh Stoller committed
260
		 "left join interface_settings as s on ".
261
		 "     s.node_id=loc.node_id and s.capkey='channel' ".
262
		 "left join reserved as r on r.node_id=loc.node_id ".
263
		 (isset($feature)?" left join node_features as f on f.node_id=loc.node_id ":"") . 
264
		 "where loc.building='$building' ".
265
		 (isset($feature)?" and f.feature='$feature' ":"") . 
266
		 (isset($floor) ? "and loc.floor='$floor'" : ""));
Leigh Stoller's avatar
Leigh Stoller committed
267 268 269 270 271

while ($row = mysql_fetch_array($query_result)) {
    $channel   = $row["capval"];
    $node_id   = $row["node_id"];
    $locfloor  = $row["floor"];
272 273 274
    $rpid      = $row["pid"];
    $reid      = $row["eid"];

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    #
    # The above query to get the channels will give us multiple rows
    # if there are multiple interface cards in use; do not want to
    # count those twice.
    # 
    if (!isset($nodes[$node_id])) {
	$nodes[$node_id] = 1;
	
	if ((!isset($pid) && !(isset($rpid))) ||
	    (isset($pid) && isset($rpid) && $pid == $rpid)) {
	    $nodecounts["free"]++;
	}
	elseif ($rpid == $NODEDEAD_PID && $reid == $NODEDEAD_EID) {
	    $nodecounts["dead"]++;
	}
	else {
	    $nodecounts["reserved"]++;
	}
293
    }
Leigh Stoller's avatar
Leigh Stoller committed
294

295 296 297 298 299
    # Make sure an empty list is displayed if nothing allocated on a floor.
    if (!isset($channels[$locfloor])) {
	$channels[$locfloor] = array();
    }

Leigh Stoller's avatar
Leigh Stoller committed
300 301 302 303 304 305
    if (!isset($channel))
	continue;

    $channels[$locfloor][$channel] = $channel;
}

306
#
307 308
# Run the Perl script. It will produce three output files; image, areamap, and state.
# We want to embed all of these images into the page we send back. This is painful!
309 310 311 312 313
#
# Need cleanup "handler" to make sure temp files get deleted! 
#
function CLEANUP()
{
314 315 316
    global $prefix;
    
    if (isset($prefix) && (connection_aborted())) {
317
	unlink("${prefix}.jpg");
318
	unlink("${prefix}.map");
319
	unlink("${prefix}.state");
320 321 322 323 324 325 326 327
	unlink($prefix);
    }
    exit();
}
register_shutdown_function("CLEANUP");

#
# Create a tempfile to use as a unique prefix; it is not actually used but
328
# serves the same purpose (The script uses ${prefix}.jpg and ${prefix}.map .)
329
# 
330
$prefix = tempnam("/tmp", "floormap");
331

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

340 341 342
$perl_args = "-o $prefix " .
	     # From clicking on a zoom button.
	     (isset($scale) ? "-s $scale " : "") .
343

344 345
	     # From clicking on a map image.
	     (isset($map_x) ? "-c $map_x,$map_y " : "") .
346

347 348 349 350 351 352
	     # Previous image state info came from an included .state
	     # file, previously output by the Perl script along with the map.
	     (isset($last_scale) ? "-S $last_scale " : "") .
	     (isset($last_x) ? "-C $last_x,$last_y " : "") .
	     (isset($last_x_off) ? "-O $last_x_off,$last_y_off " : "") .
	     (isset($last_notitles) && $last_notitles ? "-T " : "") .
353
	     ($ghost ? "-g " : "") .
354

355 356
	     (isset($pid) ? "-e $pid,$eid " : "") .
	     (isset($floor) ? "-f $floor " : "") .
357
             (isset($feature) ? "-F $feature " : "") . 
358 359 360 361 362
	     (isset($building) ? "$building" : "");  # Building arg must be last!

if (0) {    ### Put the Perl script args into the page when debugging.
    echo "\$btfv/floormap -d $perl_args\n";
}
363 364
$retval = SUEXEC($uid, "nobody", "webfloormap $perl_args",
		 SUEXEC_ACTION_IGNORE);
365 366

if ($retval) {
367
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
368 369 370 371 372 373 374 375 376 377 378 379
    # Never returns.
    die("");
}

#
# Spit the areamap contained in the file out; it is fully formatted and
# called "floormap".
#
if (! readfile("${prefix}.map")) {
    TBERROR("Could not read ${prefix}.map", 1);
}

380
echo "<font size=+1>For more info on using wireless nodes, see the
381
     <a href='$WIKIDOCURL/wireless'>
382
     wireless tutorial</a> and the 
383
     <a href='$WIKIDOCURL/gnuradio'>GNU software 
384
     defined radio tutorial</a>.</font>\n";
385

386
echo "<center>\n";
387

388 389 390 391
# Consolidate layout by pulling the info and control tables into a horizontal row.
echo "<table style=\"border-width: 0; background-color: transparent\">
      <tr> <td>\n";

392 393 394 395 396
# Legend
if (isset($pid)) {
    echo "Wireless nodes in experiment <b>".
         "<a href='showproject.php3?pid=$pid'>$pid</a>/".
         "<a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a></b>\n";
397 398

    echo "<table align=center border=2 cellpadding=5 cellspacing=2>
399
 	  <tr>
400
            <td align=right>
401
                <img src='/autostatus-icons/greenball.gif' alt=Experiment>
402 403
                <b>Experiment</b></td>
            <td align=left> &nbsp; " . $nodecounts["free"] . "</td>
404 405
          </tr>
          <tr>
406
            <td align=right>
407
                <img src='/autostatus-icons/blueball.gif' alt=Other>
408 409
                <b>Other Nodes</b></td>
            <td align=left> &nbsp; " . $nodecounts["reserved"] . "</td>
410 411
          </tr>
          <tr>
412 413 414 415
            <td align=right>
                <img src='/autostatus-icons/redball.gif' alt=Dead>
                <b>Dead</b></td>
            <td align=left> &nbsp; " . $nodecounts["dead"] . "</td>
416
          </tr>
417
          </table>\n";
418 419
}
else {
420
    echo "<table align=center border=2 cellpadding=5 cellspacing=2>
421
 	  <tr>
422
            <td align=right>
423
                <img src='/autostatus-icons/greenball.gif' alt=Free>
424 425
                <b>Free</b></td>
            <td align=left> &nbsp; " . $nodecounts["free"] . "</td>
426 427
          </tr>
          <tr>
428
            <td align=right>
429
                <img src='/autostatus-icons/blueball.gif' alt=reserved>
430 431
                <b>Reserved</b></td>
            <td align=left> &nbsp; " . $nodecounts["reserved"] . "</td>
432 433
          </tr>
          <tr>
434 435 436 437
            <td align=right>
                <img src='/autostatus-icons/redball.gif' alt=Dead>
                <b>Dead</b></td>
            <td align=left> &nbsp; " . $nodecounts["dead"] . "</td>
438
          </tr>
439
          </table>\n";
440 441
}

442
echo "</td> <td style=\" background-color: transparent\"> &nbsp; &nbsp; </td> <td>\n";
Leigh Stoller's avatar
Leigh Stoller committed
443

444
# Wrap the image and zoom controls together in an input form.
445
echo "<form method=\"get\" action=\"floormap.php3#zoom\">\n";
446

447
# Zoom controls may be clicked to set a new scale.  Otherwise, it persists.
448 449 450 451 452 453
$curr_scale = (isset($scale) ? $scale : (isset($last_scale) ? $last_scale : 1));
echo "  <a name=zoom></a>\n";
function zoom_btns($curr_scale) {
echo "  <table align=\"center\" border=\"2\" cellpadding=\"0\" cellspacing=\"2\">\n";
echo "    <tbody>\n";
echo "      <tr>\n";
454
# Wrap the scale into the button name.  Value= is not passed by IE.
455
echo "        <td><input type=\"image\" src=\"floormap/btn_zoom_out.jpg\"\n";
456
echo "                   name=\"scale_" . max($curr_scale-1,0) . "\"><br></td>\n";
457
for ($i = 0; $i <= 5; $i++) {
458
    $img = "btn_scale_" . $i . "_" . ($curr_scale==$i?"brt":"dim") . ".jpg";
459
    echo "        <td><input type=\"image\" src=\"floormap/$img\"\n";
460
    echo "                   name=\"scale_$i\"><br></td>\n";
461
}
462
echo "        <td><input type=\"image\" src=\"floormap/btn_zoom_in.jpg\"\n";
463
echo "                   name=\"scale_" . min($curr_scale+1,5) . "\"><br></td>\n";
464 465 466 467
echo "      </tr>\n";
echo "    </tbody>\n";
echo "  </table>\n";
}
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
zoom_btns($curr_scale);  # Two copies of the zoom buttons bracket the image.

echo "</td> <td style=\" background-color: transparent\"> &nbsp; &nbsp; </td> <td>\n";

if (count($channels)) {
    echo "<table align=center border=2 cellpadding=0 cellspacing=2>
 	  <tr><th>Floor</th><th>Channels in Use</th></tr>\n";
    
    while (list($floor, $chanlist) = each($channels)) {
	echo "<tr><td>$floor</td>\n";
	echo "    <td>";

	echo implode(",", array_keys($chanlist));

	echo "    </td>
              </tr>\n";
    }
    echo "</table>\n";
}

echo "</td> </tr> </table>\n";
489

490
# The image may be clicked to get node info or set a new center-point.
491 492
echo "  Click on the dots below to see information about the node.\n";
echo "  <br>\n";
Russ Fish's avatar
Russ Fish committed
493
echo "  Click elsewhere on the map to set the center point for a zoomed-in view.\n";
494
echo "  <br>\n";
495 496 497 498 499 500 501 502
# Couldn't get JavaScript submit on checkbox to be portable to IE with map in form.
# Just use image buttons to show and invert the checkbox state.  That works.
if ($ghost) {
    echo " <input type=image src=\"floormap/cb_checked.gif\" name=ghost_off>\n";
}
else {
    echo " <input type=image src=\"floormap/cb_unchecked.gif\" name=ghost_on>\n";
}
503 504
echo "  Show nodes on other floors as hollow dots.\n";
echo "  <br>\n";
505 506 507
if (isset($feature)) {
    echo "  <a href='floormap.php3'>Show all wireless nodes</a><br>\n";
}
508
echo "  <input name=map type=image style=\"border: 2px solid\" ";
509 510 511 512 513 514
echo          "src=\"floormap_aux.php3?prefix=$uniqueid\" usemap=\"#floormap\">\n";
echo "  <br>\n";
zoom_btns($curr_scale);

# Hidden items are all returned as page arguments when any input control is clicked.
echo "  <input type=\"hidden\" name=\"prefix\" value=\"$uniqueid\">\n";
515 516 517
if (isset($feature)) {
    echo "  <input type=\"hidden\" name=\"feature\" value=\"$feature\">\n";
}
518 519 520
if (isset($building)) {
    echo "  <input type=\"hidden\" name=\"building\" value=\"$building\">\n";
}
521 522 523 524 525 526 527 528

# The last_* items come from a .state file with the map, from the Perl script.
if (! readfile("${prefix}.state")) {
    TBERROR("Could not read ${prefix}.state", 1);
}

echo "</form>\n";
echo "</center>\n";
529

530 531 532 533 534
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>