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.
7
* For questions, please email jdhar@eyezm.com (http://www.eyezm.com)
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");
19
if (!isset($_REQUEST['eid'])) {
20
logXmlErr("EID not set for action delete-event");
23
$eid = validInteger($_REQUEST['eid']);
24
$url = "./index.php?view=request&request=event&id=".$eid."&action=delete";
25
header("Location: ".$url);
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");
35
if (!isset($_GET['monitor'])) {
36
logXmlErr("Not all parameters specified for spawn264");
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 */
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);
57
} else if (!strcmp($action, "kill264")) {
58
/* ACTION: Kill existing H264 stream process and cleanup files.
60
* NOTE: This will be called directly by path, so include files
61
* may not be available */
63
require_once(dirname(__FILE__)."/../includes/functions.php");
64
if (!isset($_GET['monitor'])) {
65
logXmlErr("Not all parameters specified for kill264");
68
$monitor = validInteger($_GET['monitor']);
69
kill264proc($monitor);
70
logXml("Killed Segmenter process for monitor ".$monitor);
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 */
79
require_once(dirname(__FILE__)."/../includes/functions.php");
80
if (!isset($_GET['monitor']) || !isset($_GET['timeout'])) {
81
logXmlErr("Monitor not specified for chk264");
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";
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);
99
logXml("File exists, stream created after ".(time()-$startTime)." sec");
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");
108
/* Check that required variables are set */
109
if (!isset($_REQUEST['monitor'])) {
110
logXmlErr("Not all parameters set for action view-feed");
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
127
echo "Server cannot stream H264. Check eyeZm log for details";
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");
135
$br = validString(getset('br', ZM_EYEZM_H264_DEFAULT_BR));
136
/* H264 processing */
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 */
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 */
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";
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>";
188
logXmlErr("Unsupported codec ".$vcodec." selected for streaming");
189
echo("Unsupported codec ".$vcodec." selected for streaming");
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");
199
if (!isset($_GET['eid'])) {
200
logXmlErr("Not all parameters set for Action View-event");
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 */
209
logxmlErr("Requested event ID ".$eid." does not exist");
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");
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";
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");
232
if (!requireVer("1","2")) {
233
logXmlErr("H264 Event viewing requires eyeZm v1.2 or greater");
237
$fname = "capture.mp4";
238
$ffparms = getFfmpeg264FoutParms(
239
validString(getset('br',ZM_EYEZM_H264_DEFAULT_EVBR)),
240
$baseURL."/".$fname);
243
logXmlErr("Unknown codec ".$vcodec." selected for event viewing");
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");
257
$url = "./".getEventPathSafe($event)."/".$fname;
258
logXml("Loading Event URL ".$url);
259
header("Location: ".$url);
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
269
if (!isset($_GET['eid']) || !isset($_GET['frame'])) {
270
logXmlErr("Not all parameters set for action view-frame");
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));
281
logxmlErr("Requested event ID ".$eid." does not exist");
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";
290
foreach (dbFetchAll($frameSql) as $dbframe) {
292
$frame = $dbframe['FrameId'];
298
if (isset($_GET['analyze']) && ZM_CREATE_ANALYSIS_IMAGES) {
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";
312
/* Check if the image needs any processing - check for GD if requested */
313
if (($scale != 100) || ($qty < 100)) {
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";
321
/* GD exists and we read the file ok */
322
header('Content-type: image/jpeg');
323
/* Check if resizing is needed */
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);
332
imagejpeg($img, NULL, $qty);
337
header("Location: ".$url);
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");
346
if (!isset($_GET['state'])) {
347
logXmlErr("Server state not specified for action");
350
$url = "./index.php?view=none&action=state&runState=".validString($_GET['state']);
351
header("Location: ".$url);
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");
360
if (!isset($_GET['mid']) || !isset($_GET['func']) || !isset($_GET['en'])) {
361
logXmlErr("Not all parameters specified for action Monitor state");
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);
370
} else if (!strcmp($action, "vlog")) {
371
/* ACTION: View log file. Must have debug and log to file enabled, and sufficient perms
373
if (!canEdit('System')) {
374
logXmlErr("Insufficient permissions to view log file");
375
echo "Insufficient permissions to view log file";
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";
382
if (!file_exists(ZM_EYEZM_LOG_FILE)) {
383
echo "Log file ".ZM_EYEZM_LOG_FILE." doesn't exist";
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";