~ubuntu-branches/ubuntu/saucy/zoneminder/saucy-proposed

« back to all changes in this revision

Viewing changes to web/skins/xml/views/actions.php

  • Committer: Bazaar Package Importer
  • Author(s): Vagrant Cascadian, Peter Howard, Vagrant Cascadian
  • Date: 2011-07-24 16:44:30 UTC
  • mfrom: (15.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110724164430-yibtwwjqlqb7hv1o
Tags: 1.24.4-1
[ Peter Howard ]
* Initial release of 1.24.4 (Closes: #634985).
  - Fix 32/64-bit type declarations (Closes: #614404).
* Update patches.

[ Vagrant Cascadian ]
* Add patch to fix FTBFS by using libv4l1-videodev.h from libv4l-dev.
  Thanks to Andreas Metzler for reporting the issue. 
  (Closes: #619813). 
* Document adding the www-data user to the video group in README.Debian.
  (Closes: #611324)
* Depend on libsys-mmap-perl to enable mapped memory support.
  (Closes: #607331)
* Update libjs-mootools patch to use -nc variants (Closes: #635075).
* Depend on javascript-common, to ensure that /javascript is available in 
  the web server.
* Set the upstream version in postinst at build time.
* Use dh-autoreconf to properly clean up autogenerated files during build.
* Add Vcs-HG to debian/control. 
* Add Build-Depends: libv4l-dev, libbz2-dev, dh-autoreconf, libsys-mmap-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* 
 
3
 * actions.php is created by Jai Dhar, FPS-Tech, for use with eyeZm
 
4
 * iPhone application. This is not intended for use with any other applications,
 
5
 * although source-code is provided under GPL.
 
6
 *
 
7
 * For questions, please email jdhar@eyezm.com (http://www.eyezm.com)
 
8
 *
 
9
 */
 
10
/* Parse any specific actions here */
 
11
if (isset($_GET['action'])) {
 
12
        $action = $_GET['action'];
 
13
        if (!strcmp($action, "devent")) {
 
14
                /* ACTION: Delete an Event. Parms: <eid> */
 
15
                if (!canEdit('Events')) {
 
16
                        logXmlErr("User ".$user['Username']. " doesn't have edit Events perms");
 
17
                        exit;
 
18
                }
 
19
                if (!isset($_REQUEST['eid'])) {
 
20
                        logXmlErr("EID not set for action delete-event");
 
21
                        exit;
 
22
                }
 
23
                $eid = validInteger($_REQUEST['eid']);
 
24
                $url = "./index.php?view=request&request=event&id=".$eid."&action=delete";
 
25
                header("Location: ".$url);
 
26
                exit;
 
27
 
 
28
        } else if (!strcmp($action, "spawn264")) {
 
29
                /* ACTION: Spawn 264 streaming process.
 
30
                 * Parms: <monitor>[br|width|height] */
 
31
                if (!canView('Stream')) {
 
32
                        logXmlErr("User ".$user['Username']. " doesn't have view Stream perms");
 
33
                        exit;
 
34
                }
 
35
                if (!isset($_GET['monitor'])) {
 
36
                        logXmlErr("Not all parameters specified for spawn264");
 
37
                        exit;
 
38
                }
 
39
                $monitor = validInteger($_REQUEST['monitor']);
 
40
                if (!isMonitor($monitor)) exit;
 
41
                $dims = getMonitorDims($monitor);
 
42
                $width = validInteger(getset('width', $dims['Width']));
 
43
                $height = validInteger(getset('height', $dims['Height']));
 
44
                $br = validString(getset('br', ZM_EYEZM_H264_DEFAULT_BR));
 
45
                /* Check that we can stream first */
 
46
                if (!canStream264()) {
 
47
                        /* canStream264 will print out error */
 
48
                        exit;
 
49
                }
 
50
                $streamUrl = stream264fn($monitor, $width, $height, $br);
 
51
                logXml("Using H264 Pipe Function: ".$streamUrl);
 
52
                $pid = shell_exec($streamUrl);
 
53
                logXml("Streaming Process for monitor ".$monitor." ended, cleaning up files");
 
54
                eraseH264Files($monitor);
 
55
                exit;
 
56
 
 
57
        } else if (!strcmp($action, "kill264")) {
 
58
                /* ACTION: Kill existing H264 stream process and cleanup files.
 
59
                 * Parms: <monitor>.
 
60
                 * NOTE: This will be called directly by path, so include files
 
61
                 * may not be available */
 
62
                session_start();
 
63
                require_once(dirname(__FILE__)."/../includes/functions.php");
 
64
                if (!isset($_GET['monitor'])) {
 
65
                        logXmlErr("Not all parameters specified for kill264");
 
66
                        exit;
 
67
                }
 
68
                $monitor = validInteger($_GET['monitor']);
 
69
                kill264proc($monitor);
 
70
                logXml("Killed Segmenter process for monitor ".$monitor);
 
71
                exit;
 
72
 
 
73
        } else if (!strcmp($action, "chk264")) {
 
74
                /* ACTION: Simply stalls while checking for 264 file.
 
75
                 * Parms: <monitor><timeout> 
 
76
                 * NOTE: This will be called directly by path, so include files
 
77
                 * may not be available */
 
78
                session_start();
 
79
                require_once(dirname(__FILE__)."/../includes/functions.php");
 
80
                if (!isset($_GET['monitor']) || !isset($_GET['timeout'])) {
 
81
                        logXmlErr("Monitor not specified for chk264");
 
82
                        exit;
 
83
                }
 
84
                $monitor = validInteger($_GET['monitor']);
 
85
                $path = getTempDir()."/".m3u8fname($monitor);
 
86
                /* Wait for the second sample to become available */
 
87
                $tsfile = getTempDir()."/sample_".$monitor."-2.ts";
 
88
                /* Setup timeout */
 
89
                $startTime = time();
 
90
                $timeout = validInteger($_GET['timeout']);
 
91
                while (!file_exists($path) || !file_exists($tsfile)) {
 
92
                        if (time() > $startTime + $timeout) {
 
93
                                logXmlErr("Timed out waiting for stream to start, exiting...");
 
94
                                kill264proc($monitor);
 
95
                                exit;
 
96
                        }
 
97
                        usleep(10000);
 
98
                }
 
99
                logXml("File exists, stream created after ".(time()-$startTime)." sec");
 
100
                exit;
 
101
 
 
102
        } else if (!strcmp($action, "feed")) {
 
103
                /* ACTION: View a feed. Parms: <monitor> [height|width|fps|scale|vcodec|br] */
 
104
                if (!canView('Stream')) {
 
105
                        logXmlErr("User ".$user['Username']. " doesn't have view Stream perms");
 
106
                        exit;
 
107
                }
 
108
                /* Check that required variables are set */
 
109
                if (!isset($_REQUEST['monitor'])) {
 
110
                        logXmlErr("Not all parameters set for action view-feed");
 
111
                        exit;
 
112
                }
 
113
                $monitor = validInteger($_REQUEST['monitor']);
 
114
                if (!isMonitor($monitor)) exit;
 
115
                $dims = getMonitorDims($monitor);
 
116
                $width = validInteger(getset('width', $dims['Width']));
 
117
                $height = validInteger(getset('height', $dims['Height']));
 
118
                $fps = validInteger(getset('fps', ZM_WEB_VIDEO_MAXFPS));
 
119
                $scale = validInteger(getset('scale', 100));
 
120
                $vcodec = validString(getset('vcodec', ZM_EYEZM_FEED_VCODEC));
 
121
                /* Select which codec we want */
 
122
                if (!strcmp($vcodec, "h264")) {
 
123
                        /* Validate that we can in fact stream H264 */
 
124
                        if (!canStream264()) {
 
125
                                /* canStream264 will print out error if
 
126
                                 * there is one */
 
127
                                echo "Server cannot stream H264. Check eyeZm log for details";
 
128
                                exit;
 
129
                        }
 
130
                        if (!requireVer("1", "2")) {
 
131
                                echo "H264 Streaming requires eyeZm v1.2 or above";
 
132
                                logXmlErr("H264 Streaming requires eyeZm v1.2 or above");
 
133
                                exit;
 
134
                        }
 
135
                        $br = validString(getset('br', ZM_EYEZM_H264_DEFAULT_BR));
 
136
                        /* H264 processing */
 
137
                        noCacheHeaders();
 
138
                        /* Kill any existing processes and files */
 
139
                        kill264proc($monitor);
 
140
                        eraseH264Files($monitor);
 
141
                        logXml("Streaming H264 on Monitor ".$monitor.", ".$width."x".$height." @".$br);
 
142
                        /* Get thumbnail source */
 
143
                        $thumbsrc = 
 
144
                                getStreamSrc( array( 
 
145
                                        "mode=single", 
 
146
                                        "monitor=".$monitor, 
 
147
                                        "scale=".$scale,        
 
148
                                        "maxfps=".$fps,
 
149
                                        "buffer=1000" 
 
150
                                ) );
 
151
                        logXml("Using thumbnail image from ".$thumbsrc);
 
152
                        /* Generate H264 Web-page */
 
153
                        echo "<meta name=\"viewport\" content=\"width=".$width."\" />\n";
 
154
                        h264vidHtml($width, $height, $monitor, $br, $thumbsrc);
 
155
                } else if (!strcmp($vcodec, "mjpeg")) {
 
156
                        /* MJPEG streaming */
 
157
                        /* If $fps=0, get a single-shot */
 
158
                        if (!$fps) {
 
159
                                /* single-shot */
 
160
                                $streamSrc = 
 
161
                                        getStreamSrc( array( 
 
162
                                                "mode=single", 
 
163
                                                "monitor=".$monitor, 
 
164
                                                "scale=".$scale,        
 
165
                                                "maxfps=0",
 
166
                                                "buffer=1000" 
 
167
                                        ) );
 
168
                        } else {
 
169
                                $streamSrc = 
 
170
                                        getStreamSrc( array( 
 
171
                                                "mode=jpeg", 
 
172
                                                "monitor=".$monitor, 
 
173
                                                "scale=".$scale,        
 
174
                                                "maxfps=".$fps,
 
175
                                                "buffer=1000" 
 
176
                                        ) );
 
177
                        }
 
178
                        noCacheHeaders();
 
179
                        xhtmlHeaders( __FILE__, "Stream" );
 
180
                        logXml("Streaming MJPEG on Monitor ".$monitor.", ".$width."x".$height." @".$fps."fps");
 
181
                        echo "<meta name=\"viewport\" content=\"width=".$width."\" />\n";
 
182
                        echo "<body>\n";
 
183
                        echo "<div style=\"border: 0px solid; padding: 0px; background-color: black; position: absolute; top: 0px; left; 0px; margin: 0px; width: ".$width."px; height: ".$height."px;\">\n";
 
184
                        logXml("Using stream source: ".$streamSrc);
 
185
                        outputImageStream("liveStream", $streamSrc, $width, $height, "stream");
 
186
                        echo "</div></body></html>";
 
187
                } else {
 
188
                        logXmlErr("Unsupported codec ".$vcodec." selected for streaming");
 
189
                        echo("Unsupported codec ".$vcodec." selected for streaming");
 
190
                }
 
191
                exit;
 
192
 
 
193
        } else if (!strcmp($action, "vevent")) {
 
194
                /* ACTION: View an event. Parms: <eid> [fps|vcodec|br] */
 
195
                if (!canView('Events')) {
 
196
                        logXmlErr("User ".$user['Username']. " doesn't have view Events perms");
 
197
                        exit;
 
198
                }
 
199
                if (!isset($_GET['eid'])) {
 
200
                        logXmlErr("Not all parameters set for Action View-event");
 
201
                        exit;
 
202
                }
 
203
                /* Grab event from the database */
 
204
                $eid = validInteger($_GET['eid']);
 
205
                $eventsSql = "select E.Id, E.MonitorId, E.Name, E.StartTime, E.Length, E.Frames from Events as E where (E.Id = ".$eid.")";
 
206
                $event = dbFetchOne(escapeSql($eventsSql));
 
207
                /* Check if exists */
 
208
                if (!$event) {
 
209
                        logxmlErr("Requested event ID ".$eid." does not exist");
 
210
                        exit;
 
211
                }
 
212
                /* Calculate FPS */
 
213
                $fps = validInteger(getset('fps',ceil($event['Frames'] / $event['Length'])));
 
214
                $vcodec = validString(getset('vcodec', ZM_EYEZM_EVENT_VCODEC));
 
215
                $baseURL = ZM_PATH_WEB."/".getEventPathSafe($event);
 
216
                /* Here we validate the codec.
 
217
                 * Check that FFMPEG exists and supports codecs */
 
218
                if (!strcmp($vcodec, "mpeg4")) {
 
219
                        if (!ffmpegSupportsCodec("mpeg4")) {
 
220
                                logXmlErr("FFMPEG not installed, accessible in path/ZM_PATH_FFMPEG, or doesn't support mpeg4");
 
221
                                exit;
 
222
                        }
 
223
                        /* Can generate, we are good to go */
 
224
                        $fname = "capture.mov";
 
225
                        $ffparms = "-vcodec mpeg4 -r ".ZM_EYEZM_EVENT_FPS." ".$baseURL."/".$fname." 2> /dev/null";
 
226
 
 
227
                } else if (!strcmp($vcodec, "h264")) {
 
228
                        if (!ffmpegSupportsCodec("libx264")) {
 
229
                                logXmlErr("FFMPEG not installed, accessible in path/ZM_PATH_FFMPEG, or doesn't support H264");
 
230
                                exit;
 
231
                        }
 
232
                        if (!requireVer("1","2")) {
 
233
                                logXmlErr("H264 Event viewing requires eyeZm v1.2 or greater");
 
234
                                exit;
 
235
                        }
 
236
                        /* Good to go */
 
237
                        $fname = "capture.mp4";
 
238
                        $ffparms = getFfmpeg264FoutParms(
 
239
                                validString(getset('br',ZM_EYEZM_H264_DEFAULT_EVBR)),
 
240
                                $baseURL."/".$fname);
 
241
 
 
242
                } else {
 
243
                        logXmlErr("Unknown codec ".$vcodec." selected for event viewing");
 
244
                        exit;
 
245
                }
 
246
                logXml("Selected ".$vcodec." for viewing event ".$event['Id']);
 
247
                $fnameOut = $baseURL."/".$fname;
 
248
                $shellCmd = getFfmpegPath()." -y -r ".$fps." -i ".$baseURL."/%0".ZM_EVENT_IMAGE_DIGITS."d-capture.jpg";
 
249
                $shellCmd .= " ".$ffparms;
 
250
                logXml("Encoding event with command: ".$shellCmd);
 
251
                $shellOutput = shell_exec($shellCmd);
 
252
                /* Check that file exists */
 
253
                if (!file_exists(trim($fnameOut))) {
 
254
                        logXmlErr("Generate Event ".$event['Id']." file ".$fnameOut." does not exist");
 
255
                        exit;
 
256
                }
 
257
                $url = "./".getEventPathSafe($event)."/".$fname;
 
258
                logXml("Loading Event URL ".$url);
 
259
                header("Location: ".$url);
 
260
                exit;
 
261
 
 
262
        } else if (!strcmp($action, "vframe")) {
 
263
                /* ACTION: View a frame given by an event and frame-id. Parms: <eid> <frame> [alarm | analyze | qty | scale]
 
264
                 * If 'alarm' is set, the returned frame will be the <frame>-th alarm frame. If 'analyze' is set,
 
265
                 * the returned frame will be the %03d-analyse frame instead of %03d-capture, if ZM_CREATE_ANALYSIS_IMAGES
 
266
                 * is set. Otherwise it just returns the captured frame.
 
267
                 * If qty is set, it will apply a quality factor from 0-100, and if width is set, it will scale the jpeg accordingly
 
268
                 */
 
269
                if (!isset($_GET['eid']) || !isset($_GET['frame'])) {
 
270
                        logXmlErr("Not all parameters set for action view-frame");
 
271
                        exit;
 
272
                }
 
273
                $eid = validInteger($_GET['eid']);
 
274
                $frame = validInteger($_GET['frame']);
 
275
                $eventsSql = "select E.Id, E.MonitorId, E.Name, E.StartTime, E.Length, E.Frames from Events as E where (E.Id = ".$eid.")";
 
276
                $event = dbFetchOne(escapeSql($eventsSql));
 
277
                $qty = validInteger(getset('qty', 100));
 
278
                if ($qty > 100) $qty = 100;
 
279
                $scale = validInteger(getset('scale', 100));
 
280
                if (!$event) {
 
281
                        logxmlErr("Requested event ID ".$eid." does not exist");
 
282
                        exit;
 
283
                }
 
284
                /* Figure out the frame number. If 'alarm' is not set, this is just equal to the <frame> parameter.
 
285
                 * If 'alarm' is set, need to query DB and grab the <frame>-th item */
 
286
                if (isset($_GET['alarm'])) {
 
287
                        $frameSql = escapeSql("select * from Frames as F where (F.EventId = ".$eid.") ");
 
288
                        $frameSql .= " and (F.Type = 'Alarm') order by F.FrameId";
 
289
                        $i=0;
 
290
                        foreach (dbFetchAll($frameSql) as $dbframe) {
 
291
                                if ($i == $frame) {
 
292
                                        $frame = $dbframe['FrameId'];
 
293
                                        break;
 
294
                                }
 
295
                                $i++;
 
296
                        }
 
297
                }
 
298
                if (isset($_GET['analyze']) && ZM_CREATE_ANALYSIS_IMAGES) {
 
299
                        $suffix = "analyse";
 
300
                } else {
 
301
                        $suffix = "capture";
 
302
                }
 
303
                /* A frame index of 0 is invalid, so if we see this, just use frame 1 */
 
304
                if (!$frame) $frame = 1;
 
305
                /* Suffix based on 'analyze' */
 
306
                $fname = sprintf("%0".ZM_EVENT_IMAGE_DIGITS."d-%s.jpg", $frame, $suffix);
 
307
                $url = "./".getEventPathSafe($event)."/".$fname;
 
308
                if (!file_exists($url)) {
 
309
                        logXmlErr("Invalid frame image requested: ".$url);
 
310
                        $url = "./skins/xml/views/notfound.png";
 
311
                }
 
312
                /* Check if the image needs any processing - check for GD if requested */
 
313
                if (($scale != 100) || ($qty < 100)) {
 
314
                        if (!gdExists()) {
 
315
                                logXmlErr("Lib GD is not loaded, but required for image scaling functions");
 
316
                                $url = "./skins/xml/views/notfound.png";
 
317
                        } else if (!$img = imagecreatefromjpeg($url)) {
 
318
                                logXmlErr("Could not load JPEG from ".$url);
 
319
                                $url = "./skins/xml/views/notfound.png";
 
320
                        } else {
 
321
                                /* GD exists and we read the file ok */
 
322
                                header('Content-type: image/jpeg');
 
323
                                /* Check if resizing is needed */
 
324
                                if ($scale != 100) {
 
325
                                        list($width_orig, $height_orig) = getimagesize($url);
 
326
                                        $width_new = $width_orig * ($scale/100);
 
327
                                        $height_new = $height_orig * ($scale/100);
 
328
                                        $img_new = imagecreatetruecolor($width_new, $height_new);
 
329
                                        imagecopyresampled($img_new, $img, 0, 0, 0, 0, $width_new, $height_new, $width_orig, $height_orig);
 
330
                                        imagejpeg($img_new, NULL, $qty);
 
331
                                } else {
 
332
                                        imagejpeg($img, NULL, $qty);
 
333
                                }
 
334
                                exit;
 
335
                        }
 
336
                } 
 
337
                header("Location: ".$url);
 
338
                exit;
 
339
 
 
340
        } else if (!strcmp($action, "state")) {
 
341
                /* ACTION: Change the state of the system. Parms: <state> */
 
342
                if (!canEdit('System')) {
 
343
                        logXmlErr("User ".$user['Username']. " doesn't have edit System perms");
 
344
                        exit;
 
345
                }
 
346
                if (!isset($_GET['state'])) {
 
347
                        logXmlErr("Server state not specified for action");
 
348
                        exit;
 
349
                }
 
350
                $url = "./index.php?view=none&action=state&runState=".validString($_GET['state']);
 
351
                header("Location: ".$url);
 
352
                exit;
 
353
 
 
354
        } else if (!strcmp($action, "func")) {
 
355
                /* ACTION: Change state of the monitor. Parms: <mid><func><en> */
 
356
                if (!canEdit('Monitors')) {
 
357
                        logXmlErr("User ".$user['Username']. " doesn't have monitors Edit perms");
 
358
                        exit;
 
359
                }
 
360
                if (!isset($_GET['mid']) || !isset($_GET['func']) || !isset($_GET['en'])) {
 
361
                        logXmlErr("Not all parameters specified for action Monitor state");
 
362
                        exit;
 
363
                }
 
364
                $mid = validInteger($_GET['mid']);
 
365
                if (!isMonitor($mid)) exit;
 
366
                $url = "./index.php?view=none&action=function&mid=".$mid."&newFunction=".validString($_GET['func'])."&newEnabled=".validString($_GET['en']);
 
367
                header("Location: ".$url);
 
368
                exit;
 
369
 
 
370
        } else if (!strcmp($action, "vlog")) {
 
371
                /* ACTION: View log file. Must have debug and log to file enabled, and sufficient perms 
 
372
                 * Parms: [lines] */
 
373
                if (!canEdit('System')) {
 
374
                        logXmlErr("Insufficient permissions to view log file");
 
375
                        echo "Insufficient permissions to view log file";
 
376
                        exit;
 
377
                }
 
378
                if (!ZM_EYEZM_DEBUG || !ZM_EYEZM_LOG_TO_FILE) {
 
379
                        echo "eyeZm Debug (EYEZM_DEBUG) or log-to-file (EYEZM_LOG_TO_FILE) not enabled. Please enable first";
 
380
                        exit;
 
381
                }
 
382
                if (!file_exists(ZM_EYEZM_LOG_FILE)) {
 
383
                        echo "Log file ".ZM_EYEZM_LOG_FILE." doesn't exist";
 
384
                        exit;
 
385
                }
 
386
                $lines = validInteger(getset('lines',ZM_EYEZM_LOG_LINES));
 
387
                logXml("Returning last ".$lines." lines of eyeZm Log from ".ZM_EYEZM_LOG_FILE);
 
388
                echo shell_exec("tail -n ".$lines." ".ZM_EYEZM_LOG_FILE);
 
389
                echo "\n\n--- Showing last ".$lines." lines ---\n";
 
390
                echo "--- End of Log ---\n\n";
 
391
        }
 
392
}
 
393
?>