unifi.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/unifi.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 unifiHomepagePermissions($key = null)
  95. {
  96. $permissions = [
  97. 'main' => [
  98. 'enabled' => [
  99. 'homepageUnifiEnabled'
  100. ],
  101. 'auth' => [
  102. 'homepageUnifiAuth'
  103. ],
  104. 'not_empty' => [
  105. 'unifiURL',
  106. 'unifiUsername',
  107. 'unifiPassword'
  108. ]
  109. ]
  110. ];
  111. if (array_key_exists($key, $permissions)) {
  112. return $permissions[$key];
  113. } elseif ($key == 'all') {
  114. return $permissions;
  115. } else {
  116. return [];
  117. }
  118. }
  119. public function homepageOrderunifi()
  120. {
  121. if ($this->homepageItemPermissions($this->unifiHomepagePermissions('main'))) {
  122. return '
  123. <div id="' . __FUNCTION__ . '">
  124. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Unifi...</h2></div>
  125. <script>
  126. // Unifi
  127. homepageUnifi("' . $this->config['homepageHealthChecksRefresh'] . '");
  128. // End Unifi
  129. </script>
  130. </div>
  131. ';
  132. }
  133. }
  134. public function getUnifiSiteName()
  135. {
  136. if (empty($this->config['unifiURL'])) {
  137. $this->setAPIResponse('error', 'Unifi URL is not defined', 422);
  138. return false;
  139. }
  140. if (empty($this->config['unifiUsername'])) {
  141. $this->setAPIResponse('error', 'Unifi Username is not defined', 422);
  142. return false;
  143. }
  144. if (empty($this->config['unifiPassword'])) {
  145. $this->setAPIResponse('error', 'Unifi Password is not defined', 422);
  146. return false;
  147. }
  148. $url = $this->qualifyURL($this->config['unifiURL']);
  149. try {
  150. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => false);
  151. $data = array(
  152. 'username' => $this->config['unifiUsername'],
  153. 'password' => $this->decrypt($this->config['unifiPassword']),
  154. 'remember' => true,
  155. 'strict' => true
  156. );
  157. $response = Requests::post($url . '/api/login', array(), json_encode($data), $options);
  158. if ($response->success) {
  159. $cookie['unifises'] = ($response->cookies['unifises']->value) ?? false;
  160. $cookie['csrf_token'] = ($response->cookies['csrf_token']->value) ?? false;
  161. } else {
  162. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  163. return false;
  164. }
  165. $headers = array(
  166. 'cookie' => 'unifises=' . $cookie['unifises'] . ';' . 'csrf_token=' . $cookie['csrf_token'] . ';'
  167. );
  168. $response = Requests::get($url . '/api/self/sites', $headers, $options);
  169. if ($response->success) {
  170. $body = json_decode($response->body, true);
  171. $this->setAPIResponse('success', null, 200, $body);
  172. return $body;
  173. } else {
  174. $this->setAPIResponse('error', 'Unifi response error - Error Occurred', 409);
  175. return false;
  176. }
  177. } catch (Requests_Exception $e) {
  178. $this->writeLog('error', 'Unifi Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  179. $this->setAPIResponse('error', $e->getMessage(), 500);
  180. return false;
  181. }
  182. }
  183. public function testConnectionUnifi()
  184. {
  185. if (empty($this->config['unifiURL'])) {
  186. $this->setAPIResponse('error', 'Unifi URL is not defined', 422);
  187. return false;
  188. }
  189. if (empty($this->config['unifiUsername'])) {
  190. $this->setAPIResponse('error', 'Unifi Username is not defined', 422);
  191. return false;
  192. }
  193. if (empty($this->config['unifiPassword'])) {
  194. $this->setAPIResponse('error', 'Unifi Password is not defined', 422);
  195. return false;
  196. }
  197. $api['content']['unifi'] = array();
  198. $url = $this->qualifyURL($this->config['unifiURL']);
  199. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true);
  200. $data = array(
  201. 'username' => $this->config['unifiUsername'],
  202. 'password' => $this->decrypt($this->config['unifiPassword']),
  203. 'remember' => true,
  204. 'strict' => true
  205. );
  206. try {
  207. // Is this UnifiOs or Regular
  208. $response = Requests::get($url, [], $options);
  209. if ($response->success) {
  210. $csrfToken = ($response->headers['x-csrf-token']) ?? false;
  211. $data = ($csrfToken) ? $data : json_encode($data);
  212. } else {
  213. $this->setAPIResponse('error', 'Unifi response error - Check URL', 409);
  214. return false;
  215. }
  216. $urlLogin = ($csrfToken) ? $url . '/api/auth/login' : $url . '/api/login';
  217. $urlStat = ($csrfToken) ? $url . '/proxy/network/api/s/default/stat/health' : $url . '/api/s/' . $this->config['unifiSiteName'] . '/stat/health';
  218. $response = Requests::post($urlLogin, [], $data, $options);
  219. if ($response->success) {
  220. $cookie['unifises'] = ($response->cookies['unifises']->value) ?? false;
  221. $cookie['csrf_token'] = ($response->cookies['csrf_token']->value) ?? false;
  222. $cookie['Token'] = ($response->cookies['Token']->value) ?? false;
  223. $options['cookies'] = $response->cookies;
  224. } else {
  225. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  226. return false;
  227. }
  228. $headers = array(
  229. 'cookie' => 'unifises=' . $cookie['unifises'] . ';' . 'csrf_token=' . $cookie['csrf_token'] . ';'
  230. );
  231. $response = Requests::get($urlStat, $headers, $options);
  232. if ($response->success) {
  233. $api['content']['unifi'] = json_decode($response->body, true);
  234. } else {
  235. $this->setAPIResponse('error', 'Unifi response error3', 409);
  236. return false;
  237. }
  238. } catch (Requests_Exception $e) {
  239. $this->writeLog('error', 'Unifi Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  240. $this->setAPIResponse('error', $e->getMessage(), 500);
  241. return false;
  242. };
  243. $api['content']['unifi'] = isset($api['content']['unifi']) ? $api['content']['unifi'] : false;
  244. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  245. return true;
  246. }
  247. public function getUnifiHomepageData()
  248. {
  249. if (!$this->homepageItemPermissions($this->unifiHomepagePermissions('main'), true)) {
  250. return false;
  251. }
  252. $api['content']['unifi'] = array();
  253. $url = $this->qualifyURL($this->config['unifiURL']);
  254. $options = array('verify' => false, 'verifyname' => false, 'follow_redirects' => true);
  255. $data = array(
  256. 'username' => $this->config['unifiUsername'],
  257. 'password' => $this->decrypt($this->config['unifiPassword']),
  258. 'remember' => true,
  259. 'strict' => true
  260. );
  261. try {
  262. // Is this UnifiOs or Regular
  263. $response = Requests::get($url, [], $options);
  264. if ($response->success) {
  265. $csrfToken = ($response->headers['x-csrf-token']) ?? false;
  266. $data = ($csrfToken) ? $data : json_encode($data);
  267. } else {
  268. $this->setAPIResponse('error', 'Unifi response error - Check URL', 409);
  269. return false;
  270. }
  271. $urlLogin = ($csrfToken) ? $url . '/api/auth/login' : $url . '/api/login';
  272. $urlStat = ($csrfToken) ? $url . '/proxy/network/api/s/default/stat/health' : $url . '/api/s/' . $this->config['unifiSiteName'] . '/stat/health';
  273. $response = Requests::post($urlLogin, [], $data, $options);
  274. if ($response->success) {
  275. $cookie['unifises'] = ($response->cookies['unifises']->value) ?? false;
  276. $cookie['csrf_token'] = ($response->cookies['csrf_token']->value) ?? false;
  277. $cookie['Token'] = ($response->cookies['Token']->value) ?? false;
  278. $options['cookies'] = $response->cookies;
  279. } else {
  280. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  281. return false;
  282. }
  283. $headers = array(
  284. 'cookie' => 'unifises=' . $cookie['unifises'] . ';' . 'csrf_token=' . $cookie['csrf_token'] . ';'
  285. );
  286. $response = Requests::get($urlStat, $headers, $options);
  287. if ($response->success) {
  288. $api['content']['unifi'] = json_decode($response->body, true);
  289. } else {
  290. $this->setAPIResponse('error', 'Unifi response error3', 409);
  291. return false;
  292. }
  293. } catch (Requests_Exception $e) {
  294. $this->writeLog('error', 'Unifi Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  295. $this->setAPIResponse('error', $e->getMessage(), 500);
  296. return false;
  297. };
  298. $api['content']['unifi'] = isset($api['content']['unifi']) ? $api['content']['unifi'] : false;
  299. $this->setAPIResponse('success', null, 200, $api);
  300. return $api;
  301. }
  302. }