Skip to content

Commit fa83cf8

Browse files
committed
general refactoring
1 parent 82861c0 commit fa83cf8

15 files changed

+90
-83
lines changed

ajax/fetch_about_modal_metrics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@
5454
* output the results with correct JSON formatting
5555
*/
5656
header('Content-Type: application/json; charset=utf-8');
57-
echo (json_encode($results));
57+
echo(json_encode($results));

ajax/fetch_collection.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
'login_perc' => 0,
7171
'load_perc' => 0,
7272
],
73-
'count' => 0,
74-
'data' => []
73+
'count' => 0,
74+
'data' => [],
7575
];
7676

7777
$output_method = 'json';
@@ -106,20 +106,15 @@
106106
$output_method = $_POST['selected_output_method'];
107107
}
108108

109-
if (empty($params)) {
110-
switch ($method) {
111-
case 'stat_5minutes_gateway':
112-
$params = [null, null, $gateway_stats_attribs];
113-
break;
114-
case 'stat_hourly_gateway':
115-
$params = [null, null, $gateway_stats_attribs];
116-
break;
117-
case 'stat_daily_gateway':
118-
$params = [null, null, $gateway_stats_attribs];
119-
break;
120-
case 'stat_monthly_gateway':
121-
$params = [null, null, $gateway_stats_attribs];
122-
}
109+
$gateway_stats_methods = [
110+
'stat_5minutes_gateway',
111+
'stat_hourly_gateway',
112+
'stat_daily_gateway',
113+
'stat_monthly_gateway'
114+
];
115+
116+
if (empty($params) && in_array($method, $gateway_stats_methods)) {
117+
$params = [null, null, $gateway_stats_attribs];
123118
}
124119

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

135131
/**
@@ -170,19 +166,21 @@
170166
* for Kint we need to return the results in a slightly different manner
171167
* Rich render mode
172168
*/
173-
Kint::$display_called_from = false;
174-
Kint\Renderer\RichRenderer::$folder = false;
175-
$results['data'] = @d($data_array);
176-
} else if ($output_method === 'kint_plain') {
177-
/**
178-
* Plain render mode
179-
*/
180-
Kint::$display_called_from = false;
169+
Kint::$display_called_from = false;
181170
Kint\Renderer\RichRenderer::$folder = false;
182-
Kint\Renderer\TextRenderer::$decorations = false;
183-
$results['data'] = @s($data_array);
171+
$results['data'] = @d($data_array);
184172
} else {
185-
$results['data'] = $data_array;
173+
if ($output_method === 'kint_plain') {
174+
/**
175+
* Plain render mode
176+
*/
177+
Kint::$display_called_from = false;
178+
Kint\Renderer\RichRenderer::$folder = false;
179+
Kint\Renderer\TextRenderer::$decorations = false;
180+
$results['data'] = @s($data_array);
181+
} else {
182+
$results['data'] = $data_array;
183+
}
186184
}
187185
}
188186

@@ -213,6 +211,6 @@
213211
* output the results with correct JSON formatting
214212
*/
215213
header('Content-Type: application/json; charset=utf-8');
216-
echo (json_encode($results));
214+
echo(json_encode($results));
217215

218216
$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';

ajax/fetch_sites.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'state' => 'success',
3838
'message' => 'successfully fetched sites',
3939
'count' => 0,
40-
'data' => []
40+
'data' => [],
4141
];
4242

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

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

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

87-
if (empty($sites_array)) {
88-
$sites_array = [];
89-
}
90-
9188
/**
9289
* store the cookies from the controller for faster reconnects
9390
*/
@@ -98,15 +95,15 @@
9895
*/
9996
foreach ($sites_array as $site) {
10097
$results['data'][] = [
101-
'site_id' => isset($site->name) ? $site->name : $unknown_string,
102-
'site_full_name' => isset($site->desc) ? $site->desc : $unknown_string
98+
'site_id' => $site->name ?? $unknown_string,
99+
'site_full_name' => $site->desc ?? $unknown_string,
103100
];
104101
}
105102

