unifi.php 10 KB

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