webcamimg.php3 3.84 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2005, 2006, 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 30 31 32 33 34 35 36 37 38 39
#
include("defs.php3");

#
# This script generates the contents of an image. No headers or footers,
# just spit back an image. 
#
function MyError($msg)
{
    # No Data. Spit back a stub image.
    #TBERROR($msg, 1);
    header("Content-type: image/gif");
    readfile("coming-soon-thumb.gif");
    exit(0);
}

#
40
# Only known and logged in users.
41
#
42 43 44
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
45 46

#
47 48 49 50 51 52 53 54
# Verify page arguments
#
$optargs = OptionalPageArguments("camheight",   PAGEARG_INTEGER,
				 "camwidth",    PAGEARG_INTEGER,
				 "camfps",      PAGEARG_INTEGER,
				 "fromtracker", PAGEARG_INTEGER,
				 "webcamid",    PAGEARG_INTEGER,
				 "applet",      PAGEARG_BOOLEAN);
55 56

#
57 58
# And check for entry in webcams table, which tells us the server name
# where we open the connection to. 
59
#
60 61
$query_result =
    DBQueryFatal("select * from webcams where id='$webcamid'");
62

63 64
if (!$query_result || !mysql_num_rows($query_result)) {
    MyError("No such webcam ID: '$webcamid'");
65
}
66
$row = mysql_fetch_array($query_result);
67
$URL = (isset($applet) ? $row["URL"] : $row["stillimage_URL"]);
68
if (isset($fromtracker)) {
69 70 71 72 73 74 75 76 77 78 79 80 81
    #
    # Some stuff to control the camera.
    #
    if (! isset($camheight) || !TBvalid_integer($camheight)) {
	$camheight = 180;
    }
    if (! isset($camwidth) || !TBvalid_integer($camwidth)) {
	$camwidth = 240;
    }
    if (! isset($camfps) || !TBvalid_integer($camfps)) {
	$camfps = 2;
    }
    $URL .= "&resolution=${camwidth}x${camheight}";
82 83

    if (preg_match("/fps=\d*/", $URL)) {
84
	$URL = preg_replace("/fps=\d*/", "fps=${camfps}", $URL);
85 86
    }
    else {
87
	$URL .= "&fps=${camfps}";
88
    }
89
}
90 91 92 93 94 95 96 97

#
# Check sitevar to make sure mere users are allowed to peek at us.
#
$anyone_can_view = TBGetSiteVar("webcam/anyone_can_view");
$admins_can_view = TBGetSiteVar("webcam/admins_can_view");

if (!$admins_can_view || (!$anyone_can_view && !$isadmin)) {
98
    MyError("Webcam Views are currently disabled!");
99 100 101 102 103
}

#
# Now check permission.
#
104
if (!$isadmin && !$this_user->WebCamAllowed()) {
105 106 107
    MyError("Not enough permission to view the robot cameras!");
}

108 109 110 111 112 113 114 115 116 117 118 119
$socket = fopen($URL, "r");
if (!$socket) {
    TBERROR("Error opening URL $URL", 0);
    MyError("Error opening URL");
}

#
# So, the webcam spits out its own HTTP headers, which includes this
# content-type line, but all those headers are basically lost cause
# of the interface we are using (fopen). No biggie, but we have to
# spit them out ourselves so the client knows what to do.
#
120 121 122
if (isset($applet)) {
    header("Content-type: multipart/x-mixed-replace;boundary=--myboundary");
}
123 124

#TBERROR(print_r($http_response_header, TRUE) . "\n\n", 0);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

#
# Clean up when the remote user disconnects
#
function SPEWCLEANUP()
{
    global $socket;

    if (!$socket || !connection_aborted()) {
	exit();
    }
    fclose($socket);
    exit();
}
# ignore_user_abort(1);
140
set_time_limit(0);
141 142 143 144 145 146 147 148
register_shutdown_function("SPEWCLEANUP");

#
# Spit back the image. The webcams include all the necessary headers,
# so do not spit any headers here.
#
fpassthru($socket);
fclose($socket);
149 150 151 152 153 154 155 156

#
# No Footer!
# 
?>