From dfab267a18f0e672e9b83a57e6957e0e60014659 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 8 Oct 2020 13:29:57 +0700 Subject: [PATCH 1/3] fixing --- src/Unms.php | 135 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 54 deletions(-) diff --git a/src/Unms.php b/src/Unms.php index 3d01a8d..c8a1269 100644 --- a/src/Unms.php +++ b/src/Unms.php @@ -29,9 +29,12 @@ class Unms private $last_error_message = null; private $curl_ssl_verify_peer = false; private $curl_ssl_verify_host = false; //TODO use it in the init curl - - - /** + private $last_results_raw; + private $user; + private $password; + + + /** * Constructor of the API * @param string $user The username used to connect to the UNMS Api * @param string $password The account password @@ -67,13 +70,14 @@ function __destruct() */ if ($this->is_loggedin) $this->logout(); } - - /** - * Set debug mode - * -------------- - * sets debug mode to true or false, returns false if a non-boolean parameter was passed - * @param boolean $enable true will enable debug mode, false will disable it - */ + + /** + * Set debug mode + * -------------- + * sets debug mode to true or false, returns false if a non-boolean parameter was passed + * @param boolean $enable true will enable debug mode, false will disable it + * @return bool + */ public function set_debug($enable) { if ($enable === true || $enable === false) { @@ -99,7 +103,7 @@ public function set_debug($enable) * integer height The site height above ground, as displayed in the UNMS panel * integer elevation The site elevation, as displayed in the UNMS panel. Will be calculatd automatically if nothing is present * - * @return object The return value from the API + * @return object|bool The return value from the API */ public function create_site($site) { @@ -132,7 +136,6 @@ public function create_site($site) 'note' => $site['note'], 'height' => $site['height'], 'elevation' => $site['elevation'], - 'location' => $site['location'], ]); $response = $this->exec_curl('/v2.1/sites', $json); @@ -144,7 +147,7 @@ public function create_site($site) * @param string $siteId The site ID to edit * @param string $name THe new name to be setted * - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function editSiteName($siteId, $name) { @@ -161,7 +164,7 @@ public function editSiteName($siteId, $name) /** * Return the site's object * @param string $siteId The site id to get - * @return object The entire site's object + * @return object|bool The entire site's object */ public function getSites($siteId) { @@ -175,7 +178,7 @@ public function getSites($siteId) * @param string $siteId the site id to set * @param object $data the valid data of the site to set * - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ protected function setSite($siteId, $data) { @@ -192,7 +195,7 @@ protected function setSite($siteId, $data) * Move a device from one site to the other * @param string $deviceId The device to move * @param string $siteId The site id in which to move the device - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function authorizeDevice($deviceId, $siteId) { @@ -206,7 +209,7 @@ public function authorizeDevice($deviceId, $siteId) /** * Get the Wireless configuration from an aircube * @param string $aircubeId The aircube ID to get the data from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getAircubeWirelessConfig($aircubeId) { @@ -218,7 +221,7 @@ public function getAircubeWirelessConfig($aircubeId) /** * Get the Network configuration from an aircube * @param string $aircubeId The aircube ID to get the data from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getAircubeNetworkConfig($aircubeId) { @@ -230,7 +233,7 @@ public function getAircubeNetworkConfig($aircubeId) /** * Get the System configuration from an aircube * @param string $aircubeId The aircube ID to get the data from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getAircubeSystemConfig($aircubeId) { @@ -243,7 +246,7 @@ public function getAircubeSystemConfig($aircubeId) * Set the aircube led On or Off * @param string $aircubeId The aircube ID to set the led * @param boolean $is_on Wether to turn On or Off the aircube led - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function setCubeLedOn($aircubeId, $is_on) { @@ -265,7 +268,7 @@ public function setCubeLedOn($aircubeId, $is_on) * Set the aircube name * @param string $aircubeId The aircube ID to set the name * @param string $name The new name to be setted to the cube - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function setCubeName($aircubeId, $name) { @@ -285,7 +288,7 @@ public function setCubeName($aircubeId, $name) * Set the aircube reset swith available or not * @param string $aircubeId The aircube ID to set the name * @param string $is_enabled Wether or not the reset switch is available - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function setCubeResetEnabled($aircubeId, $is_enabled) { @@ -304,7 +307,7 @@ public function setCubeResetEnabled($aircubeId, $is_enabled) * Set the aircube POE On or Off * @param string $aircubeId The aircube ID to set the name * @param string $is_enabled Wether or not the POE is turned On - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function setCubePoeEnabled($aircubeId, $is_enabled) { @@ -335,7 +338,7 @@ public function setCubePoeEnabled($aircubeId, $is_enabled) * username The Administrator username * newPassword The administrator password * - * @return object The return value from the API + * @return object|bool The return value from the API */ public function setAircubeSystemConfig($aircubeId, $config) { @@ -373,7 +376,7 @@ public function setAircubeSystemConfig($aircubeId, $config) * @param string $aircubeId the aircube ID to set the config * @param array $config the configuration array (see UNMS doc for details. Upcoming here in future version) * - * @return object The return value from the API + * @return object|bool The return value from the API */ public function setAircubeNetworkConfig($aircubeId, $config) { @@ -435,7 +438,7 @@ public function setAircubeNetworkConfig($aircubeId, $config) * @param string $aircubeId the aircube ID to set the config * @param array $config the configuration array (see UNMS doc for details. Upcoming here in future version) * - * @return object The return value from the API + * @return object|bool The return value from the API */ public function setAircubeWirelessConfig($aircubeId, $config) { @@ -489,7 +492,7 @@ public function setAircubeWirelessConfig($aircubeId, $config) /** * Get the devices at the root of a site. This will not return child sites devices * @param string $siteId the site Id to get the devices from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getDevices($siteId) { @@ -501,7 +504,7 @@ public function getDevices($siteId) /** * Get the interfaces from the router specified * @param string $deviceId The router's device ID - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getRouterInterfaces($deviceId) { @@ -513,7 +516,7 @@ public function getRouterInterfaces($deviceId) /** * Get the specified aircube's data * @param string $aircubeId the aircube Id to get the data from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getAirCubeData($aircubeId) { @@ -525,7 +528,7 @@ public function getAirCubeData($aircubeId) /** * Get the clients connected to the specified aircube * @param string $aircubeId the aircube Id to get the clients from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getAirCubeDevices($aircubeId) { @@ -537,7 +540,7 @@ public function getAirCubeDevices($aircubeId) /** * Get the DHCP Lease from the specified router * @param string $id the router device Id to get the DHCP Lease from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function geteRouterDHCPLeases($id) { @@ -549,7 +552,7 @@ public function geteRouterDHCPLeases($id) /** * Get the Wireless network informations from an aircube * @param string $aircubeId the aircube device Id to get the wireless info from - * @return object The return value from the API or FALSE if an error occured + * @return object|bool The return value from the API or FALSE if an error occured */ public function getAircubeWirelessInfo($aircubeId) { @@ -557,21 +560,12 @@ public function getAircubeWirelessInfo($aircubeId) $response = $this->exec_curl('/v2.1/devices/aircubes/'.$aircubeId.'/config/wireless'); return $this->process_response($response); } - - - - - - - - - - - - - /** - * Process regular responses where output is the content of the data array - */ + + /** + * Process regular responses where output is the content of the data array + * @param $response_json + * @return bool|mixed + */ protected function process_response($response_json) { $response = json_decode($response_json); @@ -743,10 +737,10 @@ public function login() if($content === false) { - output('Curl error: ' . curl_error($curl_login)); + $this->output('Curl error: ' . curl_error($curl_login)); } - $header = \Ubnt::get_headers_from_curl_response($content); + $header = $this->get_headers_from_curl_response($content); $header_size = curl_getinfo($curl_login, CURLINFO_HEADER_SIZE); $body = trim(substr($content, $header_size)); @@ -754,7 +748,7 @@ public function login() curl_close ($curl_login); - $token= false; + $token = false; if($header && isset($header['x-auth-token'])) { @@ -766,10 +760,13 @@ public function login() $this->is_loggedin = true; return true; } - - /** - * Execute the cURL request - */ + + /** + * Execute the cURL request + * @param $path + * @param string $data + * @return bool|string + */ protected function exec_curl($path, $data = '') { $url = 'https://'.$this->baseurl.$path; @@ -884,4 +881,34 @@ private function get_curl_obj() return $ch; } + + /** + * Parse curl header object + * @param $headerContent + * @return array + */ + private function get_headers_from_curl_response($headerContent) + { + $headers = array(); + foreach (explode(PHP_EOL, $headerContent) as $i => $line) { + if ($i === 0) { + $headers['http_code'] = $line; + } else { + if (strpos($line, ': ') !== false) { + list($key, $value) = explode(': ', $line); + $headers[$key] = $value; + } + } + } + return $headers; + } + + /** + * @param $text + */ + private function output($text){ + echo "
";
+		var_dump($text);
+		die;
+	}
 }

