Skip to content

Commit

Permalink
general refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
malle-pietje committed Oct 27, 2021
1 parent 82861c0 commit fa83cf8
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 83 deletions.
2 changes: 1 addition & 1 deletion ajax/fetch_about_modal_metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
* output the results with correct JSON formatting
*/
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($results));
echo(json_encode($results));
56 changes: 27 additions & 29 deletions ajax/fetch_collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
'login_perc' => 0,
'load_perc' => 0,
],
'count' => 0,
'data' => []
'count' => 0,
'data' => [],
];

$output_method = 'json';
Expand Down Expand Up @@ -106,20 +106,15 @@
$output_method = $_POST['selected_output_method'];
}

if (empty($params)) {
switch ($method) {
case 'stat_5minutes_gateway':
$params = [null, null, $gateway_stats_attribs];
break;
case 'stat_hourly_gateway':
$params = [null, null, $gateway_stats_attribs];
break;
case 'stat_daily_gateway':
$params = [null, null, $gateway_stats_attribs];
break;
case 'stat_monthly_gateway':
$params = [null, null, $gateway_stats_attribs];
}
$gateway_stats_methods = [
'stat_5minutes_gateway',
'stat_hourly_gateway',
'stat_daily_gateway',
'stat_monthly_gateway'
];

if (empty($params) && in_array($method, $gateway_stats_methods)) {
$params = [null, null, $gateway_stats_attribs];
}

if (!empty($method) && !empty($site_id)) {
Expand All @@ -129,7 +124,8 @@
/**
* create an instance of the Unifi API client class, log in to the controller and pull the requested data
*/
$unifi_connection = new UniFi_API\Client(trim($controller['user']), trim($controller['password']), trim(rtrim($controller['url'], "/")), $site_id);
$unifi_connection = new UniFi_API\Client(trim($controller['user']), trim($controller['password']),
trim(rtrim($controller['url'], "/")), $site_id);
$loginresults = $unifi_connection->login();

/**
Expand Down Expand Up @@ -170,19 +166,21 @@
* for Kint we need to return the results in a slightly different manner
* Rich render mode
*/
Kint::$display_called_from = false;
Kint\Renderer\RichRenderer::$folder = false;
$results['data'] = @d($data_array);
} else if ($output_method === 'kint_plain') {
/**
* Plain render mode
*/
Kint::$display_called_from = false;
Kint::$display_called_from = false;
Kint\Renderer\RichRenderer::$folder = false;
Kint\Renderer\TextRenderer::$decorations = false;
$results['data'] = @s($data_array);
$results['data'] = @d($data_array);
} else {
$results['data'] = $data_array;
if ($output_method === 'kint_plain') {
/**
* Plain render mode
*/
Kint::$display_called_from = false;
Kint\Renderer\RichRenderer::$folder = false;
Kint\Renderer\TextRenderer::$decorations = false;
$results['data'] = @s($data_array);
} else {
$results['data'] = $data_array;
}
}
}

Expand Down Expand Up @@ -213,6 +211,6 @@
* output the results with correct JSON formatting
*/
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($results));
echo(json_encode($results));

$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';
25 changes: 11 additions & 14 deletions ajax/fetch_sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
'state' => 'success',
'message' => 'successfully fetched sites',
'count' => 0,
'data' => []
'data' => [],
];

