From 774fced2da6b3836e2a21bd2d40700da840858c1 Mon Sep 17 00:00:00 2001 From: Patrice Guillemette Date: Fri, 9 Mar 2018 16:24:31 -0500 Subject: [PATCH 1/4] Update Client.php --- src/Client.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Client.php b/src/Client.php index c268a84..a466f0c 100755 --- a/src/Client.php +++ b/src/Client.php @@ -881,6 +881,22 @@ class Client return $this->process_response_boolean($response); } + /** + * Change a site's name + * ------------- + * return true on success + * required parameter = the long name for the site + * + * NOTES: immediately after being changed, the site will be available in the output of the "list_sites" function + */ + public function set_site_name($site_name) + { + if (!$this->is_loggedin) return false; + $json = json_encode(['cmd' => 'update-site', 'desc' => $site_name]); + $response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json); + return $this->process_response_boolean($response); + } + /** * List admins * ----------- @@ -1356,6 +1372,21 @@ class Client return $this->process_response_boolean($response); } + + /** + * Update guestlogin settings, base + * ------------------------------------------ + * return true on success + * required parameter = stdClass object or associative array containing the configuration to apply to the guestlogin, must be a (partial) + * object/array structured in the same manner as is returned by list_settings() for the guest_access. + */ + public function set_guestlogin_settings_base($guestlogin_settings) { + if (!$this->is_loggedin) return false; + $json = json_encode($guestlogin_settings); + $response = $this->exec_curl('/api/s/'.$this->site.'/set/setting/guest_access', 'json='.$json); + return $this->process_response_boolean($response); + } + /** * Rename access point * ------------------- From 9f5aaa983897b78e4fa2cc03d1675d0eb3bb5060 Mon Sep 17 00:00:00 2001 From: Seth Mos Date: Tue, 13 Mar 2018 13:39:38 +0100 Subject: [PATCH 2/4] Add a few more function for setting site specific parameters. It was neccesary to create several setting functions for setting the information, although the site configuration is returned whole as one object. I also added a provisioning example that I use with our internal ERP that automatically creates, updates and deletes sites according to internal ERP site information. I added a settings template that has a couple of the most useful defaults, like auto upgrade, country and locale. --- examples/README.md | 8 +- examples/provision.php | 336 +++++++++++++++++++++++++++++++++ examples/settings.template.php | 195 +++++++++++++++++++ src/Client.php | 119 ++++++++++++ 4 files changed, 657 insertions(+), 1 deletion(-) create mode 100644 examples/provision.php create mode 100644 examples/settings.template.php mode change 100755 => 100644 src/Client.php diff --git a/examples/README.md b/examples/README.md index 07eed1a..1bba36a 100755 --- a/examples/README.md +++ b/examples/README.md @@ -12,4 +12,10 @@ If you would like to share your own example file(s), please open an issue and in ## Important Disclaimer -Use these examples at your own risk! \ No newline at end of file +Use these examples at your own risk! + +## Provisioning script + +The example provision script easily creates a large number of sites with comparable network configuration. It sets the LAN config +to site specific configuration. Copy the settings.template.php and config.template.php to their respective files for testing. This is meant as a +basic provisioning only system. Currently used against controller 5.6.30. diff --git a/examples/provision.php b/examples/provision.php new file mode 100644 index 0000000..f5cb3ea --- /dev/null +++ b/examples/provision.php @@ -0,0 +1,336 @@ +"; +// Import the controller auth config +include("config.php"); + +/** + * set to true (without quotes) to enable debug output to the browser and the PHP error log + */ +$debug = true; + +$site_id = "default"; + +/** + * initialize the Unifi API connection class, log in to the controller and request the alarms collection + * (this example assumes you have already assigned the correct values to the variables used) + */ +$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version, false); +$login = $unifi_connection->login(); + +if($login > 400) { + echo "Failed to log into controller"; + die; +} +// $sites = $unifi_connection->list_sites(); // returns a PHP array containing sites + +$open_shops = array(); +// Mogrify shop info into useable arrays +foreach($fil_array as $filnr => $shop) { + if(floatval($shop['aktief']) == 0) + continue; + if($shop['divisie_code'] != "D") + continue; + + if((floatval($shop['kassa_aantal']) > 0)) + $open_shops[$filnr] = ucfirst(strtolower($shop['corr_woonplaats'])); +} + +// If debug, create Fake open shops array, otherwise unset test shops +if($debug === true) { + $open_shops = array(); + $open_shops[943] = "Test 1"; + $open_shops[965] = "Test 2"; +} else { + unset($open_shops[943]); + unset($open_shops[965]); + unset($close_shops[943]); + unset($close_shops[965]); +} + +// Check if we can find all our shop sites, otherwise add to todo list for creation, close list for deletion +$todo_shops = $open_shops; +$active_shops = array(); +$close_shops = array(); +foreach($unifi_connection->list_sites() as $site){ + $desc = $site->desc; + // Does it look like a shop? + if(preg_match("/([0-9][0-9][0-9]+)/", $desc, $matches)) { + // echo "Found site {$desc}\n"; + unset($todo_shops[floatval($matches[1])]); + $active_shops[floatval($matches[1])] = $site->name; + + if(!$open_shops[floatval($matches[1])]) { + // echo "Shop {$matches[1]} does not have hardware\n"; + $close_shops[floatval($matches[1])] = $site->name; + } + } +} + +// If debug, create Fake site entries array, otherwise unset test shops +if($debug === true) { + $active_shops = array(); + $active_shops[965] = "j103b83q"; + $active_shops[943] = "winkels"; +} else { + unset($active_shops[943]); + unset($active_shops[965]); + unset($close_shops[943]); + unset($close_shops[965]); +} + +// Any sites we need to create before we can continue? +foreach($todo_shops as $filnr => $city){ + $filnr = sprintf("%04d", $filnr); + $desc = "{$filnr} {$city}"; + echo "Create site for {$filnr}\n"; + $createsite = $unifi_connection->create_site($desc); + if($createsite === false) { + echo "Failed to create site for {$filnr}, id {$siteid}\n"; + break; + } +} +// Refresh site list +if(count($todo_shops > 0)) { + foreach($unifi_connection->list_sites() as $site){ + $desc = $site->desc; + // Does it look like a shop? + if(preg_match("/([0-9][0-9][0-9]+)/", $desc, $matches)) { + // echo "Found site {$desc}\n"; + unset($todo_shops[floatval($matches[1])]); + $active_shops[floatval($matches[1])] = $site->name; + } + } +} +// We should have 0 todo shops now +// print_r($todo_shops); + +/* +echo "Open\n"; +print_r($open_shops); +echo "Active\n"; +print_r($active_shops); +echo "Close\n"; +print_r($close_shops); +die(); +*/ + +// Foreach shop, select the site. +foreach($active_shops as $filnr => $siteid) { + $filnr = sprintf("%04d", $filnr); + $select = $unifi_connection->set_site($siteid); + + // fetch configured group settings, we need those later, we only use the Default group. + $wlangroups = $unifi_connection->list_wlan_groups($siteid); + $usergroups = $unifi_connection->list_usergroups($siteid); + + if(isset($close_shops[floatval($filnr)])) { + echo "Delete site {$siteid} with id ". $usergroups[0]->site_id ." for shop {$filnr}\n"; + $delete = $unifi_connection->delete_site($usergroups[0]->site_id); + if($delete === false) { + echo "Failed to delete site for {$filnr}, id {$siteid}\n"; + } + continue; + } + + // fetch configured group settings, we need those later, we only use the Default group. + $wlangroups = $unifi_connection->list_wlan_groups($siteid); + $usergroups = $unifi_connection->list_usergroups($siteid); + if($debug===true) { + //var_export ($wlangroups); + //var_export ($usergroups); + } + foreach($wlangroups as $group){ + // Check if template networks exist + if($group->name == "Default") { + $shawlangroup_id = $group->_id; + } + } + foreach($usergroups as $group){ + // Check if template networks exist + if($group->name == "Default") { + $shausergroup_id = $group->_id; + } + } + + // Include each time so site specific settings based on shop number are picked up + unset($wirednetworks); + unset($wlannetworks); + unset($siteconf); + include("settings.php"); + + refresh_networks(); + refresh_wlans(); + fetch_site_conf(); + + if($debug===true) { + // var_export ($siteconf); + //var_export ($wlanconf); + // var_export ($networkconf); + //print_r($wlannetworks); + } + + foreach($sitesettings as $key => $values) { + echo "Update site setting {$key} id {$setting_id[$key]} for {$filnr}, id {$siteid}\n"; + switch($key){ + case "country": + $update_site[$key] = $unifi_connection->set_site_country($setting_id[$key], $sitesettings[$key]); + break; + case "locale": + $update_site[$key] = $unifi_connection->set_site_locale($setting_id[$key], $sitesettings[$key]); + break; + case "connectivity ": + $update_site[$key] = $unifi_connection->set_site_connectivity($setting_id[$key], $sitesettings[$key]); + break; + case "mgmt": + $update_site[$key] = $unifi_connection->set_site_mgmt($setting_id[$key], $sitesettings[$key]); + break; + case "guest_access": + $update_site[$key] = $unifi_connection->set_site_guest_access($setting_id[$key], $sitesettings[$key]); + break; + case "snmp": + $update_site[$key] = $unifi_connection->set_site_snmp($setting_id[$key], $sitesettings[$key]); + break; + case "ntp": + $update_site[$key] = $unifi_connection->set_site_ntp($setting_id[$key], $sitesettings[$key]); + break; + default: + break; + } + if($update_site[$key] === false) + echo "Failed to update setting {$key} for {$filnr}, id {$siteid} ". print_r($sitesettings[$key], true) ."\n"; + } + + foreach($wirednetworks as $key => $values) { + // Template network didn't exist, create + if($wired[$key] === false) { + echo "Create new vlan {$key} for {$filnr}, id {$siteid}\n"; + $addnetwork[$key] = $unifi_connection->create_network($wirednetworks[$key]); + // echo json_encode($addvlan, JSON_PRETTY_PRINT); + } + if($addnetwork[$key] === false) + echo "Failed to add network {$key} for {$filnr}, id {$siteid}\n"; + + // Do we need to update? + if($wired[$key] === true) { + echo "Update network {$key} id {$wired_id[$key]} for {$filnr}, id {$siteid}\n"; + $updatenetwork[$key] = $unifi_connection->set_networksettings_base($wired_id[$key], $wirednetworks[$key]); + } + if($updatenetwork[$key] === false) + echo "Failed to update network {$key} for {$filnr}, id {$siteid} ". print_r($wirednetworks[$key], true) . print_r($wired_id, true) ."\n"; + + } + + foreach($wlannetworks as $key => $values) { + // Template network didn't exist, create + if($wlan[$key] === false) { + echo "Create new disabled wlan {$key} for {$filnr}, id {$siteid}\n"; + $addwlan[$key] = $unifi_connection->create_wlan($wlannetworks[$key]['name'], $wlannetworks[$key]['x_passphrase'], $wlannetworks[$key]['usergroup_id'], $wlannetworks[$key]['wlangroup_id'], false); + } + if($addwlan[$key] === false) + echo "Failed to add wlan {$key} for {$filnr}, id {$siteid} ". print_r($wlannetworks[$key], true) ."\n"; + else + refresh_wlans(); + + // Do we need to update? + if($wlan[$key] === true) { + echo "Update wlan {$key} id {$wlan_id[$key]} for {$filnr}, id {$siteid}\n"; + $updatewlan[$key] = $unifi_connection->set_wlansettings_base($wlan_id[$key], $wlannetworks[$key]); + } + if($updatewlan[$key] === false) + echo "Failed to update wlan {$key} for {$filnr}, id {$siteid} ". print_r($wlannetworks[$key], true) . print_r($wlan_id, true) ."\n"; + + } + + if($debug===true) + break; +} + +$logout = $unifi_connection->logout(); + +function refresh_networks() { + global $unifi_connection; + global $networkconf; + global $wired; + global $wired_id; + global $shasite_id; + global $wirednetworks; + + // Fetch configured wired networks + $networkconf = $unifi_connection->list_networkconf(); + + foreach($wirednetworks as $key => $values) { + $wired[$key] = false; + } + // Lan netwerken + foreach($networkconf as $network){ + // Check if template networks exist + foreach($wirednetworks as $key => $values) { + if(($network->name == "$key")) { + $wired[$key] = true; + $wired_id[$key] = $network->_id; + $shasite_id = $network->site_id; + } + } + } +} + +function refresh_wlans() { + global $unifi_connection; + global $wlanconf; + global $wlan; + global $wlan_id; + global $shasite_id; + global $wlannetworks; + + // Fetch Wireless networks + $wlanconf = $unifi_connection->list_wlanconf(); + + foreach($wlannetworks as $key => $values) + $wlan[$key] = false; + + foreach($wlanconf as $network){ + // Check if template networks exist + foreach($wlannetworks as $key => $values) { + if($network->name == "$key") { + $wlan[$key] = true; + $wlan_id[$key] = $network->_id; + $shasite_id = $network->site_id; + } + } + } +} + +function fetch_site_conf() { + global $unifi_connection; + global $siteconf; + global $siteid; + global $setting; + global $setting_id; + + // Fetch site settings + $siteconf = $unifi_connection->list_settings($siteid); + foreach($sitesettings as $key => $values) + $sitesetting[$key] = false; + + $setting = array(); + foreach($siteconf as $arr) { + $setting[$arr->key] = $arr; + $setting_id[$arr->key] = $arr->_id; + } +} + +?> \ No newline at end of file diff --git a/examples/settings.template.php b/examples/settings.template.php new file mode 100644 index 0000000..f16a2b9 --- /dev/null +++ b/examples/settings.template.php @@ -0,0 +1,195 @@ + true, + 'dhcpd_start' => "{$octet1}.{$octet2}.{$octet3}.100", + 'dhcpd_stop' => "{$octet1}.{$octet2}.{$octet3}.150", + 'domain_name' => 'foo.bar.nl', + 'dhcpd_dns_1' => '10.56.154.13', + 'dhcpd_dns_2' => '10.34.234.66', + 'dhcpd_ip_1' => "{$octet1}.{$octet2}.{$octet3}.254", + 'dhcpguard_enabled' => true, + 'ip_subnet' => "{$octet1}.{$octet2}.{$octet3}.254/24", + 'is_nat' => true, + 'name' => 'LAN', + 'networkgroup' => 'LAN', + 'purpose' => 'corporate', + 'site_id' => $shasite_id, + 'vlan_enabled' => false, + ); + $wirednetworks['shop-wifi'] = array( + 'enabled' => true, + 'is_nat' => true, + 'dhcpd_ip_1' => '192.168.2.254', + 'dhcpguard_enabled' => true, + 'name' => 'shop-wifi', + 'purpose' => 'vlan-only', + 'site_id' => $shasite_id, + 'vlan_enabled' => true, + 'vlan' => 10, + ); +// Wireless networks + $wlannetworks["UBNT-{$filnr}"] = array( + 'enabled' => true, + 'is_guest' => true, + 'mac_filter_enabled' => false, + 'mac_filter_list' => array (), + 'mac_filter_policy' => 'allow', + 'name' => "UBNT-{$filnr}", + 'usergroup_id' => $shausergroup_id, + 'wlangroup_id' => $shawlangroup_id, + 'schedule' => + array ( + 0 => 'mon|0800-1800', + 1 => 'tue|0800-1800', + 2 => 'wed|0800-1800', + 3 => 'thu|0800-1800', + 4 => 'fri|0800-1800', + 5 => 'sat|0800-1800', + 6 => 'sun|0800-1800', + ), + 'schedule_enabled' => true, + 'security' => 'wpapsk', + 'site_id' => $shasite_id, + 'vlan' => '10', + 'vlan_enabled' => true, + 'wep_idx' => 1, + 'wpa_enc' => 'ccmp', + 'wpa_mode' => 'wpa2', + 'x_passphrase' => 'datisgeheim', + ); + $wlannetworks['CorporateWifi'] = array( + 'enabled' => true, + 'is_guest' => false, + 'mac_filter_enabled' => false, + 'mac_filter_list' => array (), + 'mac_filter_policy' => 'allow', + 'name' => "CorporateWifi", + 'usergroup_id' => $shausergroup_id, + 'wlangroup_id' => $shawlangroup_id, + 'schedule' => + array ( + 0 => 'mon|0800-1800', + 1 => 'tue|0800-1800', + 2 => 'wed|0800-1800', + 3 => 'thu|0800-1800', + 4 => 'fri|0800-1800', + 5 => 'sat|0800-1800', + 6 => 'sun|0800-1800', + ), + 'schedule_enabled' => true, + 'security' => 'wpapsk', + 'site_id' => $shasite_id, + 'wep_idx' => 1, + 'wpa_enc' => 'ccmp', + 'wpa_mode' => 'wpa2', + 'x_passphrase' => 'SuperSecretPassword', + ); + + // Unset this network for test shops + if(preg_match("/[0-9][9][0-9]+)/si", $filnr)) + unset($wlannetworks['CorporateWifi']); + + // Site settings template + $sitesettings['connectivity'] = array( + 'enabled' => true, + 'key' => 'connectivity', + 'site_id' => $shasite_id, + 'uplink_type' => 'gateway', + ); + $sitesettings['guest_access'] = array( + 'auth' => 'none', + 'key' => 'guest_access', + 'redirect_https' => true, + 'redirect_to_https' => false, + 'restricted_subnet_1' => '192.168.0.0/16', + 'restricted_subnet_2' => '172.16.0.0/12', + 'restricted_subnet_3' => '10.0.0.0/8', + 'site_id' => $shasite_id, + ); + $sitesettings['country'] = array( + 'code' => '528', + 'key' => 'country', + 'site_id' => $shasite_id, + ); + $sitesettings['locale'] = array( + 'key' => 'locale', + 'site_id' => $shasite_id, + 'timezone' => 'Europe/Amsterdam', + );/* + $sitesettings['porta'] = array( + 'key' => 'porta', + 'site_id' => $shasite_id, + 'ugw3_wan2_enabled' => false, + );*/ + $sitesettings['snmp'] = array( + 'community' => 'esenempee', + 'key' => 'snmp', + 'site_id' => $shasite_id, + ); + $sitesettings['rsyslogd'] = array( + 'key' => 'rsyslogd', + 'port' => '514', + 'site_id' => $shasite_id, + );/* + $sitesettings['auto_speedtest'] = array( + 'enabled' => false, + 'interval' => 20, + 'key' => 'auto_speedtest', + 'site_id' => $shasite_id, + );*/ + $sitesettings['ntp'] = array( + 'key' => 'ntp', + 'ntp_server_1' => 'ntp.xs4all.nl', + 'ntp_server_2' => '0.ubnt.pool.ntp.org', + 'site_id' => $shasite_id, + ); + /* + $sitesettings['usg'] = array( + 'broadcast_ping' => false, + 'ftp_module' => true, + 'gre_module' => true, + 'h323_module' => true, + 'key' => 'usg', + 'mdns_enabled' => false, + 'mss_clamp' => 'auto', + 'offload_accounting' => true, + 'offload_l2_blocking' => true, + 'offload_sch' => true, + 'pptp_module' => true, + 'receive_redirects' => false, + 'send_redirects' => true, + 'sip_module' => true, + 'site_id' => $shasite_id, + 'syn_cookies' => true, + 'tftp_module' => true, + 'upnp_enabled' => false, + 'upnp_nat_pmp_enabled' => true, + 'upnp_secure_mode' => true, + );*/ + $sitesettings['mgmt'] = array( + 'advanced_feature_enabled' => false, + 'alert_enabled' => true, + 'auto_upgrade' => true, + 'key' => 'mgmt', + 'led_enabled' => true, + 'site_id' => $shasite_id, + 'unifi_idp_enabled' => true, + 'x_ssh_auth_password_enabled' => true, + 'x_ssh_bind_wildcard' => false, + 'x_ssh_enabled' => true, + ); + + +?> \ No newline at end of file diff --git a/src/Client.php b/src/Client.php old mode 100755 new mode 100644 index a466f0c..933947e --- a/src/Client.php +++ b/src/Client.php @@ -897,6 +897,125 @@ class Client return $this->process_response_boolean($response); } + /** + * Set site country + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_country($country_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/country/'.trim($country_id), 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Set site locale + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_locale($locale_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/locale/'.trim($locale_id), 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Set site snmp + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_snmp($snmp_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/snmp/'.trim($snmp_id), 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Set site mgmt + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_mgmt($mgmt_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/mgmt/'.trim($mgmt_id), 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Set site guest access + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_guest_access($guest_access_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/guest_access/'.trim($guest_access_id), 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Set site ntp + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_ntp($ntp_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/ntp/'.trim($ntp_id), 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Set site connectivity + * ----------- + * required parameter = 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. + * return true on success + */ + public function set_site_connectivity($connectivity_id, $setting) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($setting); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/connectivity/'.trim($connectivity_id), 'json='.$json); + return $this->process_response_boolean($response); + } + /** * List admins * ----------- From 93d78b2f229029597c047198b0611ba563bbc7c7 Mon Sep 17 00:00:00 2001 From: 4oo4 <4oo4@users.noreply.github.com> Date: Sun, 18 Mar 2018 13:28:40 +0000 Subject: [PATCH 3/4] Add ap_upgrade_firmware.php to Examples (#12) - add ap_upgrade_firmware.php, contributed by @4oo4 --- examples/ap_upgrade_firmware.php | 33 ++++++++++++++++++++++++++++++++ examples/config.template.php | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 examples/ap_upgrade_firmware.php diff --git a/examples/ap_upgrade_firmware.php b/examples/ap_upgrade_firmware.php new file mode 100644 index 0000000..6ce0e5d --- /dev/null +++ b/examples/ap_upgrade_firmware.php @@ -0,0 +1,33 @@ +login(); + +// Run the actual upgrade +$results = $unifi_connection->upgrade_device($device_mac); + +/** + * provide feedback in json format from $response given by upgrade_device(); + */ +echo json_encode($results, JSON_PRETTY_PRINT); + +?> diff --git a/examples/config.template.php b/examples/config.template.php index 938e21c..1052620 100755 --- a/examples/config.template.php +++ b/examples/config.template.php @@ -21,4 +21,4 @@ $controllerversion = ''; // the version of the Controller software, eg. '4.6.6' /** * set to true (without quotes) to enable debug output to the browser and the PHP error log */ -$debug = false; \ No newline at end of file +$debug = false; From 4532df8eacb09d46b155134e6d2e20d25ed6dd2e Mon Sep 17 00:00:00 2001 From: Seth Mos Date: Mon, 19 Mar 2018 23:22:24 +0100 Subject: [PATCH 4/4] - Foreach the array to verify if we really need to update the settings so we only trigger for real updates. (#13) - Move Test array settings down so you don't accidentally delete sites - Don't delete sites in debug mode. - Automatically adopt device if it matches the configured LAN settings, ofcourse this only works correctly when LAN settings are unique between sites. --- examples/provision.php | 141 ++++++++++++++++++++++++++++------------- 1 file changed, 97 insertions(+), 44 deletions(-) diff --git a/examples/provision.php b/examples/provision.php index f5cb3ea..b6f5519 100644 --- a/examples/provision.php +++ b/examples/provision.php @@ -80,18 +80,6 @@ foreach($unifi_connection->list_sites() as $site){ } } -// If debug, create Fake site entries array, otherwise unset test shops -if($debug === true) { - $active_shops = array(); - $active_shops[965] = "j103b83q"; - $active_shops[943] = "winkels"; -} else { - unset($active_shops[943]); - unset($active_shops[965]); - unset($close_shops[943]); - unset($close_shops[965]); -} - // Any sites we need to create before we can continue? foreach($todo_shops as $filnr => $city){ $filnr = sprintf("%04d", $filnr); @@ -115,6 +103,19 @@ if(count($todo_shops > 0)) { } } } + +// If debug, create Fake site entries array, otherwise unset test shops +if($debug === true) { + $close_shops = array(); + $active_shops = array(); + $active_shops[965] = "j103b83q"; + $active_shops[943] = "winkels"; +} else { + unset($active_shops[943]); + unset($active_shops[965]); + unset($close_shops[943]); + unset($close_shops[965]); +} // We should have 0 todo shops now // print_r($todo_shops); @@ -139,7 +140,9 @@ foreach($active_shops as $filnr => $siteid) { if(isset($close_shops[floatval($filnr)])) { echo "Delete site {$siteid} with id ". $usergroups[0]->site_id ." for shop {$filnr}\n"; - $delete = $unifi_connection->delete_site($usergroups[0]->site_id); + if($debug===false) { + $delete = $unifi_connection->delete_site($usergroups[0]->site_id); + } if($delete === false) { echo "Failed to delete site for {$filnr}, id {$siteid}\n"; } @@ -183,32 +186,35 @@ foreach($active_shops as $filnr => $siteid) { //print_r($wlannetworks); } + foreach($sitesettings as $key => $values) { - echo "Update site setting {$key} id {$setting_id[$key]} for {$filnr}, id {$siteid}\n"; - switch($key){ - case "country": - $update_site[$key] = $unifi_connection->set_site_country($setting_id[$key], $sitesettings[$key]); - break; - case "locale": - $update_site[$key] = $unifi_connection->set_site_locale($setting_id[$key], $sitesettings[$key]); - break; - case "connectivity ": - $update_site[$key] = $unifi_connection->set_site_connectivity($setting_id[$key], $sitesettings[$key]); - break; - case "mgmt": - $update_site[$key] = $unifi_connection->set_site_mgmt($setting_id[$key], $sitesettings[$key]); - break; - case "guest_access": - $update_site[$key] = $unifi_connection->set_site_guest_access($setting_id[$key], $sitesettings[$key]); - break; - case "snmp": - $update_site[$key] = $unifi_connection->set_site_snmp($setting_id[$key], $sitesettings[$key]); - break; - case "ntp": - $update_site[$key] = $unifi_connection->set_site_ntp($setting_id[$key], $sitesettings[$key]); - break; - default: - break; + if(compare_array_item($sitesettings[$key], $setting[$key])) { + echo "Update site setting {$key} id {$setting_id[$key]} for {$filnr}, id {$siteid}\n"; + switch($key){ + case "country": + $update_site[$key] = $unifi_connection->set_site_country($setting_id[$key], $sitesettings[$key]); + break; + case "locale": + $update_site[$key] = $unifi_connection->set_site_locale($setting_id[$key], $sitesettings[$key]); + break; + case "connectivity ": + $update_site[$key] = $unifi_connection->set_site_connectivity($setting_id[$key], $sitesettings[$key]); + break; + case "mgmt": + $update_site[$key] = $unifi_connection->set_site_mgmt($setting_id[$key], $sitesettings[$key]); + break; + case "guest_access": + $update_site[$key] = $unifi_connection->set_site_guest_access($setting_id[$key], $sitesettings[$key]); + break; + case "snmp": + $update_site[$key] = $unifi_connection->set_site_snmp($setting_id[$key], $sitesettings[$key]); + break; + case "ntp": + $update_site[$key] = $unifi_connection->set_site_ntp($setting_id[$key], $sitesettings[$key]); + break; + default: + break; + } } if($update_site[$key] === false) echo "Failed to update setting {$key} for {$filnr}, id {$siteid} ". print_r($sitesettings[$key], true) ."\n"; @@ -225,7 +231,7 @@ foreach($active_shops as $filnr => $siteid) { echo "Failed to add network {$key} for {$filnr}, id {$siteid}\n"; // Do we need to update? - if($wired[$key] === true) { + if(compare_array_item($wirednetworks[$key], $wired[$key])) { echo "Update network {$key} id {$wired_id[$key]} for {$filnr}, id {$siteid}\n"; $updatenetwork[$key] = $unifi_connection->set_networksettings_base($wired_id[$key], $wirednetworks[$key]); } @@ -246,7 +252,7 @@ foreach($active_shops as $filnr => $siteid) { refresh_wlans(); // Do we need to update? - if($wlan[$key] === true) { + if(compare_array_item($wlannetworks[$key], $wlan[$key])) { echo "Update wlan {$key} id {$wlan_id[$key]} for {$filnr}, id {$siteid}\n"; $updatewlan[$key] = $unifi_connection->set_wlansettings_base($wlan_id[$key], $wlannetworks[$key]); } @@ -254,9 +260,25 @@ foreach($active_shops as $filnr => $siteid) { echo "Failed to update wlan {$key} for {$filnr}, id {$siteid} ". print_r($wlannetworks[$key], true) . print_r($wlan_id, true) ."\n"; } + + // Any devices for adoption? + $devices[$filnr] = $unifi_connection->list_devices(); + foreach($devices[$filnr] as $device) { + if($device->adopted == 1) + continue; + + // Does this unadopted device belong to this shop network? + if(netMatch($wirednetworks['LAN']['ip_subnet'], $device->ip)) { + // Adopt device in IP range. adopt_device($mac) + echo "Adopting device {$device->mac} with ip {$device->ip} in network {$wirednetworks['LAN']['ip_subnet']} for shop {$filnr}\n"; + $unifi_connection->adopt_device($device->mac); + // print_r($device); + } + } - if($debug===true) - break; + if($debug===true) { + //break; + } } $logout = $unifi_connection->logout(); @@ -280,7 +302,7 @@ function refresh_networks() { // Check if template networks exist foreach($wirednetworks as $key => $values) { if(($network->name == "$key")) { - $wired[$key] = true; + $wired[$key] = $network; $wired_id[$key] = $network->_id; $shasite_id = $network->site_id; } @@ -306,7 +328,7 @@ function refresh_wlans() { // Check if template networks exist foreach($wlannetworks as $key => $values) { if($network->name == "$key") { - $wlan[$key] = true; + $wlan[$key] = $network; $wlan_id[$key] = $network->_id; $shasite_id = $network->site_id; } @@ -318,6 +340,7 @@ function fetch_site_conf() { global $unifi_connection; global $siteconf; global $siteid; + global $sitesettings; global $setting; global $setting_id; @@ -333,4 +356,34 @@ function fetch_site_conf() { } } +// Return true or false +function compare_array_item($setting, $existing) { + $existing = (array)$existing; + unset($setting['site_id']); + unset($setting['_id']); + unset($existing['_id']); + unset($existing['site_id']); + foreach($setting as $key => $value) { + if(!is_array($setting[$key])) { + if($setting[$key] != $existing[$key]){ + echo "setting key {$key} value {$value} differs from {$existing[$key]} - "; + // print_r($setting); + // print_r($existing); + return true; + } + } + if(is_array($setting[$key])) { + $diff = array(); + $diff = array_diff_assoc($setting[$key], $existing[$key]); + if(!empty($diff)) { + echo "setting subkey {$key} differs diff count ". count($diff)."\n"; + // print_r($diff); + // print_r($setting); + // print_r($existing); + return true; + } + } + } + return false; +} ?> \ No newline at end of file