unifi.php 10 KB

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