From 34f103a8f69517d4144e35fe1fc314295c9a7144 Mon Sep 17 00:00:00 2001
From: denser 
Date: Tue, 20 Oct 2020 15:29:51 +0700
Subject: [PATCH 2/3] Add ability to get some devices with clients. Sneaked in
 unms web UI. Unused variables commented for future use. Fixed some typos.

---
 src/Unms.php | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/Unms.php b/src/Unms.php
index c8a1269..cd5f3e2 100644
--- a/src/Unms.php
+++ b/src/Unms.php
@@ -486,18 +486,20 @@ public function setAircubeWirelessConfig($aircubeId, $config)
         return $this->process_response($response);
 
     }
-
-
-
-    /**
-     * Get the devices at the root of a site. This will not return child sites devices
-     * @param  string $siteId the site Id to get the devices from
-     * @return object|bool       The return value from the API or FALSE if an error occured
-     */
-    public function getDevices($siteId)
+	
+	/**
+	 * Get the devices at the root of a site. This will not return child sites devices
+	 * @param string $siteId the site Id to get the devices from
+	 * @param string $type airfibers|airmaxes
+	 * @param string $id UUID of device
+	 * @return object|bool       The return value from the API or FALSE if an error occurred
+	 */
+    public function getDevices($siteId, $type = '', $id = '')
     {
         if (!$this->is_loggedin) return false;
-        $response    = $this->exec_curl('/v2.1/devices?siteId='.$siteId);
+        $suffix = "?siteId=$siteId";
+        if($type) $suffix = "/$type/$id?withStations=true";
+        $response    = $this->exec_curl("/v2.1/devices{$suffix}");
         return $this->process_response($response);
     }
 
