1212 *
1313 * @var string $unknown_string
1414 */
15-
1615use UniFi_API \Exceptions \CurlExtensionNotLoadedException ;
1716use UniFi_API \Exceptions \CurlGeneralErrorException ;
1817use UniFi_API \Exceptions \CurlTimeoutException ;
6564 $ port = parse_url ($ controller ['url ' ], PHP_URL_PORT ) ?: 443 ;
6665
6766 if (!empty ($ host ) && !empty ($ port )) {
68- $ fp = @fsockopen ($ host , $ port , $ errno , $ errstr , 2 );
69- if (!$ fp ) {
70- error_log ("we have a connection error $ errstr ( $ errno) " );
67+ /**
68+ * Create an instance of the Unifi API client class, log in to the controller and pull the requested data.
69+ */
70+ try {
71+ $ unifi_connection = new UniFi_API \Client (
72+ trim ($ controller ['user ' ]),
73+ trim ($ controller ['password ' ]),
74+ trim (rtrim ($ controller ['url ' ], "/ " )),
75+ 'default '
76+ );
77+
78+ $ unifi_connection ->login ();
79+ } catch (CurlExtensionNotLoadedException $ e ) {
80+ $ results ['state ' ] = 'error ' ;
81+ $ results ['message ' ] = 'cURL is not available in your PHP installation! ' ;
82+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
83+ } catch (CurlGeneralErrorException $ e ) {
84+ $ results ['state ' ] = 'error ' ;
85+ $ results ['message ' ] = 'We have encountered a general cURL error! Response code: ' . $ e ->getHttpResponseCode ();
86+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
87+ } catch (CurlTimeoutException $ e ) {
88+ $ results ['state ' ] = 'error ' ;
89+ $ results ['message ' ] = 'UniFi controller connection timeout! ' ;
90+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
91+ } catch (InvalidBaseUrlException $ e ) {
92+ $ results ['state ' ] = 'error ' ;
93+ $ results ['message ' ] = 'UniFi controller login failure, base URL is invalid! ' ;
94+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
95+ } catch (InvalidSiteNameException $ e ) {
96+ $ results ['state ' ] = 'error ' ;
97+ $ results ['message ' ] = 'UniFi controller login failure, site name is invalid! ' ;
98+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
99+ } catch (LoginFailedException $ e ) {
100+ $ results ['state ' ] = 'error ' ;
101+ $ results ['message ' ] = 'UniFi controller login failure, please check the URL and credentials in config/config.php! ' ;
102+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
103+ } catch (Exception $ e ) {
104+ $ results ['state ' ] = 'error ' ;
105+ $ results ['message ' ] = 'An Exception was thrown: ' . $ e ->getMessage ();
106+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
107+ }
108+
109+ if ($ results ['state ' ] === 'error ' || empty ($ unifi_connection )) {
110+ error_log ('we have an error, bailing out ' );
111+ $ _SESSION ['memory_used ' ] = round (memory_get_peak_usage (false ) / 1024 / 1024 , 2 ) . 'MB ' ;
112+ returnJson ($ results );
113+
114+ exit ;
115+ }
116+
117+ /**
118+ * We can safely continue.
119+ */
120+ try {
121+ $ sites_array = $ unifi_connection ->list_sites ();
122+ } catch (JsonDecodeException $ e ) {
123+ $ results ['state ' ] = 'error ' ;
124+ $ results ['message ' ] = 'JSON decode error! ' ;
125+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
126+ return ;
127+ } catch (LoginRequiredException $ e ) {
128+ $ results ['state ' ] = 'error ' ;
129+ $ results ['message ' ] = 'Login is required for this endpoint ' ;
130+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
131+ return ;
132+ } catch (LoginFailedException $ e ) {
71133 $ results ['state ' ] = 'error ' ;
72- $ results ['message ' ] = "we are unable to connect to the UniFi controller, $ errstr ( $ errno)! " ;
73- } else {
134+ $ results ['message ' ] = 'UniFi controller login failure, please check your credentials in config/config.php! ' ;
135+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
136+ return ;
137+ } catch (Exception $ e ) {
138+ $ results ['state ' ] = 'error ' ;
139+ $ results ['message ' ] = 'An Exception was thrown: ' . $ e ->getMessage ();
140+ error_log ('Exception: ' . get_class ($ e ) . ' - Message: ' . $ e ->getMessage ());
141+ return ;
142+ }
143+
144+ if (!empty ($ sites_array )) {
145+ if ($ debug ) {
146+ error_log ('DEBUG: ' . count ($ sites_array ) . ' sites collected ' );
147+ }
148+
74149 /**
75- * And we can continue .
150+ * Store the cookies from the controller for faster reconnecting .
76151 */
77- fclose ($ fp );
78-
79- try {
80- /**
81- * Create an instance of the Unifi API client class, log in to the controller and pull the requested data.
82- */
83- $ unifi_connection = new UniFi_API \Client (
84- trim ($ controller ['user ' ]),
85- trim ($ controller ['password ' ]),
86- trim (rtrim ($ controller ['url ' ], "/ " )),
87- 'default '
88- );
89- $ login_results = $ unifi_connection ->login ();
90- } catch (CurlExtensionNotLoadedException $ e ) {
91- $ results ['state ' ] = 'error ' ;
92- $ results ['message ' ] = 'cURL is not available in your PHP installation! ' ;
93- return ;
94- } catch (CurlGeneralErrorException $ e ) {
95- $ results ['state ' ] = 'error ' ;
96- $ results ['message ' ] = 'We have encountered a general cURL error! Please check the logs ' ;
97- return ;
98- } catch (CurlTimeoutException $ e ) {
99- $ results ['state ' ] = 'error ' ;
100- $ results ['message ' ] = 'UniFi controller connection timeout! ' ;
101- return ;
102- } catch (InvalidBaseUrlException $ e ) {
103- $ results ['state ' ] = 'error ' ;
104- $ results ['message ' ] = 'UniFi controller login failure, base URL is invalid! ' ;
105- return ;
106- } catch (InvalidSiteNameException $ e ) {
107- $ results ['state ' ] = 'error ' ;
108- $ results ['message ' ] = 'UniFi controller login failure, site name is invalid! ' ;
109- return ;
110- } catch (LoginFailedException $ e ) {
111- $ results ['state ' ] = 'error ' ;
112- $ results ['message ' ] = 'UniFi controller login failure, please check your credentials in config/config.php! ' ;
113- return ;
114- }
152+ $ _SESSION ['unificookie ' ] = $ unifi_connection ->get_cookie ();
115153
116154 /**
117- * We can safely continue .
155+ * Loop through the fetched sites .
118156 */
119- try {
120- $ sites_array = $ unifi_connection ->list_sites ();
121- } catch (JsonDecodeException $ e ) {
122- $ results ['state ' ] = 'error ' ;
123- $ results ['message ' ] = 'JSON decode error! ' ;
124- return ;
125- } catch (LoginRequiredException $ e ) {
126- $ results ['state ' ] = 'error ' ;
127- $ results ['message ' ] = 'Login is required for this endpoint ' ;
128- return ;
157+ foreach ($ sites_array as $ site ) {
158+ $ results ['data ' ][] = [
159+ 'site_id ' => $ site ->name ?? $ unknown_string ,
160+ 'site_full_name ' => $ site ->desc ?? $ unknown_string ,
161+ ];
129162 }
130163
131- if (!empty ($ sites_array )) {
132- if ($ debug ) {
133- error_log ('DEBUG: ' . count ($ sites_array ) . ' sites collected ' );
164+ /**
165+ * Sort the site array by full name.
166+ */
167+ usort ($ results ['data ' ], function ($ a , $ b ) {
168+ if ($ a ['site_full_name ' ] == $ b ['site_full_name ' ]) {
169+ return 0 ;
134170 }
135171
136- /**
137- * Store the cookies from the controller for faster reconnecting.
138- */
139- $ _SESSION ['unificookie ' ] = $ unifi_connection ->get_cookie ();
140-
141- /**
142- * Loop through the fetched sites.
143- */
144- foreach ($ sites_array as $ site ) {
145- $ results ['data ' ][] = [
146- 'site_id ' => $ site ->name ?? $ unknown_string ,
147- 'site_full_name ' => $ site ->desc ?? $ unknown_string ,
148- ];
149- }
172+ return ($ a ['site_full_name ' ] < $ b ['site_full_name ' ]) ? -1 : 1 ;
173+ });
150174
151- /**
152- * Sort the site array by full name.
153- */
154- usort ($ results ['data ' ], function ($ a , $ b ) {
155- if ($ a ['site_full_name ' ] == $ b ['site_full_name ' ]) {
156- return 0 ;
157- }
158-
159- return ($ a ['site_full_name ' ] < $ b ['site_full_name ' ]) ? -1 : 1 ;
160- });
161-
162- /**
163- * Get the first site from the $results array, just to be sure we use a valid site.
164- */
165- $ switch_site = $ unifi_connection ->set_site (($ results ['data ' ][0 ]['site_id ' ]));
166- $ site_info = $ unifi_connection ->stat_sysinfo ();
167-
168- if (!empty ($ site_info ) && isset ($ site_info [0 ]->version )) {
169- $ _SESSION ['controller ' ]['detected_version ' ] = $ site_info [0 ]->version ;
170- } else {
171- $ _SESSION ['controller ' ]['detected_version ' ] = 'undetected ' ;
172- }
175+ /**
176+ * Get the first site from the $results array, just to be sure we use a valid site.
177+ */
178+ $ switch_site = $ unifi_connection ->set_site (($ results ['data ' ][0 ]['site_id ' ]));
179+ $ site_info = $ unifi_connection ->stat_sysinfo ();
180+
181+ if (!empty ($ site_info ) && isset ($ site_info [0 ]->version )) {
182+ $ _SESSION ['controller ' ]['detected_version ' ] = $ site_info [0 ]->version ;
183+ } else {
184+ $ _SESSION ['controller ' ]['detected_version ' ] = 'undetected ' ;
173185 }
174186 }
175187 } else {
179191 }
180192}
181193
182- returnJson ($ results );
183-
184194$ _SESSION ['memory_used ' ] = round (memory_get_peak_usage (false ) / 1024 / 1024 , 2 ) . 'MB ' ;
195+
196+ returnJson ($ results );
0 commit comments