mirror of
https://github.com/Art-of-WiFi/UniFi-API-client.git
synced 2024-11-22 02:00:21 +01:00
code cleanup and improvements in preparation of support for UniFi OS-based controllers
This commit is contained in:
parent
a3fc0732e2
commit
796ad9a82d
@ -72,8 +72,8 @@ $current_conf = $data[0]->port_overrides;
|
|||||||
/**
|
/**
|
||||||
* This reads in the values provided via URL or in the command line, if nothing is set than it will poe_mode will be set to "auto"
|
* This reads in the values provided via URL or in the command line, if nothing is set than it will poe_mode will be set to "auto"
|
||||||
*/
|
*/
|
||||||
if (isset($_GET[$poe_mode])) {
|
if (isset($_GET['poe_mode'])) {
|
||||||
$poe_mode = $_GET[poe_mode];
|
$poe_mode = $_GET['poe_mode'];
|
||||||
} elseif (isset($argv[1])) {
|
} elseif (isset($argv[1])) {
|
||||||
$poe_mode = $argv[1];
|
$poe_mode = $argv[1];
|
||||||
} else {
|
} else {
|
||||||
|
@ -121,11 +121,13 @@ class Client
|
|||||||
curl_setopt($ch, CURLOPT_REFERER, $this->baseurl . '/login');
|
curl_setopt($ch, CURLOPT_REFERER, $this->baseurl . '/login');
|
||||||
curl_setopt($ch, CURLOPT_URL, $this->baseurl . '/api/login');
|
curl_setopt($ch, CURLOPT_URL, $this->baseurl . '/api/login');
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type: application/json; charset=utf-8']);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* execute the cURL request
|
* execute the cURL request and get the HTTP response code
|
||||||
*/
|
*/
|
||||||
$content = curl_exec($ch);
|
$content = curl_exec($ch);
|
||||||
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
if (curl_errno($ch)) {
|
if (curl_errno($ch)) {
|
||||||
trigger_error('cURL error: ' . curl_error($ch));
|
trigger_error('cURL error: ' . curl_error($ch));
|
||||||
@ -143,28 +145,28 @@ class Client
|
|||||||
print '</pre>' . PHP_EOL;
|
print '</pre>' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* based on the HTTP response code we either trigger an error or
|
||||||
|
* extract the cookie from the headers
|
||||||
|
*/
|
||||||
|
if ($http_code === 400) {
|
||||||
|
trigger_error('We have received an HTTP response status: 400. Probably a controller login failure');
|
||||||
|
|
||||||
|
return $http_code;
|
||||||
|
}
|
||||||
|
|
||||||
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||||
$headers = substr($content, 0, $header_size);
|
$headers = substr($content, 0, $header_size);
|
||||||
$body = trim(substr($content, $header_size));
|
$body = trim(substr($content, $header_size));
|
||||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
preg_match_all('|Set-Cookie: (.*);|Ui', $headers, $results);
|
if ($http_code >= 200 && $http_code < 400 && !empty($body)) {
|
||||||
|
preg_match_all('|Set-Cookie: (.*);|Ui', $headers, $results);
|
||||||
if (isset($results[1])) {
|
if (isset($results[1])) {
|
||||||
$this->cookies = implode(';', $results[1]);
|
$this->cookies = implode(';', $results[1]);
|
||||||
if (!empty($body)) {
|
if (strpos($this->cookies, 'unifises') !== false) {
|
||||||
if (($http_code >= 200) && ($http_code < 400)) {
|
return $this->is_loggedin = true;
|
||||||
if (strpos($this->cookies, 'unifises') !== false) {
|
|
||||||
return $this->is_loggedin = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($http_code === 400) {
|
|
||||||
trigger_error('We have received an HTTP response status: 400. Probably a controller login failure');
|
|
||||||
|
|
||||||
return $http_code;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2079,6 +2081,32 @@ class Client
|
|||||||
return $this->process_response($response);
|
return $this->process_response($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List filtered DPI stats
|
||||||
|
* -----------------------
|
||||||
|
* returns an array of fileterd DPI stats
|
||||||
|
* optional parameter <type> = whether to returns stats by app or by category, valid values:
|
||||||
|
* 'by_cat' or 'by_app'
|
||||||
|
* optional parameter <cat_filter> = an array containing numeric category ids to filter by,
|
||||||
|
* only to be combined with a "by_app" value for $type
|
||||||
|
*/
|
||||||
|
public function list_dpi_stats_filtered($type = 'by_cat', $cat_filter = null)
|
||||||
|
{
|
||||||
|
if (!$this->is_loggedin || !in_array($type, ['by_cat', 'by_app'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = ['type' => $type];
|
||||||
|
|
||||||
|
if ($cat_filter !== null && $type == 'by_app' && is_array($cat_filter)) {
|
||||||
|
$payload['cats'] = $cat_filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $this->exec_curl('/api/s/' . $this->site . '/stat/sitedpi', $payload);
|
||||||
|
|
||||||
|
return $this->process_response($response);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List current channels
|
* List current channels
|
||||||
* ---------------------
|
* ---------------------
|
||||||
@ -3597,6 +3625,10 @@ class Client
|
|||||||
******************************************************************/
|
******************************************************************/
|
||||||
public function get_cookies()
|
public function get_cookies()
|
||||||
{
|
{
|
||||||
|
if (!$this->is_loggedin) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->cookies;
|
return $this->cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user