webcamimg.php3 2.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 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. 
#
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);
}


#
# Only known and logged in users can end experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

#
# Verify page arguments.
# 
if (!isset($webcamid) ||
    strcmp($webcamid, "") == 0) {
    MyError("You must provide a WebCam ID.");
}

if (! preg_match("/^[\d]+$/", $webcamid)) {
    MyError("Invalid characters in WebCam ID.");
}

#
43
44
# And check for entry in webcams table, which tells us the server name
# where we open the connection to. 
45
#
46
47
$query_result =
    DBQueryFatal("select * from webcams where id='$webcamid'");
48

49
50
if (!$query_result || !mysql_num_rows($query_result)) {
    MyError("No such webcam ID: '$webcamid'");
51
}
52
$row = mysql_fetch_array($query_result);
53
$URL = (isset($applet) ? $row["URL"] : $row["stillimage_URL"]);
54
if (isset($fromtracker)) {
55
56
57
58
59
60
61
62
    $URL .= "&resolution=240x180";

    if (preg_match("/fps=\d*/", $URL)) {
	$URL = preg_replace("/fps=\d*/", "fps=2", $URL);
    }
    else {
	$URL .= "&fps=2";
    }
63
}
64
65
66
67
68
69
70
71

#
# 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)) {
72
    MyError("Webcam Views are currently disabled!");
73
74
75
76
77
}

#
# Now check permission.
#
78
if (!$isadmin && !TBWebCamAllowed($uid)) {
79
80
81
    MyError("Not enough permission to view the robot cameras!");
}

82
83
84
85
86
87
88
89
90
91
92
93
$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.
#
94
95
96
if (isset($applet)) {
    header("Content-type: multipart/x-mixed-replace;boundary=--myboundary");
}
97
98

#TBERROR(print_r($http_response_header, TRUE) . "\n\n", 0);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

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

    if (!$socket || !connection_aborted()) {
	exit();
    }
    fclose($socket);
    exit();
}
# ignore_user_abort(1);
114
set_time_limit(0);
115
116
117
118
119
120
121
122
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);
123
124
125
126
127
128
129
130

#
# No Footer!
# 
?>