various changes and additions:

IMPORTANT CHANGE: renamed all functions/methods named add_*() to create_*()
improved data validation
modified several existing methods/functions to leverage REST endpoints
added create_network() method/function
added set_networksettings_base() method/function
added delete_network() method/function
added list_radius_profiles() method/function
added create_radius_account() method/function
added set_radius_account_base() method/function
added delete_radius_account() method/function
This commit is contained in:
malle-pietje
2017-09-08 12:36:12 +02:00
parent 0373c8369f
commit eba7819c17
2 changed files with 282 additions and 95 deletions

View File

@@ -13,28 +13,34 @@ If you'd like to support further development of this PHP API client class, pleas
This class currently supports the following functions/methods to get/set data through the UniFi controller API:
- login()
- logout()
- add_site()
- adopt_device()
- authorize_guest()
- unauthorize_guest()
- block_sta()
- unblock_sta()
- count_alarms()
- create_hotspotop()
- create_network()
- create_radius_account()
- create_site()
- create_usergroup()
- create_voucher()
- create_wlan()
- delete_network()
- delete_radius_account()
- delete_site()
- delete_usergroup()
- delete_wlan()
- disable_ap()
- edit_usergroup()
- extend_guest_validity()
- led_override()
- list_admins()
- list_alarms()
- count_alarms()
- upgrade_device()
- upgrade_device_external()
- spectrum_scan()
- spectrum_scan_state()
- list_devices()
- list_aps() (deprecated but still available as alias)
- list_clients()
- list_current_channels()
- list_dashboard()
- list_devices()
- list_dpi_stats()
- list_dynamicdns()
- list_events()
- list_extension()
@@ -46,6 +52,7 @@ This class currently supports the following functions/methods to get/set data th
- list_portforward_stats()
- list_portforwarding()
- list_radius_accounts() (supported on controller version 5.5.19 and higher)
- list_radius_profiles()
- list_rogueaps()
- list_self()
- list_settings()
@@ -55,40 +62,32 @@ This class currently supports the following functions/methods to get/set data th
- list_users()
- list_wlan_groups()
- list_wlanconf()
- list_current_channels()
- list_dpi_stats()
- locate_ap()
- reconnect_sta()
- rename_ap()
- restart_ap()
- revoke_voucher()
- extend_guest_validity()
- set_ap_radiosettings()
- set_guestlogin_settings()
- locate_ap()
- set_locate_ap() (deprecated but still available as alias)
- unset_locate_ap() (deprecated but still available as alias)
- set_networksettings_base()
- set_radius_account_base()
- set_sta_name()
- set_sta_note()
- set_usergroup()
- edit_usergroup()
- add_usergroup()
- delete_usergroup()
- edit_usergroup()
- add_usergroup()
- delete_usergroup()
- set_wlansettings_base()
- set_wlansettings()
- create_wlan()
- delete_wlan()
- set_wlan_mac_filter()
- set_wlansettings()
- set_wlansettings_base()
- site_leds()
- site_ledsoff() (deprecated but still available as alias)
- site_ledson() (deprecated but still available as alias)
- spectrum_scan()
- spectrum_scan_state()
- stat_allusers()
- stat_auths()
- stat_client()
- stat_daily_site()
- stat_daily_aps()
- stat_daily_site()
- stat_hourly_aps()
- stat_hourly_site()
- stat_payment()
@@ -97,10 +96,16 @@ This class currently supports the following functions/methods to get/set data th
- stat_sta_sessions_latest()
- stat_sysinfo()
- stat_voucher()
- unauthorize_guest()
- unblock_sta()
- unset_locate_ap() (deprecated but still available as alias)
- upgrade_device()
- upgrade_device_external()
Internal functions, getters/setters:
- set_debug()
- set_site()
- set_site()
- get_site()
- get_cookie() (renamed from getcookie())
- get_last_results_raw()

View File