if (!empty($_SESSION['controller'])) {
Expand All @@ -51,10 +51,10 @@

if (!empty($host) && !empty($port)) {
$fp = @fsockopen($host, $port, $errno, $errstr, 2);
if(!$fp) {
error_log("we have a connection error {$errstr} ({$errno})");
if (!$fp) {
error_log("we have a connection error $errstr ($errno)");
$results['state'] = 'error';
$results['message'] = "we are unable to connect to the UniFi controller, {$errstr} ({$errno})!";
$results['message'] = "we are unable to connect to the UniFi controller, $errstr ($errno)!";
} else {
/**
* and we can continue
Expand All @@ -64,7 +64,8 @@
/**
* create an instance of the Unifi API client class, log in to the controller and pull the requested data
*/
$unifi_connection = new UniFi_API\Client(trim($controller['user']), trim($controller['password']), trim(rtrim($controller['url'], "/")), 'default');
$unifi_connection = new UniFi_API\Client(trim($controller['user']), trim($controller['password']),
trim(rtrim($controller['url'], "/")), 'default');
$loginresults = $unifi_connection->login();

/**
Expand All @@ -84,10 +85,6 @@
error_log('DEBUG: ' . count($sites_array) . ' sites collected');
}

if (empty($sites_array)) {
$sites_array = [];
}

/**
* store the cookies from the controller for faster reconnects
*/
Expand All @@ -98,15 +95,15 @@
*/
foreach ($sites_array as $site) {
$results['data'][] = [
'site_id' => isset($site->name) ? $site->name : $unknown_string,
'site_full_name' => isset($site->desc) ? $site->desc : $unknown_string
'site_id' => $site->name ?? $unknown_string,
'site_full_name' => $site->desc ?? $unknown_string,
];
}

/**
* sort the sites array by full name
*/
usort($results['data'], function($a, $b) {
usort($results['data'], function ($a, $b) {
if ($a['site_full_name'] == $b['site_full_name']) {
return 0;
}
Expand Down Expand Up @@ -139,6 +136,6 @@
* output the results with correct json formatting
*/
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($results));
echo(json_encode($results));

$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';
$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';
9 changes: 5 additions & 4 deletions ajax/show_api_debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

if (!empty($host) && !empty($port)) {
$fp = @fsockopen($host, $port, $errno, $errstr, 2);
if(!$fp) {
echo "we are unable to connect to the UniFi controller {$errstr} ({$errno})" . PHP_EOL . PHP_EOL;
if (!$fp) {
echo "we are unable to connect to the UniFi controller $errstr ($errno)" . PHP_EOL . PHP_EOL;
} else {
/**
* and we can continue
Expand All @@ -53,7 +53,8 @@
/**
* create an instance of the Unifi API client class, log in to the controller and pull the sites
*/
$unifi_connection = new UniFi_API\Client(trim($controller['user']), trim($controller['password']), trim(rtrim($controller['url'], "/")), 'default');
$unifi_connection = new UniFi_API\Client(trim($controller['user']), trim($controller['password']),
trim(rtrim($controller['url'], "/")), 'default');
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();

Expand All @@ -77,4 +78,4 @@
}
} else {
echo 'ignore';
}
}
4 changes: 2 additions & 2 deletions ajax/update_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
$results = [
'status' => 'success',
'message' => 'controller updated'
'message' => 'controller updated',
];

/**
Expand All @@ -54,4 +54,4 @@
* output the results with correct JSON formatting
*/
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($results));
echo(json_encode($results));
4 changes: 2 additions & 2 deletions ajax/update_theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
$results = [
'status' => 'success',
'message' => 'theme updated'
'message' => 'theme updated',
];

/**
Expand All @@ -51,4 +51,4 @@
* output the results with correct JSON formatting
*/
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($results));
echo(json_encode($results));
4 changes: 2 additions & 2 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* This file is subject to the MIT license that is bundled with this package in the file LICENSE.md
*/
define('TOOL_VERSION', '2.0.20');
const TOOL_VERSION = '2.0.20';

/**
* gather some basic information for the About modal
Expand Down Expand Up @@ -38,7 +38,7 @@ function getClientVersion()
{
if (is_readable('composer.lock')) {
$composer_lock = file_get_contents('composer.lock');
$json_decoded = json_decode($composer_lock, true);
$json_decoded = json_decode($composer_lock, true);
if (isset($json_decoded['packages'])) {
foreach ($json_decoded['packages'] as $package) {
if ($package['name'] === 'art-of-wifi/unifi-api-client') {
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"require": {
"twig/twig": "1.*",
"kint-php/kint": "3.*",
"art-of-wifi/unifi-api-client": "^1.1"
"art-of-wifi/unifi-api-client": "^1.1",
"php": ">=7.2.5",
"ext-curl": "*",
"ext-json": "*"
}
}
8 changes: 6 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
*/

use Twig\Environment;
use Twig\Loader\FilesystemLoader;

session_start();

/**
Expand Down Expand Up @@ -37,8 +41,8 @@
/**
* initialize the Twig loader early on in case we need to render the error page
*/
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);
$loader = new FilesystemLoader('templates');
$twig = new Environment($loader);

/**
* load the configuration file, if readable
Expand Down Expand Up @@ -116,9 +120,9 @@
/**
* if needed, we request the user to login
*/
if ($user_authentication && (empty($_SESSION['logged_in']) || $_SESSION['logged_in'] === false)) {
if ($user_authentication && (empty($_SESSION['logged_in']))) {
if (!empty($_SESSION['login_error_message'])) {
$login_error = $_SESSION['login_error_message'];
$login_error = $_SESSION['login_error_message'];
$_SESSION['login_error_message'] = '';
} else {
$login_error = '';
Expand Down Expand Up @@ -152,4 +156,4 @@
]);
}

exit;
exit;
Loading

0 comments on commit fa83cf8

Please sign in to comment.