f = Rec::$db[$id]; } public function __call($name, $args) { if (count($args)) $this->f[$name] = $args[0]; return isset($this->f[$name]) ? $this->f[$name] : null; } public function Id($v = null) { return isset($this->f['Id']) ? $this->f['Id'] : null; } public function Storage() { return new Storage(); } public function zmcControl($m) {} public function zmtrackControl($m) {} public function sendControlCommand($m) {} public function commandFieldChangesNotAllowed($d) { return array(); } public function changes($new) { $c = array(); foreach ($new as $k => $v) { if (!array_key_exists($k, $this->f) or (string)$this->f[$k] !== (string)$v) $c[$k] = $v; } return $c; } public function save($changes) { Rec::$saved[] = $changes; foreach ($changes as $k => $v) $this->f[$k] = $v; return true; } public function insert($changes) { Rec::$inserted[] = $changes; $this->f = $changes + array('Id' => 99); return true; } public function get_last_error() { return ''; } } class Zone { public function save($d) { return true; } public function get_last_error() { return ''; } } } namespace { define('ZM_OPT_X10', false); define('ZM_OPT_CONTROL', false); function canEdit($p, $id = null) { return true; } function canView($p, $id = null) { return true; } function canCreate($p) { return true; } function validInt($v) { return preg_replace('/[^\-\d]/', '', $v); } function validCardinal($v) { return preg_replace('/\D/', '', $v); } function validDevicePath($v) { return $v; } function validHtmlStr($v) { return $v; } function translate($s) { return $s; } function dbFetchOne($sql, $col = null, $args = array()) { return (strpos($sql, 'MAX(Sequence)') !== false) ? 7 : null; } function dbFetchAll($sql, $col = null, $args = array()) { return (strpos($sql, 'FROM Zones') !== false) ? ZM\Rec::$zones : array(); } function dbQuery($sql, $args = array()) { if (strpos($sql, 'UPDATE Zones') === 0) ZM\Rec::$queries[] = array($sql, $args); return true; } function dbLastError() { return ''; } function coordsToPoints($c) { return array(); } function pointsToCoords($p) { return ''; } function getFormChanges($old, $new, $types = false) { $c = array(); foreach ($new as $k => $v) { if (!array_key_exists($k, $old) or (string)$old[$k] !== (string)$v) $c[$k] = "`$k` = '$v'"; } return $c; } $src = file_get_contents(__DIR__.'/../../web/includes/functions.php'); if (preg_match('/^function monitorFormUncheckedDefaults\(.*?^}\n/ms', $src, $m)) { eval($m[0]); } else { function monitorFormUncheckedDefaults() { return array(); } } $dir = sys_get_temp_dir().'/zm_test_ffmpeg_'.getmypid(); mkdir($dir.'/includes', 0755, true); mkdir(sys_get_temp_dir().'/zm_test_storage', 0755, true); foreach (array('Monitor', 'Zone', 'Storage', 'Manufacturer', 'Model') as $f) { file_put_contents("$dir/includes/$f.php", " '', 'Manufacturer' => '', 'ModelId' => '', 'Model' => '', 'Name' => 'Front', 'Type' => 'Ffmpeg', 'Width' => '640', 'Height' => '480', ); $existing = array('Id' => 5, 'Name' => 'Front', 'Type' => 'Ffmpeg', 'Deleted' => 0, 'Width' => 640, 'Height' => 480, 'Capturing' => 'None', 'Controllable' => 0); // Refused: a bare host/path or userinfo@host/path has no protocol. foreach (array('192.168.1.20/live', 'admin:pw@cam/stream') as $bad) { $err = save(array('newMonitor' => $form + array('Path' => $bad))); check("refuses Ffmpeg path without a protocol: $bad", $err !== '', true); check("does not save Ffmpeg path without a protocol: $bad", saves_anything(), 0); } // Accepted: real URL schemes. foreach (array('rtsp://192.168.1.20/live', 'http://192.168.1.20/live', 'https://192.168.1.20/live') as $good) { $err = save(array('newMonitor' => $form + array('Path' => $good))); check("accepts Ffmpeg path: $good", $err, ''); check("saves Ffmpeg path: $good", saves_anything(), 1); } // Accepted: forms ffmpeg handles without a scheme (absolute paths, v4l2:). foreach (array('/dev/video0', '/path/file.mp4', 'v4l2:/dev/video0') as $good) { $err = save(array('newMonitor' => $form + array('Path' => $good))); check("accepts scheme-less form ffmpeg handles: $good", $err, ''); } // An existing Ffmpeg monitor with no submitted Path is checked against the // stored Path. $err = save(array('mid' => '5', 'newMonitor' => $form), array(5 => $existing + array('Path' => '192.168.1.20/live'))); check('refuses the stored path without a protocol when Path is not submitted', $err !== '', true); check('does not save the stored path without a protocol', saves_anything(), 0); $err = save(array('mid' => '5', 'newMonitor' => $form), array(5 => $existing + array('Path' => 'rtsp://192.168.1.20/live'))); check('accepts the stored path with a protocol', $err, ''); // Other monitor types keep their legacy Path values. $err = save(array('newMonitor' => array('Type' => 'Remote', 'Path' => '192.168.1.20/live') + $form)); check('a non-Ffmpeg type is unaffected', $err, ''); check('a non-Ffmpeg monitor still saves', saves_anything(), 1); system('rm -rf '.escapeshellarg($dir).' '.escapeshellarg(sys_get_temp_dir().'/zm_test_storage')); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }