123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- <?php
- /**
- * WolfRecorder NVR REST APIv1 implementation
- *
- * https://wolfrecorder.com/wiki/doku.php?id=api
- */
- class WolfRecorder {
- /**
- * Current instance WolfRecorder URL
- *
- * @var string
- */
- protected $url = '';
- /**
- * Current instance API key aka Serial
- *
- * @var string
- */
- protected $apiKey = '';
- /**
- * Some predefined stuff
- */
- const ROUTE_CALL = '/?module=remoteapi&action=rest&key=';
- /**
- * Creates new WR API instance
- *
- * @param string $url
- * @param string $apiKey
- */
- public function __construct($url, $apiKey) {
- $this->setUrl($url);
- $this->setApiKey($apiKey);
- }
- /**
- * Sets current instance URL
- *
- * @param string $url
- *
- * @throws exception
- */
- protected function setUrl($url) {
- if (!empty($url)) {
- $this->url = $url;
- } else {
- throw new Exception('EX_EMPTY_URL');
- }
- }
- /**
- * Sets current instance API key
- *
- * @param string $url
- *
- * @throws exception
- */
- protected function setApiKey($apiKey) {
- if (!empty($apiKey)) {
- $this->apiKey = $apiKey;
- } else {
- throw new Exception('EX_EMPTY_APIKEY');
- }
- }
- /**
- * Performs request to remote WolfRecorder REST API, returns it result
- *
- * @param string $object
- * @param string $method
- * @param array $requestData
- *
- * @return array
- */
- protected function executeRequest($object, $method, $requestData = array()) {
- $result = array();
- $fullUrl = $this->url . self::ROUTE_CALL . $this->apiKey . '&' . $object . '=' . $method;
- $apiHandle = new OmaeUrl($fullUrl);
- if (!empty($requestData)) {
- $apiHandle->dataPost('data', json_encode($requestData));
- }
- $rawReply = $apiHandle->response();
- if (!$apiHandle->error() and $apiHandle->httpCode() == 200) {
- @$replyDecode = json_decode($rawReply, true);
- if (is_array($replyDecode)) {
- $result = $replyDecode;
- } else {
- $result = array('error' => 666, 'message' => __('Something went wrong') . ': ' . __('API') . ' ' . __('Failed'));
- }
- }
- return ($result);
- }
- /**
- * Fast check for some request is returning error or not?
- *
- * @param array $requestReply
- *
- * @return bool
- */
- public function noError($requestReply) {
- $result = true;
- if (is_array($requestReply)) {
- if (isset($requestReply['error'])) {
- if ($requestReply['error']) {
- $result = false;
- }
- }
- }
- return ($result);
- }
- /**
- * Fast check is API connection ok or not
- *
- * @return array
- */
- public function connectionOk() {
- $result = false;
- $connectionReply = $this->systemCheckConnection();
- if (!empty($connectionReply)) {
- if ($this->noError($connectionReply)) {
- if (isset($connectionReply['connection'])) {
- if ($connectionReply['connection']) {
- $result = true;
- }
- }
- }
- }
- return ($result);
- }
- /**
- * Returns list of all available models as modelId=>modelsData[id/modelname/template]
- *
- * @return array
- */
- public function modelsGetAll() {
- return ($this->executeRequest('models', 'getall'));
- }
- /**
- * Returns list of available storages as storageId=>storagesData[id/path/name]
- *
- * @return array
- */
- public function storagesGetAll() {
- return ($this->executeRequest('storages', 'getall'));
- }
- /**
- * Returns list of all storages states as storageId=>statesArr[state/total/used/free]
- *
- * @return array
- */
- public function storagesGetStates() {
- return ($this->executeRequest('storages', 'getstates'));
- }
- /**
- * Returns array of all available cameras data as:
- *
- * cameraId[CAMERA]=>id/modelid/ip/login/password/active/storageid/channel/comment
- * cameraId[TEMPLATE]=>DEVICE/PROTO/MAIN_STREAM/SUB_STREAM/RTSP_PORT/HTTP_PORT/SOUND
- * cameraId[STORAGE]=>id/path/name
- *
- * @return array
- */
- public function camerasGetAll() {
- return ($this->executeRequest('cameras', 'getall'));
- }
- /**
- * Creates new camera on NVR
- *
- * @param int $modelId
- * @param string $ip
- * @param string $login
- * @param string $password
- * @param int $active
- * @param int $storage
- * @param string $description
- *
- * @return array
- */
- public function camerasCreate($modelId, $ip, $login, $password, $active = 0, $storage = 0, $description = '') {
- $requestData = array(
- 'modelid' => $modelId,
- 'ip' => $ip,
- 'login' => $login,
- 'password' => $password,
- 'active' => $active,
- 'storageid' => $storage,
- 'description' => $description
- );
- return ($this->executeRequest('cameras', 'create', $requestData));
- }
- /**
- * Activates existing camera
- *
- * @param int $cameraId
- *
- * @return array
- */
- public function camerasActivate($cameraId) {
- $requestData = array(
- 'cameraid' => $cameraId
- );
- return ($this->executeRequest('cameras', 'activate', $requestData));
- }
- /**
- * Deactivates existing camera
- *
- * @param int $cameraId
- *
- * @return array
- */
- public function camerasDeactivate($cameraId) {
- $requestData = array(
- 'cameraid' => $cameraId
- );
- return ($this->executeRequest('cameras', 'deactivate', $requestData));
- }
- /**
- * Changes existing camera description
- *
- * @param int $cameraId
- * @param string $description
- *
- * @return array
- */
- public function camerasSetDescription($cameraId, $description = '') {
- $requestData = array(
- 'cameraid' => $cameraId,
- 'description' => $description
- );
- return ($this->executeRequest('cameras', 'setdescription', $requestData));
- }
- /**
- * Deletes existing camera
- *
- * @param int $cameraId
- *
- * @return array
- */
- public function camerasDelete($cameraId) {
- $requestData = array(
- 'cameraid' => $cameraId
- );
- return ($this->executeRequest('cameras', 'delete', $requestData));
- }
- /**
- * Checks is camera registered or not by its IP
- *
- * @param string $ip
- *
- * @return array
- */
- public function camerasIsRegistered($ip) {
- $requestData = array(
- 'ip' => $ip
- );
- return ($this->executeRequest('cameras', 'isregistered', $requestData));
- }
- /**
- * Checks is camera IP=PORT pair free or not
- *
- * @param string $ip
- * @param int $port
- *
- * @return array
- */
- public function camerasIsIpPortFree($ip, $port) {
- $requestData = array(
- 'ip' => $ip,
- 'port' => $port
- );
- return ($this->executeRequest('cameras', 'isipportfree', $requestData));
- }
- /**
- * Returns system health data as storages/database/network/channels_total/cahnnels_online/uptime/loadavg
- *
- * @return array
- */
- public function systemGetHealth() {
- return ($this->executeRequest('system', 'gethealth'));
- }
- /**
- * Returns system non empty array to connection check error/connection/message
- *
- * @return array
- */
- public function systemCheckConnection() {
- return ($this->executeRequest('system', 'checkconnection'));
- }
- /**
- * Returns all available users data
- *
- * @return array
- */
- public function usersGetAll() {
- return ($this->executeRequest('users', 'getall'));
- }
- /**
- * Creates new limited user
- *
- * @param string $login
- * @param string $password
- *
- * @return array
- */
- public function usersCreate($login, $password) {
- $requestData = array(
- 'login' => $login,
- 'password' => $password
- );
- return ($this->executeRequest('users', 'create', $requestData));
- }
- /**
- * Changes some existing user password to new one
- *
- * @param string $login
- * @param string $password
- *
- * @return array
- */
- public function usersChangePassword($login, $password) {
- $requestData = array(
- 'login' => $login,
- 'password' => $password
- );
- return ($this->executeRequest('users', 'changepassword', $requestData));
- }
- /**
- * Checks is user registered or not
- *
- * @param string $login
- *
- * @return array
- */
- public function usersIsRegistered($login) {
- $requestData = array(
- 'login' => $login
- );
- return ($this->executeRequest('users', 'isregistered', $requestData));
- }
- /**
- * Deletes an existing user
- *
- * @param string $login
- *
- * @return array
- */
- public function usersDelete($login) {
- $requestData = array(
- 'login' => $login
- );
- return ($this->executeRequest('users', 'delete', $requestData));
- }
- /**
- * Checks can be user authorized or not
- *
- * @param string $login
- * @param string $password
- *
- * @return array
- */
- public function usersCheckAuth($login, $password) {
- $requestData = array(
- 'login' => $login,
- 'password' => $password
- );
- return ($this->executeRequest('users', 'checkauth', $requestData));
- }
- /**
- * Returns all ACLs raw data
- *
- * @return array
- */
- public function aclsGetAll() {
- return ($this->executeRequest('acls', 'getall'));
- }
- /**
- * Returns array of all available user to cameras ACLs
- *
- * @return array
- */
- public function aclsGetAllCameras() {
- return ($this->executeRequest('acls', 'getallcameras'));
- }
- /**
- * Returns array of all available user to channels ACLs
- *
- * @return array
- */
- public function aclsGetAllChannels() {
- return ($this->executeRequest('acls', 'getallchannels'));
- }
- /**
- * Returns array of channels assigned to some user as channelId=>cameraId
- *
- * @param string $login
- *
- * @return array
- */
- public function aclsGetChannels($login) {
- $requestData = array(
- 'login' => $login
- );
- return ($this->executeRequest('acls', 'getchannels', $requestData));
- }
- /**
- * Returns array of channels assigned to some user as channelId=>cameraId
- *
- * @param string $login
- *
- * @return array
- */
- public function aclsGetCameras($login) {
- $requestData = array(
- 'login' => $login
- );
- return ($this->executeRequest('acls', 'getcameras', $requestData));
- }
- /**
- * Creates ACL for some user by cameraId
- *
- * @param string $login
- * @param int $cameraId
- *
- * @return array
- */
- public function aclsAssignCamera($login, $cameraId) {
- $requestData = array(
- 'login' => $login,
- 'cameraid' => $cameraId
- );
- return ($this->executeRequest('acls', 'assigncamera', $requestData));
- }
- /**
- * Creates ACL for some user by channelId
- *
- * @param string $login
- * @param int $channelId
- *
- * @return array
- */
- public function aclsAssignChannel($login, $channelId) {
- $requestData = array(
- 'login' => $login,
- 'channelid' => $channelId
- );
- return ($this->executeRequest('acls', 'assignchannel', $requestData));
- }
- /**
- * Deletes ACL for some user by cameraId
- *
- * @param string $login
- * @param int $cameraId
- *
- * @return array
- */
- public function aclsDeassignCamera($login, $cameraId) {
- $requestData = array(
- 'login' => $login,
- 'cameraid' => $cameraId
- );
- return ($this->executeRequest('acls', 'deassigncamera', $requestData));
- }
- /**
- * Deletes ACL for some user by channelId
- *
- * @param string $login
- * @param int $channelId
- *
- * @return array
- */
- public function aclsDeassignChannel($login, $channelId) {
- $requestData = array(
- 'login' => $login,
- 'channelid' => $channelId
- );
- return ($this->executeRequest('acls', 'deassignchannel', $requestData));
- }
- /**
- * Returns all available channels as channelId=>cameraId
- *
- * @return array
- */
- public function channelsGetAll() {
- return ($this->executeRequest('channels', 'getall'));
- }
- /**
- * Returns some channel screenshot URL as error/screenshot
- *
- * @return array
- */
- public function channelsGetScreenshot($channelId) {
- $requestData = array(
- 'channelid' => $channelId
- );
- return ($this->executeRequest('channels', 'getscreenshot', $requestData));
- }
- /**
- * Returns all channels screenshots URLs as channelId=>screenshotUrl
- *
- * @return array
- */
- public function channelsGetScreenshotsAll() {
- return ($this->executeRequest('channels', 'getscreenshotsall'));
- }
- /**
- * Returns some channel live preview pseudostream URL as error/livestream
- *
- * @return array
- */
- public function channelsGetLiveStream($channelId) {
- $requestData = array(
- 'channelid' => $channelId
- );
- return ($this->executeRequest('channels', 'getlivestream', $requestData));
- }
- /**
- * Returns list of running recorders as cameraId=>PID
- *
- * @return array
- */
- public function recordersGetAll() {
- return ($this->executeRequest('recorders', 'getall'));
- }
- /**
- * Returns some camera recording process state
- *
- * @param int $cameraId
- *
- * @return array
- */
- public function recordersIsRunning($cameraId) {
- $requestData = array(
- 'cameraid' => $cameraId
- );
- return ($this->executeRequest('recorders', 'isrunning', $requestData));
- }
- }
|