Skip to content

Commit

Permalink
API client class v1.1.56
Browse files Browse the repository at this point in the history
- switched to use of curl_setopt_array instead of curl_setopt which should slightly reduce the overall PHP execution time (https://gist.github.com/masakielastic/3330620)
- several minor code improvements
  • Loading branch information
malle-pietje committed Jun 13, 2020
1 parent 89ce2fd commit 0d99d4a
Showing 1 changed file with 69 additions and 42 deletions.
111 changes: 69 additions & 42 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Client
protected $is_loggedin = false;
protected $is_unifi_os = false;
protected $exec_retries = 0;
protected $class_version = '1.1.55';
protected $class_version = '1.1.56';
private $cookies = '';
private $request_type = 'GET';
private $request_types_allowed = ['GET', 'POST', 'PUT', 'DELETE'];
Expand Down Expand Up @@ -92,8 +92,7 @@ public function __construct($user, $password, $baseurl = '', $site = '', $versio
* https://www.php.net/manual/en/language.oop5.decon.php
*
* NOTES:
* to force the class instance to log out when you're done, simply call logout() or unset
* $_SESSION['unificookie']
* to force the class instance to log out when you're done, simply call logout()
*/
public function __destruct()
{
Expand Down Expand Up @@ -133,13 +132,18 @@ public function login()
}

/**
* first we check whether we have a "regular" controller or one based on UniFi OS
* first we check whether we have a "regular" controller or one based on UniFi OS,
* prepare cURL and options
*/
$ch = $this->get_curl_resource();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_URL, $this->baseurl . '/');
$curl_options = [
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_NOBODY => true,
CURLOPT_URL => $this->baseurl . '/',
];

curl_setopt_array($ch, $curl_options);

/**
* execute the cURL request and get the HTTP response code
Expand All @@ -151,18 +155,25 @@ public function login()
trigger_error('cURL error: ' . curl_error($ch));
}

/**
* we now proceed with the actual login
*/
$curl_options = [
CURLOPT_NOBODY => false,
CURLOPT_POSTFIELDS => json_encode(['username' => $this->user, 'password' => $this->password]),
CURLOPT_HTTPHEADER => ['content-type: application/json; charset=utf-8']
];

if ($http_code === 200) {
$this->is_unifi_os = true;
curl_setopt($ch, CURLOPT_REFERER, $this->baseurl . '/login');
curl_setopt($ch, CURLOPT_URL, $this->baseurl . '/api/auth/login');
$this->is_unifi_os = true;
$curl_options[CURLOPT_REFERER] = $this->baseurl . '/login';
$curl_options[CURLOPT_URL] = $this->baseurl . '/api/auth/login';
} else {
curl_setopt($ch, CURLOPT_REFERER, $this->baseurl . '/login');
curl_setopt($ch, CURLOPT_URL, $this->baseurl . '/api/login');
$curl_options[CURLOPT_REFERER] = $this->baseurl . '/login';
$curl_options[CURLOPT_URL] = $this->baseurl . '/api/login';
}

curl_setopt($ch, CURLOPT_NOBODY, false);
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']);
curl_setopt_array($ch, $curl_options);

/**
* execute the cURL request and get the HTTP response code
Expand Down Expand Up @@ -235,19 +246,21 @@ public function login()
public function logout()
{
/**
* prepare cURL
* prepare cURL and options
*/
$ch = $this->get_curl_resource();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
$curl_options = [
CURLOPT_HEADER => true,
CURLOPT_POST => true
];

/**
* constuct HTTP request headers as required
*/
$headers = ['Content-Length: 0'];
if ($this->is_unifi_os) {
$logout_path = '/api/auth/logout';
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$curl_options[CURLOPT_CUSTOMREQUEST] = 'POST';

$csrf_token = $this->extract_csrf_token_from_cookie();
if ($csrf_token) {
Expand All @@ -257,8 +270,10 @@ public function logout()
$logout_path = '/logout';
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $this->baseurl . $logout_path);
$curl_options[CURLOPT_HTTPHEADER] = $headers;
$curl_options[CURLOPT_URL] = $this->baseurl . $logout_path;

curl_setopt_array($ch, $curl_options);

/**
* execute the cURL request to logout
Expand Down Expand Up @@ -3145,7 +3160,7 @@ public function get_connection_timeout()
protected function fetch_results($path, $payload = null, $boolean = false, $login_required = true)
{
/**
* guard clause to check if logged in when needed
* guard clause to check if we are logged in when needed
*/
if ($login_required && !$this->is_loggedin) {
return false;
Expand All @@ -3170,6 +3185,7 @@ protected function fetch_results($path, $payload = null, $boolean = false, $logi
if (isset($response->meta->msg)) {
$this->last_error_message = $response->meta->msg;
}

if ($this->debug) {
trigger_error('Debug: Last error message: ' . $this->last_error_message);
}
Expand Down Expand Up @@ -3244,7 +3260,7 @@ private function catch_json_last_error()
break;
}

if ($error !== '') {
if (!empty($error)) {
trigger_error('JSON decode error: ' . $error);

return false;
Expand Down Expand Up @@ -3308,7 +3324,7 @@ private function update_unificookie()
*/
private function extract_csrf_token_from_cookie()
{
if ($this->cookies !== '') {
if (!empty($this->cookies)) {
$cookie_bits = explode('=', $this->cookies);
if (!empty($cookie_bits) && array_key_exists(1, $cookie_bits)) {
$jwt = $cookie_bits[1];
Expand Down Expand Up @@ -3349,12 +3365,17 @@ protected function exec_curl($path, $payload = null)
$url = $this->baseurl . $path;
}

curl_setopt($ch, CURLOPT_URL, $url);
/**
* prepare cURL options
*/
$curl_options = [
CURLOPT_URL => $url
];

if ($payload !== null) {
curl_setopt($ch, CURLOPT_POST, true);
$json_payload = json_encode($payload, JSON_UNESCAPED_SLASHES);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload);
$json_payload = json_encode($payload, JSON_UNESCAPED_SLASHES);
$curl_options[CURLOPT_POST] = true;
$curl_options[CURLOPT_POSTFIELDS] = $json_payload;

$headers = [
'Content-Type: application/json;charset=UTF-8',
Expand All @@ -3368,7 +3389,7 @@ protected function exec_curl($path, $payload = null)
}
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_options[CURLOPT_HTTPHEADER] = $headers;

/**
* we shouldn't be using GET (the default request type) or DELETE when passing a payload,
Expand All @@ -3379,18 +3400,20 @@ protected function exec_curl($path, $payload = null)
}

if ($this->request_type === 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$curl_options[CURLOPT_CUSTOMREQUEST] = 'PUT';
}

if ($this->request_type === 'POST') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$curl_options[CURLOPT_CUSTOMREQUEST] = 'POST';
}
}

if ($this->request_type === 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$curl_options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
}

curl_setopt_array($ch, $curl_options);

/**
* execute the cURL request
*/
Expand Down Expand Up @@ -3491,21 +3514,25 @@ protected function get_curl_resource()
{
$ch = curl_init();
if (is_resource($ch)) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->curl_ssl_verify_peer);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->curl_ssl_verify_host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
curl_setopt($ch, CURLOPT_ENCODING, '');
$curl_options = [
CURLOPT_SSL_VERIFYPEER => $this->curl_ssl_verify_peer,
CURLOPT_SSL_VERIFYHOST => $this->curl_ssl_verify_host,
CURLOPT_CONNECTTIMEOUT => $this->connect_timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
];

if ($this->debug) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
$curl_options[CURLOPT_VERBOSE] = true;
}

if ($this->cookies != '') {
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
if (!empty($this->cookies)) {
$curl_options[CURLOPT_COOKIESESSION] = true;
$curl_options[CURLOPT_COOKIE] = $this->cookies;
}

curl_setopt_array($ch, $curl_options);

return $ch;
}

Expand Down

0 comments on commit 0d99d4a

Please sign in to comment.