@@ -16,6 +16,9 @@
namespace UniFi_API;
/**
* the UniFi API client class
*/
class Client
{
/**
@@ -36,7 +39,7 @@ class Client
function __construct($user, $password, $baseurl = '', $site = '', $version = '')
{
if (!extension_loaded('curl')) {
trigger_error('The PHP curl extension is not loaded! Please correct this before proceeding!');
trigger_error('The PHP curl extension is not loaded. Please correct this before proceeding!');
}
$this->user = trim($user);
@@ -55,6 +58,10 @@ class Client
if (empty($base_url_components['scheme']) || empty($base_url_components['host']) || empty($base_url_components['port'])) {
trigger_error('The URL provided is incomplete!');
}
if (strlen($this->site) !== 8 && $this->site !== 'default' && $this->debug) {
error_log('The provided short site name is probably incorrect');
}
}
function __destruct()
@@ -152,6 +159,7 @@ class Client
* Set site
* --------
* modify the private property site, returns the new (short) site name
* returns the new short name, or false if string length is incorrect or not 'default'
* required parameter <site> = string; must be the short site name of a site to which the
* provided credentials have access
*
@@ -160,9 +168,13 @@ class Client
*/
public function set_site($site)
{
$this->site = $site;
if (strlen($site) === 8 || $site === 'default') {
$this->site = $site;
return $this->site;
return $this->site;
} else {
return false;
}
}
/**
@@ -183,10 +195,12 @@ class Client
*/
public function set_debug($enable)
{
if ($enable) {
if ($enable === true) {
$this->debug = true;
return true;
} elseif ($enable === false) {
$this->debug = false;
return true;
}
return false;
@@ -574,7 +588,7 @@ class Client
public function stat_client($client_mac)
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/user/'.trim($client_mac)));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/user/'.trim($client_mac)));
return $this->process_response($content_decoded);
}
@@ -601,7 +615,7 @@ class Client
{
if (!$this->is_loggedin) return false;
$json = json_encode(['usergroup_id' => $group_id]);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -626,14 +640,14 @@ class Client
}
/**
* Add user group (using REST)
* Create user group (using REST)
* ---------------------------
* returns an array containing a single object with attributes of the new usergroup ("_id", "name", "qos_rate_max_down", "qos_rate_max_up", "site_id") on success
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*/
public function add_usergroup($group_name, $group_dn = -1, $group_up = -1)
public function create_usergroup($group_name, $group_dn = -1, $group_up = -1)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['name' => $group_name, 'qos_rate_max_down' => $group_dn, 'qos_rate_max_up' => $group_up]);
@@ -759,14 +773,14 @@ class Client
}
/**
* Add a site
* ----------
* Create a site
* -------------
* returns an array containing a single object with attributes of the new site ("_id", "desc", "name") on success
* required parameter <description> = the long name for the new site
*
* NOTES: immediately after being added, the new site will be available in the output of the "list_sites" function
*/
public function add_site($description)
public function create_site($description)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['desc' => $description, 'cmd' => 'add-site']);
@@ -837,18 +851,6 @@ class Client
return $this->process_response($content_decoded);
}
/**
* List networkconf
* ----------------
* returns an array of network configuration data
*/
public function list_networkconf()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/list/networkconf'));
return $this->process_response($content_decoded);
}
/**
* List vouchers
* -------------
@@ -908,14 +910,14 @@ class Client
}
/**
* List hotspot operators
* ----------------------
* List hotspot operators (using REST)
* -----------------------------------
* returns an array of hotspot operators
*/
public function list_hotspotop()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/list/hotspotop'));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/hotspotop'));
return $this->process_response($content_decoded);
}
@@ -1185,8 +1187,8 @@ class Client
}
/**
* Set access point radio settings
* -------------------------------
* Update access point radio settings
* ----------------------------------
* return true on success
* required parameter <ap_id>
* required parameter <radio>(default=ng)
@@ -1204,8 +1206,8 @@ class Client
}
/**
* Set guest login settings
* ------------------------
* Update guest login settings
* ---------------------------
* return true on success
* required parameter <portal_enabled>
* required parameter <portal_customized>
@@ -1261,8 +1263,82 @@ class Client
}
/**
* Add a wlan
* ----------
* List network settings (using REST)
* ----------------------------------
* returns an array of (non-wireless) networks and their settings
*/
public function list_networkconf()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf'));
return $this->process_response($content_decoded);
}
/**
* Create a network (using REST)
* -----------------------------
* return an array with a single object containing details of the new network on success, else return false
* required parameter <network_settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
*/
public function create_network($network_settings)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'POST';
$json = json_encode($network_settings);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf/', 'json='.$json));
return $this->process_response($content_decoded);
}
/**
* Update network settings, base (using REST)
* ------------------------------------------
* return true on success
* required parameter <network_id>
* required parameter <network_settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object/array structured in the same manner as is returned by list_networkconf() for the network.
*/
public function set_networksettings_base($network_id, $network_settings)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($network_settings);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf/'.trim($network_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
/**
* Delete a network (using REST)
* -----------------------------
* return true on success
* required parameter <network_id> = 24 char string; _id of the network which can be found with the list_networkconf() function
*/
public function delete_network($network_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf/'.trim($network_id)));
return $this->process_response_boolean($content_decoded);
}
/**
* List wlan settings (using REST)
* -------------------------------
* returns an array of wireless networks and their settings, or an array containing a single wireless network when using
* the <wlan_id> parameter
* optional parameter <wlan_id> = 24 char string; _id of the wlan to fetch the settings for
*/
public function list_wlanconf($wlan_id = null)
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id)));
return $this->process_response($content_decoded);
}
/**
* Create a wlan
* -------------
* return true on success
* required parameter <name> = string; SSID
* required parameter <x_passphrase> = string; new pre-shared key, minimal length is 8 characters, maximum length is 63
@@ -1323,40 +1399,12 @@ class Client
}
/**
* List wlan settings (using REST)
* -------------------------------
* returns an array of wireless networks and their settings, or an array containing a single wireless network when using
* the <wlan_id> parameter
* optional parameter <wlan_id> = 24 char string; _id of the wlan to fetch the settings for
*/
public function list_wlanconf($wlan_id = null)
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id)));
return $this->process_response($content_decoded);
}
/**
* Delete a wlan (using REST)
* --------------------------
* return true on success
* required parameter <wlan_id> = 24 char string; _id of the wlan that can be found with the list_wlanconf() function
*/
public function delete_wlan($wlan_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id)));
return $this->process_response_boolean($content_decoded);
}
/**
* Set wlan settings, base (using REST)
* ------------------------------------
* Update wlan settings, base (using REST)
* ---------------------------------------
* return true on success
* required parameter <wlan_id>
* required parameter <wlan_settings> = stdClass object containing the configuration of the wlan to apply, must be a (partial) object structured
* in the same manner as is returned by list_wlanconf() for a specific wlan)
* required parameter <wlan_settings> = stdClass object or associative array containing the configuration to apply to the wlan, must be a
* (partial) object/array structured in the same manner as is returned by list_wlanconf() for the wlan.
*/
public function set_wlansettings_base($wlan_id, $wlan_settings)
{
@@ -1368,8 +1416,8 @@ class Client
}
/**
* Set basic wlan settings
* -----------------------
* Update basic wlan settings
* --------------------------
* return true on success
* required parameter <wlan_id>
* required parameter <x_passphrase> = new pre-shared key, minimal length is 8 characters, maximum length is 63,
@@ -1400,7 +1448,21 @@ class Client
}
/**
* Set MAC filter for a wlan
* Delete a wlan (using REST)
* --------------------------
* return true on success
* required parameter <wlan_id> = 24 char string; _id of the wlan which can be found with the list_wlanconf() function
*/
public function delete_wlan($wlan_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id)));
return $this->process_response_boolean($content_decoded);
}
/**
* Update MAC filter for a wlan
* ----------------------------
* return true on success
* required parameter <wlan_id>
@@ -1528,11 +1590,26 @@ class Client
return $this->process_response($content_decoded);
}
/**
* List Radius profiles (using REST)
* --------------------------------------
* returns an array of objects containing all Radius profiles for the current site
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function list_radius_profiles()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/radiusprofile'));
return $this->process_response($content_decoded);
}
/**
* List Radius user accounts (using REST)
* --------------------------------------
* returns an array of objects containing all Radius accounts for the current site
*
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
@@ -1543,6 +1620,100 @@ class Client
return $this->process_response($content_decoded);
}
/**
* Create a Radius user account (using REST)
* -----------------------------------------
* returns an array containing a single object for the newly created account upon success, else returns false
* required parameter <name> = string; name for the new account
* required parameter <x_password> = string; password for the new account
* required parameter <tunnel_type> = integer; must be one of the following values:
* 1 Point-to-Point Tunneling Protocol (PPTP)
* 2 Layer Two Forwarding (L2F)
* 3 Layer Two Tunneling Protocol (L2TP)
* 4 Ascend Tunnel Management Protocol (ATMP)
* 5 Virtual Tunneling Protocol (VTP)
* 6 IP Authentication Header in the Tunnel-mode (AH)
* 7 IP-in-IP Encapsulation (IP-IP)
* 8 Minimal IP-in-IP Encapsulation (MIN-IP-IP)
* 9 IP Encapsulating Security Payload in the Tunnel-mode (ESP)
* 10 Generic Route Encapsulation (GRE)
* 11 Bay Dial Virtual Services (DVS)
* 12 IP-in-IP Tunneling
* 13 Virtual LANs (VLAN)
* required parameter <tunnel_medium_type> = integer; must be one of the following values:
* 1 IPv4 (IP version 4)
* 2 IPv6 (IP version 6)
* 3 NSAP
* 4 HDLC (8-bit multidrop)
* 5 BBN 1822
* 6 802 (includes all 802 media plus Ethernet "canonical format")
* 7 E.163 (POTS)
* 8 E.164 (SMDS, Frame Relay, ATM)
* 9 F.69 (Telex)
* 10 X.121 (X.25, Frame Relay)
* 11 IPX
* 12 Appletalk
* 13 Decnet IV
* 14 Banyan Vines
* 15 E.164 with NSAP format subaddress
* optional parameter <vlan> = integer; VLAN to assign to the account
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function create_radius_account($name, $x_password, $tunnel_type, $tunnel_medium_type, $vlan = null)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'POST';
$account_details = [
'name' => $name,
'x_password' => $x_password,
'tunnel_type' => (int) $tunnel_type,
'tunnel_medium_type' => (int) $tunnel_medium_type
];
if (isset($vlan)) $account_details['vlan'] = (int) $vlan;
$json = json_encode($account_details);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account', 'json='.$json));
return $this->process_response($content_decoded);
}
/**
* Update Radius account, base (using REST)
* ----------------------------------------
* return true on success
* required parameter <account_id> = 24 char string; _id of the account which can be found with the list_radius_accounts() function
* required parameter <account_details> = stdClass object or associative array containing the new profile to apply to the account, must be a (partial)
* object/array structured in the same manner as is returned by list_radius_accounts() for the account.
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function set_radius_account_base($account_id, $account_details)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($account_details);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account/'.trim($account_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
/**
* Delete a Radius account (using REST)
* ------------------------------------
* return true on success
* required parameter <account_id> = 24 char string; _id of the account which can be found with the list_radius_accounts() function
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function delete_radius_account($account_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account/'.trim($account_id)));
return $this->process_response_boolean($content_decoded);
}
/****************************************************************
* "Aliases" for deprecated functions from here, to support
* backward compatibility:
@@ -1574,6 +1745,7 @@ class Client
'Function set_locate_ap() has been deprecated, use locate_ap() instead.',
E_USER_DEPRECATED
);
return $this->locate_ap($mac, true);
}
@@ -1589,6 +1761,7 @@ class Client
'Function unset_locate_ap() has been deprecated, use locate_ap() instead.',
E_USER_DEPRECATED
);
return $this->locate_ap($mac, false);
}
@@ -1603,6 +1776,7 @@ class Client
'Function site_ledson() has been deprecated, use site_leds() instead.',
E_USER_DEPRECATED
);
return $this->site_leds(true);
}
@@ -1617,6 +1791,7 @@ class Client
'Function site_ledsoff() has been deprecated, use site_leds() instead.',
E_USER_DEPRECATED
);
return $this->site_leds(false);
}
@@ -1648,7 +1823,7 @@ class Client
}
if ($this->debug) {
trigger_error('Debug: Last error message: ' . $this->last_error_message);
trigger_error('Debug: Last error message: '.$this->last_error_message);
}
}
}
@@ -1676,7 +1851,7 @@ class Client
}
if ($this->debug) {
trigger_error('Debug: Last error message: ' . $this->last_error_message);
trigger_error('Debug: Last error message: '.$this->last_error_message);
}
}
}
@@ -1691,6 +1866,7 @@ class Client
{
$ch = $this->get_curl_obj();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if (trim($data) != '') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
@@ -1765,6 +1941,12 @@ class Client
}
curl_close($ch);
/**
* set request_type value back to default, just in case
*/
$this->request_type = 'POST';
return $content;
}