Permalink
Browse files

code cleanup based on analysis by [Scrutinizer](https://scrutinizer-c…

…i.com/)

included PHP API client class v1.1.38
minor UX tweaks applied to alerts and the login form
  • Loading branch information...
malle-pietje committed Dec 14, 2018
1 parent 4f29f96 commit a145cbfb6ab53c12aa4e6ed72cb17dfce9625f18
@@ -1,12 +1,3 @@
>**NOTICE**
>
>The repository has been transferred to this new GitHub organisation account, the project maintainer will remain the same and will continue to actively maintain it.
> If you have previously installed the API browser tool using `git clone`, you may consider pointing the clone to the new repository URL. You can do this with the following command from within the directory of your clone:
>
>```bash
>$ git remote set-url origin https://github.com/Art-of-WiFi/UniFi-API-browser.git
>```
## UniFi API browser

This tool is for browsing data that is exposed through Ubiquiti's UniFi Controller API, written in PHP, JavaScript and the [Bootstrap](http://getbootstrap.com/) CSS framework.
@@ -163,7 +154,7 @@ Showing the online device data collection using the default Bootstrap theme:

With one of the Bootswatch themes selected:

![Dark theme selected](https://user-images.githubusercontent.com/12016131/48832369-463c9000-ed79-11e8-841b-07e7842a23a5.png "Dark theme selected")
![Slate theme selected](https://user-images.githubusercontent.com/12016131/48832369-463c9000-ed79-11e8-841b-07e7842a23a5.png "Slate theme selected")

The "About" modal:

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -17,7 +17,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.34');
define('API_BROWSER_VERSION', '1.0.35');
define('API_CLASS_VERSION', get_client_version());
/**
@@ -1053,6 +1053,7 @@ function get_client_version()
<div class="panel-body">
<h3 align="center">UniFi Controller login</h3>
<br>
<div id="login_alert_placeholder" style="display: none"></div>
<form method="post">
<?php if (empty($controller['user'])) : ?>
<div class="form-group">
@@ -1267,14 +1268,17 @@ function switchCSS(new_theme) {
* if needed we display the login form
*/
if (show_login == 1 || show_login == 'true') {
$('#login_form').show();
$('#login_alert_placeholder').html(alert_message);
$('#login_alert_placeholder').show();
$('#login_form').fadeIn(500);
} else {
$('#alert_placeholder').html(alert_message);
$('#alert_placeholder').fadeIn(500);
}
/**
* update dynamic elements in the DOM using some of the above variables
*/
$('#alert_placeholder').html(alert_message);
$('#alert_placeholder').fadeIn(1000);
$('#span_site_id').html(site_id);
$('#span_site_name').html(site_name);
$('#span_output_format').html(output_format);
@@ -1424,7 +1428,7 @@ function hideTooltip(btn) {
setTimeout(function () {
$(btn).tooltip('hide')
.attr('data-original-title', 'Copy to clipboard');
}, 1000);
}, 500);
}
/**
@@ -1,9 +1,97 @@
## 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.8.24 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [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.9.29 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).

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

## Requirements

- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.0.7 and cURL 7.37.0)
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running

## Installation ##

You can use [Composer](#composer), [Git](#git) or simply [Download the Release](#download-the-release) to install the API client class.

### Composer

The preferred method is via [composer](https://getcomposer.org). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed.

Once composer is installed, simply execute this command from the shell in your project directory:

```sh
composer require art-of-wifi/unifi-api-client
```

Or you can manually add the package to your composer.json file:

```javascript
{
"require": {
"art-of-wifi/unifi-api-client": "^1.1"
}
}
```

Finally, be sure to include the autoloader in your code:

```php
require_once('vendor/autoload.php');
```

### Git

Execute the following `git` command from the shell in your project directory:

```sh
git clone https://github.com/Art-of-WiFi/UniFi-API-client.git
```

When git is done cloning, include the file containing the class like so in your code:

```php
require_once('path/to/src/Client.php');
```

### Download the Release

If you prefer not to use composer or git, you can simply [download the package](https://github.com/Art-of-WiFi/UniFi-API-client/archive/master.zip), uncompress the zip file, then include the file containing the class in your code like so:

```php
require_once('path/to/src/Client.php');
```

## Example usage

A basic example how to use the class:

```php
/**
* load the class using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* 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, true);
$login = $unifi_connection->login();
$results = $unifi_connection->list_alarms(); // returns a PHP array containing alarm objects
```

Please refer to the `examples/` directory for some more detailed examples which you can use as a starting point for your own PHP code.

#### IMPORTANT NOTES:

1. The last optional parameter that is passed to the constructor in the above example (`true`), enables validation of the controller's SSL certificate which is otherwise **disabled** by default. It is highly recommended to enable this feature in production environments where you have a valid SSL cert installed on the UniFi Controller, and which is associated with the FQDN of the server as used in the `controller_url` parameter. This option was added with API client version 1.1.16.

2. In the example above, `$site_id` is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi Controller:

`https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard`

In this case, `jl3z2shm` is the value required for $site_id.

## Methods and functions supported

The class currently supports the following functions/methods to get/post/put/delete data through the UniFi Controller API:
@@ -139,6 +227,7 @@ The class currently supports the following functions/methods to get/post/put/del
- upgrade_device_external()
- start_rolling_upgrade()
- cancel_rolling_upgrade()
- cmd_stat()

Internal functions, getters/setters:

@@ -152,94 +241,6 @@ Internal functions, getters/setters:

Please refer to the source code for more details on the functions/methods and their parameters.

## Requirements

- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.0.7 and cURL 7.37.0)
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running

## Installation ##

You can use [Composer](#composer), [Git](#git) or simply [Download the Release](#download-the-release) to install the API client class.

### Composer

The preferred method is via [composer](https://getcomposer.org). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed.

Once composer is installed, simply execute this command from the shell in your project directory:

```sh
composer require art-of-wifi/unifi-api-client
```

Or you can manually add the package to your composer.json file:

```javascript
{
"require": {
"art-of-wifi/unifi-api-client": "^1.1"
}
}
```

Finally, be sure to include the autoloader in your code:

```php
require_once('vendor/autoload.php');
```

### Git

Execute the following `git` command from the shell in your project directory:

```sh
git clone https://github.com/Art-of-WiFi/UniFi-API-client.git
```

When git is done cloning, include the file containing the class like so in your code:

```php
require_once('path/to/src/Client.php');
```

### Download the Release

If you prefer not to use composer or git, you can simply [download the package](https://github.com/Art-of-WiFi/UniFi-API-client/archive/master.zip), uncompress the zip file, then include the file containing the class in your code like so:

```php
require_once('path/to/src/Client.php');
```

## Example usage

A basic example how to use the class:

```php
/**
* load the class using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* 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, true);
$login = $unifi_connection->login();
$results = $unifi_connection->list_alarms(); // returns a PHP array containing alarm objects
```

Please refer to the `examples/` directory for some more detailed examples which you can use as a starting point for your own PHP code.

#### IMPORTANT NOTES:

1. The last optional parameter that is passed to the constructor in the above example (`true`), enables validation of the controller's SSL certificate which is otherwise **disabled** by default. It is highly recommended to enable this feature in production environments where you have a valid SSL cert installed on the UniFi Controller, and which is associated with the FQDN of the server as used in the `controller_url` parameter. This option was added with API client version 1.1.16.

2. In the example above, `$site_id` is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi Controller:

`https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard`

In this case, `jl3z2shm` is the value required for $site_id.

## Need help or have suggestions?

There is still work to be done to add functionality and further improve the usability of this class, so all suggestions/comments are welcome. Please use the GitHub [issue list](https://github.com/Art-of-WiFi/UniFi-API-client/issues) or the Ubiquiti Community forums (https://community.ubnt.com/t5/UniFi-Wireless/PHP-class-to-access-the-UniFi-controller-API-updates-and/td-p/1512870) to share your suggestions and questions.
@@ -9,6 +9,17 @@ Then update the contents of your new config.php with your controller details and

Also make sure to update the path for the composer autoloader file (`vendor/autoload.php`) or the file containing the Class itself (`src/Client.php`) in your `require_once()` statement as required.

#### Executing scripts from the CLI

Most of the included example scripts can be run from the CLI or shell as follows after the necessary credentials and parameters have been added or updated:


```sh
$ php list_site_health.php
```

NOTE: this does require the `php-cli` module to be installed

### Contribute

If you would like to share your own example file(s), please open an issue and include your code there or else create a pull request.
Oops, something went wrong.

0 comments on commit a145cbf

Please sign in to comment.