api.ubillingvisor.php 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394
  1. <?php
  2. /**
  3. * Surveillance accounting and management implementation
  4. */
  5. class UbillingVisor {
  6. /**
  7. * Contains system alter.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains all stargazer user data as login=>data
  14. *
  15. * @var array
  16. */
  17. protected $allUserData = array();
  18. /**
  19. * Contains all available tariffs fees as tariff=>fee
  20. *
  21. * @var array
  22. */
  23. protected $allTariffPrices = array();
  24. /**
  25. * Contains all visor users data as id=>data
  26. *
  27. * @var array
  28. */
  29. protected $allUsers = array();
  30. /**
  31. * Contains all visor cameras data as id=>data
  32. *
  33. * @var array
  34. */
  35. protected $allCams = array();
  36. /**
  37. * Contains all visor dvrs data as id=>data
  38. *
  39. * @var array
  40. */
  41. protected $allDvrs = array();
  42. /**
  43. * Contains all available users payment IDs
  44. *
  45. * @var array
  46. */
  47. protected $allPaymentIDs = array();
  48. /**
  49. * Contains available DVR handler types
  50. *
  51. * @var array
  52. */
  53. protected $dvrTypes = array();
  54. /**
  55. * Visor charge mode from VISOR_CHARGE_MODE config option.
  56. *
  57. * @var int
  58. */
  59. protected $chargeMode = 1;
  60. /**
  61. * Trassir Server integration flag
  62. *
  63. * @var bool
  64. */
  65. protected $trassirEnabled = false;
  66. /**
  67. * WolfRecorder integration flag
  68. *
  69. * @var bool
  70. */
  71. protected $wolfRecorderEnabled = false;
  72. /**
  73. * System messages helper object placeholder
  74. *
  75. * @var object
  76. */
  77. protected $messages = '';
  78. /**
  79. * Contains preloaded channels to visor user bindings as visorId=>data
  80. *
  81. * @var array
  82. */
  83. protected $allChannels = array();
  84. /**
  85. * Contains channel to users bindings as channelGuid=>visorId
  86. *
  87. * @var array
  88. */
  89. protected $channelUsers = array();
  90. /**
  91. * Contains available secrets bindings with auth data as visorId=>secretsData
  92. *
  93. * @var array
  94. */
  95. protected $allSecrets = array();
  96. /**
  97. * Channels binginds database model
  98. *
  99. * @var object
  100. */
  101. protected $chans = '';
  102. /**
  103. * NVR secrets data model placeholder
  104. *
  105. * @var object
  106. */
  107. protected $secrets = '';
  108. /**
  109. * Available channel record modes
  110. *
  111. * @var array
  112. */
  113. protected $recordModes = array();
  114. /**
  115. * Default channel preview size
  116. *
  117. * @var string
  118. */
  119. protected $chanPreviewSize = '30%';
  120. /**
  121. * Quality percent for channels small preview
  122. *
  123. * @var int
  124. */
  125. protected $chanPreviewQuality = 1;
  126. /**
  127. * Channels preview
  128. *
  129. * @var int
  130. */
  131. protected $chanPreviewFramerate = 1000; // 1 fps
  132. /**
  133. * Quality percent of large channel preview
  134. *
  135. * @var int
  136. */
  137. protected $chanBigPreviewQuality = 95;
  138. /**
  139. * Large preview framerate
  140. *
  141. * @var int
  142. */
  143. protected $chanBigPreviewFramerate = 1000;
  144. /**
  145. * Global Trassir NVR stream preview container type. Now supported: mjpeg or hls.
  146. *
  147. * @var string
  148. */
  149. protected $chanPreviewContainer = 'mjpeg';
  150. /**
  151. * TrassirServer debug flag
  152. *
  153. * @var bool
  154. */
  155. protected $trassirDebug = false;
  156. /**
  157. * Contains array of users with protected from unprivileged staff
  158. *
  159. * @var array
  160. */
  161. protected $protectedUserIds = array();
  162. /**
  163. * Users database abstraction layer
  164. *
  165. * @var object
  166. */
  167. protected $usersDb = '';
  168. /**
  169. * Cameras database abstraction layer
  170. *
  171. * @var object
  172. */
  173. protected $camsDb = '';
  174. /**
  175. * DVRs database abstraction layer
  176. *
  177. * @var object
  178. */
  179. protected $dvrsDb = '';
  180. /**
  181. * Channels database abstraction layer
  182. *
  183. * @var object
  184. */
  185. protected $chansDb = '';
  186. /**
  187. * Secrets database abstraction layer
  188. *
  189. * @var object
  190. */
  191. protected $secretsDb = '';
  192. /**
  193. * Use or not cached users data?
  194. *
  195. * @var bool
  196. */
  197. protected $cachedUsersFlag = false;
  198. /**
  199. * Basic module URLs
  200. */
  201. const URL_ME = '?module=visor';
  202. const URL_USERS = '&users=true';
  203. const URL_CAMS = '&cams=true';
  204. const URL_USERCAMS = '&ajaxusercams=';
  205. const URL_ALLCAMS = '&ajaxallcams=true';
  206. const URL_DVRS = '&dvrs=true';
  207. const URL_CHANS = '&channels=true';
  208. const URL_HEALTH = '&health=true';
  209. const URL_CHANEDIT = '&editchannel=';
  210. const URL_AJUSERS = '&ajaxusers=true';
  211. const URL_DELUSER = '&deleteuserid=';
  212. const URL_DELDVR = '&deletedvrid=';
  213. const URL_USERVIEW = '&showuser=';
  214. const URL_CAMPROFILE = '?module=userprofile&username=';
  215. const URL_CAMVIEW = '&showcamera=';
  216. const URL_TARCHANGE = '&tariffchanges=true';
  217. /**
  218. * Some default database tables names
  219. */
  220. const TABLE_USERS = 'visor_users';
  221. const TABLE_CAMS = 'visor_cams';
  222. const TABLE_DVRS = 'visor_dvrs';
  223. const TABLE_CHANS = 'visor_chans';
  224. const TABLE_SECRETS = 'visor_secrets';
  225. /**
  226. * Other stuff
  227. */
  228. const PATH_MODELS = 'content/documents/visormodels/';
  229. public function __construct() {
  230. $this->loadConfigs();
  231. $this->loadDvrTypes();
  232. $this->initMessages();
  233. $this->initDbLayers();
  234. $this->loadUserData();
  235. $this->loadUsers();
  236. $this->loadTariffPricing();
  237. $this->loadPaymentIds();
  238. $this->loadCams();
  239. $this->loadDvrs();
  240. $this->loadRecordModes();
  241. $this->loadChans();
  242. $this->loadSecrets();
  243. }
  244. /**
  245. * Inits all required database abstraction layers
  246. *
  247. * @return void
  248. */
  249. protected function initDbLayers() {
  250. $this->usersDb = new NyanORM(self::TABLE_USERS);
  251. $this->camsDb = new NyanORM(self::TABLE_CAMS);
  252. $this->dvrsDb = new NyanORM(self::TABLE_DVRS);
  253. $this->chansDb = new NyanORM(self::TABLE_CHANS);
  254. $this->secretsDb = new NyanORM(self::TABLE_SECRETS);
  255. }
  256. /**
  257. * Loads reqired configs
  258. *
  259. * @global object $ubillingConfig
  260. *
  261. * @return void
  262. */
  263. protected function loadConfigs() {
  264. global $ubillingConfig;
  265. $this->altCfg = $ubillingConfig->getAlter();
  266. if (@$this->altCfg['VISOR_CHARGE_MODE']) {
  267. $this->chargeMode = $this->altCfg['VISOR_CHARGE_MODE'];
  268. }
  269. if ($this->altCfg['WOLFRECORDER_ENABLED']) {
  270. $this->wolfRecorderEnabled = true;
  271. }
  272. if (@$this->altCfg['TRASSIRMGR_ENABLED']) {
  273. $this->trassirEnabled = true;
  274. }
  275. if (@$this->altCfg['TRASSIRHLS_ENABLED']) {
  276. $this->chanPreviewContainer = 'hls';
  277. }
  278. if (@$this->altCfg['TRASSIR_DEBUG']) {
  279. $this->trassirDebug = $this->altCfg['TRASSIR_DEBUG'];
  280. }
  281. if (@$this->altCfg['VISOR_PROTUSERIDS']) {
  282. $rawProtUsers = explode(',', $this->altCfg['VISOR_PROTUSERIDS']);
  283. $this->protectedUserIds = array_flip($rawProtUsers);
  284. }
  285. if (@$this->altCfg['VISOR_CACHED_USERDATA']) {
  286. $this->cachedUsersFlag = true;
  287. }
  288. }
  289. /**
  290. * Sets available DVR types
  291. *
  292. * @return void
  293. */
  294. protected function loadDvrTypes() {
  295. $this->dvrTypes = array(
  296. 'generic' => __('No')
  297. );
  298. if ($this->wolfRecorderEnabled) {
  299. $this->dvrTypes += array('wolfrecorder' => __('WolfRecorder'));
  300. }
  301. if ($this->trassirEnabled) {
  302. $this->dvrTypes += array('trassir' => __('Trassir Server'));
  303. }
  304. }
  305. /**
  306. * Sets default available channel record modes
  307. *
  308. * @return void
  309. */
  310. protected function loadRecordModes() {
  311. $this->recordModes = array(
  312. 1 => __('Permanent record'),
  313. 2 => __('Manual record'),
  314. 3 => __('On detector')
  315. );
  316. }
  317. /**
  318. * Inits system message helper for further usage
  319. *
  320. * @return void
  321. */
  322. protected function initMessages() {
  323. $this->messages = new UbillingMessageHelper();
  324. }
  325. /**
  326. * Loads all existing users data from database
  327. *
  328. * @return void
  329. */
  330. protected function loadUserData() {
  331. if ($this->cachedUsersFlag) {
  332. $this->allUserData = zb_UserGetAllDataCache();
  333. } else {
  334. $this->allUserData = zb_UserGetAllData();
  335. }
  336. }
  337. /**
  338. * Loads tariffs pricing data from database into protected prop
  339. *
  340. * @return void
  341. */
  342. protected function loadTariffPricing() {
  343. $this->allTariffPrices = zb_TariffGetPricesAll();
  344. }
  345. /**
  346. * Loads available channels bindings from database
  347. *
  348. * @return void
  349. */
  350. protected function loadChans() {
  351. $chansTmp = $this->chansDb->getAll();
  352. if (!empty($chansTmp)) {
  353. foreach ($chansTmp as $io => $each) {
  354. $this->allChannels[$each['visorid']][] = $each;
  355. $this->channelUsers[$each['chan']] = $each['visorid'];
  356. }
  357. }
  358. }
  359. /**
  360. * Loads available secrets bindings from database
  361. *
  362. * @return void
  363. */
  364. protected function loadSecrets() {
  365. $this->allSecrets = $this->secretsDb->getAll('visorid');
  366. }
  367. /**
  368. * Loads available payment IDs from database
  369. *
  370. * @return void
  371. */
  372. protected function loadPaymentIds() {
  373. if ($this->altCfg['OPENPAYZ_SUPPORT']) {
  374. if ($this->altCfg['OPENPAYZ_REALID']) {
  375. $openPayz = new OpenPayz(false, true);
  376. $this->allPaymentIDs = $openPayz->getCustomersPaymentIds();
  377. } else {
  378. if (!empty($this->allUserData)) {
  379. foreach ($this->allUserData as $io => $each) {
  380. $this->allPaymentIDs[$each['login']] = ip2int($each['ip']);
  381. }
  382. }
  383. }
  384. }
  385. }
  386. /**
  387. * Loads all visor users data into protected property
  388. *
  389. * @return void
  390. */
  391. protected function loadUsers() {
  392. $this->usersDb->orderBy('id', 'DESC');
  393. $this->allUsers = $this->usersDb->getAll('id');
  394. }
  395. /**
  396. * Loads all visor cameras data into protected property
  397. *
  398. * @return void
  399. */
  400. protected function loadCams() {
  401. $this->camsDb->orderBy('id', 'DESC');
  402. $this->allCams = $this->camsDb->getAll('id');
  403. }
  404. /**
  405. * Loads all visor DVR data into protected property
  406. *
  407. * @return void
  408. */
  409. protected function loadDvrs() {
  410. $this->dvrsDb->orderBy('id', 'DESC');
  411. $this->allDvrs = $this->dvrsDb->getAll('id');
  412. }
  413. /**
  414. * Renders default controls panel
  415. *
  416. * @return string
  417. */
  418. public function panel() {
  419. $result = '';
  420. $result .= wf_Link(self::URL_ME . self::URL_USERS, wf_img('skins/ukv/users.png') . ' ' . __('Users'), false, 'ubButton') . ' ';
  421. if (cfr('VISOREDIT')) {
  422. $result .= wf_modalAuto(wf_img('skins/ukv/add.png') . ' ' . __('Users registration'), __('Users registration'), $this->renderUserCreateForm(), 'ubButton') . ' ';
  423. }
  424. $result .= wf_Link(self::URL_ME . self::URL_CAMS, wf_img('skins/photostorage.png') . ' ' . __('Cams'), false, 'ubButton') . ' ';
  425. if (cfr('VISOREDIT')) {
  426. $result .= wf_Link(self::URL_ME . self::URL_DVRS, wf_img('skins/icon_restoredb.png') . ' ' . __('DVRs'), false, 'ubButton') . ' ';
  427. if ($this->trassirEnabled or $this->wolfRecorderEnabled) {
  428. $result .= wf_Link(self::URL_ME . self::URL_CHANS, wf_img('skins/play.png') . ' ' . __('Channels'), false, 'ubButton') . ' ';
  429. $result .= wf_Link(self::URL_ME . self::URL_HEALTH, wf_img('skins/log_icon_small.png') . ' ' . __('DVR health'), false, 'ubButton') . ' ';
  430. }
  431. }
  432. if (@$this->altCfg['DDT_ENABLED']) {
  433. $result .= wf_Link(self::URL_ME . self::URL_TARCHANGE, wf_img_sized('skins/icon_tariff.gif', '', '16') . ' ' . __('Tariff will change'), false, 'ubButton') . ' ';
  434. }
  435. return ($result);
  436. }
  437. /**
  438. * Renders available users list container
  439. *
  440. * @return string
  441. */
  442. public function renderUsers() {
  443. $result = '';
  444. $opts = '"order": [[ 0, "desc" ]]';
  445. $columns = array('ID', 'Date', 'Name', 'Phone', 'Primary account', 'Balance', 'Charge', 'Tariffing', 'Cams', 'Actions');
  446. $result .= wf_JqDtLoader($columns, self::URL_ME . self::URL_AJUSERS, false, 'Users', 50, $opts);
  447. return ($result);
  448. }
  449. /**
  450. * Renders users datatables data
  451. *
  452. * @return void
  453. */
  454. public function ajaxUsersList() {
  455. $json = new wf_JqDtHelper();
  456. if (!empty($this->allUsers)) {
  457. foreach ($this->allUsers as $io => $each) {
  458. $tariffingLabel = '';
  459. $primaryAccountCash = 0;
  460. $data[] = $each['id'];
  461. $data[] = $each['regdate'];
  462. $visorUserLabel = $this->iconVisorUser() . ' ' . $each['realname'];
  463. $visorUserLink = wf_Link(self::URL_ME . self::URL_USERVIEW . $each['id'], $visorUserLabel);
  464. $data[] = $visorUserLink;
  465. $data[] = $each['phone'];
  466. if (!empty($each['primarylogin'])) {
  467. $primaryAccount = $each['primarylogin'];
  468. $userAddress = @$this->allUserData[$primaryAccount]['fulladress'];
  469. $primAccLink = wf_Link(self::URL_CAMPROFILE . $each['primarylogin'], web_profile_icon() . ' ' . $userAddress);
  470. if (isset($this->allUserData[$primaryAccount])) {
  471. $primaryAccountCash = $this->allUserData[$primaryAccount]['Cash'];
  472. if ($each['chargecams']) {
  473. $tariffingLabel = wf_img_sized('skins/icon_ok.gif', __('Funds for cameras will be charged from the main account at the end of the month'), 16);
  474. } else {
  475. $tariffingLabel = $tariffingNotice = wf_img_sized('skins/icon_lock.png', __('All cameras live by themselves'), 16);
  476. }
  477. if ($this->allUserData[$primaryAccount]['Passive'] and $each['chargecams']) {
  478. $tariffingLabel = wf_img_sized('skins/icon_passive.gif', __('Main account is frozen') . '. ' . __('All cameras live by themselves'), 16);
  479. }
  480. } else {
  481. $primAccLink = __('User') . ' ' . __('Not exists');
  482. }
  483. } else {
  484. $primAccLink = '';
  485. $primaryAccountCash = '';
  486. $tariffingLabel = wf_img_sized('skins/delete_small.png', __('All cameras live by themselves') . ', ' . __('no primary account set'), 16);
  487. }
  488. $data[] = $primAccLink;
  489. $data[] = $primaryAccountCash;
  490. $chargeFlag = ($each['chargecams']) ? web_bool_led(true) . ' ' . __('Yes') : web_bool_led(false) . ' ' . __('No');
  491. $data[] = $chargeFlag;
  492. $data[] = $tariffingLabel;
  493. $data[] = $this->getUserCamerasCount($each['id']);
  494. $actLinks = '';
  495. $actLinks .= wf_Link(self::URL_ME . self::URL_USERVIEW . $each['id'], web_edit_icon());
  496. $data[] = $actLinks;
  497. $json->addRow($data);
  498. unset($data);
  499. }
  500. }
  501. $json->getJson();
  502. }
  503. /**
  504. * Renders visor user creation form
  505. *
  506. * @return string
  507. */
  508. public function renderUserCreateForm() {
  509. $result = '';
  510. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  511. $inputs = wf_HiddenInput('newusercreate', 'true');
  512. $inputs .= wf_TextInput('newusername', __('Name') . $sup, '', true, 25);
  513. $inputs .= wf_TextInput('newuserphone', __('Phone'), '', true, 20, 'mobile');
  514. $inputs .= wf_CheckInput('newuserchargecams', __('Charge money from primary account for linked camera users if required'), true, false);
  515. $inputs .= wf_delimiter();
  516. $inputs .= wf_Submit(__('Create'));
  517. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  518. return ($result);
  519. }
  520. /**
  521. * Creates new user in database
  522. *
  523. * @return int
  524. */
  525. public function createUser() {
  526. $result = '';
  527. if (ubRouting::checkPost(array('newusercreate', 'newusername'))) {
  528. $newRealName = ubRouting::post('newusername');
  529. $newRealNameF = ubRouting::filters($newRealName, 'mres');
  530. $newPhone = ubRouting::post('newuserphone', 'mres');
  531. $newChargeCams = (ubRouting::checkPost('newuserchargecams')) ? 1 : 0;
  532. $date = curdatetime();
  533. $this->usersDb->data('regdate', $date);
  534. $this->usersDb->data('realname', $newRealNameF);
  535. $this->usersDb->data('phone', $newPhone);
  536. $this->usersDb->data('chargecams', $newChargeCams);
  537. $this->usersDb->create();
  538. $newId = $this->usersDb->getLastId();
  539. log_register('VISOR USER CREATE [' . $newId . '] NAME `' . $newRealName . '`');
  540. $result = $newId;
  541. }
  542. return ($result);
  543. }
  544. /**
  545. * Returns array of cameras associated to some user
  546. *
  547. * @param int $userId
  548. *
  549. * @return array
  550. */
  551. protected function getUserCameras($userId) {
  552. $result = array();
  553. if (!empty($this->allCams)) {
  554. foreach ($this->allCams as $io => $each) {
  555. if ($each['visorid'] == $userId) {
  556. $result[$each['id']] = $each;
  557. }
  558. }
  559. }
  560. return ($result);
  561. }
  562. /**
  563. * Returns camera ID if login have camera associated
  564. *
  565. * @param string $login
  566. *
  567. * @return int/void
  568. */
  569. protected function getCameraIdByLogin($login) {
  570. $result = '';
  571. if (!empty($this->allCams)) {
  572. foreach ($this->allCams as $io => $each) {
  573. if ($each['login'] == $login) {
  574. $result = $each['id'];
  575. break;
  576. }
  577. }
  578. }
  579. return ($result);
  580. }
  581. /**
  582. * Checks is some account already someones primary or not
  583. *
  584. * @param string $userLogin
  585. *
  586. * @return bool
  587. */
  588. protected function isPrimaryAccountFree($userLogin) {
  589. $result = true;
  590. if (!empty($userLogin)) {
  591. if (!empty($this->allUsers)) {
  592. foreach ($this->allUsers as $io => $each) {
  593. if ($each['primarylogin'] == $userLogin) {
  594. $result = false;
  595. break;
  596. }
  597. }
  598. }
  599. }
  600. return ($result);
  601. }
  602. /**
  603. * Returns camera user assigned visor user ID if exists
  604. *
  605. * @param string $userLogin
  606. *
  607. * @return int/void
  608. */
  609. public function getCameraUser($userLogin) {
  610. $result = '';
  611. if (!empty($this->allCams)) {
  612. foreach ($this->allCams as $io => $each) {
  613. if ($each['login'] == $userLogin) {
  614. $result = $each['visorid'];
  615. break;
  616. }
  617. }
  618. }
  619. return ($result);
  620. }
  621. /**
  622. * Returns userId by its associated primary account
  623. *
  624. * @param string $userLogin
  625. *
  626. * @return int/void
  627. */
  628. public function getPrimaryAccountUserId($userLogin) {
  629. $result = '';
  630. if (!empty($this->allUsers)) {
  631. foreach ($this->allUsers as $io => $each) {
  632. if ($each['primarylogin'] == $userLogin) {
  633. $result = $each['id'];
  634. break;
  635. }
  636. }
  637. }
  638. return ($result);
  639. }
  640. /**
  641. * Returns count of associated user cameras
  642. *
  643. * @param int $userId
  644. *
  645. * @return int
  646. */
  647. protected function getUserCamerasCount($userId) {
  648. $result = 0;
  649. $userCameras = $this->getUserCameras($userId);
  650. if (!empty($userCameras)) {
  651. $result = sizeof($userCameras);
  652. }
  653. return ($result);
  654. }
  655. /**
  656. * Deletes user from database
  657. *
  658. * @param int $userId
  659. *
  660. * @return void/string on error
  661. */
  662. public function deleteUser($userId) {
  663. $result = '';
  664. $userId = vf($userId, 3);
  665. if (isset($this->allUsers[$userId])) {
  666. $camerasCount = $this->getUserCamerasCount($userId);
  667. if ($camerasCount == 0) {
  668. if (!isset($this->allChannels[$userId])) {
  669. $query = "DELETE from `" . self::TABLE_USERS . "` WHERE `id`='" . $userId . "';";
  670. nr_query($query);
  671. log_register('VISOR USER DELETE [' . $userId . ']');
  672. } else {
  673. $result .= __('Channel have user assigned');
  674. }
  675. } else {
  676. $result .= __('User have some cameras associated');
  677. }
  678. } else {
  679. $result .= __('User not exists');
  680. }
  681. return ($result);
  682. }
  683. /**
  684. * Returns user primary camera controls if primary available
  685. *
  686. * @param int $userId
  687. *
  688. * @return string
  689. */
  690. protected function renderUserPrimaryAccount($userId) {
  691. $result = '';
  692. if (isset($this->allUsers[$userId])) {
  693. $userData = $this->allUsers[$userId];
  694. $primaryAccount = $userData['primarylogin'];
  695. if (!empty($primaryAccount)) {
  696. if (isset($this->allUserData[$primaryAccount])) {
  697. $cells = wf_TableCell(__('Primary account'), '30%', 'row2');
  698. $linkLabel = (@$this->allUserData[$primaryAccount]['fulladress']) ? $this->allUserData[$primaryAccount]['fulladress'] : $primaryAccount;
  699. $primaLink = wf_Link(self::URL_CAMPROFILE . $primaryAccount, web_profile_icon() . ' ' . $linkLabel);
  700. $cells .= wf_TableCell($primaLink);
  701. $rows = wf_TableRow($cells, 'row3');
  702. $cells = wf_TableCell(__('Balance'), '30%', 'row2');
  703. $cells .= wf_TableCell($this->allUserData[$primaryAccount]['Cash']);
  704. $rows .= wf_TableRow($cells, 'row3');
  705. if ($this->altCfg['OPENPAYZ_SUPPORT']) {
  706. $cells = wf_TableCell(__('Payment ID'), '30%', 'row2');
  707. $cells .= wf_TableCell($this->allPaymentIDs[$primaryAccount]);
  708. $rows .= wf_TableRow($cells, 'row3');
  709. $result .= $rows;
  710. }
  711. //tariffing notice here
  712. $tariffingNotice = '';
  713. if ($userData['chargecams']) {
  714. $tariffingNotice = wf_img_sized('skins/icon_ok.gif', '', 12) . ' ';
  715. $tariffingNotice .= __('Funds for cameras will be charged from the main account at the end of the month');
  716. } else {
  717. $tariffingNotice = wf_img_sized('skins/icon_lock.png', '', 12) . ' ';
  718. $tariffingNotice .= __('All cameras live by themselves');
  719. }
  720. if ($this->allUserData[$primaryAccount]['Passive'] and $userData['chargecams']) {
  721. $tariffingNotice = wf_img_sized('skins/icon_passive.gif', __('Freezed'), 12) . ' ';
  722. $tariffingNotice .= __('Main account is frozen') . '. ' . __('All cameras live by themselves');
  723. }
  724. $cells = wf_TableCell(__('Tariffing'), '30%', 'row2');
  725. $cells .= wf_TableCell($tariffingNotice);
  726. $rows = wf_TableRow($cells, 'row3');
  727. $result .= $rows;
  728. } else {
  729. $cells = wf_TableCell(__('Primary account'), '30%', 'row2');
  730. $cells .= wf_TableCell(__('Not exists') . ': ' . $primaryAccount);
  731. $rows = wf_TableRow($cells, 'row3');
  732. $result .= $rows;
  733. }
  734. } else {
  735. $cells = wf_TableCell(__('Tariffing'), '30%', 'row2');
  736. $noPrimAccLabel = wf_img_sized('skins/delete_small.png', '', 12) . ' ' . __('All cameras live by themselves') . ', ' . __('no primary account set');
  737. $cells .= wf_TableCell($noPrimAccLabel);
  738. $rows = wf_TableRow($cells, 'row3');
  739. $result .= $rows;
  740. }
  741. }
  742. return ($result);
  743. }
  744. /**
  745. *
  746. * @param int $userId
  747. *
  748. * @return array
  749. */
  750. protected function createUserSecret($userId) {
  751. $result = array();
  752. $userId = ubRouting::filters($userId, 'int');
  753. if (isset($this->allUsers[$userId])) {
  754. if (!isset($this->allSecrets[$userId])) {
  755. $loginProposal = 'view' . $userId;
  756. $passwordProposal = zb_rand_digits(8);
  757. $this->secretsDb->data('visorid', $userId);
  758. $this->secretsDb->data('login', $loginProposal);
  759. $this->secretsDb->data('password', $passwordProposal);
  760. $this->secretsDb->create();
  761. log_register('VISOR USER [' . $userId . '] CREATE SECRET');
  762. } else {
  763. $result = $this->allSecrets[$userId];
  764. }
  765. }
  766. return ($result);
  767. }
  768. /**
  769. * Renders visor user global NVR secrets data
  770. *
  771. * @param int $userId
  772. *
  773. * @return string
  774. */
  775. protected function renderUserSecrets($userId) {
  776. $result = '';
  777. $userId = ubRouting::filters($userId, 'int');
  778. if (isset($this->allUsers[$userId])) {
  779. if (isset($this->allSecrets[$userId])) {
  780. $secretData = $this->allSecrets[$userId];
  781. } else {
  782. $this->createUserSecret($userId);
  783. //update current instance data
  784. $this->loadSecrets();
  785. $secretData = $this->allSecrets[$userId];
  786. }
  787. $rows = '';
  788. $cells = wf_TableCell(__('DVR login'), '', 'row2');
  789. $cells .= wf_TableCell($secretData['login']);
  790. $rows .= wf_TableRow($cells, 'row3');
  791. $cells = wf_TableCell(__('DVR password'), '', 'row2');
  792. $cells .= wf_TableCell($secretData['password']);
  793. $rows .= wf_TableRow($cells, 'row3');
  794. $result .= $rows;
  795. }
  796. return ($result);
  797. }
  798. /**
  799. * Renders visor users profile with associated cameras and some controls
  800. *
  801. * @param int $userId
  802. *
  803. * @return string
  804. */
  805. public function renderUserProfile($userId) {
  806. $result = '';
  807. $userId = ubRouting::filters($userId, 'int');
  808. if (isset($this->allUsers[$userId])) {
  809. $userData = $this->allUsers[$userId];
  810. if (!empty($userData)) {
  811. $userCamsCount = $this->getUserCamerasCount($userId);
  812. $cells = wf_TableCell(__('Name'), '30%', 'row2');
  813. $cells .= wf_TableCell($userData['realname']);
  814. $rows = wf_TableRow($cells, 'row3');
  815. $cells = wf_TableCell(__('Phone'), '', 'row2');
  816. $cells .= wf_TableCell($userData['phone']);
  817. $rows .= wf_TableRow($cells, 'row3');
  818. $cells = wf_TableCell(__('Charge'), '', 'row2');
  819. $chargeFlag = ($userData['chargecams']) ? wf_img_sized('skins/icon_active.gif', '', '12', '12') . ' ' . __('Yes') : wf_img_sized('skins/icon_inactive.gif', '', '12', '12') . ' ' . __('No');
  820. $cells .= wf_TableCell($chargeFlag);
  821. $rows .= wf_TableRow($cells, 'row3');
  822. //global NVR secrets
  823. if (!$this->isChansProtected($userId)) {
  824. $rows .= $this->renderUserSecrets($userId);
  825. }
  826. //primary user account inline
  827. $rows .= $this->renderUserPrimaryAccount($userId);
  828. //additional cameras fee
  829. if ($userCamsCount > 0) {
  830. $cells = wf_TableCell(__('Total surveillance price'), '', 'row2');
  831. $cells .= wf_TableCell($this->getUserCamerasPricing($userId));
  832. $rows .= wf_TableRow($cells, 'row3');
  833. }
  834. $result .= wf_TableBody($rows, '100%', 0, '');
  835. $result .= $this->renderUserControls($userId);
  836. if ($userCamsCount > 0) {
  837. $result .= $this->renderCamerasContainer(self::URL_ME . self::URL_USERCAMS . $userId);
  838. } else {
  839. $result .= $this->messages->getStyledMessage(__('User have no cameras assigned'), 'warning');
  840. }
  841. //assigned channels preview & assign forms
  842. $result .= $this->renderUserAssignedChannels($userId);
  843. }
  844. } else {
  845. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('User not exists') . ' [' . $userId . ']', 'error');
  846. }
  847. return ($result);
  848. }
  849. /**
  850. * Renders channels available on all of DVRs that can be assigned to this user
  851. *
  852. * @param int $userId
  853. *
  854. * @return string
  855. */
  856. protected function renderUnassignedChannels($userId) {
  857. $result = '';
  858. if (cfr('VISOREDIT')) {
  859. $userId = ubRouting::filters($userId, 'int');
  860. $unassignedCount = 0;
  861. $chanControlLinks = '';
  862. if ($this->trassirEnabled or $this->wolfRecorderEnabled) {
  863. if (!empty($this->allDvrs)) {
  864. foreach ($this->allDvrs as $io => $eachDvr) {
  865. if ($eachDvr['type'] == 'trassir') {
  866. $dvrGate = new TrassirServer($eachDvr['ip'], $eachDvr['login'], $eachDvr['password'], $eachDvr['apikey'], $eachDvr['port'], $this->trassirDebug);
  867. $dvrChannels = $dvrGate->getChannels();
  868. if (!empty($dvrChannels)) {
  869. foreach ($dvrChannels as $eachChanGuid => $eachChanName) {
  870. //not assigned to anyone
  871. if (!isset($this->channelUsers[$eachChanGuid])) {
  872. $chanEditLink = self::URL_ME . self::URL_CHANEDIT . $eachChanGuid . '&dvrid=' . $eachDvr['id'] . '&useridpreset=' . $userId;
  873. $chanControlLinks .= wf_Link($chanEditLink, web_edit_icon() . ' ' . $eachChanGuid . ' (' . $eachChanName . ')', false, 'ubButton') . ' ';
  874. $unassignedCount++;
  875. }
  876. }
  877. }
  878. }
  879. if ($eachDvr['type'] == 'wolfrecorder') {
  880. $apiUrl = $this->getWolfRecorderApiUrl($eachDvr['id']);
  881. $dvrGate = new WolfRecorder($apiUrl, $eachDvr['apikey']);
  882. $dvrChannels = $dvrGate->channelsGetAll();
  883. if (!empty($dvrChannels)) {
  884. foreach ($dvrChannels as $eachChanId => $eachChanCameraId) {
  885. //not assigned to anyone
  886. if (!isset($this->channelUsers[$eachChanId])) {
  887. $chanEditLink = self::URL_ME . self::URL_CHANEDIT . $eachChanId . '&dvrid=' . $eachDvr['id'] . '&useridpreset=' . $userId;
  888. $chanControlLinks .= wf_Link($chanEditLink, web_edit_icon() . ' ' . $eachChanId, false, 'ubButton') . ' ';
  889. $unassignedCount++;
  890. }
  891. }
  892. }
  893. }
  894. }
  895. }
  896. }
  897. if ($unassignedCount > 0) {
  898. $result .= wf_tag('h2') . __('No user assigned') . wf_tag('h2', true);
  899. $result .= $chanControlLinks;
  900. }
  901. }
  902. return ($result);
  903. }
  904. /**
  905. * Checks is channel operations protected for unpriviliged users?
  906. *
  907. * @param int $userId
  908. *
  909. * @return bool
  910. */
  911. protected function isChansProtected($userId) {
  912. $result = false;
  913. if (cfr('ROOT')) {
  914. //thats is superuser
  915. $result = false;
  916. } else {
  917. //is userId private?
  918. if (isset($this->protectedUserIds[$userId])) {
  919. $result = true;
  920. }
  921. }
  922. return ($result);
  923. }
  924. /**
  925. * Renders list of user assigned channels with their preview and optional assign form
  926. *
  927. * @param int $userId
  928. *
  929. * @return string
  930. */
  931. protected function renderUserAssignedChannels($userId) {
  932. $result = '';
  933. $userId = ubRouting::filters($userId, 'int');
  934. if ($this->trassirEnabled or $this->wolfRecorderEnabled) {
  935. if (ubRouting::checkGet('chanspreview')) {
  936. if (!$this->isChansProtected($userId)) {
  937. $result .= wf_tag('h2', false) . __('Channels') . wf_tag('h2', true);
  938. $result .= wf_tag('div', false);
  939. //assigned channels list
  940. if (isset($this->allChannels[$userId])) {
  941. if (!empty($this->allChannels[$userId])) {
  942. foreach ($this->allChannels[$userId] as $io => $eachChan) {
  943. $chanDvrData = $this->allDvrs[$eachChan['dvrid']];
  944. if ($chanDvrData['type'] == 'trassir') {
  945. $dvrGate = new TrassirServer($chanDvrData['ip'], $chanDvrData['login'], $chanDvrData['password'], $chanDvrData['apikey'], $chanDvrData['port'], $this->trassirDebug);
  946. $streamUrl = $dvrGate->getLiveVideoStream($eachChan['chan'], 'main', $this->chanPreviewContainer, $this->chanPreviewQuality, $this->chanPreviewFramerate, $chanDvrData['customurl']);
  947. $result .= wf_tag('div', false, 'whiteboard', 'style="width:' . $this->chanPreviewSize . ';"');
  948. $chanEditLabel = web_edit_icon() . ' ' . __('Edit') . ' ' . __('channel');
  949. if (cfr('VISOREDIT')) {
  950. $channelEditControl = wf_Link(self::URL_ME . self::URL_CHANEDIT . $eachChan['chan'] . '&dvrid=' . $eachChan['dvrid'], $chanEditLabel);
  951. } else {
  952. $channelEditControl = '';
  953. }
  954. $result .= $eachChan['chan'];
  955. $result .= wf_tag('br');
  956. $result .= $this->renderChannelPlayer($streamUrl, '90%', true);
  957. $result .= wf_tag('div', false, 'todaysig');
  958. $result .= $channelEditControl;
  959. $result .= wf_tag('div', true);
  960. $result .= wf_CleanDiv();
  961. $result .= wf_tag('div', true);
  962. }
  963. if ($chanDvrData['type'] == 'wolfrecorder') {
  964. $apiUrl = $this->getWolfRecorderApiUrl($chanDvrData['id']);
  965. $webUrl = ($chanDvrData['customurl']) ? $chanDvrData['customurl'] : $apiUrl;
  966. $dvrGate = new WolfRecorder($apiUrl, $chanDvrData['apikey']);
  967. $channelScreenShotReply = $dvrGate->channelsGetScreenshot($eachChan['chan']);
  968. $channelScreenshot = 'skins/noimage.jpg';
  969. if (isset($channelScreenShotReply['screenshot'])) {
  970. if ($channelScreenShotReply['screenshot']) {
  971. $channelScreenshot = $webUrl . $channelScreenShotReply['screenshot'];
  972. }
  973. }
  974. $result .= wf_tag('div', false, 'whiteboard', 'style="width:' . $this->chanPreviewSize . ';"');
  975. $chanEditLabel = web_edit_icon() . ' ' . __('Edit') . ' ' . __('channel');
  976. if (cfr('VISOREDIT')) {
  977. $channelEditControl = wf_Link(self::URL_ME . self::URL_CHANEDIT . $eachChan['chan'] . '&dvrid=' . $eachChan['dvrid'], $chanEditLabel);
  978. } else {
  979. $channelEditControl = '';
  980. }
  981. $result .= $eachChan['chan'];
  982. $result .= wf_tag('br');
  983. $result .= wf_img_sized($channelScreenshot, '', '90%');
  984. $result .= wf_tag('div', false, 'todaysig');
  985. $result .= $channelEditControl;
  986. $result .= wf_tag('div', true);
  987. $result .= wf_CleanDiv();
  988. $result .= wf_tag('div', true);
  989. }
  990. }
  991. }
  992. } else {
  993. $result .= $this->messages->getStyledMessage(__('User have no channels assigned'), 'warning');
  994. }
  995. $result .= wf_CleanDiv();
  996. $result .= wf_tag('div', true, '');
  997. //unassigned channels list
  998. $result .= $this->renderUnassignedChannels($userId);
  999. $result .= wf_delimiter();
  1000. $result .= wf_BackLink(self::URL_ME . self::URL_USERVIEW . $userId);
  1001. } else {
  1002. log_register('VISOR USER [' . $userId . '] CHAN ACCESS VIOLATION');
  1003. show_error(__('What are your forgot there') . '?');
  1004. }
  1005. } else {
  1006. if (!$this->isChansProtected($userId)) {
  1007. $result .= wf_delimiter();
  1008. $result .= wf_Link(self::URL_ME . self::URL_USERVIEW . $userId . '&chanspreview=true', web_green_led() . ' ' . __('Channels'), false, 'ubButton');
  1009. }
  1010. }
  1011. }
  1012. return ($result);
  1013. }
  1014. /**
  1015. * Returns user assigned cameras fee
  1016. *
  1017. * @param int $userId
  1018. *
  1019. * @return float
  1020. */
  1021. protected function getUserCamerasPricing($userId) {
  1022. $result = 0;
  1023. $allCameras = $this->getUserCameras($userId);
  1024. if (!empty($allCameras)) {
  1025. foreach ($allCameras as $io => $each) {
  1026. $cameraLogin = $each['login'];
  1027. if (isset($this->allUserData[$cameraLogin])) {
  1028. $cameraTariff = $this->allUserData[$cameraLogin]['Tariff'];
  1029. if (isset($this->allTariffPrices[$cameraTariff])) {
  1030. $result += $this->allTariffPrices[$cameraTariff];
  1031. }
  1032. }
  1033. }
  1034. }
  1035. return ($result);
  1036. }
  1037. /**
  1038. * Renders Visor user defaults controls set
  1039. *
  1040. * @param int $userId
  1041. *
  1042. * @return string
  1043. */
  1044. protected function renderUserControls($userId) {
  1045. $result = '';
  1046. if (cfr('VISOREDIT')) {
  1047. if (isset($this->allUsers[$userId])) {
  1048. $taskB = wf_tag('div', false, 'dashtask', 'style="height:75px; width:75px;"');
  1049. $taskE = wf_tag('div', true);
  1050. $result .= $taskB . wf_modalAuto(wf_img('skins/ukv/useredit.png', __('Edit user')), __('Edit user'), $this->renderUserEditInterface($userId)) . __('Edit') . $taskE;
  1051. $result .= $taskB . wf_modalAuto(wf_img('skins/icon_king_big.png', __('Primary account')), __('Primary account'), $this->renderUserPrimaryEditForm($userId)) . __('Primary') . $taskE;
  1052. $result .= $taskB . wf_modalAuto(wf_img('skins/annihilation.gif', __('Deleting user')), __('Deleting user'), $this->renderUserDeletionForm($userId), '') . __('Delete') . $taskE;
  1053. $result .= wf_CleanDiv();
  1054. }
  1055. }
  1056. return ($result);
  1057. }
  1058. /**
  1059. * Renders user primary account editing interface
  1060. *
  1061. * @param int $userId
  1062. *
  1063. * @return string
  1064. */
  1065. protected function renderUserPrimaryEditForm($userId) {
  1066. $result = '';
  1067. if (isset($this->allUsers[$userId])) {
  1068. $currentUserData = $this->allUsers[$userId];
  1069. $currentPrimaryAccount = $currentUserData['primarylogin'];
  1070. $allUserCameras = $this->getUserCameras($userId);
  1071. $camerasTmp = array();
  1072. $selectedCamera = '';
  1073. $camerasTmp[''] = '-';
  1074. if (!empty($allUserCameras)) {
  1075. foreach ($allUserCameras as $io => $each) {
  1076. if ($each['login'] == $currentPrimaryAccount) {
  1077. $selectedCamera = $each['login'];
  1078. }
  1079. $camerasTmp[$each['login']] = @$this->allUserData[$each['login']]['fulladress'] . ' - ' . @$this->allUserData[$each['login']]['ip'];
  1080. }
  1081. }
  1082. $inputs = '';
  1083. $inputs = wf_Selector('newprimarycameralogin', $camerasTmp, __('Camera'), $selectedCamera, true);
  1084. $inputs .= __('Or') . wf_tag('br');
  1085. $inputs .= wf_TextInput('newprimaryuserlogin', __('Login'), $currentPrimaryAccount, true, 20);
  1086. $inputs .= wf_HiddenInput('editprimarycamerauserid', $userId);
  1087. $inputs .= wf_delimiter();
  1088. $inputs .= wf_Submit(__('Save'));
  1089. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1090. }
  1091. return ($result);
  1092. }
  1093. /**
  1094. * Sets some account as primary for some user
  1095. *
  1096. * @param int $userId
  1097. * @param string $login
  1098. *
  1099. * @return void
  1100. */
  1101. protected function setPrimaryAccount($userId, $login = '') {
  1102. $userId = vf($userId, 3);
  1103. $login = trim($login);
  1104. if (isset($this->allUsers[$userId])) {
  1105. $userCameras = $this->getUserCameras($userId);
  1106. $currentPrimary = $this->allUsers[$userId]['primarylogin'];
  1107. if ($currentPrimary != $login) {
  1108. if ($this->isPrimaryAccountFree($login)) {
  1109. //setting primary account in profile
  1110. $this->usersDb->where('id', '=', $userId);
  1111. $this->usersDb->data('primarylogin', $login);
  1112. $this->usersDb->save();
  1113. // dropping all camera primary flags
  1114. $this->camsDb->where('visorid', '=', $userId);
  1115. $this->camsDb->data('primary', '0');
  1116. $this->camsDb->save();
  1117. log_register('VISOR USER [' . $userId . '] CHANGE PRIMARY (' . $login . ')');
  1118. $cameraId = $this->getCameraIdByLogin($login);
  1119. if (!empty($cameraId)) {
  1120. //setting camera account as primary
  1121. $this->camsDb->where('id', '=', $cameraId);
  1122. $this->camsDb->data('primary', '1');
  1123. $this->camsDb->save();
  1124. }
  1125. } else {
  1126. log_register('VISOR USER [' . $userId . '] FAIL PRIMARY BUSY');
  1127. }
  1128. }
  1129. } else {
  1130. log_register('VISOR USER [' . $userId . '] FAIL PRIMARY NOUSER');
  1131. }
  1132. }
  1133. /**
  1134. * Catches primary editing request and saves changes if required
  1135. *
  1136. * @return void
  1137. */
  1138. public function savePrimary() {
  1139. if (wf_CheckPost(array('editprimarycamerauserid'))) {
  1140. $userId = vf($_POST['editprimarycamerauserid'], 3);
  1141. $newPrimaryLogin = (wf_CheckPost(array('newprimarycameralogin'))) ? $_POST['newprimarycameralogin'] : '';
  1142. if (wf_CheckPost(array('newprimaryuserlogin')) and !wf_CheckPost(array('newprimarycameralogin'))) {
  1143. $newPrimaryLogin = $_POST['newprimaryuserlogin'];
  1144. }
  1145. $this->setPrimaryAccount($userId, $newPrimaryLogin);
  1146. }
  1147. }
  1148. /**
  1149. * user deletion form
  1150. *
  1151. * @param int $userId existing user ID
  1152. *
  1153. * @return string
  1154. */
  1155. protected function renderUserDeletionForm($userId) {
  1156. $userId = vf($userId, 3);
  1157. $inputs = __('Be careful, this module permanently deletes user and all data associated with it. Opportunities to raise from the dead no longer.') . ' <br>
  1158. ' . __('To ensure that we have seen the seriousness of your intentions to enter the word сonfirm the field below.');
  1159. $inputs .= wf_HiddenInput('userdeleteprocessing', $userId);
  1160. $inputs .= wf_delimiter();
  1161. $inputs .= wf_tag('input', false, '', 'type="text" name="deleteconfirmation" autocomplete="off"');
  1162. $inputs .= wf_delimiter();
  1163. $inputs .= wf_Submit(__('I really want to stop suffering User'));
  1164. $result = wf_Form('', 'POST', $inputs, 'glamour');
  1165. return ($result);
  1166. }
  1167. /**
  1168. * Renders default cameras view container
  1169. *
  1170. * @param string $url
  1171. *
  1172. * @return string
  1173. */
  1174. public function renderCamerasContainer($url) {
  1175. $result = '';
  1176. $opts = '"order": [[ 0, "desc" ]]';
  1177. $columns = array('ID', 'Primary', 'User', 'Address', 'DVR', 'IP', 'Tariff', 'Active', 'Balance', 'Credit', 'Actions');
  1178. if ($this->altCfg['DN_ONLINE_DETECT']) {
  1179. $columns = array('ID', 'Primary', 'User', 'Address', 'DVR', 'IP', 'Tariff', 'Active', 'Online', 'Balance', 'Credit', 'Actions');
  1180. }
  1181. $result .= wf_JqDtLoader($columns, $url, false, __('Cams'), 50, $opts);
  1182. return ($result);
  1183. }
  1184. /**
  1185. * Renders ajax json backend for some user assigned cameras
  1186. *
  1187. * @param int $userId
  1188. *
  1189. * @return void
  1190. */
  1191. public function ajaxUserCams($userId) {
  1192. $userId = vf($userId, 3);
  1193. $json = new wf_JqDtHelper();
  1194. $dnFlag = ($this->altCfg['DN_ONLINE_DETECT']) ? true : false;
  1195. if (isset($this->allUsers[$userId])) {
  1196. $allUserCams = $this->getUserCameras($userId);
  1197. if (!empty($allUserCams)) {
  1198. foreach ($allUserCams as $io => $each) {
  1199. $cameraUserData = @$this->allUserData[$each['login']];
  1200. $data[] = $each['id'];
  1201. $primaryFlag = ($each['primary']) ? web_bool_led(true) . ' ' . __('Yes') : web_bool_led(false) . ' ' . __('No');
  1202. $data[] = $primaryFlag;
  1203. $visorLinkLabel = $this->iconVisorUser() . ' ' . @$this->allUsers[$each['visorid']]['realname'];
  1204. $visorUserLink = wf_Link(self::URL_ME . self::URL_USERVIEW . $each['visorid'], $visorLinkLabel);
  1205. $data[] = $visorUserLink;
  1206. $cameraLinkLabel = web_profile_icon() . ' ' . @$cameraUserData['fulladress'];
  1207. $cameraLink = wf_Link(self::URL_CAMPROFILE . $each['login'], $cameraLinkLabel);
  1208. $data[] = $cameraLink;
  1209. $cameraDvr = (!empty($each['dvrid'])) ? @$this->allDvrs[$each['dvrid']]['name'] : __('No');
  1210. $data[] = $cameraDvr;
  1211. $data[] = @$cameraUserData['ip'];
  1212. $data[] = @$cameraUserData['Tariff'];
  1213. $cameraCash = @$cameraUserData['Cash'];
  1214. $cameraCredit = @$cameraUserData['Credit'];
  1215. $cameraState = '';
  1216. if ($cameraCash >= '-' . $cameraCredit) {
  1217. $cameraState = web_bool_led(true) . ' ' . __('Yes');
  1218. } else {
  1219. $cameraState = web_bool_led(false) . ' ' . __('No');
  1220. }
  1221. $data[] = $cameraState;
  1222. if ($dnFlag) {
  1223. $onlineState = web_bool_star(false) . ' ' . __('No');
  1224. if (file_exists(DATA_PATH . 'dn/' . $each['login'])) {
  1225. $onlineState = web_bool_star(true) . ' ' . __('Yes');
  1226. }
  1227. $data[] = $onlineState;
  1228. }
  1229. $data[] = $cameraCash;
  1230. $data[] = $cameraCredit;
  1231. $actLinks = wf_Link(self::URL_ME . self::URL_CAMVIEW . $each['id'], web_edit_icon() . ' ' . __('Edit') . ' ' . __('camera'));
  1232. $data[] = $actLinks;
  1233. $json->addRow($data);
  1234. unset($data);
  1235. }
  1236. }
  1237. }
  1238. $json->getJson();
  1239. }
  1240. /**
  1241. * Returns default user icon coode
  1242. *
  1243. * @param int size
  1244. *
  1245. * @return string
  1246. */
  1247. public function iconVisorUser($size = '') {
  1248. $size = vf($size, 3);
  1249. $result = (!empty($size)) ? wf_img('skins/icon_camera_small.png') : wf_img_sized('skins/icon_camera_small.png', '', $size, $size);
  1250. return ($result);
  1251. }
  1252. /**
  1253. * Renders ajax json backend for all available cameras
  1254. *
  1255. * @return void
  1256. */
  1257. public function ajaxAllCams() {
  1258. $json = new wf_JqDtHelper();
  1259. $dnFlag = ($this->altCfg['DN_ONLINE_DETECT']) ? true : false;
  1260. if (!empty($this->allCams)) {
  1261. foreach ($this->allCams as $io => $each) {
  1262. $cameraUserData = @$this->allUserData[$each['login']];
  1263. $data[] = $each['id'];
  1264. $primaryFlag = ($each['primary']) ? web_bool_led(true) . ' ' . __('Yes') : web_bool_led(false) . ' ' . __('No');
  1265. $data[] = $primaryFlag;
  1266. $visorLinkLabel = $this->iconVisorUser() . ' ' . @$this->allUsers[$each['visorid']]['realname'];
  1267. $visorUserLink = wf_Link(self::URL_ME . self::URL_USERVIEW . $each['visorid'], $visorLinkLabel);
  1268. $data[] = $visorUserLink;
  1269. $cameraLinkLabel = web_profile_icon() . ' ' . $cameraUserData['fulladress'];
  1270. $cameraLink = wf_Link(self::URL_CAMPROFILE . $each['login'], $cameraLinkLabel);
  1271. $data[] = $cameraLink;
  1272. $cameraDvr = (!empty($each['dvrid'])) ? @$this->allDvrs[$each['dvrid']]['name'] : __('No');
  1273. $data[] = $cameraDvr;
  1274. $data[] = @$cameraUserData['ip'];
  1275. $data[] = @$cameraUserData['Tariff'];
  1276. $cameraCash = @$cameraUserData['Cash'];
  1277. $cameraCredit = @$cameraUserData['Credit'];
  1278. $cameraState = '';
  1279. if ($cameraCash >= '-' . $cameraCredit) {
  1280. $cameraState = web_bool_led(true) . ' ' . __('Yes');
  1281. } else {
  1282. $cameraState = web_bool_led(false) . ' ' . __('No');
  1283. }
  1284. $data[] = $cameraState;
  1285. if ($dnFlag) {
  1286. $onlineState = web_bool_star(false) . ' ' . __('No');
  1287. if (file_exists(DATA_PATH . 'dn/' . $each['login'])) {
  1288. $onlineState = web_bool_star(true) . ' ' . __('Yes');
  1289. }
  1290. $data[] = $onlineState;
  1291. }
  1292. $data[] = $cameraCash;
  1293. $data[] = $cameraCredit;
  1294. $actLinks = wf_Link(self::URL_ME . self::URL_CAMVIEW . $each['id'], web_edit_icon() . ' ' . __('Edit') . ' ' . __('camera'));
  1295. $data[] = $actLinks;
  1296. $json->addRow($data);
  1297. unset($data);
  1298. }
  1299. }
  1300. $json->getJson();
  1301. }
  1302. /**
  1303. * Renders initial camera creation interface
  1304. *
  1305. * @param string $userLogin
  1306. *
  1307. * @return string
  1308. */
  1309. public function renderCameraCreateInterface($userLogin) {
  1310. $result = '';
  1311. if (!empty($this->allUsers)) {
  1312. if (cfr('VISOREDIT')) {
  1313. $usersTmp = array();
  1314. $usersTmp[''] = '-';
  1315. foreach ($this->allUsers as $io => $each) {
  1316. $usersTmp[$each['id']] = $each['realname'];
  1317. }
  1318. $inputs = '';
  1319. if ($this->altCfg['VISOR_USERSEL_SEARCHBL']) {
  1320. $inputs .= wf_SelectorSearchable('newcameravisorid', $usersTmp, __('The user who will be assigned a new camera'), '', false);
  1321. } else {
  1322. $inputs .= wf_Selector('newcameravisorid', $usersTmp, __('The user who will be assigned a new camera'), '', false);
  1323. }
  1324. $inputs .= wf_delimiter();
  1325. $inputs .= wf_HiddenInput('newcameralogin', $userLogin);
  1326. $inputs .= wf_Submit(__('Create'));
  1327. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1328. } else {
  1329. $failLabel = __('This user account is not associated with any existing Visor user or any camera account') . '. ';
  1330. $failLabel .= __('Contact your system administrator to fix this issue') . '.';
  1331. $result .= $this->messages->getStyledMessage($failLabel, 'warning');
  1332. }
  1333. } else {
  1334. $result .= $this->messages->getStyledMessage(__('No existing Visor users avaliable, you must create one at least to assign cameras'), 'error');
  1335. }
  1336. return ($result);
  1337. }
  1338. /**
  1339. * Creates new camera account and assigns it to existing user
  1340. *
  1341. * @return void
  1342. */
  1343. public function createCamera() {
  1344. if (ubRouting::checkPost(array('newcameravisorid', 'newcameralogin'))) {
  1345. $newVisorId = ubRouting::post('newcameravisorid', 'int');
  1346. $newCameraLogin = ubRouting::post('newcameralogin');
  1347. $newCameraLoginF = ubRouting::filters($newCameraLogin, 'mres');
  1348. if (isset($this->allUsers[$newVisorId])) {
  1349. if (!empty($newCameraLoginF)) {
  1350. $this->camsDb->data('visorid', $newVisorId);
  1351. $this->camsDb->data('login', $newCameraLoginF);
  1352. $this->camsDb->data('primary', 0);
  1353. $this->camsDb->create();
  1354. $newId = $this->camsDb->getLastId();
  1355. log_register('VISOR CAMERA CREATE [' . $newId . '] ASSIGN [' . $newVisorId . '] LOGIN (' . $newCameraLogin . ')');
  1356. } else {
  1357. log_register('VISOR CAMERA CREATE FAIL EMPTY_LOGIN');
  1358. }
  1359. } else {
  1360. log_register('VISOR CAMERA CREATE FAIL VISORID_NOT_EXISTS');
  1361. }
  1362. }
  1363. }
  1364. /**
  1365. * Creates channel to user binding in database
  1366. *
  1367. * @param int $visorId
  1368. * @param int $dvrId
  1369. * @param string $channelGuid
  1370. *
  1371. * @return void
  1372. */
  1373. public function assignChannel($visorId, $dvrId, $channelGuid) {
  1374. $visorId = ubRouting::filters($visorId, 'int');
  1375. $dvrId = ubRouting::filters($dvrId, 'int');
  1376. $channelGuid = ubRouting::filters($channelGuid, 'mres');
  1377. $this->chansDb->data('visorid', $visorId);
  1378. $this->chansDb->data('dvrid', $dvrId);
  1379. $this->chansDb->data('chan', $channelGuid);
  1380. $this->chansDb->create();
  1381. log_register('VISOR USER [' . $visorId . '] ASSIGN CHAN `' . $channelGuid . '` ON DVR [' . $dvrId . ']');
  1382. }
  1383. /**
  1384. * Deletes channel to user binding in database
  1385. *
  1386. * @param int $visorId
  1387. * @param int $dvrId
  1388. * @param string $channelGuid
  1389. *
  1390. * @return void
  1391. */
  1392. public function unassignChannel($visorId, $dvrId, $channelGuid) {
  1393. $visorId = ubRouting::filters($visorId, 'int');
  1394. $dvrId = ubRouting::filters($dvrId, 'int');
  1395. $channelGuid = ubRouting::filters($channelGuid, 'mres');
  1396. $this->chansDb->where('visorid', '=', $visorId);
  1397. $this->chansDb->where('dvrid', '=', $dvrId);
  1398. $this->chansDb->where('chan', '=', $channelGuid);
  1399. $this->chansDb->delete();
  1400. log_register('VISOR USER [' . $visorId . '] UNASSIGN CHAN `' . $channelGuid . '` ON DVR [' . $dvrId . ']');
  1401. }
  1402. /**
  1403. * Renders users editing interface
  1404. *
  1405. * @param int $userId
  1406. *
  1407. * @return string
  1408. */
  1409. protected function renderUserEditInterface($userId) {
  1410. $result = '';
  1411. $userId = vf($userId, 3);
  1412. if (isset($this->allUsers[$userId])) {
  1413. $currentUserData = $this->allUsers[$userId];
  1414. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  1415. $inputs = wf_HiddenInput('edituserid', $userId);
  1416. $inputs .= wf_TextInput('editusername', __('Name') . $sup, $currentUserData['realname'], true, 25);
  1417. $inputs .= wf_TextInput('edituserphone', __('Phone'), $currentUserData['phone'], true, 20, 'mobile');
  1418. $inputs .= wf_CheckInput('edituserchargecams', __('Charge money from primary account for linked camera users if required'), true, $currentUserData['chargecams']);
  1419. $inputs .= wf_delimiter();
  1420. $inputs .= wf_Submit(__('Save'));
  1421. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1422. }
  1423. return ($result);
  1424. }
  1425. /**
  1426. * Catches and saves user editing request if required
  1427. *
  1428. * @return void
  1429. */
  1430. public function saveUser() {
  1431. if (ubRouting::checkPost(array('edituserid', 'editusername'))) {
  1432. $editUserId = ubRouting::post('edituserid', 'int');
  1433. if (isset($this->allUsers[$editUserId])) {
  1434. $currentUserData = $this->allUsers[$editUserId];
  1435. $where = " WHERE `id`='" . $editUserId . "'";
  1436. $newUserName = ubRouting::post('editusername', 'mres');
  1437. $newUserPhone = ubRouting::post('edituserphone', 'mres');
  1438. $newCharge = (ubRouting::checkPost('edituserchargecams')) ? 1 : 0;
  1439. $changedFlag = false;
  1440. if ($currentUserData['realname'] != $newUserName) {
  1441. $changedFlag = true;
  1442. $this->usersDb->data('realname', $newUserName);
  1443. log_register('VISOR USER [' . $editUserId . '] CHANGE NAME `' . $newUserName . '`');
  1444. }
  1445. if ($currentUserData['phone'] != $newUserPhone) {
  1446. $changedFlag = true;
  1447. $this->usersDb->data('phone', $newUserPhone);
  1448. log_register('VISOR USER [' . $editUserId . '] CHANGE PHONE `' . $newUserPhone . '`');
  1449. }
  1450. if ($currentUserData['chargecams'] != $newCharge) {
  1451. $changedFlag = true;
  1452. $this->usersDb->data('chargecams', $newCharge);
  1453. log_register('VISOR USER [' . $editUserId . '] CHANGE CHARGE `' . $newUserPhone . '`');
  1454. }
  1455. //commiting changes to DB
  1456. if ($changedFlag) {
  1457. $this->usersDb->where('id', '=', $editUserId);
  1458. $this->usersDb->save();
  1459. }
  1460. }
  1461. }
  1462. }
  1463. /**
  1464. * Returns existing camera deletion form
  1465. *
  1466. * @param int $cameraId
  1467. *
  1468. * @return string
  1469. */
  1470. protected function renderCameraDeletionForm($cameraId) {
  1471. $cameraId = ubRouting::filters($cameraId, 'int');
  1472. $result = '';
  1473. if (isset($this->allCams[$cameraId])) {
  1474. $inputs = __('To ensure that we have seen the seriousness of your intentions to enter the word сonfirm the field below.');
  1475. $inputs .= wf_delimiter();
  1476. $inputs .= wf_tag('input', false, '', 'type="text" name="deleteconfirmation" autocomplete="off"');
  1477. $inputs .= wf_tag('br');
  1478. $inputs .= wf_HiddenInput('cameradeleteprocessing', $cameraId);
  1479. $inputs .= wf_tag('br');
  1480. $inputs .= wf_Submit(__('Delete camera'));
  1481. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1482. }
  1483. return ($result);
  1484. }
  1485. /**
  1486. * Deletes existing camera from database
  1487. *
  1488. * @param int $cameraId
  1489. *
  1490. * @return void/string on error
  1491. */
  1492. public function deleteCamera($cameraId) {
  1493. $cameraId = ubRouting::filters($cameraId, 'int');
  1494. $result = '';
  1495. if (isset($this->allCams[$cameraId])) {
  1496. $cameraData = $this->allCams[$cameraId];
  1497. $this->camsDb->where('id', '=', $cameraId);
  1498. $this->camsDb->delete();
  1499. log_register('VISOR CAMERA DELETE [' . $cameraId . '] ASSIGNED [' . $cameraData['visorid'] . '] LOGIN (' . $cameraData['login'] . ')');
  1500. } else {
  1501. $result .= __('Something went wrong') . ': ' . __('No such camera exists') . ' [' . $cameraId . ']';
  1502. }
  1503. return ($result);
  1504. }
  1505. /**
  1506. * Renders camera profile with editing forms
  1507. *
  1508. * @param int $cameraId
  1509. *
  1510. * @return string
  1511. */
  1512. public function renderCameraForm($cameraId) {
  1513. $cameraId = ubRouting::filters($cameraId, 'int');
  1514. $result = '';
  1515. if (isset($this->allCams[$cameraId])) {
  1516. $cameraData = $this->allCams[$cameraId];
  1517. $camProfile = $cameraData['login'];
  1518. $usersTmp = array();
  1519. $dvrTmp = array('' => '-');
  1520. if (!empty($this->allUsers)) {
  1521. foreach ($this->allUsers as $io => $each) {
  1522. $usersTmp[$each['id']] = $each['realname'];
  1523. }
  1524. }
  1525. if (!empty($this->allDvrs)) {
  1526. foreach ($this->allDvrs as $io => $each) {
  1527. $dvrFull = false;
  1528. $dvrLabel = $each['ip'];
  1529. if ($each['camlimit'] > 0) {
  1530. $dvrCamsNow = $this->getDvrCameraCount($each['id']);
  1531. if ($dvrCamsNow >= $each['camlimit']) {
  1532. $dvrFull = true;
  1533. }
  1534. }
  1535. if (!empty($each['name'])) {
  1536. $dvrLabel .= ' - ' . $each['name'];
  1537. }
  1538. $dvrLabel .= ' (' . $dvrCamsNow . '/' . $each['camlimit'] . ')';
  1539. if ($dvrFull) {
  1540. $dvrLabel .= ' ' . __('full') . '!';
  1541. }
  1542. $dvrTmp[$each['id']] = $dvrLabel;
  1543. }
  1544. }
  1545. //is camera internet user exists?
  1546. if (isset($this->allUserData[$camProfile])) {
  1547. $camProfileData = $this->allUserData[$camProfile];
  1548. $cells = wf_TableCell(__('User'), '30%', 'row2');
  1549. $visorUserLink = wf_Link(self::URL_ME . self::URL_USERVIEW . $cameraData['visorid'], $this->iconVisorUser('12') . ' ' . @$this->allUsers[$cameraData['visorid']]['realname']);
  1550. $cells .= wf_TableCell($visorUserLink);
  1551. $rows = wf_TableRow($cells, 'row3');
  1552. $cells = wf_TableCell(__('Address'), '30%', 'row2');
  1553. $camProfileLink = wf_Link(self::URL_CAMPROFILE . $camProfile, web_profile_icon() . ' ' . @$camProfileData['fulladress']);
  1554. $cells .= wf_TableCell($camProfileLink);
  1555. $rows .= wf_TableRow($cells, 'row3');
  1556. $cells = wf_TableCell(__('IP'), '30%', 'row2');
  1557. $cells .= wf_TableCell($camProfileData['ip']);
  1558. $rows .= wf_TableRow($cells, 'row3');
  1559. $cells = wf_TableCell(__('Tariff'), '30%', 'row2');
  1560. $cells .= wf_TableCell($camProfileData['Tariff']);
  1561. $rows .= wf_TableRow($cells, 'row3');
  1562. $cameraState = '';
  1563. $cameraCash = $camProfileData['Cash'];
  1564. $cameraCredit = $camProfileData['Credit'];
  1565. if ($cameraCash >= '-' . $cameraCredit) {
  1566. $cameraState = wf_img_sized('skins/icon_active.gif', '', '12', '12') . ' ' . __('Yes');
  1567. } else {
  1568. $cameraState = wf_img_sized('skins/icon_inactive.gif', '', '12', '12') . ' ' . __('No');
  1569. }
  1570. $camPass = ($cameraData['campassword']) ? wf_img_sized('skins/icon_active.gif', '', '12', '12') : wf_img_sized('skins/icon_inactive.gif', '', '12', '12');
  1571. $dvrPass = ($cameraData['dvrpassword']) ? wf_img_sized('skins/icon_active.gif', '', '12', '12') : wf_img_sized('skins/icon_inactive.gif', '', '12', '12');
  1572. $cells = wf_TableCell(__('Active'), '30%', 'row2');
  1573. $cells .= wf_TableCell($cameraState);
  1574. $rows .= wf_TableRow($cells, 'row3');
  1575. $cells = wf_TableCell(__('Balance'), '30%', 'row2');
  1576. $cells .= wf_TableCell($cameraCash);
  1577. $rows .= wf_TableRow($cells, 'row3');
  1578. $cells = wf_TableCell(__('Credit'), '30%', 'row2');
  1579. $cells .= wf_TableCell($cameraCredit);
  1580. $rows .= wf_TableRow($cells, 'row3');
  1581. $cells = wf_TableCell(__('Camera login'), '30%', 'row2');
  1582. $cells .= wf_TableCell($cameraData['camlogin']);
  1583. $rows .= wf_TableRow($cells, 'row3');
  1584. $cells = wf_TableCell(__('Camera password'), '30%', 'row2');
  1585. $cells .= wf_TableCell($camPass);
  1586. $rows .= wf_TableRow($cells, 'row3');
  1587. $cells = wf_TableCell(__('Port'), '30%', 'row2');
  1588. $cells .= wf_TableCell($cameraData['port']);
  1589. $rows .= wf_TableRow($cells, 'row3');
  1590. if (!empty($this->allDvrs[$cameraData['dvrid']]['name'])) {
  1591. $curCamDvrLabel = $this->allDvrs[$cameraData['dvrid']]['ip'] . ' - ' . $this->allDvrs[$cameraData['dvrid']]['name'];
  1592. } else {
  1593. $curCamDvrLabel = @$this->allDvrs[$cameraData['dvrid']]['ip'];
  1594. }
  1595. $cells = wf_TableCell(__('DVR'), '30%', 'row2');
  1596. $cells .= wf_TableCell($curCamDvrLabel);
  1597. $rows .= wf_TableRow($cells, 'row3');
  1598. $cells = wf_TableCell(__('DVR login'), '30%', 'row2');
  1599. $cells .= wf_TableCell($cameraData['dvrlogin']);
  1600. $rows .= wf_TableRow($cells, 'row3');
  1601. $cells = wf_TableCell(__('DVR password'), '30%', 'row2');
  1602. $cells .= wf_TableCell($dvrPass);
  1603. $rows .= wf_TableRow($cells, 'row3');
  1604. $result .= wf_TableBody($rows, '100%', 0);
  1605. $result .= wf_tag('br');
  1606. $inputs = '';
  1607. $inputs .= wf_HiddenInput('editcameraid', $cameraId);
  1608. $inputs .= wf_Selector('editvisorid', $usersTmp, __('User'), $cameraData['visorid'], true);
  1609. $loginPreset = (!empty($cameraData['camlogin'])) ? $cameraData['camlogin'] : 'admin';
  1610. $inputs .= wf_TextInput('editcamlogin', __('Camera login'), $loginPreset, true, 15);
  1611. $inputs .= wf_PasswordInput('editcampassword', __('Camera password'), $cameraData['campassword'], true, 15);
  1612. $portPreset = ($cameraData['port'] != 0) ? $cameraData['port'] : 80;
  1613. $inputs .= wf_TextInput('editport', __('Port'), $portPreset, true, 5);
  1614. $inputs .= wf_tag('br');
  1615. $inputs .= wf_Selector('editdvrid', $dvrTmp, __('DVR'), $cameraData['dvrid'], true);
  1616. $inputs .= wf_TextInput('editdvrlogin', __('DVR login'), $cameraData['dvrlogin'], true, 15);
  1617. $inputs .= wf_PasswordInput('editdvrpassword', __('DVR password'), $cameraData['dvrpassword'], true, 15);
  1618. $inputs .= wf_tag('br');
  1619. $inputs .= wf_Submit(__('Save'));
  1620. $cameraEditForm = wf_Form('', 'POST', $inputs, 'glamour');
  1621. $result .= wf_Link(self::URL_ME . self::URL_USERVIEW . $cameraData['visorid'], $this->iconVisorUser() . ' ' . __('Back to user profile'), false, 'ubButton');
  1622. if (cfr('VISOREDIT')) {
  1623. $result .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit'), __('Edit'), $cameraEditForm, 'ubButton');
  1624. $result .= wf_modalAuto(web_delete_icon() . ' ' . __('Delete'), __('Delete'), $this->renderCameraDeletionForm($cameraId), 'ubButton');
  1625. if ($this->wolfRecorderEnabled) {
  1626. $result .= $this->renderWolfRecorderCameraControls($cameraId);
  1627. }
  1628. if ($this->trassirEnabled) {
  1629. $result .= $this->renderTrassirCameraControls($cameraId);
  1630. }
  1631. }
  1632. } else {
  1633. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('User not exists') . ' (' . $cameraData['login'] . ')', 'error');
  1634. }
  1635. } else {
  1636. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('No such camera exists') . ' [' . $cameraId . ']', 'error');
  1637. }
  1638. return ($result);
  1639. }
  1640. /**
  1641. * Returns popular and most frequently used camera models for some protocol/vendor
  1642. *
  1643. * @param string $protocol
  1644. *
  1645. * @return array
  1646. */
  1647. protected function getPopularCameraModels($protocol) {
  1648. $result = array();
  1649. if (file_exists(self::PATH_MODELS . $protocol)) {
  1650. $allModels = rcms_scandir(self::PATH_MODELS . $protocol . '/');
  1651. if (!empty($allModels)) {
  1652. foreach ($allModels as $io => $each) {
  1653. $result[$each] = $each . ' *';
  1654. }
  1655. }
  1656. }
  1657. return ($result);
  1658. }
  1659. /**
  1660. * Returns camera "model mismatch" warning editing form. Also catches change requests.
  1661. *
  1662. * @param int $cameraId
  1663. * @param int $curState
  1664. *
  1665. * @return string
  1666. */
  1667. protected function renderTrassirCameraMismatchForm($cameraId, $curState) {
  1668. $result = '';
  1669. $cameraId = ubRouting::filters($cameraId, 'int');
  1670. if (isset($this->allCams[$cameraId])) {
  1671. $cameraData = $this->allCams[$cameraId];
  1672. $cameraDvrId = $cameraData['dvrid'];
  1673. $dvrData = $this->allDvrs[$cameraDvrId];
  1674. $cameraUserData = $this->allUserData[$cameraData['login']];
  1675. $cameraIp = $cameraUserData['ip'];
  1676. //change model mismatch warning request catched
  1677. if (ubRouting::checkPost('disablemodelmismatchcameraid')) {
  1678. $newDisableState = (ubRouting::checkPost('modelmismatchdisabled')) ? 1 : 0; //need int as param
  1679. $trassirGate = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  1680. $trassirGate->setModelMismatch($cameraIp, $newDisableState);
  1681. log_register('VISOR CAMERA [' . $cameraId . '] MMIS `' . $newDisableState . '` ON DVR [' . $cameraDvrId . '] AS `' . $cameraIp . '`');
  1682. ubRouting::nav(self::URL_ME . '&' . self::URL_CAMVIEW . $cameraId); //preventing form data duplication
  1683. }
  1684. if ($curState == 1 or $curState == 0) {
  1685. $inputs = wf_HiddenInput('disablemodelmismatchcameraid', $cameraId);
  1686. $inputs .= wf_CheckInput('modelmismatchdisabled', __('Model mismatch warning disabled on this DVR'), false, $curState);
  1687. $inputs .= wf_Submit(__('Save'));
  1688. $result .= wf_tag('br');
  1689. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1690. } else {
  1691. //may be caused by wrong camera IP or NVR connection issues
  1692. $result .= $this->messages->getStyledMessage(__('Cant detect mismatch warning state for camera') . ' ' . $cameraIp, 'warning'); // Awesome Oo
  1693. }
  1694. } else {
  1695. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Camera') . ' ' . __('Not exists') . ' [' . $cameraId . ']', 'error');
  1696. }
  1697. return ($result);
  1698. }
  1699. /**
  1700. * Rders camera DVR registering form if its not registered yet
  1701. *
  1702. * @param int $cameraId
  1703. *
  1704. * @return string
  1705. */
  1706. protected function renderTrassirCameraCreateForm($cameraId) {
  1707. $result = '';
  1708. $cameraId = ubRouting::filters($cameraId, 'int');
  1709. if (isset($this->allCams[$cameraId])) {
  1710. $cameraData = $this->allCams[$cameraId];
  1711. $cameraDvrId = $cameraData['dvrid'];
  1712. $dvrData = $this->allDvrs[$cameraDvrId];
  1713. $cameraUserData = $this->allUserData[$cameraData['login']];
  1714. $cameraIp = $cameraUserData['ip'];
  1715. $trassir = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  1716. $serverHealth = $trassir->getHealth();
  1717. //dummy connection check
  1718. if (!empty($serverHealth)) {
  1719. $result .= $this->messages->getStyledMessage(__('DVR') . ' ' . $dvrData['name'] . ': ' . __('Connected'), 'success');
  1720. $allCameraIps = $trassir->getAllCameraIps();
  1721. if (isset($allCameraIps[$cameraIp])) {
  1722. $successLabel = __('Camera') . ': ' . __('Registered') . ' ' . __('On') . ' ' . __('DVR') . ' ' . $dvrData['name'];
  1723. $result .= $this->messages->getStyledMessage($successLabel, 'success');
  1724. //Model mismatch disabling interface
  1725. $curMissmatchState = $trassir->getModelMismatch($cameraIp);
  1726. $result .= $this->renderTrassirCameraMismatchForm($cameraId, $curMissmatchState);
  1727. } else {
  1728. //here registering form.. MB...
  1729. $result .= $this->messages->getStyledMessage(__('Camera is not registered at') . ' ' . $dvrData['name'], 'warning');
  1730. $protoTmp = $trassir->getCameraProtocols();
  1731. if (!empty($protoTmp)) {
  1732. $supportedCameraProtocols = array('TRASSIR' => 'TRASSIR', 'Hikvision' => 'Hikvision'); //popular protocols
  1733. //Protocols received from DVR
  1734. foreach ($protoTmp as $io => $each) {
  1735. $supportedCameraProtocols[$each] = $each;
  1736. }
  1737. //camera registering form processing
  1738. if (!ubRouting::checkPost(array('newtrassircamera', 'newtrassircameraprotocol', 'newtrassircameramodel'))) {
  1739. $supportedCameraModels = array();
  1740. $newCamProtocol = (ubRouting::checkPost('newtrassircameraprotocol')) ? ubRouting::post('newtrassircameraprotocol') : '';
  1741. $inputs = wf_HiddenInput('newtrassircamera', 'true');
  1742. if (!empty($newCamProtocol)) {
  1743. //getting protocol supported models
  1744. $supportedCameraModelsTmp = $trassir->getCameraModels($newCamProtocol);
  1745. //Protocol is supported on NVR
  1746. if (!empty($supportedCameraModelsTmp)) {
  1747. $supportedCameraModels = $this->getPopularCameraModels($newCamProtocol); //frequently used models
  1748. }
  1749. $supportedCameraModels += $supportedCameraModelsTmp;
  1750. $inputs .= $newCamProtocol . ' ';
  1751. $inputs .= wf_HiddenInput('newtrassircameraprotocol', $newCamProtocol);
  1752. $inputs .= wf_Selector('newtrassircameramodel', $supportedCameraModels, __('Model'), '', false) . ' ';
  1753. $inputs .= wf_Submit(__('Create camera') . ' ' . __('on') . ' ' . __('DVR') . ' ' . $dvrData['name']);
  1754. } else {
  1755. $inputs .= wf_Selector('newtrassircameraprotocol', $supportedCameraProtocols, __('Device vendor'), '', false) . ' ';
  1756. $inputs .= wf_Submit(__('Continue'));
  1757. }
  1758. $result .= wf_delimiter();
  1759. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1760. } else {
  1761. //or just push that camera to DVR
  1762. $trassir->createCamera(ubRouting::post('newtrassircameraprotocol'), ubRouting::post('newtrassircameramodel'), $cameraIp, $cameraData['port'], $cameraData['camlogin'], $cameraData['campassword']);
  1763. log_register('VISOR CAMERA [' . $cameraId . '] CONNECTED DVR [' . $cameraDvrId . '] AS `' . $cameraIp . '`');
  1764. ubRouting::nav(self::URL_ME . '&' . self::URL_CAMVIEW . $cameraId); //preventing form data duplication
  1765. }
  1766. }
  1767. }
  1768. } else {
  1769. $result .= $this->messages->getStyledMessage(__('DVR connection error') . ' [' . $dvrData['id'] . ']', 'error');
  1770. }
  1771. } else {
  1772. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Camera') . ' ' . __('Not exists') . ' [' . $cameraId . ']', 'error');
  1773. }
  1774. return ($result);
  1775. }
  1776. /**
  1777. * Renders camera DVR registering form if its not registered yet
  1778. *
  1779. * @param int $cameraId
  1780. *
  1781. * @return string
  1782. */
  1783. protected function renderWolfRecorderCameraCreateForm($cameraId) {
  1784. $result = '';
  1785. $cameraId = ubRouting::filters($cameraId, 'int');
  1786. if (isset($this->allCams[$cameraId])) {
  1787. $cameraData = $this->allCams[$cameraId];
  1788. $cameraDvrId = $cameraData['dvrid'];
  1789. $dvrData = $this->allDvrs[$cameraDvrId];
  1790. $cameraUserData = $this->allUserData[$cameraData['login']];
  1791. $cameraIp = $cameraUserData['ip'];
  1792. $apiUrl = $this->getWolfRecorderApiUrl($dvrData['id']);
  1793. $wolfRecorder = new WolfRecorder($apiUrl, $dvrData['apikey']);
  1794. $isCameraRegistered = $wolfRecorder->camerasIsRegistered($cameraIp);
  1795. //dummy connection check
  1796. if ($wolfRecorder->noError($isCameraRegistered)) {
  1797. $result .= $this->messages->getStyledMessage(__('DVR') . ' ' . $dvrData['name'] . ': ' . __('Connected'), 'success');
  1798. if ($isCameraRegistered['registered']) {
  1799. $wrCameraId = '';
  1800. if (isset($isCameraRegistered['id'])) {
  1801. $wrCameraId = $isCameraRegistered['id'];
  1802. }
  1803. //nothing to do here
  1804. $successLabel = __('Camera') . ': ' . __('Registered') . ' ' . __('On') . ' ' . __('DVR') . ' ' . $dvrData['name'] . ' ' . __('as') . ' [' . $wrCameraId . ']';
  1805. $result .= $this->messages->getStyledMessage($successLabel, 'success');
  1806. //excepting recorder process check
  1807. if ($wrCameraId) {
  1808. $wrCameraId = $isCameraRegistered['id'];
  1809. $recorderIsRunning = $wolfRecorder->recordersIsRunning($wrCameraId);
  1810. if ($wolfRecorder->noError($recorderIsRunning)) {
  1811. $recState = ($recorderIsRunning['running']) ? true : false;
  1812. if ($recState) {
  1813. $result .= $this->messages->getStyledMessage(__('Recording now is running'), 'success');
  1814. } else {
  1815. $result .= $this->messages->getStyledMessage(__('Recording is not running'), 'warning');
  1816. }
  1817. }
  1818. }
  1819. } else {
  1820. //here registering form.. MB...
  1821. $result .= $this->messages->getStyledMessage(__('Camera is not registered at') . ' ' . $dvrData['name'], 'warning');
  1822. $modelsTmp = $wolfRecorder->modelsGetAll();
  1823. if (!empty($modelsTmp)) {
  1824. $supportedCameraModels = array();
  1825. //models received from DVR
  1826. foreach ($modelsTmp as $io => $each) {
  1827. $supportedCameraModels[$each['id']] = $each['modelname'];
  1828. }
  1829. //camera registering form processing
  1830. if (!ubRouting::checkPost(array('newwolfrecordercamera', 'newwolfrecordercameramodel'))) {
  1831. $storagesTmp = $wolfRecorder->storagesGetAll();
  1832. if (!empty($storagesTmp)) {
  1833. $availableStorages = array(0 => __('Auto'));
  1834. foreach ($storagesTmp as $io => $each) {
  1835. $availableStorages[$each['id']] = __($each['name']);
  1836. }
  1837. $inputs = wf_HiddenInput('newwolfrecordercamera', 'true');
  1838. $inputs .= wf_Selector('newwolfrecordercameramodel', $supportedCameraModels, __('Model'), '', false) . ' ';
  1839. $inputs .= wf_Selector('newwolfrecordercamerastorage', $availableStorages, __('Storage'), '', false) . ' ';
  1840. $inputs .= wf_Submit(__('Create camera') . ' ' . __('on') . ' ' . __('DVR') . ' ' . $dvrData['name']);
  1841. $result .= wf_delimiter();
  1842. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1843. } else {
  1844. $result .= $this->messages->getStyledMessage(__('Storages is not available'), 'error');
  1845. }
  1846. } else {
  1847. //or just push that camera to DVR
  1848. $newCamStorageId = (ubRouting::checkPost('newwolfrecordercamerastorage')) ? ubRouting::post('newwolfrecordercamerastorage', 'int') : 0; //explict storage?
  1849. $newCamAct = 1; //enabled by default
  1850. $newCamDesc = zb_UserGetFullAddress($cameraData['login']); //address as default decription
  1851. $wolfRecorder->camerasCreate(ubRouting::post('newwolfrecordercameramodel'), $cameraIp, $cameraData['camlogin'], $cameraData['campassword'], $newCamAct, $newCamStorageId, $newCamDesc);
  1852. log_register('VISOR CAMERA [' . $cameraId . '] CONNECTED DVR [' . $cameraDvrId . '] AS `' . $cameraIp . '`');
  1853. ubRouting::nav(self::URL_ME . '&' . self::URL_CAMVIEW . $cameraId); //preventing form data duplication
  1854. }
  1855. } else {
  1856. $result .= $this->messages->getStyledMessage(__('Models') . ' ' . __('is empty'), 'error');
  1857. }
  1858. }
  1859. } else {
  1860. $result .= $this->messages->getStyledMessage(__('DVR connection error') . ' [' . $dvrData['id'] . ']', 'error');
  1861. }
  1862. } else {
  1863. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Camera') . ' ' . __('Not exists') . ' [' . $cameraId . ']', 'error');
  1864. }
  1865. return ($result);
  1866. }
  1867. /**
  1868. * Renders IP device controls if camera is served by trassir based DVR
  1869. *
  1870. * @param int $cameraId
  1871. *
  1872. * @return string
  1873. */
  1874. protected function renderTrassirCameraControls($cameraId) {
  1875. $result = '';
  1876. $cameraId = ubRouting::filters($cameraId, 'int');
  1877. if (isset($this->allCams[$cameraId])) {
  1878. $cameraData = $this->allCams[$cameraId];
  1879. $cameraDvrId = $cameraData['dvrid'];
  1880. //DVR assigned
  1881. if ($cameraDvrId) {
  1882. if (isset($this->allDvrs[$cameraDvrId])) {
  1883. $dvrData = $this->allDvrs[$cameraDvrId];
  1884. //Here we go! That DVR can be managable
  1885. if ($dvrData['type'] == 'trassir') {
  1886. if (!empty($cameraData['camlogin'])) {
  1887. if (!empty($cameraData['campassword'])) {
  1888. if (!empty($cameraData['port'])) {
  1889. if (isset($this->allUserData[$cameraData['login']])) {
  1890. //DVD configuration is acceptable?
  1891. if (!empty($dvrData['login']) and !empty($dvrData['password']) and !empty($dvrData['port']) and !empty($dvrData['apikey'])) {
  1892. //Camera looks like it may be registgered on DVR
  1893. $result .= $this->renderTrassirCameraCreateForm($cameraId);
  1894. } else {
  1895. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('DVR') . ' ' . __('Configuration') . ' ' . __('is empty'), 'error');
  1896. }
  1897. } else {
  1898. $result .= $this->messages->getStyledMessage(__('Camera') . ' ' . __('User') . ' ' . __('Not exists') . ' (' . $cameraData['login'] . ')', 'error');
  1899. }
  1900. } else {
  1901. $result .= $this->messages->getStyledMessage(__('Camera') . ' ' . __('Port') . ' ' . __('is empty'), 'error');
  1902. }
  1903. } else {
  1904. $result .= $this->messages->getStyledMessage(__('Camera password') . ' ' . __('is empty'), 'error');
  1905. }
  1906. } else {
  1907. $result .= $this->messages->getStyledMessage(__('Camera login') . ' ' . __('is empty'), 'error');
  1908. }
  1909. }
  1910. } else {
  1911. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('DVR') . ' ' . __('Not exists') . ' [' . $cameraDvrId . ']', 'error');
  1912. }
  1913. }
  1914. } else {
  1915. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Camera') . ' ' . __('Not exists') . ' [' . $cameraId . ']', 'error');
  1916. }
  1917. return ($result);
  1918. }
  1919. /**
  1920. * Renders IP device controls if camera is served by WolfRecorder NVR
  1921. *
  1922. * @param int $cameraId
  1923. *
  1924. * @return string
  1925. */
  1926. protected function renderWolfRecorderCameraControls($cameraId) {
  1927. $result = '';
  1928. $cameraId = ubRouting::filters($cameraId, 'int');
  1929. if (isset($this->allCams[$cameraId])) {
  1930. $cameraData = $this->allCams[$cameraId];
  1931. $cameraDvrId = $cameraData['dvrid'];
  1932. //DVR assigned
  1933. if ($cameraDvrId) {
  1934. if (isset($this->allDvrs[$cameraDvrId])) {
  1935. $dvrData = $this->allDvrs[$cameraDvrId];
  1936. //Here we go! That DVR can be managable
  1937. if ($dvrData['type'] == 'wolfrecorder') {
  1938. if (!empty($cameraData['camlogin'])) {
  1939. if (!empty($cameraData['campassword'])) {
  1940. if (!empty($cameraData['port'])) {
  1941. if (isset($this->allUserData[$cameraData['login']])) {
  1942. //DVD configuration is acceptable?
  1943. if (!empty($dvrData['login']) and !empty($dvrData['password']) and !empty($dvrData['port']) and !empty($dvrData['apikey'])) {
  1944. //Camera looks like it may be registgered on DVR
  1945. $result .= $this->renderWolfRecorderCameraCreateForm($cameraId);
  1946. } else {
  1947. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('DVR') . ' ' . __('Configuration') . ' ' . __('is empty'), 'error');
  1948. }
  1949. } else {
  1950. $result .= $this->messages->getStyledMessage(__('Camera') . ' ' . __('User') . ' ' . __('Not exists') . ' (' . $cameraData['login'] . ')', 'error');
  1951. }
  1952. } else {
  1953. $result .= $this->messages->getStyledMessage(__('Camera') . ' ' . __('Port') . ' ' . __('is empty'), 'error');
  1954. }
  1955. } else {
  1956. $result .= $this->messages->getStyledMessage(__('Camera password') . ' ' . __('is empty'), 'error');
  1957. }
  1958. } else {
  1959. $result .= $this->messages->getStyledMessage(__('Camera login') . ' ' . __('is empty'), 'error');
  1960. }
  1961. }
  1962. } else {
  1963. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('DVR') . ' ' . __('Not exists') . ' [' . $cameraDvrId . ']', 'error');
  1964. }
  1965. }
  1966. } else {
  1967. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Camera') . ' ' . __('Not exists') . ' [' . $cameraId . ']', 'error');
  1968. }
  1969. return ($result);
  1970. }
  1971. /**
  1972. * Catches camera editing request and saves data if required
  1973. *
  1974. * @return void
  1975. */
  1976. public function saveCamera() {
  1977. if (wf_CheckPost(array('editcameraid'))) {
  1978. $cameraId = ubRouting::post('editcameraid', 'int');
  1979. if (isset($this->allCams[$cameraId])) {
  1980. $changedFlag = false;
  1981. $cameraData = $this->allCams[$cameraId];
  1982. $where = " WHERE `id`='" . $cameraId . "'";
  1983. $newVisorId = ubRouting::post('editvisorid', 'int');
  1984. $newCamLogin = ubRouting::post('editcamlogin', 'mres');
  1985. $newCamPassword = ubRouting::post('editcampassword', 'mres');
  1986. $newPort = ubRouting::post('editport', 'int');
  1987. $newDvrId = ubRouting::post('editdvrid', 'int');
  1988. $newDvrLogin = ubRouting::post('editdvrlogin', 'mres');
  1989. $newDvrPassword = ubRouting::post('editdvrpassword', 'mres');
  1990. if ($newVisorId != $cameraData['visorid']) {
  1991. $changedFlag = true;
  1992. $this->camsDb->data('visorid', $newVisorId);
  1993. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE ASSIGN [' . $newVisorId . ']');
  1994. }
  1995. if ($newCamLogin != $cameraData['camlogin']) {
  1996. $changedFlag = true;
  1997. $this->camsDb->data('camlogin', $newCamLogin);
  1998. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE LOGIN `' . $newCamLogin . '`');
  1999. }
  2000. if ($newCamPassword != $cameraData['campassword']) {
  2001. $changedFlag = true;
  2002. $this->camsDb->data('campassword', $newCamPassword);
  2003. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE PASSWORD `' . $newCamPassword . '`');
  2004. }
  2005. if ($newPort != $cameraData['port']) {
  2006. $changedFlag = true;
  2007. $this->camsDb->data('port', $newPort);
  2008. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE PORT `' . $newPort . '`');
  2009. }
  2010. if ($newDvrId != $cameraData['dvrid']) {
  2011. $changedFlag = true;
  2012. $this->camsDb->data('dvrid', $newDvrId);
  2013. if (!empty($newDvrId)) {
  2014. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE DVR [' . $newDvrId . ']');
  2015. } else {
  2016. log_register('VISOR CAMERA [' . $cameraId . '] UNSET DVR');
  2017. }
  2018. }
  2019. if ($newDvrLogin != $cameraData['dvrlogin']) {
  2020. $changedFlag = true;
  2021. $this->camsDb->data('dvrlogin', $newDvrLogin);
  2022. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE DVRLOGIN `' . $newDvrLogin . '`');
  2023. }
  2024. if ($newDvrLogin != $cameraData['dvrpassword']) {
  2025. $changedFlag = true;
  2026. $this->camsDb->data('dvrpassword', $newDvrPassword);
  2027. log_register('VISOR CAMERA [' . $cameraId . '] CHANGE DVRPASSWORD `' . $newDvrPassword . '`');
  2028. }
  2029. //commiting changes to DB
  2030. if ($changedFlag) {
  2031. $this->camsDb->where('id', '=', $cameraId);
  2032. $this->camsDb->save();
  2033. }
  2034. }
  2035. }
  2036. }
  2037. /**
  2038. * Renders DVR creation form
  2039. *
  2040. * @return string
  2041. */
  2042. protected function renderDVRsCreateForm() {
  2043. $result = '';
  2044. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  2045. $inputs = wf_HiddenInput('newdvr', 'true');
  2046. $inputs .= wf_TextInput('newdvrname', __('Name'), '', true, 15);
  2047. $inputs .= wf_Selector('newdvrtype', $this->dvrTypes, __('Type'), '', true);
  2048. $inputs .= wf_TextInput('newdvrip', __('IP') . $sup, '', true, 15, 'ip');
  2049. $inputs .= wf_TextInput('newdvrport', __('Port'), '', true, 5, 'digits');
  2050. $inputs .= wf_TextInput('newdvrlogin', __('Login'), '', true, 20);
  2051. $inputs .= wf_PasswordInput('newdvrpassword', __('Password'), '', true, 20);
  2052. $inputs .= wf_TextInput('newdvrapiurl', __('API URL'), '', true, 20, 'url');
  2053. $inputs .= wf_PasswordInput('newdvrapikey', __('API key'), '', true, 20);
  2054. $inputs .= wf_TextInput('newdvrcamlimit', __('Cameras limit'), '0', true, 3, 'digits');
  2055. $inputs .= wf_TextInput('newdvrcustomurl', __('Custom preview URL'), '', true, 20);
  2056. $inputs .= wf_Submit(__('Create'));
  2057. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  2058. return ($result);
  2059. }
  2060. /**
  2061. * Catches new DVR creation request/performs new DVR registering
  2062. *
  2063. * @return void
  2064. */
  2065. public function createDVR() {
  2066. if (ubRouting::checkPost(array('newdvr', 'newdvrip'))) {
  2067. $ip = ubRouting::post('newdvrip');
  2068. $ip_f = ubRouting::filters($ip, 'mres');
  2069. $port = ubRouting::post('newdvrport', 'int');
  2070. $login = ubRouting::post('newdvrlogin', 'mres');
  2071. $password = ubRouting::post('newdvrpassword', 'mres');
  2072. $name = ubRouting::post('newdvrname', 'mres');
  2073. $type = ubRouting::post('newdvrtype', 'mres');
  2074. $apiurl = ubRouting::post('newdvrapiurl', 'mres');
  2075. $apikey = ubRouting::post('newdvrapikey', 'mres');
  2076. $camlimit = ubRouting::post('newdvrcamlimit', 'int');
  2077. $customurl = ubRouting::post('newdvrcustomurl', 'mres');
  2078. $this->dvrsDb->data('ip', $ip_f);
  2079. $this->dvrsDb->data('port', $port);
  2080. $this->dvrsDb->data('login', $login);
  2081. $this->dvrsDb->data('password', $password);
  2082. $this->dvrsDb->data('apiurl', $apiurl);
  2083. $this->dvrsDb->data('apikey', $apikey);
  2084. $this->dvrsDb->data('name', $name);
  2085. $this->dvrsDb->data('type', $type);
  2086. $this->dvrsDb->data('camlimit', $camlimit);
  2087. $this->dvrsDb->data('customurl', $customurl);
  2088. $this->dvrsDb->create();
  2089. $newId = $this->dvrsDb->getLastId();
  2090. log_register('VISOR DVR CREATE [' . $newId . '] IP `' . $ip . '`');
  2091. }
  2092. }
  2093. /**
  2094. * Renders DVR editing form
  2095. *
  2096. * @param int $dvrId
  2097. *
  2098. * @return string
  2099. */
  2100. protected function renderDVREditForm($dvrId) {
  2101. $dvrId = vf($dvrId, 3);
  2102. $result = '';
  2103. if (isset($this->allDvrs[$dvrId])) {
  2104. $dvrData = $this->allDvrs[$dvrId];
  2105. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  2106. $inputs = wf_HiddenInput('editdvrid', $dvrId);
  2107. $inputs .= wf_TextInput('editdvrname', __('Name'), $dvrData['name'], true, 15);
  2108. $inputs .= wf_Selector('editdvrtype', $this->dvrTypes, __('Type'), $dvrData['type'], true);
  2109. $inputs .= wf_TextInput('editdvrip', __('IP') . $sup, $dvrData['ip'], true, 15, 'ip');
  2110. $inputs .= wf_TextInput('editdvrport', __('Port'), $dvrData['port'], true, 5, 'digits');
  2111. $inputs .= wf_TextInput('editdvrlogin', __('Login'), $dvrData['login'], true, 12);
  2112. $inputs .= wf_PasswordInput('editdvrpassword', __('Password'), $dvrData['password'], true, 12);
  2113. $inputs .= wf_TextInput('editdvrapiurl', __('API URL'), $dvrData['apiurl'], true, 20, 'url');
  2114. $inputs .= wf_PasswordInput('editdvrapikey', __('API key'), $dvrData['apikey'], true, 20);
  2115. $inputs .= wf_TextInput('editdvrcamlimit', __('Cameras limit'), $dvrData['camlimit'], true, 20);
  2116. $inputs .= wf_TextInput('editdvrcustomurl', __('Custom preview URL'), $dvrData['customurl'], true, 20);
  2117. $inputs .= wf_tag('br');
  2118. $inputs .= wf_Submit(__('Save'));
  2119. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  2120. } else {
  2121. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('No such DVR exists'), 'error');
  2122. }
  2123. return ($result);
  2124. }
  2125. /**
  2126. * Catches DVR modification request and saves new data to database if it was changed
  2127. *
  2128. * @return void
  2129. */
  2130. public function saveDVR() {
  2131. if (ubRouting::checkPost(array('editdvrid', 'editdvrip'))) {
  2132. $dvrId = ubRouting::post('editdvrid', 'int');
  2133. if (isset($this->allDvrs[$dvrId])) {
  2134. $saveRequired = false;
  2135. $dvrData = $this->allDvrs[$dvrId];
  2136. $where = " WHERE `id`='" . $dvrId . "'";
  2137. $newIp = ubRouting::post('editdvrip', 'mres');
  2138. $newPort = ubRouting::post('editdvrport', 'int');
  2139. $newLogin = ubRouting::post('editdvrlogin', 'mres');
  2140. $newPassword = ubRouting::post('editdvrpassword', 'mres');
  2141. $newName = ubRouting::post('editdvrname', 'mres');
  2142. $newType = ubRouting::post('editdvrtype', 'mres');
  2143. $newApiurl = ubRouting::post('editdvrapiurl', 'mres');
  2144. $newApikey = ubRouting::post('editdvrapikey', 'mres');
  2145. $newCamlimit = ubRouting::post('editdvrcamlimit', 'int');
  2146. $newCustomUrl = ubRouting::post('editdvrcustomurl', 'mres');
  2147. if ($dvrData['ip'] != $newIp) {
  2148. $saveRequired = true;
  2149. $this->dvrsDb->data('ip', $newIp);
  2150. log_register('VISOR DVR [' . $dvrId . '] CHANGE IP `' . $newIp . '`');
  2151. }
  2152. if ($dvrData['port'] != $newPort) {
  2153. $saveRequired = true;
  2154. $this->dvrsDb->data('port', $newPort);
  2155. log_register('VISOR DVR [' . $dvrId . '] CHANGE PORT `' . $newPort . '`');
  2156. }
  2157. if ($dvrData['login'] != $newLogin) {
  2158. $saveRequired = true;
  2159. $this->dvrsDb->data('login', $newLogin);
  2160. log_register('VISOR DVR [' . $dvrId . '] CHANGE LOGIN `' . $newLogin . '`');
  2161. }
  2162. if ($dvrData['password'] != $newPassword) {
  2163. $saveRequired = true;
  2164. $this->dvrsDb->data('password', $newPassword);
  2165. log_register('VISOR DVR [' . $dvrId . '] CHANGE PASSWORD `' . $newPassword . '`');
  2166. }
  2167. if ($dvrData['name'] != $newName) {
  2168. $saveRequired = true;
  2169. $this->dvrsDb->data('name', $newName);
  2170. log_register('VISOR DVR [' . $dvrId . '] CHANGE NAME `' . $newName . '`');
  2171. }
  2172. if ($dvrData['type'] != $newType) {
  2173. $saveRequired = true;
  2174. $this->dvrsDb->data('type', $newType);
  2175. log_register('VISOR DVR [' . $dvrId . '] CHANGE TYPE `' . $newType . '`');
  2176. }
  2177. if ($dvrData['apiurl'] != $newApiurl) {
  2178. $saveRequired = true;
  2179. $this->dvrsDb->data('apiurl', $newApiurl);
  2180. log_register('VISOR DVR [' . $dvrId . '] CHANGE APIURL `' . $newApiurl . '`');
  2181. }
  2182. if ($dvrData['apikey'] != $newApikey) {
  2183. $saveRequired = true;
  2184. $this->dvrsDb->data('apikey', $newApikey);
  2185. log_register('VISOR DVR [' . $dvrId . '] CHANGE APIKEY `' . $newApikey . '`');
  2186. }
  2187. if ($dvrData['camlimit'] != $newCamlimit) {
  2188. $saveRequired = true;
  2189. $this->dvrsDb->data('camlimit', $newCamlimit);
  2190. log_register('VISOR DVR [' . $dvrId . '] CHANGE CAMLIMIT `' . $newCamlimit . '`');
  2191. }
  2192. if ($dvrData['customurl'] != $newCustomUrl) {
  2193. $saveRequired = true;
  2194. $this->dvrsDb->data('customurl', $newCustomUrl);
  2195. log_register('VISOR DVR [' . $dvrId . '] CHANGE CUSTOMURL `' . $newCustomUrl . '`');
  2196. }
  2197. if ($saveRequired) {
  2198. $this->dvrsDb->where('id', '=', $dvrId);
  2199. $this->dvrsDb->save();
  2200. }
  2201. }
  2202. }
  2203. }
  2204. /**
  2205. * Returns count of cameras (channels) registered on some existing DVR
  2206. *
  2207. * @param int $dvrId
  2208. *
  2209. * @return int
  2210. */
  2211. protected function getDvrCameraCount($dvrId) {
  2212. $result = 0;
  2213. if (!empty($this->allCams)) {
  2214. foreach ($this->allCams as $io => $each) {
  2215. if ($each['dvrid'] == $dvrId) {
  2216. $result++;
  2217. }
  2218. }
  2219. }
  2220. return ($result);
  2221. }
  2222. /**
  2223. * Renders tariffs changes report based on DDT log
  2224. *
  2225. * @return string
  2226. */
  2227. public function renderTariffChangesReport() {
  2228. $result = '';
  2229. $curMonth = curmonth();
  2230. if (@$this->altCfg['DDT_ENABLED']) {
  2231. $ddtDb = new NyanORM('ddt_users');
  2232. $allDoomedUsers = $ddtDb->getAll();
  2233. $reportTmp = array();
  2234. if (!empty($allDoomedUsers)) {
  2235. foreach ($allDoomedUsers as $io => $each) {
  2236. if (ispos($each['enddate'], $curMonth)) {
  2237. if ($this->getCameraIdByLogin($each['login'])) {
  2238. $reportTmp[$io] = $each;
  2239. }
  2240. }
  2241. }
  2242. //rendering report
  2243. if (!empty($reportTmp)) {
  2244. $cells = wf_TableCell(__('Camera'));
  2245. $cells .= wf_TableCell(__('Tariff'));
  2246. $cells .= wf_TableCell(__('End date'));
  2247. $cells .= wf_TableCell(__('New tariff'));
  2248. $cells .= wf_TableCell(__('User'));
  2249. $rows = wf_TableRow($cells, 'row1');
  2250. foreach ($reportTmp as $io => $each) {
  2251. $cameraUserId = $this->getCameraUser($each['login']);
  2252. $cameraUserData = @$this->allUserData[$each['login']];
  2253. $visorLinkLabel = $this->iconVisorUser() . ' ' . @$this->allUsers[$cameraUserId]['realname'];
  2254. $visorUserLink = wf_Link(self::URL_ME . self::URL_USERVIEW . $cameraUserId, $visorLinkLabel);
  2255. $cameraLinkLabel = web_profile_icon() . ' ' . @$cameraUserData['fulladress'];
  2256. $cameraLink = wf_Link(self::URL_CAMPROFILE . $each['login'], $cameraLinkLabel);
  2257. $cells = wf_TableCell($cameraLink);
  2258. $cells .= wf_TableCell($each['curtariff']);
  2259. $cells .= wf_TableCell($each['enddate']);
  2260. $cells .= wf_TableCell($each['nexttariff']);
  2261. $cells .= wf_TableCell($visorUserLink);
  2262. $rows .= wf_TableRow($cells, 'row5');
  2263. }
  2264. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  2265. } else {
  2266. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'success');
  2267. }
  2268. } else {
  2269. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  2270. }
  2271. } else {
  2272. $result .= $this->messages->getStyledMessage(__('This module is disabled') . ': ' . __('Doomsday tariffs'), 'error');
  2273. }
  2274. return ($result);
  2275. }
  2276. /**
  2277. * Returns usable WolfRecorder API URL depends on existing DVR settings
  2278. *
  2279. * @param int $dvrId
  2280. *
  2281. * @return string
  2282. */
  2283. protected function getWolfRecorderApiUrl($dvrId) {
  2284. $result = '';
  2285. if (isset($this->allDvrs[$dvrId])) {
  2286. $dvrData = $this->allDvrs[$dvrId];
  2287. //just use explict URL?
  2288. if (!empty($dvrData['apiurl'])) {
  2289. $result = $dvrData['apiurl'];
  2290. } else {
  2291. //try to guess
  2292. $proto = 'http://';
  2293. $port = ($dvrData['port']) ? ':' . $dvrData['port'] . '/' : '/';
  2294. $host = $dvrData['ip'];
  2295. $defaultUrl = 'wr/';
  2296. $result = $proto . $host . $port . $defaultUrl;
  2297. }
  2298. }
  2299. return ($result);
  2300. }
  2301. /**
  2302. * Renders available DVRs health report
  2303. *
  2304. * @return string
  2305. */
  2306. public function renderDVRsHealth() {
  2307. $result = '';
  2308. if (!empty($this->allDvrs)) {
  2309. $cells = wf_TableCell(__('ID'));
  2310. $cells .= wf_TableCell(__('IP'));
  2311. $cells .= wf_TableCell(__('Name'));
  2312. $cells .= wf_TableCell(__('Disks'));
  2313. $cells .= wf_TableCell(__('Database'));
  2314. $cells .= wf_TableCell(__('Network'));
  2315. $cells .= wf_TableCell(__('Channels') . ' / ' . __('Online'));
  2316. $cells .= wf_TableCell(__('Uptime'));
  2317. $cells .= wf_TableCell(__('CPU load'));
  2318. $cells .= wf_TableCell(__('Archive days'));
  2319. $rows = wf_TableRowStyled($cells, 'row1');
  2320. foreach ($this->allDvrs as $io => $each) {
  2321. if ($each['type'] == 'trassir') {
  2322. if (!empty($each['ip']) and !empty($each['login']) and !empty($each['password']) and !empty($each['apikey']) and !empty($each['port'])) {
  2323. $dvrGate = new TrassirServer($each['ip'], $each['login'], $each['password'], $each['apikey'], $each['port'], $this->trassirDebug);
  2324. $health = $dvrGate->getHealth();
  2325. $cells = wf_TableCell($each['id']);
  2326. $cells .= wf_TableCell($each['ip']);
  2327. $cells .= wf_TableCell($each['name']);
  2328. $cells .= wf_TableCell(web_bool_led($health['disks']));
  2329. $cells .= wf_TableCell(web_bool_led($health['database']));
  2330. $cells .= wf_TableCell(web_bool_led($health['network']));
  2331. $cells .= wf_TableCell($health['channels_total'] . ' / ' . $health['channels_online']);
  2332. $cells .= wf_TableCell(zb_formatTime($health['uptime']));
  2333. $cells .= wf_TableCell($health['cpu_load'] . '%');
  2334. $cells .= wf_TableCell($health['disks_stat_main_days'] . ' / ' . $health['disks_stat_subs_days']);
  2335. $rows .= wf_TableRow($cells, 'row5');
  2336. }
  2337. }
  2338. if ($each['type'] == 'wolfrecorder') {
  2339. if (!empty($each['ip']) and !empty($each['apikey'])) {
  2340. $apiUrl = $this->getWolfRecorderApiUrl($each['id']);
  2341. $dvrGate = new WolfRecorder($apiUrl, $each['apikey']);
  2342. if ($dvrGate->connectionOk()) {
  2343. $health = $dvrGate->systemGetHealth();
  2344. //>= 0.1.0
  2345. if (isset($health['cpuload'])) {
  2346. $uptime = zb_formatTime($health['uptime']);
  2347. $cpuLoad = $health['cpuload'] . ' %';
  2348. } else {
  2349. //0.0.9 legacy
  2350. $uptime = $health['uptime'];
  2351. $cpuLoad = $health['loadavg'] . ' LA';
  2352. }
  2353. $cells = wf_TableCell($each['id']);
  2354. $cells .= wf_TableCell($each['ip']);
  2355. $cells .= wf_TableCell($each['name']);
  2356. $cells .= wf_TableCell(web_bool_led($health['storages']));
  2357. $cells .= wf_TableCell(web_bool_led($health['database']));
  2358. $cells .= wf_TableCell(web_bool_led($health['network']));
  2359. $cells .= wf_TableCell($health['channels_total'] . ' / ' . $health['channels_online']);
  2360. $cells .= wf_TableCell($uptime);
  2361. $cells .= wf_TableCell($cpuLoad);
  2362. $cells .= wf_TableCell('-');
  2363. $rows .= wf_TableRow($cells, 'row5');
  2364. } else {
  2365. $failLabel = __('Connection') . ' ' . __('Failed');
  2366. $cells = wf_TableCell($each['id']);
  2367. $cells .= wf_TableCell($each['ip']);
  2368. $cells .= wf_TableCell($each['name']);
  2369. $cells .= wf_TableCell($failLabel);
  2370. $cells .= wf_TableCell($failLabel);
  2371. $cells .= wf_TableCell($failLabel);
  2372. $cells .= wf_TableCell($failLabel);
  2373. $cells .= wf_TableCell($failLabel);
  2374. $cells .= wf_TableCell($failLabel);
  2375. $cells .= wf_TableCell('-');
  2376. $rows .= wf_TableRow($cells, 'row5');
  2377. }
  2378. }
  2379. }
  2380. }
  2381. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  2382. } else {
  2383. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  2384. }
  2385. return ($result);
  2386. }
  2387. /**
  2388. * Renders existing DVRs list wit some controls
  2389. *
  2390. * @return string
  2391. */
  2392. public function renderDVRsList() {
  2393. $result = '';
  2394. if (!empty($this->allDvrs)) {
  2395. $cells = wf_TableCell(__('ID'));
  2396. $cells .= wf_TableCell(__('Name'));
  2397. $cells .= wf_TableCell(__('IP'));
  2398. $cells .= wf_TableCell(__('Port'));
  2399. $cells .= wf_TableCell(__('Cameras'));
  2400. $cells .= wf_TableCell(__('Actions'));
  2401. $rows = wf_TableRow($cells, 'row1');
  2402. foreach ($this->allDvrs as $io => $each) {
  2403. $cells = wf_TableCell($each['id']);
  2404. $cells .= wf_TableCell($each['name']);
  2405. $cells .= wf_TableCell($each['ip']);
  2406. $cells .= wf_TableCell($each['port']);
  2407. $cells .= wf_TableCell($this->getDvrCameraCount($each['id']) . ' / ' . $each['camlimit']);
  2408. $actLinks = wf_JSAlert(self::URL_ME . self::URL_DELDVR . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  2409. $actLinks .= wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . $each['ip'], $this->renderDVREditForm($each['id']));
  2410. $cells .= wf_TableCell($actLinks);
  2411. $rows .= wf_TableRow($cells, 'row5');
  2412. }
  2413. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  2414. } else {
  2415. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  2416. }
  2417. $result .= wf_delimiter();
  2418. $result .= wf_modalAuto(wf_img('skins/ukv/add.png') . ' ' . __('Create'), __('Create'), $this->renderDVRsCreateForm(), 'ubButton');
  2419. return ($result);
  2420. }
  2421. /**
  2422. * Checks is DVR used by some existing cameras
  2423. *
  2424. * @param int $dvrId
  2425. *
  2426. * @return bool
  2427. */
  2428. protected function isDVRProtected($dvrId) {
  2429. $dvrId = ubRouting::filters($dvrId, 'int');
  2430. $result = false;
  2431. if (!empty($this->allCams)) {
  2432. foreach ($this->allCams as $io => $each) {
  2433. if ($each['dvrid'] == $dvrId) {
  2434. $result = true;
  2435. }
  2436. }
  2437. }
  2438. return ($result);
  2439. }
  2440. /**
  2441. * Deletes existing DVR from database
  2442. *
  2443. * @param int $dvrId
  2444. *
  2445. * @return void/string on error
  2446. */
  2447. public function deleteDVR($dvrId) {
  2448. $dvrId = ubRouting::filters($dvrId, 'int');
  2449. $result = '';
  2450. if (isset($this->allDvrs[$dvrId])) {
  2451. if (!$this->isDVRProtected($dvrId)) {
  2452. $dvrData = $this->allDvrs[$dvrId];
  2453. $this->dvrsDb->where('id', '=', $dvrId);
  2454. $this->dvrsDb->delete();
  2455. log_register('VISOR DVR DELETE [' . $dvrId . '] IP `' . $dvrData['ip'] . '`');
  2456. } else {
  2457. $result .= __('Something went wrong') . ': ' . __('This DVR is used for some cameras');
  2458. log_register('VISOR DVR DELETE [' . $dvrId . '] TRY');
  2459. }
  2460. } else {
  2461. $result .= __('Something went wrong') . ': ' . __('No such DVR exists') . ' [' . $dvrId . ']';
  2462. }
  2463. return ($result);
  2464. }
  2465. /**
  2466. * Renders preview of channels from all Trassir based DVRs
  2467. *
  2468. * @return string
  2469. */
  2470. public function renderChannelsPreview() {
  2471. $result = '';
  2472. $chanCount = 0;
  2473. //chan controls here
  2474. $result .= wf_Link(self::URL_ME . self::URL_CHANS, web_yellow_led() . ' ' . __('No user assigned'), false, 'ubButton') . ' ';
  2475. $result .= wf_Link(self::URL_ME . self::URL_CHANS . '&allchannels=true', web_green_led() . ' ' . __('All channels'), false, 'ubButton') . ' ';
  2476. $result .= wf_delimiter();
  2477. $allFlag = (ubRouting::checkGet('allchannels')) ? true : false;
  2478. if (!empty($this->allDvrs)) {
  2479. $result .= wf_tag('div', false, '');
  2480. foreach ($this->allDvrs as $io => $eachDvr) {
  2481. if ($eachDvr['type'] == 'trassir') {
  2482. $dvrGate = new TrassirServer($eachDvr['ip'], $eachDvr['login'], $eachDvr['password'], $eachDvr['apikey'], $eachDvr['port'], $this->trassirDebug);
  2483. $serverHealth = $dvrGate->getHealth();
  2484. if (!empty($serverHealth)) {
  2485. if (isset($serverHealth['channels_health'])) {
  2486. $dvrChannels = $serverHealth['channels_health'];
  2487. if (!empty($dvrChannels)) {
  2488. foreach ($dvrChannels as $ia => $eachChan) {
  2489. $renderChannel = false;
  2490. if ($allFlag) {
  2491. $renderChannel = true;
  2492. } else {
  2493. if (!isset($this->channelUsers[$eachChan['guid']])) {
  2494. $renderChannel = true;
  2495. }
  2496. }
  2497. if ($renderChannel) {
  2498. $streamUrl = $dvrGate->getLiveVideoStream($eachChan['guid'], 'main', $this->chanPreviewContainer, $this->chanPreviewQuality, $this->chanPreviewFramerate, $eachDvr['customurl']);
  2499. $result .= wf_tag('div', false, 'whiteboard', 'style="width:' . $this->chanPreviewSize . ';"');
  2500. $channelEditControl = wf_Link(self::URL_ME . self::URL_CHANEDIT . $eachChan['guid'] . '&dvrid=' . $eachDvr['id'], web_edit_icon(__('Edit') . ' ' . __('channel')));
  2501. $result .= $eachChan['name'] . ' / ' . $eachChan['guid'] . ' @ ' . $eachDvr['id'];
  2502. $result .= wf_tag('br');
  2503. $result .= wf_tag('div', false, '', 'style="overflow:hidden; height:220px; max-height:250px;"');
  2504. $result .= $this->renderChannelPlayer($streamUrl, '90%');
  2505. $result .= wf_tag('div', true);
  2506. $assignedUserId = (isset($this->channelUsers[$eachChan['guid']])) ? $this->channelUsers[$eachChan['guid']] : '';
  2507. $assignedUserLabel = (isset($this->allUsers[$assignedUserId])) ? $this->iconVisorUser() . ' ' . $this->allUsers[$assignedUserId]['realname'] : '';
  2508. $userAssignedLink = ($assignedUserId) ? wf_Link(self::URL_ME . self::URL_USERVIEW . $assignedUserId, $assignedUserLabel) : __('No');
  2509. $userLinkClass = ($assignedUserId) ? 'todaysig' : 'undone';
  2510. $result .= wf_tag('div', false, $userLinkClass);
  2511. $result .= $channelEditControl . ' ' . __('User') . ': ' . $userAssignedLink;
  2512. $result .= wf_tag('div', true);
  2513. $result .= __('Signal') . ' ' . web_bool_led($eachChan['signal']);
  2514. $result .= wf_CleanDiv();
  2515. $result .= wf_tag('div', true);
  2516. $chanCount++;
  2517. }
  2518. }
  2519. } else {
  2520. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  2521. }
  2522. }
  2523. } else {
  2524. $result .= $this->messages->getStyledMessage(__('DVR connection error') . ': [' . $eachDvr['id'] . ']', 'error');
  2525. }
  2526. }
  2527. if ($eachDvr['type'] == 'wolfrecorder') {
  2528. $apiUrl = $this->getWolfRecorderApiUrl($eachDvr['id']);
  2529. $webUrl = ($eachDvr['customurl']) ? $eachDvr['customurl'] : $apiUrl;
  2530. $dvrGate = new WolfRecorder($apiUrl, $eachDvr['apikey']);
  2531. if ($dvrGate->connectionOk()) {
  2532. $dvrChannels = $dvrGate->channelsGetAll();
  2533. if (!empty($dvrChannels)) {
  2534. $allChannelScreenshots = $dvrGate->channelsGetScreenshotsAll();
  2535. $allRunningRecorders = $dvrGate->recordersGetAll();
  2536. foreach ($dvrChannels as $eachChan => $eachCamId) {
  2537. $renderChannel = false;
  2538. if ($allFlag) {
  2539. $renderChannel = true;
  2540. } else {
  2541. if (!isset($this->channelUsers[$eachChan])) {
  2542. $renderChannel = true;
  2543. }
  2544. }
  2545. if ($renderChannel) {
  2546. $screenShotUrl = '';
  2547. if (isset($allChannelScreenshots[$eachChan])) {
  2548. $screenShotUrl = $webUrl . $allChannelScreenshots[$eachChan];
  2549. } else {
  2550. $screenShotUrl = 'skins/noimage.jpg';
  2551. }
  2552. $recorderState = (isset($allRunningRecorders[$eachCamId])) ? true : false;
  2553. $result .= wf_tag('div', false, 'whiteboard', 'style="width:' . $this->chanPreviewSize . ';"');
  2554. $channelEditControl = wf_Link(self::URL_ME . self::URL_CHANEDIT . $eachChan . '&dvrid=' . $eachDvr['id'], web_edit_icon(__('Edit') . ' ' . __('channel')));
  2555. $result .= $eachChan . ' @ ' . $eachDvr['id'];
  2556. $result .= wf_tag('br');
  2557. $result .= wf_tag('div', false, '', 'style="overflow:hidden; height:220px; max-height:250px;"');
  2558. $result .= wf_img_sized($screenShotUrl, '', '90%');
  2559. $result .= wf_tag('div', true);
  2560. $assignedUserId = (isset($this->channelUsers[$eachChan])) ? $this->channelUsers[$eachChan] : '';
  2561. $assignedUserLabel = (isset($this->allUsers[$assignedUserId])) ? $this->iconVisorUser() . ' ' . $this->allUsers[$assignedUserId]['realname'] : '';
  2562. $userAssignedLink = ($assignedUserId) ? wf_Link(self::URL_ME . self::URL_USERVIEW . $assignedUserId, $assignedUserLabel) : __('No');
  2563. $userLinkClass = ($assignedUserId) ? 'todaysig' : 'undone';
  2564. $result .= wf_tag('div', false, $userLinkClass);
  2565. $result .= $channelEditControl . ' ' . __('User') . ': ' . $userAssignedLink;
  2566. $result .= wf_tag('div', true);
  2567. $result .= __('Recording') . ' ' . web_bool_led($recorderState);
  2568. $result .= wf_CleanDiv();
  2569. $result .= wf_tag('div', true);
  2570. $chanCount++;
  2571. }
  2572. }
  2573. } else {
  2574. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  2575. }
  2576. } else {
  2577. $result .= $this->messages->getStyledMessage(__('DVR connection error') . ': [' . $eachDvr['id'] . ']', 'error');
  2578. }
  2579. }
  2580. }
  2581. //all channels assigned, no channels registered alert
  2582. if ($chanCount == 0) {
  2583. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  2584. }
  2585. $result .= wf_CleanDiv();
  2586. $result .= wf_tag('div', true);
  2587. } else {
  2588. $result .= $this->messages->getStyledMessage(__('DVRs') . ' ' . __('Not exists'), 'warning');
  2589. }
  2590. return ($result);
  2591. }
  2592. /**
  2593. * Renders channel record mode editing form
  2594. *
  2595. * @param string $channelGuid
  2596. * @param int $dvrId
  2597. * @param int $currentModeId
  2598. *
  2599. * @return string
  2600. */
  2601. protected function renderChannelRecordForm($channelGuid, $dvrId, $currentModeId) {
  2602. $result = '';
  2603. $channelGuid = ubRouting::filters($channelGuid, 'mres');
  2604. $dvrId = ubRouting::filters($dvrId, 'int');
  2605. $currentModeId = ubRouting::filters($currentModeId, 'int');
  2606. $inputs = wf_HiddenInput('recordchannelguid', $channelGuid);
  2607. $inputs .= wf_HiddenInput('recordchanneldvrid', $dvrId);
  2608. $inputs .= wf_Selector('recordchannelmode', $this->recordModes, __('Archive record mode'), $currentModeId, false) . ' ';
  2609. $inputs .= wf_Submit(__('Save'));
  2610. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  2611. return ($result);
  2612. }
  2613. /**
  2614. * Changes some channel record mode
  2615. *
  2616. * @return void
  2617. */
  2618. public function saveChannelRecordMode() {
  2619. if (ubRouting::checkPost(array('recordchannelguid', 'recordchanneldvrid', 'recordchannelmode'))) {
  2620. $channellGuid = ubRouting::post('recordchannelguid', 'mres');
  2621. $dvrId = ubRouting::post('recordchanneldvrid', 'int');
  2622. $mode = ubRouting::post('recordchannelmode', 'int');
  2623. if (isset($this->allDvrs[$dvrId])) {
  2624. $dvrData = $this->allDvrs[$dvrId];
  2625. if ($dvrData['type'] == 'trassir') {
  2626. $trassir = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  2627. //channel avail check
  2628. $allChannels = $trassir->getChannels();
  2629. if (isset($allChannels[$channellGuid])) {
  2630. $trassir->setChannelRecordMode($channellGuid, $mode);
  2631. log_register('VISOR DVR [' . $dvrId . '] CHAN `' . $channellGuid . '` SET RECMODE [' . $mode . ']');
  2632. }
  2633. }
  2634. }
  2635. }
  2636. }
  2637. /**
  2638. * Returns channel preview container/player based on stream type
  2639. *
  2640. * @param string $streamUrl
  2641. * @param string $width
  2642. * @param bool $autoPlay
  2643. *
  2644. * @return string
  2645. */
  2646. protected function renderChannelPlayer($streamUrl, $width, $autoPlay = false) {
  2647. $result = '';
  2648. if ($this->chanPreviewContainer == 'mjpeg') {
  2649. $result .= wf_img_sized($streamUrl, '', $width);
  2650. }
  2651. if ($this->chanPreviewContainer == 'hls') {
  2652. $autoPlayMode = ($autoPlay) ? 'true' : 'false';
  2653. $uniqId = 'hlsplayer' . wf_InputId();
  2654. $result .= wf_tag('script', false, '', 'src="modules/jsc/playerjs/playerjs.js"') . wf_tag('script', true);
  2655. $result .= wf_tag('div', false, '', 'id="' . $uniqId . '" style="width:' . $width . ';"') . wf_tag('div', true);
  2656. $result .= wf_tag('script', false);
  2657. $result .= 'var player = new Playerjs({id:"' . $uniqId . '", file:"' . $streamUrl . '", autoplay:' . $autoPlayMode . '});';
  2658. $result .= wf_tag('script', true);
  2659. }
  2660. return ($result);
  2661. }
  2662. /**
  2663. * Renders channel editing form
  2664. *
  2665. * @param string $channelGuid
  2666. * @param int $dvrId
  2667. *
  2668. * @return string
  2669. */
  2670. public function renderChannelEditForm($channelGuid, $dvrId) {
  2671. $result = '';
  2672. $channelGuid = ubRouting::filters($channelGuid, 'mres');
  2673. $dvrId = ubRouting::filters($dvrId, 'int');
  2674. if (isset($this->allDvrs[$dvrId])) {
  2675. $curUserId = '';
  2676. if (isset($this->channelUsers[$channelGuid])) {
  2677. //already assigned to someone
  2678. $curUserId = $this->channelUsers[$channelGuid];
  2679. } else {
  2680. $curUserId = (ubRouting::checkGet('useridpreset')) ? ubRouting::get('useridpreset', 'int') : '';
  2681. }
  2682. //some users preparing
  2683. $usersTmp = array('' => '-');
  2684. if (!empty($this->allUsers)) {
  2685. foreach ($this->allUsers as $io => $each) {
  2686. $usersTmp[$each['id']] = $each['realname'];
  2687. }
  2688. }
  2689. $inputs = wf_HiddenInput('editchannelguid', $channelGuid);
  2690. $inputs .= wf_HiddenInput('editchanneldvrid', $dvrId);
  2691. if ($this->altCfg['VISOR_USERSEL_SEARCHBL']) {
  2692. $inputs .= wf_SelectorSearchable('editchannelvisorid', $usersTmp, __('User'), $curUserId, false) . ' ';
  2693. } else {
  2694. $inputs .= wf_Selector('editchannelvisorid', $usersTmp, __('User'), $curUserId, false) . ' ';
  2695. }
  2696. $inputs .= wf_Submit(__('Save'));
  2697. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  2698. $result .= wf_tag('br');
  2699. $dvrData = $this->allDvrs[$dvrId];
  2700. if ($dvrData['type'] == 'trassir') {
  2701. $trassir = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  2702. $channelUrl = $trassir->getLiveVideoStream($channelGuid, 'main', $this->chanPreviewContainer, $this->chanBigPreviewQuality, $this->chanBigPreviewFramerate, $dvrData['customurl']);
  2703. $result .= $this->renderChannelPlayer($channelUrl, '60%', true);
  2704. $result .= wf_delimiter();
  2705. //Channel record mode form here
  2706. $currentRecordMode = $trassir->getChannelRecordMode($channelGuid);
  2707. $result .= $this->renderChannelRecordForm($channelGuid, $dvrId, $currentRecordMode);
  2708. }
  2709. if ($dvrData['type'] == 'wolfrecorder') {
  2710. $apiUrl = $this->getWolfRecorderApiUrl($dvrId);
  2711. $webUrl = ($dvrData['customurl']) ? $dvrData['customurl'] : $apiUrl;
  2712. $wolfRecorder = new WolfRecorder($apiUrl, $dvrData['apikey']);
  2713. if ($wolfRecorder->connectionOk()) {
  2714. $screenshotUrl = '';
  2715. $screenshotReply = $wolfRecorder->channelsGetScreenshot($channelGuid);
  2716. if ($wolfRecorder->noError($screenshotReply)) {
  2717. if (isset($screenshotReply['screenshot']) and !empty($screenshotReply['screenshot'])) {
  2718. $screenshotUrl = $webUrl . $screenshotReply['screenshot'];
  2719. } else {
  2720. $screenshotUrl = 'skins/noimage.jpg';
  2721. }
  2722. $result .= wf_img_sized($screenshotUrl, '', '70%');
  2723. } else {
  2724. $result .= $this->messages->getStyledMessage(__('Oh no') . ': ' . __('DVR') . ' ' . __('Connection') . ' ' . __('Failed'), 'error');
  2725. }
  2726. }
  2727. }
  2728. if (!isset($this->channelUsers[$channelGuid])) {
  2729. $result .= $this->messages->getStyledMessage(__('Channel without assigned user'), 'warning');
  2730. $result .= wf_delimiter();
  2731. } else {
  2732. $result .= $this->messages->getStyledMessage(__('Channel have user assigned') . ': ' . @$this->allUsers[$this->channelUsers[$channelGuid]]['realname'], 'success');
  2733. $result .= wf_delimiter();
  2734. }
  2735. } else {
  2736. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('No such DVR exists') . ' [' . $dvrId . ']', 'error');
  2737. }
  2738. $result .= wf_link(self::URL_ME . self::URL_CHANS, wf_img('skins/play.png') . ' ' . __('Channels'), false, 'ubButton');
  2739. if (isset($this->channelUsers[$channelGuid])) {
  2740. $assignedUserId = $this->channelUsers[$channelGuid];
  2741. $result .= wf_link(self::URL_ME . self::URL_USERVIEW . $assignedUserId, $this->iconVisorUser() . ' ' . @$this->allUsers[$assignedUserId]['realname'], false, 'ubButton');
  2742. } else {
  2743. if (!empty($curUserId)) {
  2744. $result .= wf_link(self::URL_ME . self::URL_USERVIEW . $curUserId, $this->iconVisorUser() . ' ' . @$this->allUsers[$curUserId]['realname'], false, 'ubButton');
  2745. }
  2746. }
  2747. return ($result);
  2748. }
  2749. /**
  2750. * Catches channel to user assign request and do required actions (update/delete)
  2751. *
  2752. * @return void
  2753. */
  2754. public function saveChannelAssign() {
  2755. if (ubRouting::checkPost(array('editchannelguid', 'editchanneldvrid'))) {
  2756. $channelGuid = ubRouting::post('editchannelguid', 'mres');
  2757. $dvrId = ubRouting::post('editchanneldvrid', 'int');
  2758. if (ubRouting::checkPost('editchannelvisorid')) {
  2759. //create/update of assign
  2760. $visorId = ubRouting::post('editchannelvisorid', 'int'); //new channel owner ID
  2761. if (isset($this->channelUsers[$channelGuid])) {
  2762. $oldChannelOwnerId = $this->channelUsers[$channelGuid];
  2763. //change existing assign
  2764. if ($visorId != $this->channelUsers[$channelGuid]) {
  2765. $this->unassignChannel($this->channelUsers[$channelGuid], $dvrId, $channelGuid);
  2766. $this->regenerateDvrChannelAcl($oldChannelOwnerId, $dvrId); //NVR sync on channel owner change for old owner
  2767. $this->assignChannel($visorId, $dvrId, $channelGuid);
  2768. $this->regenerateDvrChannelAcl($visorId, $dvrId); //NVR sync on channel owner change for new owner
  2769. }
  2770. } else {
  2771. //create new channel assign
  2772. $this->assignChannel($visorId, $dvrId, $channelGuid);
  2773. $this->regenerateDvrChannelAcl($visorId, $dvrId); //NVR sync on new channel assign
  2774. }
  2775. } else {
  2776. //existing assign deletion
  2777. if (isset($this->channelUsers[$channelGuid])) {
  2778. $currentUserAssignId = $this->channelUsers[$channelGuid];
  2779. if (!empty($currentUserAssignId)) {
  2780. $this->unassignChannel($currentUserAssignId, $dvrId, $channelGuid);
  2781. $this->regenerateDvrChannelAcl($currentUserAssignId, $dvrId); //NVR sync on assign deletion
  2782. }
  2783. }
  2784. }
  2785. }
  2786. }
  2787. /**
  2788. * Regenerates all ACL for some visor user on Some DVR
  2789. *
  2790. * @param int $visorId
  2791. * @param int $dvrId
  2792. *
  2793. * @return string
  2794. */
  2795. public function regenerateDvrChannelAcl($visorId, $dvrId) {
  2796. $result = '';
  2797. $visorId = ubRouting::filters($visorId, 'int');
  2798. $dvrId = ubRouting::filters($dvrId, 'int');
  2799. if (!empty($dvrId) and !empty($visorId)) {
  2800. if (isset($this->allSecrets[$visorId])) {
  2801. if (isset($this->allDvrs[$dvrId])) {
  2802. if (isset($this->allUsers[$visorId])) {
  2803. $dvrData = $this->allDvrs[$dvrId];
  2804. //getting currently assigned channels on Visor
  2805. $this->chansDb->where('visorid', '=', $visorId);
  2806. $this->chansDb->where('dvrid', '=', $dvrId);
  2807. $userChans = $this->chansDb->getAll();
  2808. //Trassir Server Here
  2809. if ($dvrData['type'] == 'trassir') {
  2810. $secretData = $this->allSecrets[$visorId];
  2811. $dvrGate = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  2812. $userRegistered = $dvrGate->getUserGuid($secretData['login']);
  2813. if (!$userRegistered) {
  2814. //perform creating user on DVR
  2815. $dvrGate->createUser($secretData['login'], $secretData['password']);
  2816. log_register('VISOR USER [' . $visorId . '] REGISTERED ON DVR [' . $dvrId . '] AS `' . $secretData['login'] . '` SYNC');
  2817. }
  2818. //setting valid ACL for this DVR
  2819. $dvrChans = array();
  2820. if (!empty($userChans)) {
  2821. foreach ($userChans as $io => $eachChan) {
  2822. $dvrChans[] = $eachChan['chan'];
  2823. }
  2824. }
  2825. $aclGate = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  2826. $aclGate->assignUserChannels($secretData['login'], $dvrChans);
  2827. log_register('VISOR USER [' . $visorId . '] REGEN ACL ON DVR [' . $dvrId . '] AS `' . $secretData['login'] . '` SYNC');
  2828. }
  2829. //WolfRecorder regeneration
  2830. if ($dvrData['type'] == 'wolfrecorder') {
  2831. $secretData = $this->allSecrets[$visorId];
  2832. $apiUrl = $this->getWolfRecorderApiUrl($dvrId);
  2833. $wolfRecorder = new WolfRecorder($apiUrl, $dvrData['apikey']);
  2834. $userRegistered = $wolfRecorder->usersIsRegistered($secretData['login']);
  2835. if (!$userRegistered['registered']) {
  2836. $wolfRecorder->usersCreate($secretData['login'], $secretData['password']);
  2837. log_register('VISOR USER [' . $visorId . '] REGISTERED ON DVR [' . $dvrId . '] AS `' . $secretData['login'] . '` SYNC');
  2838. }
  2839. //here channels ACL assigns
  2840. $dvrChans = array();
  2841. if (!empty($userChans)) {
  2842. foreach ($userChans as $io => $eachChan) {
  2843. $dvrChans[] = $eachChan['chan'];
  2844. }
  2845. }
  2846. $existingChansAcl = $wolfRecorder->aclsGetChannels($secretData['login']);
  2847. $visorChans = array();
  2848. if (!empty($userChans)) {
  2849. foreach ($userChans as $io => $eachChan) {
  2850. $visorChans[$eachChan['chan']] = $eachChan['chan'];
  2851. }
  2852. }
  2853. //puhing new to DVR
  2854. if (!empty($visorChans)) {
  2855. //sync visor=>dvr
  2856. foreach ($visorChans as $io => $eachChan) {
  2857. if (!isset($existingChansAcl[$eachChan])) {
  2858. $wolfRecorder->aclsAssignChannel($secretData['login'], $eachChan);
  2859. log_register('VISOR USER [' . $visorId . '] CREATE ACL ON DVR [' . $dvrId . '] AS `' . $secretData['login'] . '` CHAN `' . $eachChan . '`');
  2860. }
  2861. }
  2862. }
  2863. //deleting not existing in visor
  2864. if (!empty($existingChansAcl)) {
  2865. foreach ($existingChansAcl as $eachChan => $eachCam) {
  2866. if (!isset($visorChans[$eachChan])) {
  2867. $wolfRecorder->aclsDeassignChannel($secretData['login'], $eachChan);
  2868. log_register('VISOR USER [' . $visorId . '] DELETE ACL ON DVR [' . $dvrId . '] AS `' . $secretData['login'] . '` CHAN `' . $eachChan . '`');
  2869. }
  2870. }
  2871. }
  2872. log_register('VISOR USER [' . $visorId . '] REGEN ACL ON DVR [' . $dvrId . '] AS `' . $secretData['login'] . '` SYNC');
  2873. }
  2874. }
  2875. }
  2876. }
  2877. }
  2878. return ($result);
  2879. }
  2880. /**
  2881. * Returns JSON list of channel preview URLs of channels assigned for user
  2882. *
  2883. * @param int $visorId
  2884. * @param bool $maxQual
  2885. *
  2886. * @return string
  2887. */
  2888. public function getUserChannelsPreviewJson($visorId, $maxQual = false) {
  2889. $result = '';
  2890. $visorId = ubRouting::filters($visorId, 'int');
  2891. $urlTmp = array();
  2892. if (isset($this->allChannels[$visorId])) {
  2893. foreach ($this->allChannels[$visorId] as $io => $each) {
  2894. if (isset($this->allDvrs[$each['dvrid']])) {
  2895. $dvrData = $this->allDvrs[$each['dvrid']];
  2896. if ($dvrData['type'] == 'trassir') {
  2897. $trassir = new TrassirServer($dvrData['ip'], $dvrData['login'], $dvrData['password'], $dvrData['apikey'], $dvrData['port'], $this->trassirDebug);
  2898. if (!$maxQual) {
  2899. $url = $trassir->getLiveVideoStream($each['chan'], 'main', $this->chanPreviewContainer, $this->chanPreviewQuality, $this->chanPreviewFramerate, $dvrData['customurl']);
  2900. } else {
  2901. $url = $trassir->getLiveVideoStream($each['chan'], 'main', $this->chanPreviewContainer, $this->chanBigPreviewQuality, $this->chanBigPreviewFramerate, $dvrData['customurl']);
  2902. }
  2903. $urlTmp[$each['chan']] = $url;
  2904. }
  2905. if ($dvrData['type'] == 'wolfrecorder') {
  2906. $apiUrl = $this->getWolfRecorderApiUrl($dvrData['id']);
  2907. $webUrl = ($dvrData['customurl']) ? $dvrData['customurl'] : $apiUrl;
  2908. $wolfRecorder = new WolfRecorder($apiUrl, $dvrData['apikey']);
  2909. if ($maxQual) {
  2910. $liveStreamRaw = $wolfRecorder->channelsGetLiveStream($each['chan']);
  2911. if (isset($liveStreamRaw['livestream'])) {
  2912. $url = $webUrl . $liveStreamRaw['livestream'] . '&file=stream.m3u8';
  2913. $urlTmp[$each['chan']] = $url;
  2914. }
  2915. } else {
  2916. $screenshotRaw = $wolfRecorder->channelsGetScreenshot($each['chan']);
  2917. if (isset($screenshotRaw['screenshot'])) {
  2918. if (!empty($screenshotRaw['screenshot'])) {
  2919. $url = $webUrl . $screenshotRaw['screenshot'];
  2920. $urlTmp[$each['chan']] = $url;
  2921. }
  2922. }
  2923. }
  2924. }
  2925. }
  2926. }
  2927. }
  2928. $result = json_encode($urlTmp);
  2929. return ($result);
  2930. }
  2931. /**
  2932. * Returns some DVRs authorization data if user have some channels assigned on managable DVRs
  2933. *
  2934. * @param int $visorId
  2935. *
  2936. * @return string
  2937. */
  2938. public function getUserDvrAuthData($visorId) {
  2939. $result = array();
  2940. $visorId = ubRouting::filters($visorId, 'int');
  2941. if (isset($this->allUsers[$visorId])) {
  2942. if (isset($this->allSecrets[$visorId])) {
  2943. $secretsData = $this->allSecrets[$visorId];
  2944. if (isset($this->allChannels[$visorId])) {
  2945. if (!empty($this->allChannels[$visorId])) {
  2946. $allChanData = $this->allChannels[$visorId];
  2947. foreach ($allChanData as $io => $each) {
  2948. if (isset($this->allDvrs[$each['dvrid']])) {
  2949. $prefill = '';
  2950. $dvrData = $this->allDvrs[$each['dvrid']];
  2951. $result[$each['dvrid']]['dvrid'] = $dvrData['id'];
  2952. $result[$each['dvrid']]['dvrname'] = $dvrData['name'];
  2953. $result[$each['dvrid']]['ip'] = $dvrData['ip'];
  2954. $result[$each['dvrid']]['port'] = $dvrData['port'];
  2955. $result[$each['dvrid']]['login'] = $secretsData['login'];
  2956. $result[$each['dvrid']]['password'] = $secretsData['password'];
  2957. $result[$each['dvrid']]['customlink'] = 0;
  2958. //trassir nvr
  2959. if ($dvrData['type'] == 'trassir') {
  2960. $result[$each['dvrid']]['weburl'] = 'https://' . $dvrData['ip'] . ':' . $dvrData['port'] . '/webgui/';
  2961. }
  2962. //wolfrecorder nvr
  2963. if ($dvrData['type'] == 'wolfrecorder') {
  2964. if (!empty($secretsData['login']) and !empty($secretsData['password'])) {
  2965. $prefill = '?authprefill=' . $secretsData['login'] . '_' . $secretsData['password'];
  2966. }
  2967. if (empty($dvrData['apiurl'])) {
  2968. $result[$each['dvrid']]['weburl'] = 'http://' . $dvrData['ip'] . '/wr/' . $prefill;
  2969. } else {
  2970. $result[$each['dvrid']]['weburl'] = $dvrData['apiurl'] . $prefill;
  2971. }
  2972. //custom weburl handling
  2973. if (!empty($dvrData['customurl'])) {
  2974. $result[$each['dvrid']]['weburl'] = $dvrData['customurl'] . $prefill;
  2975. $result[$each['dvrid']]['customlink'] = 1;
  2976. }
  2977. }
  2978. }
  2979. }
  2980. }
  2981. }
  2982. }
  2983. }
  2984. $result = json_encode($result);
  2985. return ($result);
  2986. }
  2987. /**
  2988. * Returns existing DVR name and IP
  2989. *
  2990. * @param int $dvrId
  2991. *
  2992. * @return string
  2993. */
  2994. public function getDvrLabel($dvrId) {
  2995. $result = '';
  2996. if (isset($this->allDvrs[$dvrId])) {
  2997. $result .= $this->allDvrs[$dvrId]['name'] . ' - ' . $this->allDvrs[$dvrId]['ip'];
  2998. }
  2999. return ($result);
  3000. }
  3001. /**
  3002. * Returns existing DVR name
  3003. *
  3004. * @param int $dvrId
  3005. *
  3006. * @return string
  3007. */
  3008. public function getDvrName($dvrId) {
  3009. $result = '';
  3010. if (isset($this->allDvrs[$dvrId])) {
  3011. $result .= $this->allDvrs[$dvrId]['name'];
  3012. }
  3013. return ($result);
  3014. }
  3015. /**
  3016. * Performs default fee charge processing to prevent cameras offline
  3017. *
  3018. * @return void
  3019. */
  3020. public function chargeProcessing() {
  3021. $chargedCounter = 0;
  3022. if (!empty($this->allUsers)) {
  3023. //we need some fresh data
  3024. $this->allUserData = zb_UserGetAllData();
  3025. //and tariffs fee
  3026. $allTariffsFee = zb_TariffGetPricesAll();
  3027. foreach ($this->allUsers as $eachUserId => $eachUserData) {
  3028. if (($eachUserData['chargecams']) and (!empty($eachUserData['primarylogin']))) {
  3029. if (isset($this->allUserData[$eachUserData['primarylogin']])) {
  3030. //further actions is required
  3031. $primaryAccountData = $this->allUserData[$eachUserData['primarylogin']];
  3032. $primaryAccountLogin = $primaryAccountData['login'];
  3033. $primaryAccountBalance = $primaryAccountData['Cash'];
  3034. $primaryAccountCredit = $primaryAccountData['Credit'];
  3035. $primaryAccountTariff = $primaryAccountData['Tariff'];
  3036. $primaryPossibleBalance = $primaryAccountBalance + $primaryAccountCredit; //global primary balance counter
  3037. $primaryAccountFee = $allTariffsFee[$primaryAccountTariff];
  3038. //loading user cameras
  3039. $userCameras = $this->getUserCameras($eachUserId);
  3040. if (!empty($userCameras)) {
  3041. foreach ($userCameras as $eachCameraId => $eachCameraData) {
  3042. if (isset($this->allUserData[$eachCameraData['login']])) {
  3043. $cameraUserData = $this->allUserData[$eachCameraData['login']];
  3044. $cameraLogin = $cameraUserData['login'];
  3045. $cameraTariff = $cameraUserData['Tariff'];
  3046. if (isset($allTariffsFee[$cameraTariff])) {
  3047. $cameraBalance = $cameraUserData['Cash'];
  3048. $cameraCredit = $cameraUserData['Credit'];
  3049. $cameraFee = $allTariffsFee[$cameraTariff];
  3050. $cameraLack = ($cameraBalance + $cameraCredit) - $cameraFee;
  3051. //this camera needs some money to continue functioning
  3052. if ($cameraLack < 0) {
  3053. //is this not a same user?
  3054. if ($cameraLogin != $primaryAccountLogin) {
  3055. $chargeThisCam = false;
  3056. //camera online priority
  3057. if ($this->chargeMode == 1) {
  3058. $chargeThisCam = true;
  3059. }
  3060. //primary account internet priority
  3061. if ($this->chargeMode == 2) {
  3062. $primaryPossibleBalance = ($primaryPossibleBalance) - abs($cameraLack);
  3063. if ($primaryPossibleBalance >= '-' . $primaryAccountCredit) {
  3064. //that doesnt disable primary account
  3065. $chargeThisCam = true;
  3066. } else {
  3067. //and this will
  3068. $chargeThisCam = false;
  3069. }
  3070. }
  3071. //dont charge money for frozen cameras
  3072. if ($cameraUserData['Passive'] == 1) {
  3073. $chargeThisCam = false;
  3074. }
  3075. //perform money movement from primary account
  3076. if ($chargeThisCam) {
  3077. //charge some money from primary account
  3078. zb_CashAdd($primaryAccountLogin, $cameraLack, 'add', 1, 'VISORCHARGE:' . $eachCameraId);
  3079. //and put in onto camera account
  3080. zb_CashAdd($cameraLogin, abs($cameraLack), 'correct', 1, 'VISORPUSH:' . $eachUserId);
  3081. //correcting operation here to prevent figure that as true payment in reports.
  3082. $chargedCounter++;
  3083. }
  3084. }
  3085. }
  3086. } else {
  3087. log_register('VISOR CAMERA [' . $eachCameraId . '] CHARGE FAIL NO_TARIFF `' . $cameraTariff . '`');
  3088. }
  3089. } else {
  3090. log_register('VISOR CAMERA [' . $eachCameraId . '] CHARGE FAIL NO_USER (' . $eachCameraData['login'] . ')');
  3091. }
  3092. }
  3093. }
  3094. } else {
  3095. log_register('VISOR USER [' . $eachUserId . '] PRIMARY NO_USER (' . $eachUserData['primarylogin'] . ')');
  3096. }
  3097. }
  3098. }
  3099. //flush old cached users data
  3100. if ($chargedCounter > 0) {
  3101. zb_UserGetAllDataCacheClean();
  3102. }
  3103. }
  3104. }
  3105. }