unifi.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageUnifiEnabled'),
  21. $this->settingsOption('auth', 'homepageUnifiAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'unifiURL'),
  25. $this->settingsOption('blank'),
  26. $this->settingsOption('disable-cert-check', 'unifiDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'unifiUseCustomCertificate'),
  28. $this->settingsOption('username', 'unifiUsername', ['help' => 'Username is case-sensitive']),
  29. $this->settingsOption('password', 'unifiPassword'),
  30. $this->settingsOption('input', 'unifiSiteName', ['label' => 'Site Name (Not for UnifiOS)', 'help' => 'Site Name - not Site ID nor Site Description']),
  31. $this->settingsOption('button', '', ['label' => 'Grab Unifi Site (Not for UnifiOS)', 'icon' => 'fa fa-building', 'text' => 'Get Unifi Site', 'attr' => 'onclick="getUnifiSite()"']),
  32. ],
  33. 'Misc Options' => [
  34. $this->settingsOption('refresh', 'homepageUnifiRefresh'),
  35. ],
  36. 'Test Connection' => [
  37. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  38. $this->settingsOption('test', 'unifi'),
  39. ]
  40. ]
  41. ];
  42. return array_merge($homepageInformation, $homepageSettings);
  43. }
  44. public function unifiHomepagePermissions($key = null)
  45. {
  46. $permissions = [
  47. 'main' => [
  48. 'enabled' => [
  49. 'homepageUnifiEnabled'
  50. ],
  51. 'auth' => [
  52. 'homepageUnifiAuth'
  53. ],
  54. 'not_empty' => [
  55. 'unifiURL',
  56. 'unifiUsername',
  57. 'unifiPassword'
  58. ]
  59. ],
  60. 'test' => [
  61. 'auth' => [
  62. 'homepageUnifiAuth'
  63. ],
  64. 'not_empty' => [
  65. 'unifiURL',
  66. 'unifiUsername',
  67. 'unifiPassword'
  68. ]
  69. ]
  70. ];
  71. return $this->homepageCheckKeyPermissions($key, $permissions);
  72. }
  73. public function homepageOrderunifi()
  74. {
  75. if ($this->homepageItemPermissions($this->unifiHomepagePermissions('main'))) {
  76. return '
  77. <div id="' . __FUNCTION__ . '">
  78. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Unifi...</h2></div>
  79. <script>
  80. // Unifi
  81. homepageUnifi("' . $this->config['homepageHealthChecksRefresh'] . '");
  82. // End Unifi
  83. </script>
  84. </div>
  85. ';
  86. }
  87. }
  88. public function getUnifiSiteName()
  89. {
  90. if (!$this->homepageItemPermissions($this->unifiHomepagePermissions('test'), true)) {
  91. return false;
  92. }
  93. try {
  94. $login = $this->unifiLogin();
  95. if ($login) {
  96. $url = $this->qualifyURL($this->config['unifiURL']);
  97. $unifiOS = $login['unifiOS'];
  98. if ($unifiOS) {
  99. $this->setResponse(500, 'Unifi OS does not support Multi Site');
  100. return false;
  101. }
  102. $response = Requests::get($url . '/api/self/sites', [], $login['options']);
  103. if ($response->success) {
  104. $body = json_decode($response->body, true);
  105. $this->setAPIResponse('success', null, 200, $body);
  106. return $body;
  107. } else {
  108. $this->setAPIResponse('error', 'Unifi response error3', 409);
  109. return false;
  110. }
  111. } else {
  112. return false;
  113. }
  114. } catch (Requests_Exception $e) {
  115. $this->setLoggerChannel('Unifi')->error($e);
  116. $this->setResponse(500, $e->getMessage());
  117. return false;
  118. }
  119. }
  120. public function isUnifiOS()
  121. {
  122. try {
  123. // Is this UnifiOs or Regular
  124. $url = $this->qualifyURL($this->config['unifiURL']);
  125. $options = $this->requestOptions($url, $this->config['homepageUnifiRefresh'], $this->config['unifiDisableCertCheck'], $this->config['unifiUseCustomCertificate'], ['follow_redirects' => true]);
  126. $response = Requests::get($url, [], $options);
  127. if ($response->success) {
  128. return ($response->headers['x-csrf-token']) ?? false;
  129. } else {
  130. $this->setAPIResponse('error', 'Unifi response error - Check URL', 409);
  131. return false;
  132. }
  133. } catch (Requests_Exception $e) {
  134. $this->setLoggerChannel('Unifi')->error($e);
  135. $this->setResponse(500, $e->getMessage());
  136. return false;
  137. }
  138. }
  139. public function unifiLogin()
  140. {
  141. $csrfToken = $this->isUnifiOS();
  142. $url = $this->qualifyURL($this->config['unifiURL']);
  143. $options = $this->requestOptions($url, $this->config['homepageUnifiRefresh'], $this->config['unifiDisableCertCheck'], $this->config['unifiUseCustomCertificate'], ['follow_redirects' => true]);
  144. $data = array(
  145. 'username' => $this->config['unifiUsername'],
  146. 'password' => $this->decrypt($this->config['unifiPassword']),
  147. 'remember' => true,
  148. 'strict' => true
  149. );
  150. try {
  151. $data = ($csrfToken) ? $data : json_encode($data);
  152. $headers = ($csrfToken) ? ['x-csrf-token' => $csrfToken] : [];
  153. $urlLogin = ($csrfToken) ? $url . '/api/auth/login' : $url . '/api/login';
  154. $response = Requests::post($urlLogin, $headers, $data, $options);
  155. if ($response->success) {
  156. $options['cookies'] = $response->cookies;
  157. return [
  158. 'unifiOS' => $csrfToken,
  159. 'options' => $options
  160. ];
  161. } else {
  162. $this->setAPIResponse('error', 'Unifi response error - Check Credentials', 409);
  163. return false;
  164. }
  165. } catch (Requests_Exception $e) {
  166. $this->setLoggerChannel('Unifi')->error($e);
  167. $this->setResponse(500, $e->getMessage());
  168. return false;
  169. }
  170. }
  171. public function testConnectionUnifi()
  172. {
  173. if (!$this->homepageItemPermissions($this->unifiHomepagePermissions('test'), true)) {
  174. return false;
  175. }
  176. try {
  177. // Is this UnifiOs or Regular
  178. $api['content']['unifi'] = array();
  179. $login = $this->unifiLogin();
  180. if ($login) {
  181. $url = $this->qualifyURL($this->config['unifiURL']);
  182. $unifiOS = $login['unifiOS'];
  183. $headers = ($unifiOS) ? ['x-csrf-token' => $unifiOS] : [];
  184. $urlStat = ($unifiOS) ? $url . '/proxy/network/api/s/default/stat/health' : $url . '/api/s/' . $this->config['unifiSiteName'] . '/stat/health';
  185. $response = Requests::get($urlStat, $headers, $login['options']);
  186. if ($response->success) {
  187. $api['content']['unifi'] = json_decode($response->body, true);
  188. } else {
  189. $this->setAPIResponse('error', 'Unifi response error3', 409);
  190. return false;
  191. }
  192. }
  193. } catch (Requests_Exception $e) {
  194. $this->setLoggerChannel('Unifi')->error($e);
  195. $this->setResponse(500, $e->getMessage());
  196. return false;
  197. }
  198. $api['content']['unifi'] = $api['content']['unifi'] ?? false;
  199. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  200. return true;
  201. }
  202. public function getUnifiHomepageData()
  203. {
  204. if (!$this->homepageItemPermissions($this->unifiHomepagePermissions('main'), true)) {
  205. return false;
  206. }
  207. try {
  208. $api['content']['unifi'] = array();
  209. $login = $this->unifiLogin();
  210. if ($login) {
  211. $url = $this->qualifyURL($this->config['unifiURL']);
  212. $unifiOS = $login['unifiOS'];
  213. $headers = ($unifiOS) ? ['x-csrf-token' => $unifiOS] : [];
  214. $urlStat = ($unifiOS) ? $url . '/proxy/network/api/s/default/stat/health' : $url . '/api/s/' . $this->config['unifiSiteName'] . '/stat/health';
  215. $response = Requests::get($urlStat, $headers, $login['options']);
  216. if ($response->success) {
  217. $api['content']['unifi'] = json_decode($response->body, true);
  218. } else {
  219. $this->setAPIResponse('error', 'Unifi response error3', 409);
  220. return false;
  221. }
  222. }
  223. } catch (Requests_Exception $e) {
  224. $this->setLoggerChannel('Unifi')->error($e);
  225. $this->setResponse(500, $e->getMessage());
  226. return false;
  227. }
  228. $api['content']['unifi'] = $api['content']['unifi'] ?? false;
  229. $this->setAPIResponse('success', null, 200, $api);
  230. return $api;
  231. }
  232. }