d = $e; } public static function find_one($p) { $e = new Event($p['Id']); return $e->Id() ? $e : null; } public function canView() { return true; } public function Id() { return isset($this->d['Id']) ? $this->d['Id'] : 0; } public function MonitorId() { return 1; } public function Path() { return $this->d['Path']; } public function SaveJPEGs() { return $this->d['SaveJPEGs']; } public function DefaultVideo() { return $this->d['DefaultVideo']; } public function MaxScore() { return 0; } public function Frames() { return $this->d['Frames']; } public function EndDateTime() { return $this->d['EndDateTime']; } public function DiskSpace($v) {} public function find_virtual_frame($fid) { $f = new Frame(); $f->FrameId($fid); $f->Delta($fid * 0.1); return $f; } } EOT); file_put_contents($tmp.'/inc/includes/Frame.php', <<<'EOT' 0, 'Delta' => 0); public static function find_one($p, $o = array()) { foreach ($GLOBALS['ZMT']['frames'] as $row) { $ok = true; foreach ($p as $k => $v) if (!isset($row[$k]) or $row[$k] != $v) $ok = false; if ($ok) { $f = new Frame(); $f->d = $row; return $f; } } return null; } public function FrameId($n = null) { if ($n !== null) $this->d['FrameId'] = $n; return $this->d['FrameId']; } public function Delta($n = null) { if ($n !== null) $this->d['Delta'] = $n; return $this->d['Delta']; } public function Id($n = null) { if ($n !== null) $this->d['Id'] = $n; return isset($this->d['Id']) ? $this->d['Id'] : 0; } } EOT); // A fake ffmpeg: logs its arguments and writes a small jpeg to the last argument. $ffmpegLog = $tmp.'/ffmpeg.log'; $srcJpg = $tmp.'/src.jpg'; $im = imagecreatetruecolor(64, 48); imagejpeg($im, $srcJpg); file_put_contents($tmp.'/ffmpeg', "#!/bin/sh\nfor a in \"\$@\"; do l=\"\$a\"; done\nprintf '%s\\n' \"\$*\" >> ".escapeshellarg($ffmpegLog)."\ncp ".escapeshellarg($srcJpg)." \"\$l\"\n"); chmod($tmp.'/ffmpeg', 0700); $port = 20000 + (getmypid() % 20000); $base = 'http://127.0.0.1:'.$port; $server = proc_open(array(PHP_BINARY, '-d', 'display_errors=0', '-d', 'log_errors=1', '-d', 'error_log='.$tmp.'/php.log', '-S', '127.0.0.1:'.$port, __FILE__), array(0 => array('file', '/dev/null', 'r'), 1 => array('file', '/dev/null', 'w'), 2 => array('file', '/dev/null', 'w')), $pipes); for ($i = 0; $i < 50; $i++) { $c = @fsockopen('127.0.0.1', $port); if ($c) { fclose($c); break; } usleep(100000); } $scn = 0; function run_view($view, $request, $eventOver = array(), $files = array(), $opts = array()) { global $tmp, $scn, $ffmpegLog, $srcJpg; $scn++; $dir = $tmp.'/ev'.$scn; mkdir($dir, 0700, true); foreach ($files as $name => $content) { file_put_contents($dir.'/'.$name, $content === true ? file_get_contents($srcJpg) : $content); } @unlink($ffmpegLog); $event = array_merge(array( 'Id' => 5, 'Path' => $dir, 'SaveJPEGs' => 0, 'DefaultVideo' => '', 'Frames' => 30, 'EndDateTime' => '2026-01-01 00:00:00', ), $eventOver); $cfg = array( 'view' => $view, 'request' => $request, 'event' => $event, 'frames' => isset($opts['frames']) ? $opts['frames'] : array(), 'canView' => isset($opts['canView']) ? $opts['canView'] : array('Events', 'Snapshots'), 'stubdir' => $tmp.'/inc', 'ffmpeg' => $tmp.'/ffmpeg', ); file_put_contents($tmp.'/cfg'.$scn.'.json', json_encode($cfg)); $errFile = $tmp.'/cfg'.$scn.'.json.errors'; @unlink($errFile); $ctx = stream_context_create(array('http' => array('ignore_errors' => true, 'timeout' => 20))); $body = @file_get_contents($GLOBALS['base'].'/?cfg='.urlencode($tmp.'/cfg'.$scn.'.json'), false, $ctx); $res = array('headers' => isset($http_response_header) ? $http_response_header : array(), 'status' => 0); if ($res['headers'] && preg_match('@^HTTP/\S+ (\d+)@', $res['headers'][0], $m)) $res['status'] = (int)$m[1]; $res['body'] = (string)$body; $res['errors'] = file_exists($errFile) ? array_values(array_filter(explode("\n", file_get_contents($errFile)))) : array(); $res['fatal'] = $res['status'] >= 500; $res['raw'] = $res['body']; $res['dir'] = $dir; $res['ffmpeg'] = file_exists($ffmpegLog) ? trim(file_get_contents($ffmpegLog)) : ''; return $res; } function header_of($res, $name) { foreach ($res['headers'] as $h) { if (stripos($h, $name.':') === 0) return trim(substr($h, strlen($name) + 1)); } return null; } echo "control: stored frame is served as is\n"; $r = run_view('image', array('eid' => 5, 'fid' => 3), array(), array('00003-capture.jpg' => true), array('frames' => array(array('EventId' => 5, 'FrameId' => 3, 'Delta' => 0.3)))); check("image/jpeg content type", header_of($r, "Content-type") === "image/jpeg", json_encode($r["headers"])); check('bytes are the stored jpeg', $r['body'] === file_get_contents($srcJpg)); echo "control: width and height scale the image\n"; $r = run_view('image', array('eid' => 5, 'fid' => 3, 'width' => 32, 'height' => 24), array(), array('00003-capture.jpg' => true), array('frames' => array(array('EventId' => 5, 'FrameId' => 3, 'Delta' => 0.3)))); $size = @getimagesizefromstring($r['body']); check('scaled to 32x24', $size && $size[0] == 32 && $size[1] == 24); echo "view_video.php on an event with no video file\n"; // DefaultVideo is empty, so the path is the event directory itself. fopen() // succeeds on a directory, so the view answered 200 with Content-Length 4096 // and no body instead of 404. $r = run_view('view_video', array('eid' => 5)); check('answers 404', $r['status'] === 404, 'status='.var_export($r['status'], true)); check('does not announce a 4096 byte body', header_of($r, 'Content-Length') === null); check('no PHP warnings', $r['errors'] === array(), json_encode($r['errors'])); echo "image.php Content-Disposition for objdetect gif and mp4\n"; foreach (array('objdetect_gif' => 'gif', 'objdetect_mp4' => 'mp4') as $fid => $ext) { $r = run_view('image', array('eid' => 5, 'fid' => $fid), array(), array('objdetect.'.$ext => 'GIF89a-not-really-an-image')); $cd = (string)header_of($r, 'Content-Disposition'); check("$fid file name ends in .$ext", preg_match('/_objdetect\.'.$ext.'"$/', $cd) === 1, $cd); } echo "image.php scale on a jpeg that cannot be decoded\n"; // A zero length jpeg (zmc has created the file but not written it yet). $r = run_view('image', array('eid' => 5, 'fid' => 3, 'scale' => 50), array(), array('00003-capture.jpg' => ''), array('frames' => array(array('EventId' => 5, 'FrameId' => 3, 'Delta' => 0.3)))); check('no fatal error', !$r['fatal'], 'status='.$r['status'].' '.@file_get_contents($tmp.'/php.log')); echo "image.php fid past the last frame of a closed event\n"; $ev = array('DefaultVideo' => 'video.mp4', 'Frames' => 30); $r = run_view('image', array('eid' => 5, 'fid' => 400), $ev, array('video.mp4' => 'x')); check('answers 404', $r['status'] === 404, 'status='.var_export($r['status'], true)); check('does not run ffmpeg', $r['ffmpeg'] === '', $r['ffmpeg']); check('does not create an image file', !file_exists($r['dir'].'/00400-capture.jpg')); $r = run_view('image', array('eid' => 5, 'fid' => 25), $ev, array('video.mp4' => 'x')); check('last-frames-in-range fid is still extracted', file_exists($r['dir'].'/00025-capture.jpg') && $r['ffmpeg'] !== ''); $r = run_view('image', array('eid' => 5, 'fid' => 400), array_merge($ev, array('EndDateTime' => null)), array('video.mp4' => 'x')); check('open event (Frames not final) is not clamped', file_exists($r['dir'].'/00400-capture.jpg')); echo "image.php fractional width\n"; $r = run_view('image', array('eid' => 5, 'fid' => 3, 'width' => '32.5', 'height' => '24'), array(), array('00003-capture.jpg' => true), array('frames' => array(array('EventId' => 5, 'FrameId' => 3, 'Delta' => 0.3)))); check('no deprecation raised', $r['errors'] === array(), json_encode($r['errors'])); check('cache file name uses whole pixels', count(glob($r['dir'].'/00003-capture-32x24.jpg')) === 1, implode(',', array_map('basename', glob($r['dir'].'/*')))); echo "image.php with no fid and no Events permission\n"; $r = run_view('image', array(), array(), array(), array('canView' => array())); check('no undefined index warning', $r['errors'] === array(), json_encode($r['errors'])); echo "\n$passed passed, $failed failed\n"; proc_terminate($server); exec('rm -rf '.escapeshellarg($tmp)); exit($failed ? 1 : 0);