Skip to content

Commit

Permalink
completed the implementation of support for UniFi API Client v2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
malle-pietje committed Nov 28, 2024
1 parent e60ffb2 commit ff0d89c
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 301 deletions.
28 changes: 25 additions & 3 deletions ajax/fetch_collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
/**
* Create an instance of the Unifi API client class, log in to the controller and pull the requested data.
*/

try {
$unifi_connection = new ApiClient(
trim($controller['user']),
Expand All @@ -144,31 +143,42 @@
$site_id
);

$login_results = $unifi_connection->login();
$unifi_connection->login();
} catch (CurlExtensionNotLoadedException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'cURL is not available in your PHP installation!';
return;
} catch (CurlGeneralErrorException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'We have encountered a general cURL error! Please check the logs';
$results['message'] = 'We have encountered a general cURL error! Response code: ' . $e->getHttpResponseCode();
return;
} catch (CurlTimeoutException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'UniFi controller connection timeout!';
return;
} catch (InvalidBaseUrlException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, base URL is invalid!';
return;
} catch (InvalidSiteNameException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, site name is invalid!';
return;
} catch (LoginFailedException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, please check your credentials in config/config.php!';
return;
} catch (Exception $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'An Exception was thrown:' . $e->getMessage();
return;
}

/**
Expand All @@ -188,13 +198,25 @@
$request_results = $unifi_connection->{$method}(...$params);
}
} catch (JsonDecodeException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'JSON decode error!';
return;
} catch (LoginRequiredException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'Login is required for this endpoint';
return;
} catch (LoginFailedException $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, please check your credentials in config/config.php!';
return;
} catch (Exception $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
$results['state'] = 'error';
$results['message'] = 'An Exception was thrown:' . $e->getMessage();
return;
}

if (!empty($request_results)) {
Expand Down
206 changes: 109 additions & 97 deletions ajax/fetch_sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*
* @var string $unknown_string
*/

use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
use UniFi_API\Exceptions\CurlGeneralErrorException;
use UniFi_API\Exceptions\CurlTimeoutException;
Expand Down Expand Up @@ -65,111 +64,124 @@
$port = parse_url($controller['url'], PHP_URL_PORT) ?: 443;

