From 7c6cd4eb852f947e2d676e980a53ab1433438f1c Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Tue, 2 Oct 2018 13:16:48 +0200 Subject: [PATCH] removed and added spaces, and changed brackets usage following PSR-2 guidelines --- examples/block_list.php | 26 +++++++---------- examples/extend_guest_auth.php | 14 ++++------ examples/unblock_list.php | 27 +++++++----------- examples/update_ac-iw_ports.php | 2 +- .../update_device_wlan_settings_5.5.X.php | 8 +++--- src/Client.php | 28 ++++++++++--------- 6 files changed, 46 insertions(+), 59 deletions(-) diff --git a/examples/block_list.php b/examples/block_list.php index 302394b..1eeb58a 100755 --- a/examples/block_list.php +++ b/examples/block_list.php @@ -25,9 +25,9 @@ require_once('vendor/autoload.php'); */ require_once('config.php'); -$debug=false; +$debug = false; /** - * the MAC address(es) of the device(s) to block + * the MAC address(es) of the device(s) to block */ $macs_to_block = explode(',',$argv[1]); @@ -35,8 +35,7 @@ $macs_to_block = explode(',',$argv[1]); * The site to authorize the device with */ $site_id = 'MUST_DEFINE_THIS'; -if ($site_id == "MUST_DEFINE_THIS") -{ +if ($site_id == "MUST_DEFINE_THIS") { print 'ERROR: set the site id in your script'; return; } @@ -48,8 +47,7 @@ $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $ $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login(); // always true regardless of site id -foreach ($macs_to_block as &$mac) -{ +foreach ($macs_to_block as &$mac) { // block_result is always true even if mac address does not exist :( $block_result = $unifi_connection->block_sta($mac); @@ -65,20 +63,16 @@ foreach ($macs_to_block as &$mac) $getid_result = $unifi_connection->stat_client($mac); - if (property_exists($getid_result[0], "oui")) // this field(manufacturer) seems to exist on valid mac addresses - { - if (property_exists($getid_result[0], "name")) // this is the alias field if it has been defined - { + if (property_exists($getid_result[0], "oui")) { + // this field(manufacturer) seems to exist on valid mac addresses + if (property_exists($getid_result[0], "name")) { + // this is the alias field if it has been defined $name = $getid_result[0]->name; - } - else - { + } else { $name = $getid_result[0]->hostname; } print 'blocked ' . $name . PHP_EOL; - } - else - { + } else { print 'ERROR: could not block ' . $mac . PHP_EOL; print ' check mac address is valid and part of your network' . PHP_EOL; } diff --git a/examples/extend_guest_auth.php b/examples/extend_guest_auth.php index a6780e8..a49d017 100755 --- a/examples/extend_guest_auth.php +++ b/examples/extend_guest_auth.php @@ -29,20 +29,18 @@ if ($loginresults === 400) { print "UniFi controller login failure, please check your credentials in config.php.\n"; } else { $guestlist = $unifi_connection->list_guests(); - // print "
"; print_r ($guestlist); print "
"; // loop thru all known guests foreach ($guestlist as $guest) { - // print "
"; print_r ($guest); print "
"; - print "
" . $guest->_id . " (" . $guest->mac . "), valid until " . date (DATE_ATOM, $guest->end) . " (" . $guest->end . ")
"; + print "
" . $guest->_id . " (" . $guest->mac . "), valid until " . date(DATE_ATOM, $guest->end) . " (" . $guest->end . ")
"; // just a sample: only extend validity of guests which have end date after 2017-04-02 if ($guest->end > 1491166482) { // extend clients five times = five days - if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; - if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; - if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; - if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; - if (!$unifi_connection->extend_guest_validity ($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; + if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; + if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; + if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; + if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; + if (!$unifi_connection->extend_guest_validity($guest->_id)) print "Extend failed for guest with id " . $guest->_id . "\n"; } } diff --git a/examples/unblock_list.php b/examples/unblock_list.php index 042259f..0b04a0e 100755 --- a/examples/unblock_list.php +++ b/examples/unblock_list.php @@ -29,14 +29,13 @@ $debug=false; /** * the MAC addresses of the device(s) to unblock */ -$macs_to_unblock = explode(',',$argv[1]); +$macs_to_unblock = explode(',', $argv[1]); /** * The site to authorize the device with */ $site_id = 'MUST_DEFINE_THIS'; -if ($site_id == "MUST_DEFINE_THIS") -{ +if ($site_id == "MUST_DEFINE_THIS") { print 'ERROR: set the site id in your script'; return; } @@ -48,10 +47,9 @@ $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $ $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login(); // always true regardless of site id -foreach ($macs_to_unblock as &$mac) -{ +foreach ($macs_to_unblock as &$mac) { // block_result is always true even if mac address does not exist :( - $block_result = $unifi_connection->unblock_sta($mac); + $block_result = $unifi_connection->unblock_sta($mac); /** * NOTE: @@ -63,22 +61,17 @@ foreach ($macs_to_unblock as &$mac) */ sleep(1); - $getid_result = $unifi_connection->stat_client($mac); + $getid_result = $unifi_connection->stat_client($mac); - if (property_exists($getid_result[0], "oui")) // this field(manufacturer) seems to exist on valid mac addresses - { - if (property_exists($getid_result[0], "name")) - { + if (property_exists($getid_result[0], "oui")) { + // this field(manufacturer) seems to exist on valid mac addresses + if (property_exists($getid_result[0], "name")) { $name = $getid_result[0]->name; - } - else - { + } else { $name = $getid_result[0]->hostname; } print 'unblocked ' . $name . PHP_EOL; - } - else - { + } else { print 'ERROR: could not unblock ' . $mac . PHP_EOL; print ' check mac address is valid and part of your network' . PHP_EOL; } diff --git a/examples/update_ac-iw_ports.php b/examples/update_ac-iw_ports.php index 391fd5c..ec6f8c8 100755 --- a/examples/update_ac-iw_ports.php +++ b/examples/update_ac-iw_ports.php @@ -67,7 +67,7 @@ $data = $unifi_connection->list_devices($device_mac); $device_id = $data[0]->device_id; $update_device = $unifi_connection->set_device_settings_base($device_id, $new_ports_config); -if(!$update_device){ +if (!$update_device) { $error = $unifi_connection->get_last_results_raw(); echo json_encode($error, JSON_PRETTY_PRINT); } diff --git a/examples/update_device_wlan_settings_5.5.X.php b/examples/update_device_wlan_settings_5.5.X.php index 847a363..cd594de 100755 --- a/examples/update_device_wlan_settings_5.5.X.php +++ b/examples/update_device_wlan_settings_5.5.X.php @@ -58,13 +58,13 @@ $data = $unifi_connection->list_devices($ap_mac); $radio_table = $data[0]->radio_table; $device_id = $data[0]->device_id; -foreach($radio_table as $radio){ - if($radio->radio === 'ng'){ +foreach ($radio_table as $radio) { + if ($radio->radio === 'ng') { $radio->tx_power_mode = $ng_tx_power_mode; $radio->channel = $ng_channel; } - if($radio->radio === 'na'){ + if ($radio->radio === 'na') { $radio->tx_power_mode = $na_tx_power_mode; $radio->channel = $na_channel; } @@ -72,7 +72,7 @@ foreach($radio_table as $radio){ $update_device = $unifi_connection->set_device_settings_base($device_id, ['radio_table' => $radio_table]); -if(!$update_device){ +if (!$update_device) { $error = $unifi_connection->get_last_results_raw(); echo json_encode($error, JSON_PRETTY_PRINT); } diff --git a/src/Client.php b/src/Client.php index 7ba8efd..e2a9050 100755 --- a/src/Client.php +++ b/src/Client.php @@ -540,6 +540,7 @@ class Client if (!is_null($mac)) { $json['mac'] = strtolower($mac); } + $json = json_encode($json); $response = $this->exec_curl('/api/s/' . $this->site . '/stat/report/5minutes.ap', 'json=' . $json); @@ -600,6 +601,7 @@ class Client if (!is_null($mac)) { $json['mac'] = strtolower($mac); } + $json = json_encode($json); $response = $this->exec_curl('/api/s/' . $this->site . '/stat/report/daily.ap', 'json=' . $json); @@ -728,11 +730,11 @@ class Client return false; } - $end = is_null($end) ? ((time())*1000) : intval($end); - $start = is_null($start) ? $end-(12*3600*1000) : intval($start); + $end = is_null($end) ? ((time()) * 1000) : intval($end); + $start = is_null($start) ? $end - (12 * 3600 * 1000) : intval($start); $attribs = is_null($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs); $json = json_encode(['attrs' => $attribs, 'start' => $start, 'end' => $end]); - $response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/5minutes.gw', 'json='.$json); + $response = $this->exec_curl('/api/s/' . $this->site.'/stat/report/5minutes.gw', 'json=' . $json); return $this->process_response($response); } @@ -758,11 +760,11 @@ class Client return false; } - $end = is_null($end) ? ((time())*1000) : intval($end); - $start = is_null($start) ? $end-(7*24*3600*1000) : intval($start); + $end = is_null($end) ? ((time()) * 1000) : intval($end); + $start = is_null($start) ? $end - (7 * 24 * 3600 * 1000) : intval($start); $attribs = is_null($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs); $json = json_encode(['attrs' => $attribs, 'start' => $start, 'end' => $end]); - $response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/hourly.gw', 'json='.$json); + $response = $this->exec_curl('/api/s/' . $this->site.'/stat/report/hourly.gw', 'json=' . $json); return $this->process_response($response); } @@ -788,11 +790,11 @@ class Client return false; } - $end = is_null($end) ? ((time()-(time() % 3600))*1000) : intval($end); - $start = is_null($start) ? $end-(52*7*24*3600*1000) : intval($start); + $end = is_null($end) ? ((time() - (time() % 3600)) * 1000) : intval($end); + $start = is_null($start) ? $end - (52 * 7 * 24 * 3600 * 1000) : intval($start); $attribs = is_null($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs); $json = json_encode(['attrs' => $attribs, 'start' => $start, 'end' => $end]); - $response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/daily.gw', 'json='.$json); + $response = $this->exec_curl('/api/s/' . $this->site.'/stat/report/daily.gw', 'json=' . $json); return $this->process_response($response); } @@ -2699,7 +2701,7 @@ class Client $macs = array_map('strtolower', $macs); $payload = [ - 'mac_filter_enabled' => (bool)$mac_filter_enabled, + 'mac_filter_enabled' => (bool) $mac_filter_enabled, 'mac_filter_policy' => $mac_filter_policy, 'mac_filter_list' => $macs ]; @@ -3054,11 +3056,11 @@ class Client $account_details = [ 'name' => $name, 'x_password' => $x_password, - 'tunnel_type' => (int)$tunnel_type, - 'tunnel_medium_type' => (int)$tunnel_medium_type + 'tunnel_type' => (int) $tunnel_type, + 'tunnel_medium_type' => (int) $tunnel_medium_type ]; if (isset($vlan)) { - $account_details['vlan'] = (int)$vlan; + $account_details['vlan'] = (int) $vlan; } $json = json_encode($account_details);