unifi.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. trait UnifiHomepageItem
  3. {
  4. public function unifiSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Unifi',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/ubnt.png',
  10. 'category' => 'Monitor',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageUnifiEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageUnifiEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageUnifiAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageUnifiAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'unifiURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['unifiURL'],
  33. 'help' => 'URL for Unifi',
  34. 'placeholder' => 'Unifi API URL'
  35. ),
  36. array(
  37. 'type' => 'blank',
  38. 'label' => ''
  39. ),
  40. array(
  41. 'type' => 'input',
  42. 'name' => 'unifiUsername',
  43. 'label' => 'Username',
  44. 'value' => $this->config['unifiUsername'],
  45. 'help' => 'Username is case-sensitive',
  46. ),
  47. array(
  48. 'type' => 'password',
  49. 'name' => 'unifiPassword',
  50. 'label' => 'Password',
  51. 'value' => $this->config['unifiPassword']
  52. ),
  53. array(
  54. 'type' => 'input',
  55. 'name' => 'unifiSiteName',
  56. 'label' => 'Site Name (Not for UnifiOS)',
  57. 'value' => $this->config['unifiSiteName'],
  58. 'help' => 'Site Name - not Site ID nor Site Description',
  59. ),
  60. array(
  61. 'type' => 'button',
  62. 'label' => 'Grab Unifi Site (Not for UnifiOS)',
  63. 'icon' => 'fa fa-building',
  64. 'text' => 'Get Unifi Site',
  65. 'attr' => 'onclick="getUnifiSite()"'
  66. ),
  67. ),
  68. 'Misc Options' => array(
  69. array(
  70. 'type' => 'select',
  71. 'name' => 'homepageUnifiRefresh',
  72. 'label' => 'Refresh Seconds',
  73. 'value' => $this->config['homepageUnifiRefresh'],
  74. 'options' => $this->timeOptions()
  75. ),
  76. ),
  77. 'Test Connection' => array(
  78. array(
  79. 'type' => 'blank',
  80. 'label' => 'Please Save before Testing'
  81. ),
  82. array(
  83. 'type' => 'button',
  84. 'label' => '',
  85. 'icon' => 'fa fa-flask',
  86. 'class' => 'pull-right',
  87. 'text' => 'Test Connection',
  88. 'attr' => 'onclick="testAPIConnection(\'unifi\')"'
  89. ),
  90. )
  91. )
  92. );
  93. }
  94. public function getUnifiSiteName()
  95. {
  96. if (empty($this->config['unifiURL'])) {
  97. $this->setAPIResponse('error', 'Unifi URL is not defined', 422);
  98. return false;
  99. }
  100. if (empty($this->config['unifiUsername'])) {
  101. $this->setAPIResponse('error', 'Unifi Username is not defined', 422);
  102. return false;
  103. }
  104. if (empty($this->config['unifiPassword'])) {
  105. $this->setAPIResponse('error', 'Unifi Password is not defined', 422);
  106. return false;
  107. }
  108. $url = $this->qualifyURL($this->config['unifiURL']);
  109. try {
  110. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => false);
  111. $data = array(
  112. 'username' => $this->config['unifiUsername'],
  113. 'password' => $this->decrypt($this->config['unifiPassword']),
  114. 'remember' => true,
  115. 'strict' => true
  116. );
  117. $response = Requests::post($url . '/api/login', array(), json_encode($data), $options);
  118. if ($response->success) {
  119. $cookie['unifises'] = ($response->cookies['unifises']->value) ?? false;
  120. $cookie['csrf_token'] = ($response->cookies['csrf_token']->value) ?? false;
  121. } else {
  122. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  123. return false;
  124. }
  125. $headers = array(
  126. 'cookie' => 'unifises=' . $cookie['unifises'] . ';' . 'csrf_token=' . $cookie['csrf_token'] . ';'
  127. );
  128. $response = Requests::get($url . '/api/self/sites', $headers, $options);
  129. if ($response->success) {
  130. $body = json_decode($response->body, true);
  131. $this->setAPIResponse('success', null, 200, $body);
  132. return $body;
  133. } else {
  134. $this->setAPIResponse('error', 'Unifi response error - Error Occurred', 409);
  135. return false;
  136. }
  137. } catch (Requests_Exception $e) {
  138. $this->writeLog('error', 'Unifi Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  139. $this->setAPIResponse('error', $e->getMessage(), 500);
  140. return false;
  141. }
  142. }
  143. public function testConnectionUnifi()
  144. {
  145. if (empty($this->config['unifiURL'])) {
  146. $this->setAPIResponse('error', 'Unifi URL is not defined', 422);
  147. return false;
  148. }
  149. if (empty($this->config['unifiUsername'])) {
  150. $this->setAPIResponse('error', 'Unifi Username is not defined', 422);
  151. return false;
  152. }
  153. if (empty($this->config['unifiPassword'])) {
  154. $this->setAPIResponse('error', 'Unifi Password is not defined', 422);
  155. return false;
  156. }
  157. $api['content']['unifi'] = array();
  158. $url = $this->qualifyURL($this->config['unifiURL']);
  159. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true);
  160. $data = array(
  161. 'username' => $this->config['unifiUsername'],
  162. 'password' => $this->decrypt($this->config['unifiPassword']),
  163. 'remember' => true,
  164. 'strict' => true
  165. );
  166. try {
  167. // Is this UnifiOs or Regular
  168. $response = Requests::get($url, [], $options);
  169. if ($response->success) {
  170. $csrfToken = ($response->headers['x-csrf-token']) ?? false;
  171. $data = ($csrfToken) ? $data : json_encode($data);
  172. } else {
  173. $this->setAPIResponse('error', 'Unifi response error - Check URL', 409);
  174. return false;
  175. }
  176. $urlLogin = ($csrfToken) ? $url . '/api/auth/login' : $url . '/api/login';
  177. $urlStat = ($csrfToken) ? $url . '/proxy/network/api/s/default/stat/health' : $url . '/api/s/' . $this->config['unifiSiteName'] . '/stat/health';
  178. $response = Requests::post($urlLogin, [], $data, $options);
  179. if ($response->success) {
  180. $cookie['unifises'] = ($response->cookies['unifises']->value) ?? false;
  181. $cookie['csrf_token'] = ($response->cookies['csrf_token']->value) ?? false;
  182. $cookie['Token'] = ($response->cookies['Token']->value) ?? false;
  183. $options['cookies'] = $response->cookies;
  184. } else {
  185. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  186. return false;
  187. }
  188. $headers = array(
  189. 'cookie' => 'unifises=' . $cookie['unifises'] . ';' . 'csrf_token=' . $cookie['csrf_token'] . ';'
  190. );
  191. $response = Requests::get($urlStat, $headers, $options);
  192. if ($response->success) {
  193. $api['content']['unifi'] = json_decode($response->body, true);
  194. } else {
  195. $this->setAPIResponse('error', 'Unifi response error3', 409);
  196. return false;
  197. }
  198. } catch (Requests_Exception $e) {
  199. $this->writeLog('error', 'Unifi Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  200. $this->setAPIResponse('error', $e->getMessage(), 500);
  201. return false;
  202. };
  203. $api['content']['unifi'] = isset($api['content']['unifi']) ? $api['content']['unifi'] : false;
  204. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  205. return true;
  206. }
  207. public function getUnifiHomepageData()
  208. {
  209. if (!$this->config['homepageUnifiEnabled']) {
  210. $this->setAPIResponse('error', 'Unifi homepage item is not enabled', 409);
  211. return false;
  212. }
  213. if (!$this->qualifyRequest($this->config['homepageUnifiAuth'])) {
  214. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  215. return false;
  216. }
  217. if (empty($this->config['unifiURL'])) {
  218. $this->setAPIResponse('error', 'Unifi URL is not defined', 422);
  219. return false;
  220. }
  221. if (empty($this->config['unifiUsername'])) {
  222. $this->setAPIResponse('error', 'Unifi Username is not defined', 422);
  223. return false;
  224. }
  225. if (empty($this->config['unifiPassword'])) {
  226. $this->setAPIResponse('error', 'Unifi Password is not defined', 422);
  227. return false;
  228. }
  229. $api['content']['unifi'] = array();
  230. $url = $this->qualifyURL($this->config['unifiURL']);
  231. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true);
  232. $data = array(
  233. 'username' => $this->config['unifiUsername'],
  234. 'password' => $this->decrypt($this->config['unifiPassword']),
  235. 'remember' => true,
  236. 'strict' => true
  237. );
  238. try {
  239. // Is this UnifiOs or Regular
  240. $response = Requests::get($url, [], $options);
  241. if ($response->success) {
  242. $csrfToken = ($response->headers['x-csrf-token']) ?? false;
  243. $data = ($csrfToken) ? $data : json_encode($data);
  244. } else {
  245. $this->setAPIResponse('error', 'Unifi response error - Check URL', 409);
  246. return false;
  247. }
  248. $urlLogin = ($csrfToken) ? $url . '/api/auth/login' : $url . '/api/login';
  249. $urlStat = ($csrfToken) ? $url . '/proxy/network/api/s/default/stat/health' : $url . '/api/s/' . $this->config['unifiSiteName'] . '/stat/health';
  250. $response = Requests::post($urlLogin, [], $data, $options);
  251. if ($response->success) {
  252. $cookie['unifises'] = ($response->cookies['unifises']->value) ?? false;
  253. $cookie['csrf_token'] = ($response->cookies['csrf_token']->value) ?? false;
  254. $cookie['Token'] = ($response->cookies['Token']->value) ?? false;
  255. $options['cookies'] = $response->cookies;
  256. } else {
  257. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  258. return false;
  259. }
  260. $headers = array(
  261. 'cookie' => 'unifises=' . $cookie['unifises'] . ';' . 'csrf_token=' . $cookie['csrf_token'] . ';'
  262. );
  263. $response = Requests::get($urlStat, $headers, $options);
  264. if ($response->success) {
  265. $api['content']['unifi'] = json_decode($response->body, true);
  266. } else {
  267. $this->setAPIResponse('error', 'Unifi response error3', 409);
  268. return false;
  269. }
  270. } catch (Requests_Exception $e) {
  271. $this->writeLog('error', 'Unifi Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  272. $this->setAPIResponse('error', $e->getMessage(), 500);
  273. return false;
  274. };
  275. $api['content']['unifi'] = isset($api['content']['unifi']) ? $api['content']['unifi'] : false;
  276. $this->setAPIResponse('success', null, 200, $api);
  277. return $api;
  278. }
  279. }