diff --git a/README.md b/README.md index 040768d..943eb3d 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,8 @@ Alternatively you may choose to download the zip file and unzip it in your direc ### Configuration - credentials for access to the UniFi Controller API need to be configured in the file named `config.template.php` which should be copied/renamed to `config.php` before using the UniFi API browser tool -- please see the `config.template.php` file for further instructions +- starting with API Browser tool version 1.0.3 you can store **multiple controller configurations** in a single `config.php` file +- please refer to the comments in the `config.template.php` file for further instructions - after following these steps, you can open the tool in your browser (assuming you installed it in the root folder of your web server as suggested above) by going to this url: `http://serverip/UniFi-API-browser/` ### Updates diff --git a/config.template.php b/config.template.php index d460f43..7cf87c8 100644 --- a/config.template.php +++ b/config.template.php @@ -1,37 +1,81 @@ '', // the user name for access to the UniFi Controller + 'password' => '', // the password for access to the UniFi Controller + 'url' => '', // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443' + 'name' => '', + 'version' => '' // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0) +]; -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +$controllers[1] = [ + 'user' => '', // the user name for access to the UniFi Controller + 'password' => '', // the password for access to the UniFi Controller + 'url' => '', // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443' + 'name' => '', + 'version' => '' // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0) +]; -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +$controllers[2] = [ + 'user' => '', // the user name for access to the UniFi Controller + 'password' => '', // the password for access to the UniFi Controller + 'url' => '', // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443' + 'name' => '', + 'version' => '' // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0) +]; */ -$controlleruser = ''; // the user name for access to the Unifi Controller -$controllerpassword = ''; // the password for access to the Unifi Controller -$controllerurl = ''; // full url to the Unifi Controller, eg. 'https://22.22.11.11:8443' -$controllerversion = ''; // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0) $cookietimeout = '3600'; // time of inactivity in seconds, after which the PHP session cookie will be refreshed // after the cookie refresh the site and data collection will need to be selected again -$theme = 'bootstrap'; /* your default theme of choice, pick one from the list below - bootstrap, cerulean, cosmo, cyborg, darkly, flatly, journal, lumen, paper - readable, sandstone, simplex, slate, spacelab, superhero, united, yeti - */ + +$theme = 'bootstrap'; // your default theme of choice, pick one from the list below: + // bootstrap, cerulean, cosmo, cyborg, darkly, flatly, journal, lumen, paper + // readable, sandstone, simplex, slate, spacelab, superhero, united, yeti + $debug = false; // set to true (without quotes) to enable debug output to the browser and the PHP error log ?> \ No newline at end of file diff --git a/index.php b/index.php index 1e5c6f0..8443949 100644 --- a/index.php +++ b/index.php @@ -1,385 +1,428 @@ The file config.php is not readable or does not exist.' . '
If you have not yet done so, please copy/rename the config.template.php file to config.php and modify' . 'the contents as required.'; +} else { + include('config.php'); } -include('config.php'); - -/* -determine whether we have reached the cookie timeout, if so, refresh the PHP session -else, update last activity time stamp -*/ +/** + * determine whether we have reached the cookie timeout, if so, refresh the PHP session + * else, update last activity time stamp + */ if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $cookietimeout)) { - /* - last activity was longer than "$cookietimeout" seconds ago - */ + /** + * last activity was longer than "$cookietimeout" seconds ago + */ session_unset(); session_destroy(); } -$_SESSION['last_activity'] = time(); -/* -collect cURL version details for the info modal -*/ -$curl_info = curl_version(); -$curl_version = $curl_info['version']; +$_SESSION['last_activity'] = time(); -/* -process the GET variables and store them in the $_SESSION array, -if a GET variable is not set, get the value from $_SESSION (if available) -- site_id -Only process these after site_id is set: -- action -- output_format -- theme -*/ -if (isset($_GET['site_id'])) { - $site_id = $_GET['site_id']; - $_SESSION['site_id'] = $site_id; - $site_name = $_GET['site_name']; - $_SESSION['site_name'] = $site_name; +/** + * collect cURL version details for the info modal + */ +$curl_info = curl_version(); +$curl_version = $curl_info['version']; + +/** + * process the GET variables and store them in the $_SESSION array + * if a GET variable is not set, get the values from $_SESSION (if available) + * + * Process in this order: + * - controller_id + * Only process this after controller_id is set: + * - site_id + * Only process these after site_id is set: + * - action + * - output_format + * - theme + */ +if (isset($_GET['controller_id'])) { + $controller = $controllers[$_GET['controller_id']]; + $_SESSION['controller'] = $controller; + + unset($_SESSION['site_id']); + unset($_SESSION['site_name']); + unset($_SESSION['sites']); } else { - if (isset($_SESSION['site_id'])) { - $site_id = $_SESSION['site_id']; - $site_name = $_SESSION['site_name']; + if (isset($_SESSION['controller']) && isset($controllers)) { + $controller = $_SESSION['controller']; + } else { + /** + * if the user has configured a single controller, we push it's details + * to the $_SESSION and $controller arrays + */ + $_SESSION['controller'] = array('user' => $controlleruser, + 'password' => $controllerpassword, + 'url' => $controllerurl, + 'name' => 'Default Controller', + 'version' => $controllerversion + ); + $controller = $_SESSION['controller']; + } - if (isset($_GET['action'])) { - $action = $_GET['action']; - $_SESSION['action'] = $action; - } else { - if (isset($_SESSION['action'])) { - $action = $_SESSION['action']; + if (isset($_GET['site_id'])) { + $site_id = $_GET['site_id']; + $_SESSION['site_id'] = $site_id; + $site_name = $_GET['site_name']; + $_SESSION['site_name'] = $site_name; + } else { + if (isset($_SESSION['site_id'])) { + $site_id = $_SESSION['site_id']; + $site_name = $_SESSION['site_name']; + + if (isset($_GET['action'])) { + $action = $_GET['action']; + $_SESSION['action'] = $action; + } else { + if (isset($_SESSION['action'])) { + $action = $_SESSION['action']; + } } - } - if (isset($_GET['output_format'])) { - $output_format = $_GET['output_format']; - $_SESSION['output_format'] = $output_format; - } else { - if (isset($_SESSION['output_format'])) { - $output_format = $_SESSION['output_format']; + if (isset($_GET['output_format'])) { + $output_format = $_GET['output_format']; + $_SESSION['output_format'] = $output_format; + } else { + if (isset($_SESSION['output_format'])) { + $output_format = $_SESSION['output_format']; + } } - } - if (isset($_GET['theme'])) { - $theme = $_GET['theme']; - $_SESSION['theme'] = $theme; - } else { - if (isset($_SESSION['theme'])) { - $theme = $_SESSION['theme']; + if (isset($_GET['theme'])) { + $theme = $_GET['theme']; + $_SESSION['theme'] = $theme; + } else { + if (isset($_SESSION['theme'])) { + $theme = $_SESSION['theme']; + } } } } } -/* -display info message when no site is selected or no data collection is selected -placed here so they can be overwritten by more "severe" error messages later down -*/ -if ($action === '') { +/** + * display info message when no controller, site or data collection is selected + * placed here so they can be overwritten by more "severe" error messages later down + */ +if ($action == '') { $alert_message = ''; } -if ($site_id === '') { + +if ($site_id == '' && isset($_SESSION['controller'])) { $alert_message = ''; } -/* -load the Unifi API connection class and log in to the controller -- if an error occurs during the login process, an alert is displayed on the page -*/ -require('phpapi/class.unifi.php'); +if (!isset($_SESSION['controller'])) { + $alert_message = ''; +} -$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$unifidata->debug = $debug; -$loginresults = $unifidata->login(); +/** + * load the UniFi API client class and log in to the controller + * - if an error occurs during the login process, an alert is displayed on the page + */ +require('phpapi/class.unifi.php'); -if($loginresults === 400) { - $alert_message = ''; -} +/** + * Do this when a controller has been selected and was stored in $_SESSION + */ +if (isset($_SESSION['controller'])) { + $unifidata = new unifiapi($controller['user'], $controller['password'], $controller['url'], $site_id, $controller['version']); + $unifidata->debug = $debug; + $loginresults = $unifidata->login(); + + if($loginresults === 400) { + $alert_message = ''; + } -/* -get the list of sites managed by the controller (if not already stored in $_SESSION) -*/ -if (!isset($_SESSION['sites']) || $_SESSION['sites'] === '') { - $sites = $unifidata->list_sites(); - $_SESSION['sites'] = $sites; -} else { - $sites = $_SESSION['sites']; -} + /** + * Get the list of sites managed by the controller (if not already stored in $_SESSION) + */ + if (!isset($_SESSION['sites']) || $_SESSION['sites'] == '') { + $sites = $unifidata->list_sites(); + $_SESSION['sites'] = $sites; + } else { + $sites = $_SESSION['sites']; + } -/* -get the version of the controller (if not already stored in $_SESSION or when empty) -only get the version once a site has been selected -*/ -if($site_id != '') { - if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] === '') { - $site_info = $unifidata->stat_sysinfo(); - $detected_controller_version = $site_info[0]->version; - $_SESSION['detected_controller_version'] = $detected_controller_version; + /** + * get the version of the controller (if not already stored in $_SESSION or when empty) + * only get the version once a site has been selected + */ + if($site_id != '') { + if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] === '') { + $site_info = $unifidata->stat_sysinfo(); + $detected_controller_version = $site_info[0]->version; + $_SESSION['detected_controller_version'] = $detected_controller_version; + } else { + $detected_controller_version = $_SESSION['detected_controller_version']; + } } else { - $detected_controller_version = $_SESSION['detected_controller_version']; + $detected_controller_version = 'undetected'; } -} else { - $detected_controller_version = 'undetected'; } -/* -execute timing of controller login -*/ +/** + * execute timing of controller login + */ $time_1 = microtime(true); $time_after_login = $time_1 - $time_start; -/* -select the required call to the Unifi Controller API based on the selected action -*/ +/** + * select the required call to the UniFi Controller API based on the selected action + */ switch ($action) { case 'list_clients': - $selection = 'list online clients'; - $data = $unifidata->list_clients(); + $selection = 'list online clients'; + $data = $unifidata->list_clients(); break; case 'stat_allusers': - $selection = 'stat all users'; - $data = $unifidata->stat_allusers(); + $selection = 'stat all users'; + $data = $unifidata->stat_allusers(); break; case 'stat_auths': - $selection = 'stat active authorisations'; - $data = $unifidata->stat_auths(); + $selection = 'stat active authorisations'; + $data = $unifidata->stat_auths(); break; case 'list_guests': - $selection = 'list guests'; - $data = $unifidata->list_guests(); + $selection = 'list guests'; + $data = $unifidata->list_guests(); break; case 'list_usergroups': - $selection = 'list usergroups'; - $data = $unifidata->list_usergroups(); + $selection = 'list usergroups'; + $data = $unifidata->list_usergroups(); break; case 'stat_hourly_site': - $selection = 'hourly site stats'; - $data = $unifidata->stat_hourly_site(); + $selection = 'hourly site stats'; + $data = $unifidata->stat_hourly_site(); break; case 'stat_sysinfo': - $selection = 'sysinfo'; - $data = $unifidata->stat_sysinfo(); + $selection = 'sysinfo'; + $data = $unifidata->stat_sysinfo(); break; case 'stat_hourly_aps': - $selection = 'hourly ap stats'; - $data = $unifidata->stat_hourly_aps(); + $selection = 'hourly ap stats'; + $data = $unifidata->stat_hourly_aps(); break; case 'stat_daily_site': - $selection = 'daily site stats'; - $data = $unifidata->stat_daily_site(); + $selection = 'daily site stats'; + $data = $unifidata->stat_daily_site(); break; case 'list_devices': - $selection = 'list devices'; - $data = $unifidata->list_aps(); + $selection = 'list devices'; + $data = $unifidata->list_aps(); break; case 'list_wlan_groups': - $selection = 'list wlan groups'; - $data = $unifidata->list_wlan_groups(); + $selection = 'list wlan groups'; + $data = $unifidata->list_wlan_groups(); break; case 'stat_sessions': - $selection = 'stat sessions'; - $data = $unifidata->stat_sessions(); + $selection = 'stat sessions'; + $data = $unifidata->stat_sessions(); break; case 'list_users': - $selection = 'list users'; - $data = $unifidata->list_users(); + $selection = 'list users'; + $data = $unifidata->list_users(); break; case 'list_rogueaps': - $selection = 'list rogue access points'; - $data = $unifidata->list_rogueaps(); + $selection = 'list rogue access points'; + $data = $unifidata->list_rogueaps(); break; case 'list_events': - $selection = 'list events'; - $data = $unifidata->list_events(); + $selection = 'list events'; + $data = $unifidata->list_events(); break; case 'list_alarms': - $selection = 'list alerts'; - $data = $unifidata->list_alarms(); + $selection = 'list alerts'; + $data = $unifidata->list_alarms(); break; case 'list_wlanconf': - $selection = 'list wlan config'; - $data = $unifidata->list_wlanconf(); + $selection = 'list wlan config'; + $data = $unifidata->list_wlanconf(); break; case 'list_health': - $selection = 'site health metrics'; - $data = $unifidata->list_health(); + $selection = 'site health metrics'; + $data = $unifidata->list_health(); break; case 'list_dashboard': - $selection = 'site dashboard metrics'; - $data = $unifidata->list_dashboard(); + $selection = 'site dashboard metrics'; + $data = $unifidata->list_dashboard(); break; case 'list_settings': - $selection = 'list site settings'; - $data = $unifidata->list_settings(); + $selection = 'list site settings'; + $data = $unifidata->list_settings(); break; case 'list_sites': - $selection = 'details of available sites'; - $data = $sites; + $selection = 'details of available sites'; + $data = $sites; break; case 'list_extension': - $selection = 'list VoIP extensions'; - $data = $unifidata->list_extension(); + $selection = 'list VoIP extensions'; + $data = $unifidata->list_extension(); break; case 'list_portconf': - $selection = 'list port configuration'; - $data = $unifidata->list_portconf(); + $selection = 'list port configuration'; + $data = $unifidata->list_portconf(); break; case 'list_networkconf': - $selection = 'list network configuration'; - $data = $unifidata->list_networkconf(); + $selection = 'list network configuration'; + $data = $unifidata->list_networkconf(); break; case 'list_dynamicdns': - $selection = 'dynamic dns configuration'; - $data = $unifidata->list_dynamicdns(); + $selection = 'dynamic dns configuration'; + $data = $unifidata->list_dynamicdns(); break; case 'list_portforwarding': - $selection = 'list port forwarding rules'; - $data = $unifidata->list_portforwarding(); + $selection = 'list port forwarding rules'; + $data = $unifidata->list_portforwarding(); break; case 'list_portforward_stats': - $selection = 'list port forwarding stats'; - $data = $unifidata->list_portforward_stats(); + $selection = 'list port forwarding stats'; + $data = $unifidata->list_portforward_stats(); break; case 'stat_voucher': - $selection = 'list hotspot vouchers'; - $data = $unifidata->stat_voucher(); + $selection = 'list hotspot vouchers'; + $data = $unifidata->stat_voucher(); break; case 'stat_payment': - $selection = 'list hotspot payments'; - $data = $unifidata->stat_payment(); + $selection = 'list hotspot payments'; + $data = $unifidata->stat_payment(); break; case 'list_hotspotop': - $selection = 'list hotspot operators'; - $data = $unifidata->list_hotspotop(); + $selection = 'list hotspot operators'; + $data = $unifidata->list_hotspotop(); break; case 'list_self': - $selection = 'self'; - $data = $unifidata->list_self(); + $selection = 'self'; + $data = $unifidata->list_self(); break; default: break; } -/* -count the number of objects collected from the controller -*/ +/** + * count the number of objects collected from the controller + */ if($action!=''){ $objects_count = count($data); } -/* -create the url to the css file based on the selected theme (standard Bootstrap or one of the Bootswatch themes) -*/ +/** + * create the url to the css file based on the selected theme (standard Bootstrap or one of the Bootswatch themes) + */ if ($theme === 'bootstrap') { $cssurl = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'; } else { $cssurl = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/' . $theme . '/bootstrap.min.css'; } -/* -execute timing of data collection from controller -*/ -$time_2 = microtime(true); -$time_after_load = $time_2 - $time_start; +/** + * execute timing of data collection from controller + */ +$time_2 = microtime(true); +$time_after_load = $time_2 - $time_start; -/* -calculate all the timings/percentages -*/ +/** + * calculate all the timings/percentages + */ $time_end = microtime(true); $time_total = $time_end - $time_start; $login_perc = ($time_after_login/$time_total)*100; $load_perc = (($time_after_load - $time_after_login)/$time_total)*100; $remain_perc = 100-$login_perc-$load_perc; -/* -shared functions -*/ +/** + * shared functions + */ function print_output($output_format, $data) { - /* - function to print the output - switch depending on the selected $output_format - */ + /** + * function to print the output + * switch depending on the selected $output_format + */ switch ($output_format) { case 'json': echo json_encode($data, JSON_PRETTY_PRINT); @@ -404,322 +447,367 @@ function to print the output } } -/* -log off from the Unifi controller API -*/ -$logout_results = $unifidata->logout(); - +if (isset($_SESSION['controller'])) { + /** + * log off from the UniFi controller API + */ + $logout_results = $unifidata->logout(); +} ?> - - - Unifi API browser - - - - - - + + + UniFi API browser + + + + + +
-
- -
-
-
- - site id: - site name: - - - collection: - - output: - - # of objects: - +
+
-
- - - - total elapsed time: seconds
-
-
- API login time +
+
+ + site id: + site name: + + + collection: + + output: + + # of objects: +
-
- data load time +
+ + + + total elapsed time: seconds
+
+
+ API login time +
+
+ data load time +
+
+ PHP overhead +
+
+
+
-
- PHP overhead -
-
-
-
-