@@ -588,7 +590,6 @@ protected function process_response($response_json)
 
     }
 
-
     /**
      * Capture the latest JSON error when $this->debug is true
      */
@@ -742,9 +743,9 @@ public function login()
 
             $header = $this->get_headers_from_curl_response($content);
 
-            $header_size = curl_getinfo($curl_login, CURLINFO_HEADER_SIZE);
-            $body        = trim(substr($content, $header_size));
-            $code        = curl_getinfo($curl_login, CURLINFO_HTTP_CODE);
+            //$header_size = curl_getinfo($curl_login, CURLINFO_HEADER_SIZE);
+            //$body        = trim(substr($content, $header_size));
+            //$code        = curl_getinfo($curl_login, CURLINFO_HTTP_CODE);
 
             curl_close ($curl_login);
 

From 389a7595c44ab4b6a0d1f3e3c7ebcc26152f373a Mon Sep 17 00:00:00 2001
From: denser 
Date: Thu, 5 Nov 2020 15:03:09 +0700
Subject: [PATCH 3/3] Add ability to get some devices with clients. Sneaked in
 unms web UI. Unused variables commented for future use. Fixed some typos. Add
 method to get device config Add method to try any API string Fix warnings
 about CURLOPT_SSL_VERIFYHOST (0, 1 nad 2 instead of bool)

---
 src/Unms.php | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/Unms.php b/src/Unms.php
index cd5f3e2..21ef762 100644
--- a/src/Unms.php
+++ b/src/Unms.php
@@ -28,7 +28,7 @@ class Unms
     private $request_type         = 'POST';
     private $last_error_message   = null;
     private $curl_ssl_verify_peer = false;
-    private $curl_ssl_verify_host = false; //TODO use it in the init curl
+    private $curl_ssl_verify_host = 0;
 	private $last_results_raw;
 	private $user;
 	private $password;
@@ -490,18 +490,39 @@ public function setAircubeWirelessConfig($aircubeId, $config)
 	/**
 	 * Get the devices at the root of a site. This will not return child sites devices
 	 * @param string $siteId the site Id to get the devices from
-	 * @param string $type airfibers|airmaxes
-	 * @param string $id UUID of device
 	 * @return object|bool       The return value from the API or FALSE if an error occurred
 	 */
-    public function getDevices($siteId, $type = '', $id = '')
+    public function getDevices($siteId)
     {
         if (!$this->is_loggedin) return false;
-        $suffix = "?siteId=$siteId";
-        if($type) $suffix = "/$type/$id?withStations=true";
-        $response    = $this->exec_curl("/v2.1/devices{$suffix}");
+        $response = $this->exec_curl("/v2.1/devices?siteId=$siteId");
         return $this->process_response($response);
     }
+	
+	/**
+	 * @param string $type airmaxes|airfibers etc
+	 * @param string $id
+	 * @return bool|mixed
+	 */
+	public function getDevice($type, $id)
+	{
+		if (!$this->is_loggedin) return false;
+		$suffix = "/$type/$id?withStations=true";
+		$response = $this->exec_curl("/v2.1/devices{$suffix}");
+		return $this->process_response($response);
+	}
+    
+    public function getDeviceWirelessConfig($type, $devId){
+	    if (!$this->is_loggedin) return false;
+		$response = $this->exec_curl("/v2.1/devices/{$type}/{$devId}/config/wireless");
+	    return $this->process_response($response);
+    }
+    
+    public function getAnyResult($str){
+	    if (!$this->is_loggedin) return false;
+		$response = $this->exec_curl("/v2.1/$str");
+	    return $this->process_response($response);
+    }
 
     /**
      * Get the interfaces from the router specified
@@ -875,7 +896,7 @@ private function get_curl_obj()
         }
 
         if($this->curl_ssl_verify_host) {
-            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
+            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->curl_ssl_verify_host);
         }
 
         if ($this->debug) curl_setopt($ch, CURLOPT_VERBOSE, true);