if (!empty($host) && !empty($port)) {
$fp = @fsockopen($host, $port, $errno, $errstr, 2);
if (!$fp) {
error_log("we have a connection error $errstr ($errno)");
/**
* Create an instance of the Unifi API client class, log in to the controller and pull the requested data.
*/
try {
$unifi_connection = new UniFi_API\Client(
trim($controller['user']),
trim($controller['password']),
trim(rtrim($controller['url'], "/")),
'default'
);

$unifi_connection->login();
} catch (CurlExtensionNotLoadedException $e) {
$results['state'] = 'error';
$results['message'] = 'cURL is not available in your PHP installation!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
} catch (CurlGeneralErrorException $e) {
$results['state'] = 'error';
$results['message'] = 'We have encountered a general cURL error! Response code: ' . $e->getHttpResponseCode();
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
} catch (CurlTimeoutException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller connection timeout!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
} catch (InvalidBaseUrlException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, base URL is invalid!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
} catch (InvalidSiteNameException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, site name is invalid!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
} catch (LoginFailedException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, please check the URL and credentials in config/config.php!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
} catch (Exception $e) {
$results['state'] = 'error';
$results['message'] = 'An Exception was thrown:' . $e->getMessage();
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
}

if ($results['state'] === 'error' || empty($unifi_connection)) {
error_log('we have an error, bailing out');
$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';
returnJson($results);

exit;
}

/**
* We can safely continue.
*/
try {
$sites_array = $unifi_connection->list_sites();
} catch (JsonDecodeException $e) {
$results['state'] = 'error';
$results['message'] = 'JSON decode error!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
return;
} catch (LoginRequiredException $e) {
$results['state'] = 'error';
$results['message'] = 'Login is required for this endpoint';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
return;
} catch (LoginFailedException $e) {
$results['state'] = 'error';
$results['message'] = "we are unable to connect to the UniFi controller, $errstr ($errno)!";
} else {
$results['message'] = 'UniFi controller login failure, please check your credentials in config/config.php!';
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
return;
} catch (Exception $e) {
$results['state'] = 'error';
$results['message'] = 'An Exception was thrown:' . $e->getMessage();
error_log('Exception: ' . get_class($e) . ' - Message: ' . $e->getMessage());
return;
}

if (!empty($sites_array)) {
if ($debug) {
error_log('DEBUG: ' . count($sites_array) . ' sites collected');
}

/**
* And we can continue.
* Store the cookies from the controller for faster reconnecting.
*/
fclose($fp);

try {
/**
* Create an instance of the Unifi API client class, log in to the controller and pull the requested data.
*/
$unifi_connection = new UniFi_API\Client(
trim($controller['user']),
trim($controller['password']),
trim(rtrim($controller['url'], "/")),
'default'
);
$login_results = $unifi_connection->login();
} catch (CurlExtensionNotLoadedException $e) {
$results['state'] = 'error';
$results['message'] = 'cURL is not available in your PHP installation!';
return;
} catch (CurlGeneralErrorException $e) {
$results['state'] = 'error';
$results['message'] = 'We have encountered a general cURL error! Please check the logs';
return;
} catch (CurlTimeoutException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller connection timeout!';
return;
} catch (InvalidBaseUrlException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, base URL is invalid!';
return;
} catch (InvalidSiteNameException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, site name is invalid!';
return;
} catch (LoginFailedException $e) {
$results['state'] = 'error';
$results['message'] = 'UniFi controller login failure, please check your credentials in config/config.php!';
return;
}
$_SESSION['unificookie'] = $unifi_connection->get_cookie();

/**
* We can safely continue.
* Loop through the fetched sites.
*/
try {
$sites_array = $unifi_connection->list_sites();
} catch (JsonDecodeException $e) {
$results['state'] = 'error';
$results['message'] = 'JSON decode error!';
return;
} catch (LoginRequiredException $e) {
$results['state'] = 'error';
$results['message'] = 'Login is required for this endpoint';
return;
foreach ($sites_array as $site) {
$results['data'][] = [
'site_id' => $site->name ?? $unknown_string,
'site_full_name' => $site->desc ?? $unknown_string,
];
}

if (!empty($sites_array)) {
if ($debug) {
error_log('DEBUG: ' . count($sites_array) . ' sites collected');
/**
* Sort the site array by full name.
*/
usort($results['data'], function ($a, $b) {
if ($a['site_full_name'] == $b['site_full_name']) {
return 0;
}

/**
* Store the cookies from the controller for faster reconnecting.
*/
$_SESSION['unificookie'] = $unifi_connection->get_cookie();

/**
* Loop through the fetched sites.
*/
foreach ($sites_array as $site) {
$results['data'][] = [
'site_id' => $site->name ?? $unknown_string,
'site_full_name' => $site->desc ?? $unknown_string,
];
}
return ($a['site_full_name'] < $b['site_full_name']) ? -1 : 1;
});

/**
* Sort the site array by full name.
*/
usort($results['data'], function ($a, $b) {
if ($a['site_full_name'] == $b['site_full_name']) {
return 0;
}

return ($a['site_full_name'] < $b['site_full_name']) ? -1 : 1;
});

/**
* Get the first site from the $results array, just to be sure we use a valid site.
*/
$switch_site = $unifi_connection->set_site(($results['data'][0]['site_id']));
$site_info = $unifi_connection->stat_sysinfo();

if (!empty($site_info) && isset($site_info[0]->version)) {
$_SESSION['controller']['detected_version'] = $site_info[0]->version;
} else {
$_SESSION['controller']['detected_version'] = 'undetected';
}
/**
* Get the first site from the $results array, just to be sure we use a valid site.
*/
$switch_site = $unifi_connection->set_site(($results['data'][0]['site_id']));
$site_info = $unifi_connection->stat_sysinfo();

if (!empty($site_info) && isset($site_info[0]->version)) {
$_SESSION['controller']['detected_version'] = $site_info[0]->version;
} else {
$_SESSION['controller']['detected_version'] = 'undetected';
}
}
} else {
Expand All @@ -179,6 +191,6 @@
}
}

returnJson($results);

$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';

returnJson($results);
Loading

0 comments on commit ff0d89c

Please sign in to comment.