Commit 32bba625 authored by Leigh Stoller's avatar Leigh Stoller

Switch to using temp files, which get deleted via cleanup handlers.

Still, will need to watch for temp file buildup.
parent 4b5b7cd5
......@@ -24,6 +24,23 @@ if (! $isadmin) {
# Careful with this local variable
unset($prefix);
#
# Verify page arguments.
#
if (!isset($building) ||
strcmp($building, "") == 0) {
PAGEARGERROR("Must provide a building and an optional floor!");
}
# 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.");
}
#
# Run the script. It will produce two output files; an image and an areamap.
# We want to embed both of these images into the page we send back. This
......@@ -33,7 +50,9 @@ unset($prefix);
#
function CLEANUP()
{
if (isset($prefix)) {
global $prefix;
if (isset($prefix) && (connection_aborted())) {
unlink("${prefix}.png");
unlink("${prefix}.map");
unlink($prefix);
......@@ -46,13 +65,22 @@ 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}.png and ${prefix}.map)
#
$prefix = tempnam("/tmp", "floomap");
$prefix = tempnam("/tmp", "floormap");
$retval = SUEXEC("nobody", "nobody", "webfloormap -o $prefix MEB",
#
# 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", "webfloormap -o $prefix " .
(isset($floor) ? "-f $floor " : "") . "$building",
SUEXEC_ACTION_IGNORE);
if ($retval) {
SUEXECERROR(SUEXEC_ACTION_DIE);
SUEXECERROR(SUEXEC_ACTION_USERERROR);
# Never returns.
die("");
}
......@@ -65,42 +93,27 @@ if (! readfile("${prefix}.map")) {
TBERROR("Could not read ${prefix}.map", 1);
}
#
# Convert the image binary data into a javascript variable.
#
$fp = fopen("${prefix}.png", "r");
if (! $fp) {
TBERROR("Could not open ${prefix}.png", 1);
}
echo "<script language=JavaScript>
<!--
function binary(d) {
var o = '';
for (var i=0; i<d.length; i=i+2)
o+=String.fromCharCode(eval('0x' +
(d.substring(i,i+2)).toString(16)));
return o;
}
var imagedata = binary('";
# Read image data and spit out hex representation of it.
do {
$data = fread($fp, 8192);
if (strlen($data) == 0) {
break;
}
echo bin2hex($data);
} while (true);
fclose($fp);
echo "');
//-->
</script>\n";
# And the img ...
echo "<center>
<img src=\"javascript:imagedata\" usemap=\"#floormap\">
<table class=nogrid align=center border=0 cellpadding=6 cellspacing=0>
<tr>
<td align=right>Free</td>
<td align=left><img src='/autostatus-icons/greenball.gif' alt=Free>
</td>
</tr>
<tr>
<td align=right>Reserved</td>
<td align=left><img src='/autostatus-icons/blueball.gif' alt=reserved>
</td>
</tr>
<tr>
<td align=right>Dead</td>
<td align=left><img src='/autostatus-icons/redball.gif' alt=Free>
</td>
</tr>
</table>
<br><br>
<img src=\"floormap_aux.php3?prefix=$uniqueid\" usemap=\"#floormap\">
</center>\n";
#
......
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
#
# This script generates the contents of an image. No headers or footers,
# just spit back an image.
#
#
# Only admin people for now.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);
if (! $isadmin) {
USERERROR("You do not have permission to view this page!", 1);
}
#
# Verify arguments.
#
if (!isset($prefix) ||
strcmp($prefix, "") == 0) {
PAGEARGERROR("You must provide a prefix argument.");
}
if (!preg_match("/^floormap[-\w]+$/", $prefix)) {
PAGEARGERROR("Invalid prefix argument.");
}
$prefix = "/tmp/$prefix";
#
# Need cleanup "handler" to make sure temp files get deleted!
#
function CLEANUP()
{
global $prefix;
if (isset($prefix)) {
unlink("${prefix}.png");
unlink("${prefix}.map");
unlink($prefix);
}
exit();
}
register_shutdown_function("CLEANUP");
#
# Spit the areamap contained in the file out; it is fully formatted and
# called "floormap".
#
if (($fp = fopen("${prefix}.png", "r"))) {
header("Content-type: image/png");
fpassthru($fp);
}
else {
# No Data. Spit back a stub image.
header("Content-type: image/gif");
readfile("coming-soon-thumb.gif");
}
#
# No Footer!
#
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment