From e72dea735707ed308eef3c467e244a0f7248ab49 Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Thu, 6 Feb 2020 11:51:20 +0100 Subject: [PATCH] API client class v1.1.48 - applied several patches to Client.php as suggested by scrutinizer-ci.com - moved resource checks to get_curl_resource() method/function - extended create_user() function/method with several optional parameters - changed headers that are passed with each request containing a payload --- src/Client.php | 57 +++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Client.php b/src/Client.php index 93e9ef9..39e6d52 100755 --- a/src/Client.php +++ b/src/Client.php @@ -127,9 +127,9 @@ class Client } /** - * check we have a "regular" controller or one based on UniFi OS + * check whether we have a "regular" controller or one based on UniFi OS */ - if (!is_resource($ch = $this->get_curl_resource())) { + if (!($ch = $this->get_curl_resource())) { trigger_error('$ch as returned by get_curl_resource() is not a resource'); } else { curl_setopt($ch, CURLOPT_HEADER, true); @@ -140,7 +140,7 @@ class Client /** * execute the cURL request and get the HTTP response code */ - $content = curl_exec($ch); + curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (curl_errno($ch)) { @@ -393,14 +393,16 @@ class Client * can be obtained from the output of list_usergroups() * optional parameter = name to be given to the new user/client-device * optional parameter = note to be applied to the new user/client-device + * optional parameter = boolean; defines whether the new user/client-device is a guest or not + * optional parameter = boolean; defines whether the new user/client-device is wired or not */ - public function create_user($mac, $user_group_id, $name = null, $note = null) + public function create_user($mac, $user_group_id, $name = null, $note = null, $is_guest = null, $is_wired = null) { if (!$this->is_loggedin) { return false; } - $new_user = ['mac' => strtolower($mac), 'usergroup_id' => $user_group_id]; + $new_user = ['mac' => strtolower($mac), 'usergroup_id' => $user_group_id]; if (!is_null($name)) { $new_user['name'] = $name; } @@ -410,6 +412,14 @@ class Client $new_user['noted'] = true; } + if (!is_null($is_guest) && is_bool($is_guest)) { + $new_user['is_guest'] = $is_guest; + } + + if (!is_null($is_wired) && is_bool($is_wired)) { + $new_user['is_wired'] = $is_wired; + } + $payload = ['objects' => [['data' => $new_user]]]; $response = $this->exec_curl('/api/s/' . $this->site . '/group/user', $payload); @@ -3967,10 +3977,10 @@ class Client trigger_error('an invalid HTTP request type was used: ' . $this->request_type); } - if (!is_resource($ch = $this->get_curl_resource())) { + if (!($ch = $this->get_curl_resource())) { trigger_error('$ch as returned by get_curl_resource() is not a resource'); } else { - $json_payload = []; + $json_payload = ''; if ($this->is_unifi_os) { $url = $this->baseurl . '/proxy/network' . $path; @@ -3986,9 +3996,8 @@ class Client curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload); $headers = [ - 'Content-Type: application/json', - 'Content-Length: ' . strlen($json_payload), - 'Accept: application/json' + 'Content-Type: application/json;charset=UTF-8', + 'Content-Length: ' . strlen($json_payload) ]; if ($this->is_unifi_os) { @@ -4112,20 +4121,24 @@ class Client protected function get_curl_resource() { $ch = curl_init(); - 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); + 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); - if ($this->debug) { - curl_setopt($ch, CURLOPT_VERBOSE, true); + if ($this->debug) { + curl_setopt($ch, CURLOPT_VERBOSE, true); + } + + if ($this->cookies != '') { + curl_setopt($ch, CURLOPT_COOKIESESSION, true); + curl_setopt($ch, CURLOPT_COOKIE, $this->cookies); + } + + return $ch; } - if ($this->cookies != '') { - curl_setopt($ch, CURLOPT_COOKIESESSION, true); - curl_setopt($ch, CURLOPT_COOKIE, $this->cookies); - } - - return $ch; + return false; } } \ No newline at end of file