Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 117 additions & 68 deletions src/Unms.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ 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;


/**
* Constructor of the API
* @param string $user The username used to connect to the UNMS Api
* @param string $password The account password
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -483,25 +486,48 @@ 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 The return value from the API or FALSE if an error occured
*/

/**
* 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 occurred
*/
public function getDevices($siteId)
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/v2.1/devices?siteId='.$siteId);
$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
* @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)
{
Expand All @@ -513,7 +539,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)
{
Expand All @@ -525,7 +551,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)
{
Expand All @@ -537,7 +563,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)
{
Expand All @@ -549,29 +575,20 @@ 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)
{
if (!$this->is_loggedin) return false;
$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);
Expand All @@ -594,7 +611,6 @@ protected function process_response($response_json)

}


/**
* Capture the latest JSON error when $this->debug is true
*/
Expand Down Expand Up @@ -743,18 +759,18 @@ 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));
$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);

$token= false;
$token = false;

if($header && isset($header['x-auth-token']))
{
Expand All @@ -766,10 +782,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;
Expand Down Expand Up @@ -877,11 +896,41 @@ 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);

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 "<pre>";
var_dump($text);
die;
}
}