106103
/**
107104
* sort the sites array by full name
108105
*/
109-
usort($results['data'], function($a, $b) {
106+
usort($results['data'], function ($a, $b) {
110107
if ($a['site_full_name'] == $b['site_full_name']) {
111108
return 0;
112109
}
@@ -139,6 +136,6 @@
139136
* output the results with correct json formatting
140137
*/
141138
header('Content-Type: application/json; charset=utf-8');
142-
echo (json_encode($results));
139+
echo(json_encode($results));
143140

144-
$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';
141+
$_SESSION['memory_used'] = round(memory_get_peak_usage(false) / 1024 / 1024, 2) . 'MB';

ajax/show_api_debug.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242

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

@@ -77,4 +78,4 @@
7778
}
7879
} else {
7980
echo 'ignore';
80-
}
81+
}

ajax/update_controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
$results = [
3333
'status' => 'success',
34-
'message' => 'controller updated'
34+
'message' => 'controller updated',
3535
];
3636

3737
/**
@@ -54,4 +54,4 @@
5454
* output the results with correct JSON formatting
5555
*/
5656
header('Content-Type: application/json; charset=utf-8');
57-
echo (json_encode($results));
57+
echo(json_encode($results));

ajax/update_theme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
$results = [
3333
'status' => 'success',
34-
'message' => 'theme updated'
34+
'message' => 'theme updated',
3535
];
3636

3737
/**
@@ -51,4 +51,4 @@
5151
* output the results with correct JSON formatting
5252
*/
5353
header('Content-Type: application/json; charset=utf-8');
54-
echo (json_encode($results));
54+
echo(json_encode($results));

common.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* This file is subject to the MIT license that is bundled with this package in the file LICENSE.md
77
*/
8-
define('TOOL_VERSION', '2.0.20');
8+
const TOOL_VERSION = '2.0.20';
99

1010
/**
1111
* gather some basic information for the About modal
@@ -38,7 +38,7 @@ function getClientVersion()
3838
{
3939
if (is_readable('composer.lock')) {
4040
$composer_lock = file_get_contents('composer.lock');
41-
$json_decoded = json_decode($composer_lock, true);
41+
$json_decoded = json_decode($composer_lock, true);
4242
if (isset($json_decoded['packages'])) {
4343
foreach ($json_decoded['packages'] as $package) {
4444
if ($package['name'] === 'art-of-wifi/unifi-api-client') {

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"require": {
33
"twig/twig": "1.*",
44
"kint-php/kint": "3.*",
5-
"art-of-wifi/unifi-api-client": "^1.1"
5+
"art-of-wifi/unifi-api-client": "^1.1",
6+
"php": ">=7.2.5",
7+
"ext-curl": "*",
8+
"ext-json": "*"
69
}
710
}

composer.lock

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
/**
1010
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
1111
*/
12+
13+
use Twig\Environment;
14+
use Twig\Loader\FilesystemLoader;
15+
1216
session_start();
1317

1418
/**
@@ -37,8 +41,8 @@
3741
/**
3842
* initialize the Twig loader early on in case we need to render the error page
3943
*/
40-
$loader = new \Twig\Loader\FilesystemLoader('templates');
41-
$twig = new \Twig\Environment($loader);
44+
$loader = new FilesystemLoader('templates');
45+
$twig = new Environment($loader);
4246

4347
/**
4448
* load the configuration file, if readable
@@ -116,9 +120,9 @@
116120
/**
117121
* if needed, we request the user to login
118122
*/
119-
if ($user_authentication && (empty($_SESSION['logged_in']) || $_SESSION['logged_in'] === false)) {
123+
if ($user_authentication && (empty($_SESSION['logged_in']))) {
120124
if (!empty($_SESSION['login_error_message'])) {
121-
$login_error = $_SESSION['login_error_message'];
125+
$login_error = $_SESSION['login_error_message'];
122126
$_SESSION['login_error_message'] = '';
123127
} else {
124128
$login_error = '';
@@ -152,4 +156,4 @@
152156
]);
153157
}
154158

155-
exit;
159+
exit;

0 commit comments

Comments
 (0)