Skip to content

Commit

Permalink
addition of several new functions/methods and minor code cleanup
Browse files Browse the repository at this point in the history
-  added list_known_rogueaps() function/method
-  added stat_status() function/method
-  added power_cycle_switch_port() function/method
  • Loading branch information
malle-pietje committed Oct 18, 2017
1 parent 746a29f commit e6ffb2e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 47 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## UniFi controller API client class

A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in the API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).

This class can now also be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.

Expand Down Expand Up @@ -49,6 +49,7 @@ The class currently supports the following functions/methods to get/post/put/del
- list_guests()
- list_health()
- list_hotspotop()
- list_known_rogueaps()
- list_networkconf()
- list_portconf()
- list_portforward_stats()
Expand All @@ -65,11 +66,13 @@ The class currently supports the following functions/methods to get/post/put/del
- list_wlan_groups()
- list_wlanconf()
- locate_ap()
- power_cycle_switch_port()
- reconnect_sta()
- rename_ap()
- restart_ap()
- revoke_voucher()
- set_ap_radiosettings()
- set_device_settings_base()
- set_guestlogin_settings()
- set_locate_ap() (deprecated but still available as alias)
- set_networksettings_base()
Expand Down Expand Up @@ -98,6 +101,7 @@ The class currently supports the following functions/methods to get/post/put/del
- stat_sessions()
- stat_sites()
- stat_sta_sessions_latest()
- stat_status()
- stat_sysinfo()
- stat_voucher()
- unauthorize_guest()
Expand Down
136 changes: 90 additions & 46 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,9 @@ public function list_tags()
}

/**
* List rogue access points
* ------------------------
* returns an array of known rogue access point objects
* List rogue/neighboring access points
* ------------------------------------
* returns an array of rogue/neighboring access point objects
* optional parameter <within> = hours to go back to list discovered "rogue" access points (default = 24 hours)
*/
public function list_rogueaps($within = 24)
Expand All @@ -795,6 +795,18 @@ public function list_rogueaps($within = 24)
return $this->process_response($response);
}

/**
* List known rogue access points
* ------------------------------
* returns an array of known rogue access point objects
*/
public function list_known_rogueaps()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/rogueknown');
return $this->process_response($response);
}

/**
* List sites
* ----------
Expand Down Expand Up @@ -888,6 +900,17 @@ public function stat_sysinfo()
return $this->process_response($response);
}

/**
* List controller status
* ----------------------
* returns an array containing general controller status info
*/
public function stat_status()
{
$response = $this->exec_curl('/status');
return $this->process_response($response);
}

/**
* List self
* ---------
Expand Down Expand Up @@ -1177,14 +1200,10 @@ public function led_override($device_id, $override_mode)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$override_mode_options = ['off', 'on', 'default'];
if (in_array($override_mode, $override_mode_options)) {
$json = json_encode(['led_override' => $override_mode]);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), $json);
return $this->process_response_boolean($response);
}

return false;
if (!in_array($override_mode, ['off', 'on', 'default'])) return false;
$json = json_encode(['led_override' => $override_mode]);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), $json);
return $this->process_response_boolean($response);
}

/**
Expand Down Expand Up @@ -1253,14 +1272,10 @@ public function set_ap_radiosettings($ap_id, $radio, $channel, $ht, $tx_power_mo
*/
public function set_ap_wlangroup($wlantype_id, $device_id, $wlangroup_id) {
if (!$this->is_loggedin) return false;
$wlantype_id_options = ['ng', 'na'];
if (in_array($wlantype_id, $wlantype_id_options)) {
$json = json_encode(['wlan_overrides' => [],'wlangroup_id_'.$wlantype_id => $wlangroup_id]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/device/'.trim($device_id),'json='.$json);
return $this->process_response_boolean($response);
}

return false;
if (in_array($wlantype_id, ['ng', 'na'])) return false;
$json = json_encode(['wlan_overrides' => [],'wlangroup_id_'.$wlantype_id => $wlangroup_id]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/device/'.trim($device_id),'json='.$json);
return $this->process_response_boolean($response);
}

/**
Expand Down Expand Up @@ -1532,16 +1547,12 @@ public function delete_wlan($wlan_id)
*/
public function set_wlan_mac_filter($wlan_id, $mac_filter_policy, $mac_filter_enabled, array $macs)
{
$mac_filter_policy_options = ['allow', 'deny'];
if (in_array($mac_filter_policy, $mac_filter_policy_options)) {
$payload = new \stdClass();
$payload->mac_filter_enabled = (bool)$mac_filter_enabled;
$payload->mac_filter_policy = $mac_filter_policy;
$payload->mac_filter_list = $macs;
return $this->set_wlansettings_base($wlan_id, $payload);
}

return false;
if (in_array($mac_filter_policy, ['allow', 'deny'])) return false;
$payload = new \stdClass();
$payload->mac_filter_enabled = (bool)$mac_filter_enabled;
$payload->mac_filter_policy = $mac_filter_policy;
$payload->mac_filter_list = $macs;
return $this->set_wlansettings_base($wlan_id, $payload);
}

/**
Expand Down Expand Up @@ -1625,6 +1636,25 @@ public function upgrade_device_external($firmware_url, $device_mac)
return $this->process_response_boolean($response);
}

/**
* Power-cycle the PoE output of a switch port
* -------------------------------------------
* return true on success
* required parameter <switch_mac> = string; main MAC address of the switch
* required parameter <port_idx> = integer; port number/index of the port to be affected
*
* NOTES:
* - only applies to switches and their PoE ports...
*/
public function power_cycle_switch_port($switch_mac, $port_idx)
{
if (!$this->is_loggedin) return false;
$json = ['mac' => $switch_mac, 'port_idx' => intval($port_idx), 'cmd' => 'power-cycle'];
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/devmgr', 'json='.$json);
return $this->process_response_boolean($response);
}

/**
* Trigger an RF scan by an AP
* ---------------------------
Expand Down Expand Up @@ -1653,6 +1683,23 @@ public function spectrum_scan_state($ap_mac)
return $this->process_response($response);
}

/**
* Update device settings, base (using REST)
* -----------------------------------------
* return true on success
* required parameter <device_id> = 24 char string; _id of the device which can be found with the list_devices() function
* required parameter <device_settings> = stdClass object or associative array containing the configuration to apply to the device, must be a
* (partial) object/array structured in the same manner as is returned by list_devices() for the device.
*/
public function set_device_settings_base($device_id, $device_settings)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($device_settings);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), 'json='.$json);
return $this->process_response_boolean($response);
}

/**
* List Radius profiles (using REST)
* --------------------------------------
Expand Down Expand Up @@ -1727,23 +1774,20 @@ public function list_radius_accounts()
public function create_radius_account($name, $x_password, $tunnel_type, $tunnel_medium_type, $vlan = null)
{
if (!$this->is_loggedin) return false;
$tunnel_type_options = [1,2,3,4,5,6,7,8,9,10,11,12,13];
$tunnel_medium_type_options = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
if (in_array($tunnel_type, $tunnel_type_options) && in_array($tunnel_medium_type, $tunnel_medium_type_options)) {
$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);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/account', 'json='.$json);
return $this->process_response($response);
}

return false;
$tunnel_types = [1,2,3,4,5,6,7,8,9,10,11,12,13];
$tunnel_medium_types = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
if (!in_array($tunnel_type, $tunnel_types) || !in_array($tunnel_medium_type, $tunnel_medium_types)) 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);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/account', 'json='.$json);
return $this->process_response($response);
}

/**
Expand Down

0 comments on commit e6ffb2e

Please sign in to comment.