api.cameras.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. <?php
  2. /**
  3. * Camera devices management
  4. */
  5. class Cameras {
  6. /**
  7. * Contains alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains binpaths config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $binPaths = array();
  18. /**
  19. * Cameras database abstraction layer placeholder
  20. *
  21. * @var object
  22. */
  23. protected $camerasDb = '';
  24. /**
  25. * Custom cameras options database abstraction layer placeholder
  26. *
  27. * @var object
  28. */
  29. protected $camoptsDb = '';
  30. /**
  31. * Contains all available cameras as id=>cameraData
  32. *
  33. * @var array
  34. */
  35. protected $allCameras = array();
  36. /**
  37. * Contains all available cameras custom options as cameraid=>optsData
  38. *
  39. * @var array
  40. */
  41. protected $allCamOpts = array();
  42. /**
  43. * Camera models instnce placeholder
  44. *
  45. * @var object
  46. */
  47. protected $models = '';
  48. /**
  49. * Storages instance placeholder.
  50. *
  51. * @var object
  52. */
  53. protected $storages = '';
  54. /**
  55. * System messages helper instance placeholder
  56. *
  57. * @var object
  58. */
  59. protected $messages = '';
  60. /**
  61. * some predefined stuff here
  62. */
  63. const DATA_TABLE = 'cameras';
  64. const OPTS_TABLE = 'camopts';
  65. const URL_ME = '?module=cameras';
  66. const AJ_ARCHSTATS = 'archivestatscontainer';
  67. const PROUTE_NEWMODEL = 'newcameramodelid';
  68. const PROUTE_NEWIP = 'newcameraip';
  69. const PROUTE_NEWLOGIN = 'newcameralogin';
  70. const PROUTE_NEWPASS = 'newcamerapassword';
  71. const PROUTE_NEWACT = 'newcameraactive';
  72. const PROUTE_NEWSTORAGE = 'newcamerastorageid';
  73. const PROUTE_NEWCOMMENT = 'newcameracomment';
  74. const PROUTE_NEWCUSTPORT = 'newcameracustomport';
  75. const PROUTE_ED_CAMERAID = 'editcameraid';
  76. const PROUTE_ED_MODEL = 'editcameramodelid';
  77. const PROUTE_ED_IP = 'editcameraip';
  78. const PROUTE_ED_LOGIN = 'editcameralogin';
  79. const PROUTE_ED_PASS = 'editcamerapassword';
  80. const PROUTE_ED_CUSTPORT = 'editcamerartspport';
  81. const PROUTE_ED_STORAGE = 'editcamerastorageid';
  82. const PROUTE_ED_COMMENT = 'editcameracomment';
  83. const PROUTE_ED_CAMERAID_ACT = 'renamecameraid';
  84. const PROUTE_ED_COMMENT_ACT = 'renamecameracomment';
  85. const ROUTE_DEL = 'deletecameraid';
  86. const ROUTE_EDIT = 'editcameraid';
  87. const ROUTE_ACTIVATE = 'activatecameraid';
  88. const ROUTE_DEACTIVATE = 'deactivatecameraid';
  89. const ROUTE_AJ_ARCHSTATS = 'renderarchivestats';
  90. const CHANNEL_ID_LEN = 11; // 4.738 * 10^18
  91. /**
  92. * Dinosaurs are my best friends
  93. * Through thick and thin, until the very end
  94. * People tell me, do not pretend
  95. * Stop living in your made up world again
  96. * But the dinosaurs, they're real to me
  97. * They bring me up and make me happy
  98. * I wish that the world could see
  99. * The dinosaurs are a part of me
  100. */
  101. public function __construct() {
  102. $this->initMessages();
  103. $this->loadConfigs();
  104. $this->initCamerasDb();
  105. $this->initCamOptsDb();
  106. $this->loadAllCamOpts();
  107. $this->initStorages();
  108. $this->initModels();
  109. $this->loadAllCameras();
  110. }
  111. /**
  112. * Inits system messages helper
  113. *
  114. * @return void
  115. */
  116. protected function initMessages() {
  117. $this->messages = new UbillingMessageHelper();
  118. }
  119. /**
  120. * Loads all required configs
  121. *
  122. * @global object $ubillingConfig
  123. *
  124. * @return void
  125. */
  126. protected function loadConfigs() {
  127. global $ubillingConfig;
  128. $this->altCfg = $ubillingConfig->getAlter();
  129. $this->binPaths = $ubillingConfig->getBinPaths();
  130. }
  131. /**
  132. * Inits storages into protected prop for further usage
  133. *
  134. * @return void
  135. */
  136. protected function initStorages() {
  137. $this->storages = new Storages();
  138. }
  139. /**
  140. * Inits camera models in protected prop
  141. *
  142. * @return void
  143. */
  144. protected function initModels() {
  145. $this->models = new Models();
  146. }
  147. /**
  148. * Inits cameras database abstraction layer for further usage
  149. *
  150. * @return void
  151. */
  152. protected function initCamerasDb() {
  153. $this->camerasDb = new NyanORM(self::DATA_TABLE);
  154. }
  155. /**
  156. * Inits camopts abstraction layer for further usage
  157. *
  158. * @return void
  159. */
  160. protected function initCamOptsDb() {
  161. $this->camoptsDb = new NyanORM(self::OPTS_TABLE);
  162. }
  163. /**
  164. * Loads all existing cameras from database
  165. *
  166. * @return void
  167. */
  168. protected function loadAllCameras() {
  169. $this->camerasDb->orderBy('id', 'DESC');
  170. $this->allCameras = $this->camerasDb->getAll('id');
  171. }
  172. /**
  173. * Loads all existing cameras custom options from database
  174. *
  175. * @return void
  176. */
  177. protected function loadAllCamOpts() {
  178. $this->allCamOpts = $this->camoptsDb->getAll('cameraid');
  179. }
  180. /**
  181. * Changes comment for existing camera in database
  182. *
  183. * @param int $cameraId
  184. * @param string $comment
  185. *
  186. * @return void/string
  187. */
  188. public function saveComment($cameraId, $comment) {
  189. $result = '';
  190. $cameraId = ubRouting::filters($cameraId, 'int');
  191. $commentF = ubRouting::filters($comment, 'safe');
  192. if (isset($this->allCameras[$cameraId])) {
  193. $this->camerasDb->where('id', '=', $cameraId);
  194. $this->camerasDb->data('comment', $commentF);
  195. $this->camerasDb->save();
  196. log_register('CAMERA EDIT [' . $cameraId . '] COMMENT `' . $comment . '`');
  197. } else {
  198. $result .= __('Camera') . ' [' . $cameraId . '] ' . __('not exists');
  199. }
  200. return ($result);
  201. }
  202. /**
  203. * Saves changes in existing camera database record
  204. *
  205. * @param int $cameraId
  206. * @param int $modelId
  207. * @param string $ip
  208. * @param string $login
  209. * @param string $password
  210. * @param int $storageId
  211. * @param string $comment
  212. * @param int $customPort
  213. *
  214. * @return void/string on error
  215. */
  216. public function save($cameraId, $modelId, $ip, $login, $password, $storageId, $comment = '', $customPort = 0) {
  217. $result = '';
  218. $cameraId = ubRouting::filters($cameraId, 'int');
  219. $modelId = ubRouting::filters($modelId, 'int');
  220. $ipF = ubRouting::filters($ip, 'mres');
  221. $loginF = ubRouting::filters($login, 'mres');
  222. $passwordF = ubRouting::filters($password, 'mres');
  223. $storageId = ubRouting::filters($storageId, 'int');
  224. $commentF = ubRouting::filters($comment, 'safe');
  225. if (isset($this->allCameras[$cameraId])) {
  226. $fullCamerasData = $this->getAllCamerasFullData();
  227. $cameraData = $fullCamerasData[$cameraId]['CAMERA'];
  228. $cameraCurrentIp = $cameraData['ip'];
  229. $cameraCurrentRealPort = $cameraData['realport'];
  230. $credentialsOk = false;
  231. if ($cameraData['active'] == 0) {
  232. $allStorages = $this->storages->getAllStorageNames();
  233. $allModelsData = $this->models->getAllModelData();
  234. $allModelsTemplates = $this->models->getAllModelTemplates();
  235. if (isset($allStorages[$storageId])) {
  236. $storageData = $this->storages->getStorageData($storageId);
  237. $storagePathValid = $this->storages->checkPath($storageData['path']);
  238. if ($storagePathValid) {
  239. if (isset($allModelsData[$modelId])) {
  240. $modelTemplate = $allModelsTemplates[$modelId];
  241. $templatePort = $modelTemplate['RTSP_PORT'];
  242. $futurePort = $templatePort;
  243. if (!empty($customPort)) {
  244. //overrided with some custom?
  245. $futurePort = $customPort;
  246. }
  247. //checking base IP+port pair if its tries to change
  248. if ($ipF != $cameraCurrentIp or $futurePort != $cameraCurrentRealPort) {
  249. $credentialsOk = $this->isCameraIpPortFree($ipF, $futurePort);
  250. } else {
  251. $credentialsOk = true;
  252. }
  253. if (zb_isIPValid($ipF)) {
  254. if ($credentialsOk) {
  255. if (!empty($loginF) and !empty($passwordF)) {
  256. //storage migration?
  257. if ($cameraData['storageid'] != $storageId) {
  258. $this->storages->migrateChannel($storageId, $cameraData['channel']);
  259. }
  260. //updating db
  261. $this->camerasDb->where('id', '=', $cameraId);
  262. $this->camerasDb->data('modelid', $modelId);
  263. $this->camerasDb->data('ip', $ipF);
  264. $this->camerasDb->data('login', $loginF);
  265. $this->camerasDb->data('password', $passwordF);
  266. $this->camerasDb->data('storageid', $storageId);
  267. $this->camerasDb->data('comment', $commentF);
  268. $this->camerasDb->save();
  269. //updating custom port
  270. $this->saveCamoptsRtspPort($cameraId, $customPort);
  271. log_register('CAMERA EDIT [' . $cameraId . '] MODEL [' . $modelId . '] IP `' . $ip . '` STORAGE [' . $storageId . '] COMMENT `' . $comment . '`');
  272. } else {
  273. $result .= __('Login or password is empty');
  274. }
  275. } else {
  276. $result .= __('Camera IP already registered');
  277. }
  278. } else {
  279. $result .= __('Wrong IP format') . ': `' . $ip . '`';
  280. }
  281. } else {
  282. $result .= __('Model') . ' [' . $modelId . '] ' . __('not exists');
  283. }
  284. } else {
  285. $result .= __('Storage path is not writable');
  286. }
  287. } else {
  288. $result .= __('Storage') . ' [' . $storageId . '] ' . __('not exists');
  289. }
  290. } else {
  291. $result .= __('Camera') . ' ' . __('Active') . '!';
  292. }
  293. } else {
  294. $result .= __('Camera') . ' [' . $cameraId . '] ' . __('not exists');
  295. }
  296. return ($result);
  297. }
  298. /**
  299. * Sets camera custom option value by its name
  300. *
  301. * @param int $cameraId
  302. * @param string $key
  303. * @param mixed $value
  304. *
  305. * @return void
  306. */
  307. protected function setCamOptsValue($cameraId, $key, $value) {
  308. $cameraId = ubRouting::filters($cameraId, 'int');
  309. $keyF = ubRouting::filters($key, 'mres');
  310. $valueF = ubRouting::filters($value, 'mres');
  311. if (isset($this->allCameras[$cameraId])) {
  312. $camOpts = $this->getCamOpts($cameraId);
  313. //no opts record exists?
  314. if (empty($camOpts)) {
  315. $this->createCamOpts($cameraId);
  316. }
  317. //setting new value
  318. $this->camoptsDb->data($keyF, $valueF);
  319. $this->camoptsDb->where('cameraid', '=', $cameraId);
  320. $this->camoptsDb->save();
  321. log_register('CAMOPTS CAMERA [' . $cameraId . '] SET `' . strtoupper($key) . '` ON `' . $value . '`');
  322. } else {
  323. log_register('CAMOPTS FAIL CAMERA [' . $cameraId . '] NOT EXISTS');
  324. }
  325. }
  326. /**
  327. * Sets camera custom RTSP port option
  328. *
  329. * @param int $cameraId
  330. * @param int $port
  331. * @return void
  332. */
  333. public function saveCamoptsRtspPort($cameraId, $port) {
  334. $cameraId = ubRouting::filters($cameraId, 'int');
  335. $port = ubRouting::filters($port, 'int');
  336. if (!is_numeric($port)) {
  337. $port = 0;
  338. }
  339. if (isset($this->allCameras[$cameraId])) {
  340. $this->setCamOptsValue($cameraId, 'rtspport', $port);
  341. }
  342. }
  343. /**
  344. * Returns unique channelId
  345. *
  346. * @return string
  347. */
  348. protected function getChannelId() {
  349. $result = '';
  350. $busyCnannelIds = array();
  351. if (!empty($this->allCameras)) {
  352. foreach ($this->allCameras as $io => $each) {
  353. $busyCnannelIds[$each['channel']] = $each['id'];
  354. }
  355. }
  356. $result = zb_rand_string(self::CHANNEL_ID_LEN);
  357. while (isset($busyCnannelIds[$result])) {
  358. $result = zb_rand_string(self::CHANNEL_ID_LEN);
  359. }
  360. return ($result);
  361. }
  362. /**
  363. * Returns all cameras channels as struct channelId=>cameraId
  364. *
  365. * @return array
  366. */
  367. public function getAllCamerasChannels() {
  368. $result = array();
  369. if (!empty($this->allCameras)) {
  370. foreach ($this->allCameras as $io => $each) {
  371. $result[$each['channel']] = $each['id'];
  372. }
  373. }
  374. return ($result);
  375. }
  376. /**
  377. * Returns camera ID by its channel
  378. *
  379. * @param string $channelId
  380. *
  381. * @return int/bool
  382. */
  383. public function getCameraIdByChannel($channelId) {
  384. $result = false;
  385. $allCameraChannels = $this->getAllCamerasChannels();
  386. if (isset($allCameraChannels[$channelId])) {
  387. $result = $allCameraChannels[$channelId];
  388. }
  389. return ($result);
  390. }
  391. /**
  392. * Checks is camera with some IP already registered or not?
  393. *
  394. * @param string $ip
  395. *
  396. * @return bool
  397. */
  398. protected function isCameraIpUsed($ip) {
  399. $result = false;
  400. if (!empty($this->allCameras)) {
  401. foreach ($this->allCameras as $io => $each) {
  402. if ($ip == $each['ip']) {
  403. $result = true;
  404. }
  405. }
  406. }
  407. return ($result);
  408. }
  409. /**
  410. * Checks is camera IP + ralport pair already registered or not?
  411. *
  412. * @param string $ip
  413. *
  414. * @return bool
  415. */
  416. public function isCameraIpPortFree($ip, $port) {
  417. $result = true;
  418. $fullCamerasData = $this->getAllCamerasFullData();
  419. if (!empty($fullCamerasData)) {
  420. foreach ($fullCamerasData as $io => $each) {
  421. if ($each['CAMERA']['ip'] == $ip) {
  422. if ($each['CAMERA']['realport'] == $port) {
  423. $result = false;
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. return ($result);
  430. }
  431. /**
  432. * Creates new camera database record
  433. *
  434. * @param int $modelId
  435. * @param string $ip
  436. * @param string $login
  437. * @param string $password
  438. * @param bool $active
  439. * @param int $storageId
  440. * @param string $comment
  441. * @param int $customPort
  442. *
  443. * @return void|string on error
  444. */
  445. public function create($modelId, $ip, $login, $password, $active, $storageId, $comment = '', $customPort = 0) {
  446. $result = '';
  447. $modelId = ubRouting::filters($modelId, 'int');
  448. $ipF = ubRouting::filters($ip, 'mres');
  449. $loginF = ubRouting::filters($login, 'mres');
  450. $passwordF = ubRouting::filters($password, 'mres');
  451. $actF = ($active) ? 1 : 0;
  452. $storageId = ubRouting::filters($storageId, 'int');
  453. $customPort = ubRouting::filters($customPort, 'int');
  454. //automatic storage selection?
  455. if ($storageId == 0) {
  456. $storageId = $this->storages->getLeastUsedStorage();
  457. }
  458. $commentF = ubRouting::filters($comment, 'safe');
  459. $channelId = $this->getChannelId();
  460. $allStorages = $this->storages->getAllStorageNames();
  461. $allModelsData = $this->models->getAllModelData();
  462. $allModelsTemplates = $this->models->getAllModelTemplates();
  463. if (isset($allStorages[$storageId])) {
  464. $storageData = $this->storages->getStorageData($storageId);
  465. $storagePathValid = $this->storages->checkPath($storageData['path']);
  466. if ($storagePathValid) {
  467. if (isset($allModelsData[$modelId])) {
  468. $modelTemplate = $allModelsTemplates[$modelId];
  469. $templatePort = $modelTemplate['RTSP_PORT'];
  470. $futurePort = $templatePort;
  471. if (!empty($customPort)) {
  472. //overrided with some custom?
  473. $futurePort = $customPort;
  474. }
  475. if (zb_isIPValid($ipF)) {
  476. if ($this->isCameraIpPortFree($ipF, $futurePort)) {
  477. if (!empty($loginF) and !empty($passwordF)) {
  478. $this->camerasDb->data('modelid', $modelId);
  479. $this->camerasDb->data('ip', $ipF);
  480. $this->camerasDb->data('login', $loginF);
  481. $this->camerasDb->data('password', $passwordF);
  482. $this->camerasDb->data('active', $actF);
  483. $this->camerasDb->data('storageid', $storageId);
  484. $this->camerasDb->data('channel', $channelId);
  485. $this->camerasDb->data('comment', $commentF);
  486. $this->camerasDb->create();
  487. $newId = $this->camerasDb->getLastId();
  488. log_register('CAMERA CREATE [' . $newId . '] MODEL [' . $modelId . '] IP `' . $ip . '` STORAGE [' . $storageId . '] COMMENT `' . $comment . '` PORT `' . $futurePort . '`');
  489. //custom options new empty record creation
  490. $this->createCamOpts($newId);
  491. //reload cameras data
  492. $this->loadAllCameras();
  493. //set custom rtsp port if required
  494. if (!empty($customPort)) {
  495. $this->saveCamoptsRtspPort($newId, $customPort);
  496. }
  497. } else {
  498. $result .= __('Login or password is empty');
  499. }
  500. } else {
  501. $result .= __('Camera IP already registered');
  502. }
  503. } else {
  504. $result .= __('Wrong IP format') . ': `' . $ip . '`';
  505. }
  506. } else {
  507. $result .= __('Model') . ' [' . $modelId . '] ' . __('not exists');
  508. }
  509. } else {
  510. $result .= __('Storage path is not writable');
  511. }
  512. } else {
  513. $result .= __('Storage') . ' [' . $storageId . '] ' . __('not exists');
  514. }
  515. return ($result);
  516. }
  517. /**
  518. * Returns running cameras recording processes as cameraId=>realPid
  519. *
  520. * @return array
  521. */
  522. protected function getRunningRecorders() {
  523. $result = array();
  524. $recorderPids = array();
  525. $command = $this->binPaths['PS'] . ' ax | ' . $this->binPaths['GREP'] . ' ' . $this->binPaths['FFMPG_PATH'] . ' | ' . $this->binPaths['GREP'] . ' -v grep';
  526. $rawResult = shell_exec($command);
  527. if (!empty($rawResult)) {
  528. $rawResult = explodeRows($rawResult);
  529. foreach ($rawResult as $io => $eachLine) {
  530. $eachLine = trim($eachLine);
  531. $rawLine = $eachLine;
  532. $eachLine = explode(' ', $eachLine);
  533. if (isset($eachLine[0])) {
  534. $eachPid = $eachLine[0];
  535. if (is_numeric($eachPid)) {
  536. //is this really capture process?
  537. if (ispos($rawLine, $this->binPaths['FFMPG_PATH']) and ispos($rawLine, 'segment_format')) {
  538. $recorderPids[$eachPid] = $rawLine;
  539. }
  540. }
  541. }
  542. }
  543. }
  544. if (!empty($this->allCameras)) {
  545. if (!empty($recorderPids)) {
  546. $fullCamerasData = $this->getAllCamerasFullData();
  547. foreach ($fullCamerasData as $eachCameraId => $eachCameraData) {
  548. foreach ($recorderPids as $eachPid => $eachProcess) {
  549. $camIp = $eachCameraData['CAMERA']['ip'];
  550. $camLogin = $eachCameraData['CAMERA']['login'];
  551. $camPass = $eachCameraData['CAMERA']['password'];
  552. $camPort = $eachCameraData['TEMPLATE']['RTSP_PORT'];
  553. if (isset($eachCameraData['OPTS'])) {
  554. if (!empty($eachCameraData['OPTS']['rtspport'])) {
  555. $camPort = $eachCameraData['OPTS']['rtspport'];
  556. }
  557. }
  558. //looks familiar?
  559. if (ispos($eachProcess, $camIp) and ispos($eachProcess, $camLogin) and ispos($eachProcess, $camPass) and ispos($eachProcess, ':' . $camPort)) {
  560. $result[$eachCameraId] = $eachPid;
  561. }
  562. }
  563. }
  564. }
  565. }
  566. return ($result);
  567. }
  568. /**
  569. * Returns running cameras live stream processes as cameraId=>realPid
  570. *
  571. * @return array
  572. */
  573. protected function getRunningStreams() {
  574. $result = array();
  575. $liveStreamsPids = array();
  576. $command = $this->binPaths['PS'] . ' ax | ' . $this->binPaths['GREP'] . ' ' . $this->binPaths['FFMPG_PATH'] . ' | ' . $this->binPaths['GREP'] . ' -v grep';
  577. $rawResult = shell_exec($command);
  578. if (!empty($rawResult)) {
  579. $rawResult = explodeRows($rawResult);
  580. foreach ($rawResult as $io => $eachLine) {
  581. $eachLine = trim($eachLine);
  582. $rawLine = $eachLine;
  583. $eachLine = explode(' ', $eachLine);
  584. if (isset($eachLine[0])) {
  585. $eachPid = $eachLine[0];
  586. if (is_numeric($eachPid)) {
  587. //is this really live stream process?
  588. if (ispos($rawLine, 'hls') and ispos($rawLine, LiveCams::STREAM_PLAYLIST)) {
  589. $liveStreamsPids[$eachPid] = $rawLine;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. if (!empty($this->allCameras)) {
  596. if (!empty($liveStreamsPids)) {
  597. $fullCamerasData = $this->getAllCamerasFullData();
  598. foreach ($fullCamerasData as $eachCameraId => $eachCameraData) {
  599. foreach ($liveStreamsPids as $eachPid => $eachProcess) {
  600. $camIp = $eachCameraData['CAMERA']['ip'];
  601. $camLogin = $eachCameraData['CAMERA']['login'];
  602. $camPass = $eachCameraData['CAMERA']['password'];
  603. $camPort = $eachCameraData['TEMPLATE']['RTSP_PORT'];
  604. if (isset($eachCameraData['OPTS'])) {
  605. if (!empty($eachCameraData['OPTS']['rtspport'])) {
  606. $camPort = $eachCameraData['OPTS']['rtspport'];
  607. }
  608. }
  609. //looks familiar?
  610. if (ispos($eachProcess, $camIp) and ispos($eachProcess, $camLogin) and ispos($eachProcess, $camPass) and ispos($eachProcess, ':' . $camPort)) {
  611. $result[$eachCameraId] = $eachPid;
  612. }
  613. }
  614. }
  615. }
  616. }
  617. return ($result);
  618. }
  619. /**
  620. * Returns running cameras live sub-stream processes as cameraId=>realPid
  621. *
  622. * @return array
  623. */
  624. protected function getRunningSubStreams() {
  625. $result = array();
  626. $liveStreamsPids = array();
  627. $command = $this->binPaths['PS'] . ' ax | ' . $this->binPaths['GREP'] . ' ' . $this->binPaths['FFMPG_PATH'] . ' | ' . $this->binPaths['GREP'] . ' -v grep';
  628. $rawResult = shell_exec($command);
  629. if (!empty($rawResult)) {
  630. $rawResult = explodeRows($rawResult);
  631. foreach ($rawResult as $io => $eachLine) {
  632. $eachLine = trim($eachLine);
  633. $rawLine = $eachLine;
  634. $eachLine = explode(' ', $eachLine);
  635. if (isset($eachLine[0])) {
  636. $eachPid = $eachLine[0];
  637. if (is_numeric($eachPid)) {
  638. //is this really live stream process?
  639. if (ispos($rawLine, 'hls') and ispos($rawLine, LiveCams::SUBSTREAM_PLAYLIST)) {
  640. $liveStreamsPids[$eachPid] = $rawLine;
  641. }
  642. }
  643. }
  644. }
  645. }
  646. if (!empty($this->allCameras)) {
  647. if (!empty($liveStreamsPids)) {
  648. $fullCamerasData = $this->getAllCamerasFullData();
  649. foreach ($fullCamerasData as $eachCameraId => $eachCameraData) {
  650. foreach ($liveStreamsPids as $eachPid => $eachProcess) {
  651. $camIp = $eachCameraData['CAMERA']['ip'];
  652. $camLogin = $eachCameraData['CAMERA']['login'];
  653. $camPass = $eachCameraData['CAMERA']['password'];
  654. $camPort = $eachCameraData['TEMPLATE']['RTSP_PORT'];
  655. if (isset($eachCameraData['OPTS'])) {
  656. if (!empty($eachCameraData['OPTS']['rtspport'])) {
  657. $camPort = $eachCameraData['OPTS']['rtspport'];
  658. }
  659. }
  660. //looks familiar?
  661. if (ispos($eachProcess, $camIp) and ispos($eachProcess, $camLogin) and ispos($eachProcess, $camPass) and ispos($eachProcess, ':' . $camPort)) {
  662. $result[$eachCameraId] = $eachPid;
  663. }
  664. }
  665. }
  666. }
  667. }
  668. return ($result);
  669. }
  670. /**
  671. * Flushes camera custom options database record
  672. *
  673. * @param int $cameraId
  674. * @return void
  675. */
  676. protected function flushCamOpts($cameraId) {
  677. $cameraId = ubRouting::filters($cameraId, 'int');
  678. $this->camoptsDb->where('cameraid', '=', $cameraId);
  679. $this->camoptsDb->delete();
  680. log_register('CAMOPTS FLUSH CAMERA [' . $cameraId . ']');
  681. }
  682. /**
  683. * Deletes existing camera from database
  684. *
  685. * @param int $cameraId
  686. *
  687. * @return void/string on error
  688. */
  689. public function delete($cameraId) {
  690. $result = '';
  691. $cameraId = ubRouting::filters($cameraId, 'int');
  692. //TODO: do something around camera deactivation and checks for running recording
  693. if (isset($this->allCameras[$cameraId])) {
  694. $cameraData = $this->allCameras[$cameraId];
  695. if ($cameraData['active'] == 0) {
  696. //flushing camera ACLs
  697. $acl = new ACL();
  698. $acl->flushCamera($cameraId);
  699. //deleting camera database record
  700. $this->camerasDb->where('id', '=', $cameraId);
  701. $this->camerasDb->delete();
  702. log_register('CAMERA DELETE [' . $cameraId . ']');
  703. //flushing camera channel
  704. $this->storages->flushChannel($cameraData['storageid'], $cameraData['channel']);
  705. //flushing camera custom options
  706. $this->flushCamOpts($cameraId);
  707. } else {
  708. $result .= __('You cant delete camera which is now active');
  709. }
  710. } else {
  711. $result .= __('Camera') . ' [' . $cameraId . '] ' . __('not exists');
  712. }
  713. return ($result);
  714. }
  715. /**
  716. * Returns full cameras data with all info required for recorder as struct id=>[CAMERA,TEMPLATE,STORAGE]
  717. *
  718. * @return array
  719. */
  720. public function getAllCamerasFullData() {
  721. $result = array();
  722. if (!empty($this->allCameras)) {
  723. $allModelsTemplates = $this->models->getAllModelTemplates();
  724. $allStoragesData = $this->storages->getAllStoragesData();
  725. foreach ($this->allCameras as $io => $each) {
  726. $eachCamData = $each;
  727. $eachCamData['realport'] = 0;
  728. $result[$each['id']]['CAMERA'] = $eachCamData;
  729. $result[$each['id']]['TEMPLATE'] = $allModelsTemplates[$each['modelid']];
  730. $result[$each['id']]['STORAGE'] = $allStoragesData[$each['storageid']];
  731. $result[$each['id']]['OPTS'] = $this->getCamOpts($each['id']);
  732. //setting real rtsp port value depend on template and extopts
  733. if (isset($result[$each['id']]['TEMPLATE']['RTSP_PORT'])) {
  734. $result[$each['id']]['CAMERA']['realport'] = $result[$each['id']]['TEMPLATE']['RTSP_PORT'];
  735. }
  736. //realport overrided by opts
  737. if (isset($result[$each['id']]['OPTS']['rtspport'])) {
  738. if (!empty($result[$each['id']]['OPTS']['rtspport'])) {
  739. $result[$each['id']]['CAMERA']['realport'] = $result[$each['id']]['OPTS']['rtspport'];
  740. }
  741. }
  742. }
  743. }
  744. return ($result);
  745. }
  746. /**
  747. * Retrieves the camera options for a specific camera.
  748. *
  749. * @param int $cameraId The ID of the camera.
  750. * @return array The camera options for the specified camera.
  751. */
  752. public function getCamOpts($cameraId) {
  753. $result = array();
  754. if (isset($this->allCamOpts[$cameraId])) {
  755. $result = $this->allCamOpts[$cameraId];
  756. }
  757. return ($result);
  758. }
  759. /**
  760. * Shutdown camera to unlock its settings
  761. *
  762. * @param int $cameraId
  763. *
  764. * @return void/string
  765. */
  766. public function deactivate($cameraId) {
  767. $result = '';
  768. $cameraId = ubRouting::filters($cameraId, 'int');
  769. if (isset($this->allCameras[$cameraId])) {
  770. $recorder = new Recorder();
  771. //shutdown recording process if it runs
  772. $recorder->stopRecord($cameraId); //this method locks execution until capture process will be really killed
  773. //shutdown camera live streams if it runs
  774. $liveCams = new LiveCams();
  775. $streamStopResult = $liveCams->stopStream($cameraId);
  776. $subStreamStopResult = $liveCams->stopSubStream($cameraId);
  777. if ($streamStopResult) {
  778. log_register('LIVESTREAM STOPPED [' . $cameraId . ']');
  779. }
  780. if ($streamStopResult) {
  781. log_register('SUBSTREAM STOPPED [' . $cameraId . ']');
  782. }
  783. //disabling camera activity flag
  784. $this->camerasDb->where('id', '=', $cameraId);
  785. $this->camerasDb->data('active', 0);
  786. $this->camerasDb->save();
  787. log_register('CAMERA DEACTIVATE [' . $cameraId . ']');
  788. } else {
  789. $result = __('Camera') . ' [' . $cameraId . '] ' . __('not exists');
  790. }
  791. return ($result);
  792. }
  793. /**
  794. * Enables camera to lock its settings
  795. *
  796. * @param int $cameraId
  797. *
  798. * @return void/string
  799. */
  800. public function activate($cameraId) {
  801. $result = '';
  802. $cameraId = ubRouting::filters($cameraId, 'int');
  803. if (isset($this->allCameras[$cameraId])) {
  804. //enabling camera activity flag
  805. $this->camerasDb->where('id', '=', $cameraId);
  806. $this->camerasDb->data('active', 1);
  807. $this->camerasDb->save();
  808. $this->allCameras[$cameraId]['active'] = 1;
  809. log_register('CAMERA ACTIVATE [' . $cameraId . ']');
  810. //starting capture now if enabled
  811. if ($this->altCfg['RECORDER_ON_CAMERA_ACTIVATION']) {
  812. $recorder = new Recorder();
  813. $recorder->runRecordBackground($cameraId);
  814. }
  815. } else {
  816. $result = __('Camera') . ' [' . $cameraId . '] ' . __('not exists');
  817. }
  818. return ($result);
  819. }
  820. /**
  821. * Returns camera comment or IP by its channelId
  822. *
  823. * @param string $channelId
  824. *
  825. * @return string
  826. */
  827. public function getCameraComment($channelId) {
  828. $result = '';
  829. if (!empty($this->allCameras)) {
  830. foreach ($this->allCameras as $io => $each) {
  831. if ($each['channel'] == $channelId) {
  832. if (!empty($each['comment'])) {
  833. $result = $each['comment'];
  834. } else {
  835. $result = $each['ip'];
  836. }
  837. }
  838. }
  839. }
  840. if (empty($result)) {
  841. $result = __('Lost');
  842. }
  843. return ($result);
  844. }
  845. /**
  846. * Returns camera comment or IP by its cameraId
  847. *
  848. * @param string $cameraId
  849. *
  850. * @return string
  851. */
  852. public function getCameraCommentById($cameraId) {
  853. $result = '';
  854. if (!empty($this->allCameras)) {
  855. foreach ($this->allCameras as $io => $each) {
  856. if ($each['id'] == $cameraId) {
  857. if (!empty($each['comment'])) {
  858. $result = $each['comment'];
  859. } else {
  860. $result = $each['ip'];
  861. }
  862. }
  863. }
  864. }
  865. if (empty($result)) {
  866. $result = __('Lost');
  867. }
  868. return ($result);
  869. }
  870. /**
  871. * Checks is camera with some IP registered or not?
  872. *
  873. * @param string $ip
  874. *
  875. * @return int/bool
  876. */
  877. public function isRegisteredIp($ip) {
  878. $result = false;
  879. if (!empty($this->allCameras)) {
  880. foreach ($this->allCameras as $io => $each) {
  881. if ($each['ip'] == $ip) {
  882. $result = $each['id'];
  883. break;
  884. }
  885. }
  886. }
  887. return ($result);
  888. }
  889. /**
  890. * Creates new empty custom camera options record
  891. *
  892. * @param int $cameraId
  893. *
  894. * @return void
  895. */
  896. protected function createCamOpts($cameraId) {
  897. $cameraId = ubRouting::filters($cameraId, 'int');
  898. if (!isset($this->allCamOpts[$cameraId])) {
  899. $this->camoptsDb->data('cameraid', $cameraId);
  900. $this->camoptsDb->create();
  901. log_register('CAMOPTS CREATE [' . $cameraId . ']');
  902. }
  903. }
  904. /**
  905. * Returns camera creation form
  906. *
  907. * @return string
  908. */
  909. public function renderCreateForm() {
  910. $result = '';
  911. $allStorages = $this->storages->getAllStorageNames();
  912. $allModels = $this->models->getAllModelNames();
  913. if (!empty($allStorages)) {
  914. $storagesParams = array();
  915. $storagesParams = array(0 => __('Auto'));
  916. foreach ($allStorages as $eachStorageId => $eachStorageName) {
  917. $storagesParams[$eachStorageId] = __($eachStorageName);
  918. }
  919. if (!empty($allModels)) {
  920. $inputs = wf_Selector(self::PROUTE_NEWMODEL, $allModels, __('Model'), '', true) . ' ';
  921. $inputs .= wf_TextInput(self::PROUTE_NEWIP, __('IP'), '', true, 12, 'ip') . ' ';
  922. $inputs .= wf_TextInput(self::PROUTE_NEWLOGIN, __('Login'), '', true, 14, 'alphanumeric') . ' ';
  923. $inputs .= wf_PasswordInput(self::PROUTE_NEWPASS, __('Password'), '', true, 14) . ' ';
  924. $inputs .= wf_CheckInput(self::PROUTE_NEWACT, __('Enabled'), true, true) . ' ';
  925. $inputs .= wf_TextInput(self::PROUTE_NEWCUSTPORT, __('Custom RTSP port'), '', true, 4, 'digits');
  926. $inputs .= wf_Selector(self::PROUTE_NEWSTORAGE, $storagesParams, __('Storage'), '', true) . ' ';
  927. $inputs .= wf_TextInput(self::PROUTE_NEWCOMMENT, __('Description'), '', true, 18, '') . ' ';
  928. $inputs .= wf_Submit(__('Create'));
  929. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  930. } else {
  931. $result .= $this->messages->getStyledMessage(__('Any device models exists'), 'error');
  932. }
  933. } else {
  934. $result .= $this->messages->getStyledMessage(__('Any storages exists'), 'error');
  935. }
  936. return ($result);
  937. }
  938. /**
  939. * Returns camera editing form
  940. *
  941. * @param int $cameraId
  942. *
  943. * @return string
  944. */
  945. public function renderEditForm($cameraId) {
  946. $result = '';
  947. $cameraId = ubRouting::filters($cameraId, 'int');
  948. $allStorages = $this->storages->getAllStorageNames();
  949. $allModels = $this->models->getAllModelNames();
  950. if (isset($this->allCameras[$cameraId])) {
  951. $cameraData = $this->allCameras[$cameraId];
  952. $camOpts = $this->getCamOpts($cameraId);
  953. if (!empty($allStorages)) {
  954. $storagesParams = array();
  955. foreach ($allStorages as $eachStorageId => $eachStorageName) {
  956. $storagesParams[$eachStorageId] = __($eachStorageName);
  957. }
  958. if (!empty($allModels)) {
  959. $custRtspPort = (!empty($camOpts['rtspport'])) ? $camOpts['rtspport'] : '';
  960. $inputs = wf_HiddenInput(self::PROUTE_ED_CAMERAID, $cameraId);
  961. $inputs .= wf_Selector(self::PROUTE_ED_MODEL, $allModels, __('Model'), $cameraData['modelid'], true) . ' ';
  962. $inputs .= wf_TextInput(self::PROUTE_ED_IP, __('IP'), $cameraData['ip'], true, 12, 'ip') . ' ';
  963. $inputs .= wf_TextInput(self::PROUTE_ED_LOGIN, __('Login'), $cameraData['login'], true, 14, 'alphanumeric') . ' ';
  964. $inputs .= wf_PasswordInput(self::PROUTE_ED_PASS, __('Password'), $cameraData['password'], true, 14) . ' ';
  965. $inputs .= wf_TextInput(self::PROUTE_ED_CUSTPORT, __('Custom RTSP port'), $custRtspPort, true, 4, 'digits');
  966. $inputs .= wf_Selector(self::PROUTE_ED_STORAGE, $storagesParams, __('Storage'), $cameraData['storageid'], true) . ' ';
  967. $inputs .= wf_TextInput(self::PROUTE_ED_COMMENT, __('Description'), $cameraData['comment'], true, 18, '') . ' ';
  968. $inputs .= wf_Submit(__('Save'));
  969. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  970. } else {
  971. $result .= $this->messages->getStyledMessage(__('Any device models exists'), 'error');
  972. }
  973. } else {
  974. $result .= $this->messages->getStyledMessage(__('Any storages exists'), 'error');
  975. }
  976. } else {
  977. $result .= $this->messages->getStyledMessage(__('Camera') . ' [' . $cameraId . '] ' . __('not exists'), 'error');
  978. }
  979. return ($result);
  980. }
  981. /**
  982. * Returns camera comments editing form
  983. *
  984. * @param int $cameraId
  985. *
  986. * @return string
  987. */
  988. public function renderRenameForm($cameraId) {
  989. $result = '';
  990. $cameraId = ubRouting::filters($cameraId, 'int');
  991. if (isset($this->allCameras[$cameraId])) {
  992. $cameraData = $this->allCameras[$cameraId];
  993. $inputs = wf_HiddenInput(self::PROUTE_ED_CAMERAID_ACT, $cameraId);
  994. $inputs .= wf_TextInput(self::PROUTE_ED_COMMENT_ACT, __('Description'), $cameraData['comment'], true, 20, '') . ' ';
  995. $inputs .= wf_Submit(__('Save'));
  996. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  997. $result .= __('You can only change the description of cameras that are active at this moment');
  998. } else {
  999. $result .= $this->messages->getStyledMessage(__('Camera') . ' [' . $cameraId . '] ' . __('not exists'), 'error');
  1000. }
  1001. return ($result);
  1002. }
  1003. /**
  1004. * Renders available cameras list
  1005. *
  1006. * @return string
  1007. */
  1008. public function renderList() {
  1009. $result = '';
  1010. if (!empty($this->allCameras)) {
  1011. $totalCount = 0;
  1012. $enabledCount = 0;
  1013. $recorderCount = 0;
  1014. $liveCount = 0;
  1015. $allRunningRecorders = $this->getRunningRecorders();
  1016. $allLiveStreams = $this->getRunningStreams();
  1017. $cells = wf_TableCell(__('ID'));
  1018. $cells .= wf_TableCell(__('IP'));
  1019. $cells .= wf_TableCell(__('Enabled'));
  1020. $cells .= wf_TableCell(__('Recording'));
  1021. $cells .= wf_TableCell(__('Live'));
  1022. $cells .= wf_TableCell(__('Description'));
  1023. $cells .= wf_TableCell(__('Actions'));
  1024. $rows = wf_TableRow($cells, 'row1');
  1025. foreach ($this->allCameras as $io => $each) {
  1026. $cells = wf_TableCell($each['id']);
  1027. $cells .= wf_TableCell($each['ip'], '', '', 'sorttable_customkey="' . ip2int($each['ip']) . '"');
  1028. $cells .= wf_TableCell(web_bool_led($each['active']), '', '', 'sorttable_customkey="' . $each['active'] . '"');
  1029. $recordingFlag = isset($allRunningRecorders[$each['id']]) ? 1 : 0;
  1030. $cells .= wf_TableCell(web_bool_led($recordingFlag), '', '', 'sorttable_customkey="' . $recordingFlag . '"');
  1031. $liveFlag = isset($allLiveStreams[$each['id']]) ? 1 : 0;
  1032. $cells .= wf_TableCell(web_bool_led($liveFlag), '', '', 'sorttable_customkey="' . $liveFlag . '"');
  1033. $cells .= wf_TableCell($each['comment']);
  1034. $actLinks = wf_Link(self::URL_ME . '&' . self::ROUTE_EDIT . '=' . $each['id'], web_edit_icon(), false);
  1035. $cells .= wf_TableCell($actLinks);
  1036. $rows .= wf_TableRow($cells, 'row5');
  1037. $totalCount++;
  1038. if ($each['active']) {
  1039. $enabledCount++;
  1040. if ($recordingFlag) {
  1041. $recorderCount++;
  1042. }
  1043. if ($liveFlag) {
  1044. $liveCount++;
  1045. }
  1046. }
  1047. }
  1048. $result .= wf_TableBody($rows, '100%', 0, 'sortable resp-table');
  1049. $result .= wf_tag('b') . __('Total') . ': ' . $totalCount . wf_tag('b', true);
  1050. if ($totalCount) {
  1051. $result .= wf_delimiter(0);
  1052. $result .= __('Enabled') . '/' . __('Recording') . '/' . __('Live') . ': (' . $enabledCount . '/' . $recorderCount . '/' . $liveCount . ')';
  1053. }
  1054. } else {
  1055. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  1056. }
  1057. return ($result);
  1058. }
  1059. /**
  1060. * Renders camera archive stats, like depth, bitrate, size...
  1061. *
  1062. * @param array $cameraId
  1063. *
  1064. * @return string
  1065. */
  1066. public function renderCameraArchiveStats($cameraId) {
  1067. $result = '';
  1068. $cameraId = ubRouting::filters($cameraId, 'int');
  1069. if (isset($this->allCameras[$cameraId])) {
  1070. $cameraData = $this->allCameras[$cameraId];
  1071. $rows = '';
  1072. //some channel data collecting
  1073. $channelChunks = $this->storages->getChannelChunks($cameraData['storageid'], $cameraData['channel']);
  1074. $chunksCount = sizeof($channelChunks);
  1075. $archiveDepth = '-';
  1076. $archiveSeconds = 0;
  1077. if ($chunksCount > 0) {
  1078. $archiveSeconds = $this->altCfg['RECORDER_CHUNK_TIME'] * $chunksCount;
  1079. $archiveDepth = wr_formatTimeArchive($archiveSeconds);
  1080. }
  1081. $chanSizeRaw = $this->storages->getChannelChunksSize($channelChunks);
  1082. $chanSizeLabel = wr_convertSize($chanSizeRaw);
  1083. $chanBitrateLabel = '-';
  1084. if ($archiveSeconds and $chanSizeRaw) {
  1085. $chanBitrate = ($chanSizeRaw * 8) / $archiveSeconds / 1024; // in kbits
  1086. $chanBitrateLabel = round(($chanBitrate / 1024), 2) . ' ' . __('Mbit/s');
  1087. }
  1088. $cells = wf_TableCell(__('Archive depth'), '40%', 'row2');
  1089. $cells .= wf_TableCell($archiveDepth);
  1090. $rows .= wf_TableRow($cells, 'row3');
  1091. $cells = wf_TableCell(__('Average bitrate'), '', 'row2');
  1092. $cells .= wf_TableCell($chanBitrateLabel);
  1093. $rows .= wf_TableRow($cells, 'row3');
  1094. $cells = wf_TableCell(__('Size'), '', 'row2');
  1095. $cells .= wf_TableCell($chanSizeLabel);
  1096. $rows .= wf_TableRow($cells, 'row3');
  1097. $result .= wf_TableBody($rows, '100%', 0, 'resp-table');
  1098. }
  1099. return ($result);
  1100. }
  1101. /**
  1102. * Renders camera profile
  1103. *
  1104. * @param int $cameraId
  1105. *
  1106. * @return string
  1107. */
  1108. public function renderCameraProfile($cameraId) {
  1109. $result = '';
  1110. $cameraControls = '';
  1111. $cameraId = ubRouting::filters($cameraId, 'int');
  1112. if (isset($this->allCameras[$cameraId])) {
  1113. $cameraData = $this->allCameras[$cameraId];
  1114. $allModels = $this->models->getAllModelNames();
  1115. $allStorages = $this->storages->getAllStorageNames();
  1116. $allTemplates = $this->models->getAllModelTemplates();
  1117. $camOpt = $this->getCamOpts($cameraId);
  1118. $cameraTemplate = $allTemplates[$cameraData['modelid']];
  1119. $acl = new ACL();
  1120. $portLabel = '';
  1121. //model template rtsp port
  1122. $rtspPort = (!empty($cameraTemplate['RTSP_PORT'])) ? $cameraTemplate['RTSP_PORT'] : 554;
  1123. //is custom rtsp port used?
  1124. if (!empty($camOpt)) {
  1125. if ($camOpt['rtspport']) {
  1126. $rtspPort = $camOpt['rtspport'];
  1127. $portLabel = ' ⚙️';
  1128. }
  1129. }
  1130. //recorder process now is running?
  1131. $allRunningRecorders = $this->getRunningRecorders();
  1132. $recordingFlag = (isset($allRunningRecorders[$cameraId])) ? 1 : 0;
  1133. //live stream process now is running?
  1134. $allRunningLiveStreams = $this->getRunningStreams();
  1135. $liveStreamFlag = (isset($allRunningLiveStreams[$cameraId])) ? 1 : 0;
  1136. //live substreams live-well process is running?
  1137. $allRunningSubStreams = $this->getRunningSubStreams();
  1138. $subStreamFlag = (isset($allRunningSubStreams[$cameraId])) ? 1 : 0;
  1139. $ajaxArchiveStatsUrl = self::URL_ME . '&' . self::ROUTE_AJ_ARCHSTATS . '=' . $cameraId;
  1140. $channelLabel = $cameraData['channel'];
  1141. if (cfr('ARCHIVE')) {
  1142. $channelLabel = wf_AjaxLink($ajaxArchiveStatsUrl, $cameraData['channel'], self::AJ_ARCHSTATS);
  1143. }
  1144. //ACL users access
  1145. $aclUsersList = '';
  1146. $rawAcls = $acl->getAllCameraAclsData();
  1147. if (!empty($rawAcls)) {
  1148. foreach ($rawAcls as $eachUser => $accessibleCameras) {
  1149. if (isset($accessibleCameras[$cameraId])) {
  1150. $aclUsersList .= $eachUser . ' ';
  1151. }
  1152. }
  1153. }
  1154. $aclUsersList = (empty($aclUsersList)) ? '-' : $aclUsersList;
  1155. //camera profile here
  1156. $cells = wf_TableCell(__('Model'), '40%', 'row2');
  1157. $cells .= wf_TableCell($allModels[$cameraData['modelid']]);
  1158. $rows = wf_TableRow($cells, 'row3');
  1159. $cells = wf_TableCell(__('IP'), '', 'row2');
  1160. $cells .= wf_TableCell($cameraData['ip']);
  1161. $rows .= wf_TableRow($cells, 'row3');
  1162. $cells = wf_TableCell(__('Login'), '', 'row2');
  1163. $cells .= wf_TableCell($cameraData['login']);
  1164. $rows .= wf_TableRow($cells, 'row3');
  1165. $cells = wf_TableCell(__('Password'), '', 'row2');
  1166. $cells .= wf_TableCell(wf_ShowHide($cameraData['password'],__('Show')));
  1167. $rows .= wf_TableRow($cells, 'row3');
  1168. $cells = wf_TableCell(__('RTSP') . ' ' . __('Port'), '', 'row2');
  1169. $cells .= wf_TableCell($rtspPort . $portLabel);
  1170. $rows .= wf_TableRow($cells, 'row3');
  1171. $cells = wf_TableCell(__('Enabled'), '', 'row2');
  1172. $cells .= wf_TableCell(web_bool_led($cameraData['active']));
  1173. $rows .= wf_TableRow($cells, 'row3');
  1174. $cells = wf_TableCell(__('Recording'), '', 'row2');
  1175. $cells .= wf_TableCell(web_bool_led($recordingFlag));
  1176. $rows .= wf_TableRow($cells, 'row3');
  1177. $cells = wf_TableCell(__('Live'), '', 'row2');
  1178. $cells .= wf_TableCell(web_bool_led($liveStreamFlag));
  1179. $rows .= wf_TableRow($cells, 'row3');
  1180. $cells = wf_TableCell(__('Substream'), '', 'row2');
  1181. $cells .= wf_TableCell(web_bool_led($subStreamFlag));
  1182. $rows .= wf_TableRow($cells, 'row3');
  1183. $cells = wf_TableCell(__('Description'), '', 'row2');
  1184. $cells .= wf_TableCell($cameraData['comment']);
  1185. $rows .= wf_TableRow($cells, 'row3');
  1186. $cells = wf_TableCell(__('Storage'), '', 'row2');
  1187. $cells .= wf_TableCell(__($allStorages[$cameraData['storageid']]));
  1188. $rows .= wf_TableRow($cells, 'row3');
  1189. $cells = wf_TableCell(__('Channel'), '', 'row2');
  1190. $cells .= wf_TableCell($channelLabel);
  1191. $rows .= wf_TableRow($cells, 'row3');
  1192. $cells = wf_TableCell(__('Users access'), '', 'row2');
  1193. $cells .= wf_TableCell($aclUsersList);
  1194. $rows .= wf_TableRow($cells, 'row3');
  1195. $result .= wf_TableBody($rows, '100%', 0, 'resp-table');
  1196. //archive stats container here
  1197. $result .= wf_AjaxLoader();
  1198. $result .= wf_AjaxContainer(self::AJ_ARCHSTATS);
  1199. //some controls here
  1200. if ($cameraData['active']) {
  1201. $deactUrl = self::URL_ME . '&' . self::ROUTE_EDIT . '=' . $cameraData['id'] . '&' . self::ROUTE_DEACTIVATE . '=' . $cameraData['id'];
  1202. $cameraControls .= wf_Link($deactUrl, web_bool_led(0) . ' ' . __('Disable'), false, 'ubButton') . ' ';
  1203. } else {
  1204. $cameraControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_ACTIVATE . '=' . $cameraData['id'], web_bool_led(1) . ' ' . __('Enable'), false, 'ubButton') . ' ';
  1205. }
  1206. if ($cameraData['active']) {
  1207. if (cfr('LIVECAMS')) {
  1208. $cameraControls .= wf_Link(LiveCams::URL_ME . '&' . LiveCams::ROUTE_VIEW . '=' . $cameraData['channel'], wf_img('skins/icon_live_small.png') . ' ' . __('Live'), false, 'ubButton');
  1209. }
  1210. }
  1211. if (cfr('ARCHIVE')) {
  1212. $cameraControls .= wf_Link(Archive::URL_ME . '&' . Archive::ROUTE_VIEW . '=' . $cameraData['channel'], wf_img('skins/icon_archive_small.png') . ' ' . __('Video from camera'), false, 'ubButton');
  1213. }
  1214. if (cfr('EXPORT')) {
  1215. $cameraControls .= wf_Link(Export::URL_ME . '&' . Export::ROUTE_CHANNEL . '=' . $cameraData['channel'], wf_img('skins/icon_export.png') . ' ' . __('Save record'), false, 'ubButton');
  1216. }
  1217. if (cfr('ARCHIVE')) {
  1218. $cameraControls .= wf_AjaxLink($ajaxArchiveStatsUrl, wf_img('skins/icon_charts.png') . ' ' . __('Archive'), self::AJ_ARCHSTATS, false, 'ubButton');
  1219. }
  1220. if (!$cameraData['active']) {
  1221. //editing interface here
  1222. $editingForm = $this->renderEditForm($cameraId);
  1223. $cameraControls .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit'), __('Edit camera') . ': ' . $cameraData['comment'], $editingForm, 'ubButton');
  1224. //deletion interface here
  1225. $deletionUrl = self::URL_ME . '&' . self::ROUTE_DEL . '=' . $cameraId;
  1226. $cancelUrl = self::URL_ME . '&' . self::ROUTE_EDIT . '=' . $cameraId;
  1227. $deletionAlert = $this->messages->getDeleteAlert() . '. ' . wf_tag('br');
  1228. $deletionAlert .= __('Also all archive data for this camera will be destroyed permanently') . '.';
  1229. $deletionTitle = __('Delete') . ' ' . __('Camera') . ' ' . $cameraData['ip'] . '?';
  1230. $cameraControls .= wf_ConfirmDialog($deletionUrl, web_delete_icon() . ' ' . __('Delete'), $deletionAlert, 'ubButton', $cancelUrl, $deletionTitle) . ' ';
  1231. } else {
  1232. //only comments editing accessible for active cameras
  1233. $renameForm = $this->renderRenameForm($cameraId);
  1234. $cameraControls .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit'), __('Edit camera') . ': ' . $cameraData['comment'], $renameForm, 'ubButton');
  1235. }
  1236. } else {
  1237. $result .= $this->messages->getStyledMessage(__('Camera') . ' [' . $cameraId . '] ' . __('not exists'), 'error');
  1238. }
  1239. $result .= wf_delimiter(0);
  1240. $result .= wf_BackLink(self::URL_ME) . ' ';
  1241. $result .= $cameraControls;
  1242. return ($result);
  1243. }
  1244. }