Skip to content

Commit

Permalink
Move php & create admin.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jun 19, 2020
1 parent ad9c9f2 commit e3cae4c
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 110 deletions.
12 changes: 12 additions & 0 deletions _api/guests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
header('Content-Type: application/json');

$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();
$clients = [];
foreach ($unifi_connection->list_clients() as $client) {
$client = get_object_vars($client);
if( $client['is_guest'] ) $clients[] = $client;
}
print(json_encode($clients));
88 changes: 44 additions & 44 deletions submit.php → _api/submit.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

$mac = filter_input(INPUT_POST, 'id');
$ap_mac = filter_input(INPUT_POST, 'ap');
$time = filter_input(INPUT_POST, 't');
$url = filter_input(INPUT_POST, 'url', FILTER_SANITIZE_URL);
$ssid = filter_input(INPUT_POST, 'ssid');
$code = str_replace('-', '', filter_input(INPUT_POST, 'code', FILTER_SANITIZE_NUMBER_INT));

if (!filter_var($mac, FILTER_VALIDATE_MAC) || !filter_var($ap_mac, FILTER_VALIDATE_MAC)) {
header('Status: 400');
print(json_encode(['error' => "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;
<?php

require_once __DIR__ . '/vendor/autoload.php';

$mac = filter_input(INPUT_POST, 'id');
$ap_mac = filter_input(INPUT_POST, 'ap');
$time = filter_input(INPUT_POST, 't');
$url = filter_input(INPUT_POST, 'url', FILTER_SANITIZE_URL);
$ssid = filter_input(INPUT_POST, 'ssid');
$code = str_replace('-', '', filter_input(INPUT_POST, 'code', FILTER_SANITIZE_NUMBER_INT));

if (!filter_var($mac, FILTER_VALIDATE_MAC) || !filter_var($ap_mac, FILTER_VALIDATE_MAC)) {
header('Status: 400');
print(json_encode(['error' => "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;
8 changes: 4 additions & 4 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
<link rel="apple-touch-icon" href="/img/apple-touch-icon.png" sizes="256x256" />
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/all.min.css"/>
<link rel="stylesheet" href="/css/all.min.css" />
</head>

<body>
<div class="container">
{{ content }}
</div>
{{ content }}
</div>
<script src="/js/jquery.min.js"></script>
<script src="/js/jquery-ui.min.js"></script>
<script src="/js/main.js"></script>
<script src="/js/modenizr-webp.min.js"></script>
{% if page.name == 'index.html' %}<script src="/js/main.js"></script>{% endif %}
{% if page.name == 'admin.html' %}<script src="/js/admin.js"></script>{% endif %}
</body>

</html>
31 changes: 31 additions & 0 deletions admin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
layout: default
---
<div class="main col-12 col-md-10 col-xl-5">

<!-- Requests -->
<div class="col-md-6 noselect" style="float:left;text-align: left;">
<h1 style="text-align: center;">Requests</h1>
<ul class="list-group" id="requests">


</ul>
</div>

<!-- Online users -->
<div class="col-md-6 noselect" style="float:right; text-align: left;">
<h1 style="text-align: center;">Online</h1>
<ul class="list-group" id="online"></ul>
</div>

</div>

<div style="display: none;">
<li class="list-group-item d-flex justify-content-between align-items-center" id="request">
<div class="request-buttons">
<button type="button" class="btn btn-success request-approve"><i class="fas fa-check"></i></button>
<button type="button" class="btn btn-danger request-deny"><i class="fas fa-times"></i></button>
</div>
</li>
<li class="list-group-item" id="online" style="height:56px"></li>
</div>
60 changes: 0 additions & 60 deletions admin.php

This file was deleted.

36 changes: 36 additions & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
@@ -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');
}
4 changes: 2 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand All @@ -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']);

}
Expand Down

0 comments on commit e3cae4c

Please sign in to comment.