api.wolfrecorder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. /**
  3. * WolfRecorder NVR REST APIv1 implementation
  4. *
  5. * https://wolfrecorder.com/wiki/doku.php?id=api
  6. */
  7. class WolfRecorder {
  8. /**
  9. * Current instance WolfRecorder URL
  10. *
  11. * @var string
  12. */
  13. protected $url = '';
  14. /**
  15. * Current instance API key aka Serial
  16. *
  17. * @var string
  18. */
  19. protected $apiKey = '';
  20. /**
  21. * Some predefined stuff
  22. */
  23. const ROUTE_CALL = '/?module=remoteapi&action=rest&key=';
  24. /**
  25. * Creates new WR API instance
  26. *
  27. * @param string $url
  28. * @param string $apiKey
  29. */
  30. public function __construct($url, $apiKey) {
  31. $this->setUrl($url);
  32. $this->setApiKey($apiKey);
  33. }
  34. /**
  35. * Sets current instance URL
  36. *
  37. * @param string $url
  38. *
  39. * @throws exception
  40. */
  41. protected function setUrl($url) {
  42. if (!empty($url)) {
  43. $this->url = $url;
  44. } else {
  45. throw new Exception('EX_EMPTY_URL');
  46. }
  47. }
  48. /**
  49. * Sets current instance API key
  50. *
  51. * @param string $url
  52. *
  53. * @throws exception
  54. */
  55. protected function setApiKey($apiKey) {
  56. if (!empty($apiKey)) {
  57. $this->apiKey = $apiKey;
  58. } else {
  59. throw new Exception('EX_EMPTY_APIKEY');
  60. }
  61. }
  62. /**
  63. * Performs request to remote WolfRecorder REST API, returns it result
  64. *
  65. * @param string $object
  66. * @param string $method
  67. * @param array $requestData
  68. *
  69. * @return array
  70. */
  71. protected function executeRequest($object, $method, $requestData = array()) {
  72. $result = array();
  73. $fullUrl = $this->url . self::ROUTE_CALL . $this->apiKey . '&' . $object . '=' . $method;
  74. $apiHandle = new OmaeUrl($fullUrl);
  75. if (!empty($requestData)) {
  76. $apiHandle->dataPost('data', json_encode($requestData));
  77. }
  78. $rawReply = $apiHandle->response();
  79. if (!$apiHandle->error() and $apiHandle->httpCode() == 200) {
  80. @$replyDecode = json_decode($rawReply, true);
  81. if (is_array($replyDecode)) {
  82. $result = $replyDecode;
  83. } else {
  84. $result = array('error' => 666, 'message' => __('Something went wrong') . ': ' . __('API') . ' ' . __('Failed'));
  85. }
  86. }
  87. return ($result);
  88. }
  89. /**
  90. * Fast check for some request is returning error or not?
  91. *
  92. * @param array $requestReply
  93. *
  94. * @return bool
  95. */
  96. public function noError($requestReply) {
  97. $result = true;
  98. if (is_array($requestReply)) {
  99. if (isset($requestReply['error'])) {
  100. if ($requestReply['error']) {
  101. $result = false;
  102. }
  103. }
  104. }
  105. return ($result);
  106. }
  107. /**
  108. * Fast check is API connection ok or not
  109. *
  110. * @return array
  111. */
  112. public function connectionOk() {
  113. $result = false;
  114. $connectionReply = $this->systemCheckConnection();
  115. if (!empty($connectionReply)) {
  116. if ($this->noError($connectionReply)) {
  117. if (isset($connectionReply['connection'])) {
  118. if ($connectionReply['connection']) {
  119. $result = true;
  120. }
  121. }
  122. }
  123. }
  124. return ($result);
  125. }
  126. /**
  127. * Returns list of all available models as modelId=>modelsData[id/modelname/template]
  128. *
  129. * @return array
  130. */
  131. public function modelsGetAll() {
  132. return ($this->executeRequest('models', 'getall'));
  133. }
  134. /**
  135. * Returns list of available storages as storageId=>storagesData[id/path/name]
  136. *
  137. * @return array
  138. */
  139. public function storagesGetAll() {
  140. return ($this->executeRequest('storages', 'getall'));
  141. }
  142. /**
  143. * Returns list of all storages states as storageId=>statesArr[state/total/used/free]
  144. *
  145. * @return array
  146. */
  147. public function storagesGetStates() {
  148. return ($this->executeRequest('storages', 'getstates'));
  149. }
  150. /**
  151. * Returns array of all available cameras data as:
  152. *
  153. * cameraId[CAMERA]=>id/modelid/ip/login/password/active/storageid/channel/comment
  154. * cameraId[TEMPLATE]=>DEVICE/PROTO/MAIN_STREAM/SUB_STREAM/RTSP_PORT/HTTP_PORT/SOUND
  155. * cameraId[STORAGE]=>id/path/name
  156. *
  157. * @return array
  158. */
  159. public function camerasGetAll() {
  160. return ($this->executeRequest('cameras', 'getall'));
  161. }
  162. /**
  163. * Creates new camera on NVR
  164. *
  165. * @param int $modelId
  166. * @param string $ip
  167. * @param string $login
  168. * @param string $password
  169. * @param int $active
  170. * @param int $storage
  171. * @param string $description
  172. *
  173. * @return array
  174. */
  175. public function camerasCreate($modelId, $ip, $login, $password, $active = 0, $storage = 0, $description = '') {
  176. $requestData = array(
  177. 'modelid' => $modelId,
  178. 'ip' => $ip,
  179. 'login' => $login,
  180. 'password' => $password,
  181. 'active' => $active,
  182. 'storageid' => $storage,
  183. 'description' => $description
  184. );
  185. return ($this->executeRequest('cameras', 'create', $requestData));
  186. }
  187. /**
  188. * Activates existing camera
  189. *
  190. * @param int $cameraId
  191. *
  192. * @return array
  193. */
  194. public function camerasActivate($cameraId) {
  195. $requestData = array(
  196. 'cameraid' => $cameraId
  197. );
  198. return ($this->executeRequest('cameras', 'activate', $requestData));
  199. }
  200. /**
  201. * Deactivates existing camera
  202. *
  203. * @param int $cameraId
  204. *
  205. * @return array
  206. */
  207. public function camerasDeactivate($cameraId) {
  208. $requestData = array(
  209. 'cameraid' => $cameraId
  210. );
  211. return ($this->executeRequest('cameras', 'deactivate', $requestData));
  212. }
  213. /**
  214. * Changes existing camera description
  215. *
  216. * @param int $cameraId
  217. * @param string $description
  218. *
  219. * @return array
  220. */
  221. public function camerasSetDescription($cameraId, $description = '') {
  222. $requestData = array(
  223. 'cameraid' => $cameraId,
  224. 'description' => $description
  225. );
  226. return ($this->executeRequest('cameras', 'setdescription', $requestData));
  227. }
  228. /**
  229. * Deletes existing camera
  230. *
  231. * @param int $cameraId
  232. *
  233. * @return array
  234. */
  235. public function camerasDelete($cameraId) {
  236. $requestData = array(
  237. 'cameraid' => $cameraId
  238. );
  239. return ($this->executeRequest('cameras', 'delete', $requestData));
  240. }
  241. /**
  242. * Checks is camera registered or not by its IP
  243. *
  244. * @param string $ip
  245. *
  246. * @return array
  247. */
  248. public function camerasIsRegistered($ip) {
  249. $requestData = array(
  250. 'ip' => $ip
  251. );
  252. return ($this->executeRequest('cameras', 'isregistered', $requestData));
  253. }
  254. /**
  255. * Checks is camera IP=PORT pair free or not
  256. *
  257. * @param string $ip
  258. * @param int $port
  259. *
  260. * @return array
  261. */
  262. public function camerasIsIpPortFree($ip, $port) {
  263. $requestData = array(
  264. 'ip' => $ip,
  265. 'port' => $port
  266. );
  267. return ($this->executeRequest('cameras', 'isipportfree', $requestData));
  268. }
  269. /**
  270. * Returns system health data as storages/database/network/channels_total/cahnnels_online/uptime/loadavg
  271. *
  272. * @return array
  273. */
  274. public function systemGetHealth() {
  275. return ($this->executeRequest('system', 'gethealth'));
  276. }
  277. /**
  278. * Returns system non empty array to connection check error/connection/message
  279. *
  280. * @return array
  281. */
  282. public function systemCheckConnection() {
  283. return ($this->executeRequest('system', 'checkconnection'));
  284. }
  285. /**
  286. * Returns all available users data
  287. *
  288. * @return array
  289. */
  290. public function usersGetAll() {
  291. return ($this->executeRequest('users', 'getall'));
  292. }
  293. /**
  294. * Creates new limited user
  295. *
  296. * @param string $login
  297. * @param string $password
  298. *
  299. * @return array
  300. */
  301. public function usersCreate($login, $password) {
  302. $requestData = array(
  303. 'login' => $login,
  304. 'password' => $password
  305. );
  306. return ($this->executeRequest('users', 'create', $requestData));
  307. }
  308. /**
  309. * Changes some existing user password to new one
  310. *
  311. * @param string $login
  312. * @param string $password
  313. *
  314. * @return array
  315. */
  316. public function usersChangePassword($login, $password) {
  317. $requestData = array(
  318. 'login' => $login,
  319. 'password' => $password
  320. );
  321. return ($this->executeRequest('users', 'changepassword', $requestData));
  322. }
  323. /**
  324. * Checks is user registered or not
  325. *
  326. * @param string $login
  327. *
  328. * @return array
  329. */
  330. public function usersIsRegistered($login) {
  331. $requestData = array(
  332. 'login' => $login
  333. );
  334. return ($this->executeRequest('users', 'isregistered', $requestData));
  335. }
  336. /**
  337. * Deletes an existing user
  338. *
  339. * @param string $login
  340. *
  341. * @return array
  342. */
  343. public function usersDelete($login) {
  344. $requestData = array(
  345. 'login' => $login
  346. );
  347. return ($this->executeRequest('users', 'delete', $requestData));
  348. }
  349. /**
  350. * Checks can be user authorized or not
  351. *
  352. * @param string $login
  353. * @param string $password
  354. *
  355. * @return array
  356. */
  357. public function usersCheckAuth($login, $password) {
  358. $requestData = array(
  359. 'login' => $login,
  360. 'password' => $password
  361. );
  362. return ($this->executeRequest('users', 'checkauth', $requestData));
  363. }
  364. /**
  365. * Returns all ACLs raw data
  366. *
  367. * @return array
  368. */
  369. public function aclsGetAll() {
  370. return ($this->executeRequest('acls', 'getall'));
  371. }
  372. /**
  373. * Returns array of all available user to cameras ACLs
  374. *
  375. * @return array
  376. */
  377. public function aclsGetAllCameras() {
  378. return ($this->executeRequest('acls', 'getallcameras'));
  379. }
  380. /**
  381. * Returns array of all available user to channels ACLs
  382. *
  383. * @return array
  384. */
  385. public function aclsGetAllChannels() {
  386. return ($this->executeRequest('acls', 'getallchannels'));
  387. }
  388. /**
  389. * Returns array of channels assigned to some user as channelId=>cameraId
  390. *
  391. * @param string $login
  392. *
  393. * @return array
  394. */
  395. public function aclsGetChannels($login) {
  396. $requestData = array(
  397. 'login' => $login
  398. );
  399. return ($this->executeRequest('acls', 'getchannels', $requestData));
  400. }
  401. /**
  402. * Returns array of channels assigned to some user as channelId=>cameraId
  403. *
  404. * @param string $login
  405. *
  406. * @return array
  407. */
  408. public function aclsGetCameras($login) {
  409. $requestData = array(
  410. 'login' => $login
  411. );
  412. return ($this->executeRequest('acls', 'getcameras', $requestData));
  413. }
  414. /**
  415. * Creates ACL for some user by cameraId
  416. *
  417. * @param string $login
  418. * @param int $cameraId
  419. *
  420. * @return array
  421. */
  422. public function aclsAssignCamera($login, $cameraId) {
  423. $requestData = array(
  424. 'login' => $login,
  425. 'cameraid' => $cameraId
  426. );
  427. return ($this->executeRequest('acls', 'assigncamera', $requestData));
  428. }
  429. /**
  430. * Creates ACL for some user by channelId
  431. *
  432. * @param string $login
  433. * @param int $channelId
  434. *
  435. * @return array
  436. */
  437. public function aclsAssignChannel($login, $channelId) {
  438. $requestData = array(
  439. 'login' => $login,
  440. 'channelid' => $channelId
  441. );
  442. return ($this->executeRequest('acls', 'assignchannel', $requestData));
  443. }
  444. /**
  445. * Deletes ACL for some user by cameraId
  446. *
  447. * @param string $login
  448. * @param int $cameraId
  449. *
  450. * @return array
  451. */
  452. public function aclsDeassignCamera($login, $cameraId) {
  453. $requestData = array(
  454. 'login' => $login,
  455. 'cameraid' => $cameraId
  456. );
  457. return ($this->executeRequest('acls', 'deassigncamera', $requestData));
  458. }
  459. /**
  460. * Deletes ACL for some user by channelId
  461. *
  462. * @param string $login
  463. * @param int $channelId
  464. *
  465. * @return array
  466. */
  467. public function aclsDeassignChannel($login, $channelId) {
  468. $requestData = array(
  469. 'login' => $login,
  470. 'channelid' => $channelId
  471. );
  472. return ($this->executeRequest('acls', 'deassignchannel', $requestData));
  473. }
  474. /**
  475. * Returns all available channels as channelId=>cameraId
  476. *
  477. * @return array
  478. */
  479. public function channelsGetAll() {
  480. return ($this->executeRequest('channels', 'getall'));
  481. }
  482. /**
  483. * Returns some channel screenshot URL as error/screenshot
  484. *
  485. * @return array
  486. */
  487. public function channelsGetScreenshot($channelId) {
  488. $requestData = array(
  489. 'channelid' => $channelId
  490. );
  491. return ($this->executeRequest('channels', 'getscreenshot', $requestData));
  492. }
  493. /**
  494. * Returns all channels screenshots URLs as channelId=>screenshotUrl
  495. *
  496. * @return array
  497. */
  498. public function channelsGetScreenshotsAll() {
  499. return ($this->executeRequest('channels', 'getscreenshotsall'));
  500. }
  501. /**
  502. * Returns some channel live preview pseudostream URL as error/livestream
  503. *
  504. * @return array
  505. */
  506. public function channelsGetLiveStream($channelId) {
  507. $requestData = array(
  508. 'channelid' => $channelId
  509. );
  510. return ($this->executeRequest('channels', 'getlivestream', $requestData));
  511. }
  512. /**
  513. * Returns list of running recorders as cameraId=>PID
  514. *
  515. * @return array
  516. */
  517. public function recordersGetAll() {
  518. return ($this->executeRequest('recorders', 'getall'));
  519. }
  520. /**
  521. * Returns some camera recording process state
  522. *
  523. * @param int $cameraId
  524. *
  525. * @return array
  526. */
  527. public function recordersIsRunning($cameraId) {
  528. $requestData = array(
  529. 'cameraid' => $cameraId
  530. );
  531. return ($this->executeRequest('recorders', 'isrunning', $requestData));
  532. }
  533. }