diff --git a/_api/guests.php b/_api/guests.php new file mode 100644 index 0000000..5cfe909 --- /dev/null +++ b/_api/guests.php @@ -0,0 +1,12 @@ +login(); +$clients = []; +foreach ($unifi_connection->list_clients() as $client) { + $client = get_object_vars($client); + if( $client['is_guest'] ) $clients[] = $client; +} +print(json_encode($clients)); diff --git a/submit.php b/_api/submit.php similarity index 97% rename from submit.php rename to _api/submit.php index 9464912..de15d74 100644 --- a/submit.php +++ b/_api/submit.php @@ -1,44 +1,44 @@ - "Invalid mac address", 'success' => FALSE], JSON_PRETTY_PRINT)); - return; -} - -$unifi_connection = new UniFi_API\Client($_ENV['hotspot_user'], $_ENV['hotspot_password'], $_ENV['unifi_url'], $_ENV['unifi_site'], $_ENV['unifi_version'], FALSE); -$login = $unifi_connection->login(); -$vouchers = $unifi_connection->stat_voucher(); - -if (isset($code)) { - foreach ($vouchers as $voucher) { - $voucher = get_object_vars($voucher); - if ($voucher['code'] == $code) { - $max_up = NULL; - $max_down = NULL; - $usage_quota = NULL; - if (isset($voucher['qos_rate_max_up'])) $max_up = $voucher['qos_rate_max_up']; - if (isset($voucher['qos_rate_max_down'])) $max_down = $voucher['qos_rate_max_down']; - if (isset($voucher['qos_usage_quota'])) $usage_quota = $voucher['qos_usage_quota']; - - $authorized = $unifi_connection->authorize_guest($mac, $voucher['duration'], $max_up, $max_down, $usage_quota, $ap_mac); - $unifi_connection->revoke_voucher($voucher['_id']); - header('Status: 202'); - print(json_encode(['success' => TRUE], JSON_PRETTY_PRINT)); - return; - } - } -} - -header('Status: 400'); -print(json_encode(['success' => FALSE, 'error' => 'Voucher code not found'], JSON_PRETTY_PRINT)); -return; + "Invalid mac address", 'success' => FALSE], JSON_PRETTY_PRINT)); + return; +} + +$unifi_connection = new UniFi_API\Client($_ENV['hotspot_user'], $_ENV['hotspot_password'], $_ENV['unifi_url'], $_ENV['unifi_site'], $_ENV['unifi_version'], FALSE); +$login = $unifi_connection->login(); +$vouchers = $unifi_connection->stat_voucher(); + +if (isset($code)) { + foreach ($vouchers as $voucher) { + $voucher = get_object_vars($voucher); + if ($voucher['code'] == $code) { + $max_up = NULL; + $max_down = NULL; + $usage_quota = NULL; + if (isset($voucher['qos_rate_max_up'])) $max_up = $voucher['qos_rate_max_up']; + if (isset($voucher['qos_rate_max_down'])) $max_down = $voucher['qos_rate_max_down']; + if (isset($voucher['qos_usage_quota'])) $usage_quota = $voucher['qos_usage_quota']; + + $authorized = $unifi_connection->authorize_guest($mac, $voucher['duration'], $max_up, $max_down, $usage_quota, $ap_mac); + $unifi_connection->revoke_voucher($voucher['_id']); + header('Status: 202'); + print(json_encode(['success' => TRUE], JSON_PRETTY_PRINT)); + return; + } + } +} + +header('Status: 400'); +print(json_encode(['success' => FALSE, 'error' => 'Voucher code not found'], JSON_PRETTY_PRINT)); +return; diff --git a/_layouts/default.html b/_layouts/default.html index 4acef1d..fd36e5c 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -9,18 +9,18 @@ - +
- {{ content }} -
+ {{ content }} - + {% if page.name == 'index.html' %}{% endif %} + {% if page.name == 'admin.html' %}{% endif %} \ No newline at end of file diff --git a/admin.html b/admin.html new file mode 100644 index 0000000..09ad0b8 --- /dev/null +++ b/admin.html @@ -0,0 +1,31 @@ +--- +layout: default +--- +
+ + +
+

Requests

+ +
+ + +
+

Online

+ +
+ +
+ +
+
  • +
    + + +
    +
  • +
  • +
    \ No newline at end of file diff --git a/admin.php b/admin.php deleted file mode 100644 index 30ce105..0000000 --- a/admin.php +++ /dev/null @@ -1,60 +0,0 @@ ---- -layout: default ---- -login(); -$guests = $unifi_connection->list_guests(); -$clients = $unifi_connection->list_clients(); -?> -
    - -
    -

    Requests

    - -
    - -
    -

    Online

    - -
    -
    \ No newline at end of file diff --git a/js/admin.js b/js/admin.js new file mode 100644 index 0000000..8287114 --- /dev/null +++ b/js/admin.js @@ -0,0 +1,36 @@ +$(document).ready(function () { + $.ajax({ + type: 'GET', + url: '/_api/guests.php', + success: function (res) { + //console.info(res); + res.forEach(client => { + if (client['authorized']) { + addOnline(client); + + } else { + addRequest(client) + } + }); + }, + error: function (res) { + console.error(res); + } + }); +}); + +function addRequest(client) { + let req = $('#request').clone(); + req.prop('id', client['mac']); + req.prepend(client['hostname']); + req.show(); + req.appendTo('#requests'); +} + +function addOnline(client) { + let online = $('li#online').clone(); + online.prop('id', client['mac']); + online.prepend(client['hostname']); + online.show(); + online.appendTo('#online'); +} \ No newline at end of file diff --git a/js/main.js b/js/main.js index aba4500..ab65610 100644 --- a/js/main.js +++ b/js/main.js @@ -34,7 +34,7 @@ $('form').submit(function (e) { success: function () { // Display success window? displayView('#success'); - setTimeout(function () { + setTimeout(function () { window.location.replace(decodeURIComponent(getUrlVars()['url'])); }, 2000); }, @@ -45,7 +45,7 @@ $('form').submit(function (e) { error: function (res) { // Display #error with #error-msg set displayView('#error'); - console.error(res); + console.error(res); $('#error-msg').text('Error: ' + res.responseJSON['error']); }