flooricon.php3 2.78 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2007 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 27 28 29
#
chdir("..");
include("defs.php3");

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

#
35
# Verify page args
36
#
37 38
$reqargs = RequiredPageArguments("building",      PAGEARG_STRING,
				 "floor",         PAGEARG_STRING);
39

40 41 42
# Sanitize for the shell.
if (!preg_match("/^[-\w]+$/", $building)) {
    PAGEARGERROR("Invalid building argument.");
43
}
44 45
if (!preg_match("/^[-\w]+$/", $floor)) {
    PAGEARGERROR("Invalid floor argument.");
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

#
# Make sure it exists in the DB.
#
$query_result =
    DBQueryFatal("select pixels_per_meter from floorimages ".
		 "where building='$building' and floor='$floor'");
if (!mysql_num_rows($query_result)) {
    PAGEARGERROR("No such building/floor $building/$floor");
}

#
# Need cleanup "handler" to make sure temp files get deleted! 
#
function CLEANUP()
{
    global $prefix, $uid;

    #
    # The backend script (vis/floormap.in) removes all the temp files
    # with the -c option. Yucky, but file perms and owners make this
    # the easiest way to do it.
    # 
    if (isset($prefix)) {
71 72
	SUEXEC($uid, "nobody", "webfloormap -o $prefix -k ",
	       SUEXEC_ACTION_IGNORE);
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 106 107 108 109 110 111 112
	# This file does belong to the web server.
	unlink($prefix);
    }
    exit();
}
register_shutdown_function("CLEANUP");

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

#
# Build the image.
# 
$perl_args = "-o $prefix -t -z -n -x -v -y -f $floor $building";

$retval = SUEXEC($uid, "nobody", "webfloormap $perl_args",
		 SUEXEC_ACTION_IGNORE);

if ($retval) {
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    # Never returns.
    die("");
}

#
# Now spit it back.
# 
if (($fp = fopen("${prefix}.jpg", "r"))) {
    header("Content-type: image/jpg");
    fpassthru($fp);
}
else {
    # No Data. Spit back a stub image.
    header("Content-type: image/gif");
    readfile("coming-soon-thumb.gif");
}
?>