Skip to content

Commit 79fe7f4

Browse files
committed
add enable/disable login, add vnstat
1 parent 5ac761f commit 79fe7f4

File tree

13 files changed

+371
-9
lines changed

13 files changed

+371
-9
lines changed

files/www/auth/auth_functions.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
session_start();
3+
4+
// Function to check if the user is logged in
5+
function checkUserLogin() {
6+
if (!isset($_SESSION['user_id'])) {
7+
header('Location: /auth/login.php'); // Redirect to login page
8+
exit;
9+
}
10+
}
11+
12+
// Function to check if login is enabled
13+
function isLoginEnabled() {
14+
$config_file = '/data/adb/php7/files/www/auth/config.json';
15+
if (file_exists($config_file)) {
16+
$config = json_decode(file_get_contents($config_file), true);
17+
return isset($config['LOGIN_ENABLED']) && $config['LOGIN_ENABLED'];
18+
}
19+
return false;
20+
}
21+
22+
// Set a flag in session or query parameter if login is disabled
23+
if (!isLoginEnabled()) {
24+
$_SESSION['login_disabled'] = true; // Set a session flag or
25+
// header("Location: /?login_disabled=1"); // Or use a query parameter
26+
// exit;
27+
}
28+
?>

files/www/auth/change_password.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
<?php
22
session_start();
33

4-
// Redirect to login if not authenticated
5-
if (!isset($_SESSION['user_id'])) {
6-
$_SESSION['redirect_url'] = $_SERVER['REQUEST_URI'];
7-
header('Location: login.php');
8-
exit;
4+
require_once '/data/adb/php7/files/www/auth/auth_functions.php';
5+
6+
// If login is disabled, set the current page but do not redirect to login
7+
if (isset($_SESSION['login_disabled']) && $_SESSION['login_disabled'] === true) {
8+
// Login is disabled, handle accordingly
9+
// You can show a message or just let the user stay on the page
10+
//echo "<p>Login is currently disabled.</p>";
11+
} else {
12+
// Proceed to check if the user is logged in
13+
checkUserLogin();
914
}
1015

16+
17+
// Redirect to login if not authenticated
18+
//if (!isset($_SESSION['user_id'])) {
19+
// $_SESSION['redirect_url'] = $_SERVER['REQUEST_URI'];
20+
// header('Location: login.php');
21+
// exit;
22+
//}
23+
1124
// Include credentials file
1225
$credentials = include 'credentials.php';
1326
$stored_username = $credentials['username'];

files/www/auth/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"LOGIN_ENABLED": false
3+
}

files/www/auth/config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
// Define the path to the config file
3+
$config_file = '/data/adb/php7/files/www/auth/config.json';
4+
5+
6+
// Check if the config file exists
7+
if (!file_exists($config_file)) {
8+
die('Error: Configuration file not found.');
9+
}
10+
11+
// Load the configuration from the JSON file
12+
$config = json_decode(file_get_contents($config_file), true);
13+
14+
// Define the LOGIN_ENABLED constant based on the JSON file
15+
define('LOGIN_ENABLED', $config['LOGIN_ENABLED']);

files/www/auth/login.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
$stored_username = $credentials['username'];
77
$stored_hashed_password = $credentials['hashed_password'];
88

9+
$config_file = '/data/adb/php7/files/www/auth/config.json';
10+
11+
if (!file_exists($config_file)) {
12+
die('Error: Configuration file not found.');
13+
}
14+
15+
$config = json_decode(file_get_contents($config_file), true);
16+
17+
// Define the LOGIN_ENABLED constant based on the JSON file
18+
define('LOGIN_ENABLED', $config['LOGIN_ENABLED']);
19+
20+
// Check if login is disabled
21+
$login_disabled = !LOGIN_ENABLED;
22+
23+
// If login is disabled, set a session flag or a message variable
24+
if ($login_disabled) {
25+
$_SESSION['login_disabled'] = true;
26+
}
27+
928
// Check if the user is already logged in and redirect accordingly
1029
if (isset($_SESSION['user_id'])) {
1130
$redirect_to = isset($_SESSION['redirect_to']) ? $_SESSION['redirect_to'] : '/';
@@ -24,14 +43,15 @@
2443
$_SESSION['username'] = $username;
2544
$redirect_to = isset($_SESSION['redirect_to']) ? $_SESSION['redirect_to'] : '/';
2645
unset($_SESSION['redirect_to']);
27-
header("Location: $redirect_to"); // Redirect to the last visited page or index.php
46+
header("Location: $redirect_to");
2847
exit;
2948
} else {
3049
$error = 'Invalid username or password.';
3150
}
3251
}
3352
?>
3453

54+
3555
<!DOCTYPE html>
3656
<html>
3757
<head>

files/www/auth/manage_login.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
session_start();
3+
4+
require_once '/data/adb/php7/files/www/auth/auth_functions.php';
5+
6+
// If login is disabled, set the current page but do not redirect to login
7+
if (isset($_SESSION['login_disabled']) && $_SESSION['login_disabled'] === true) {
8+
// Login is disabled, handle accordingly
9+
// You can show a message or just let the user stay on the page
10+
//echo "<p>Login is currently disabled.</p>";
11+
} else {
12+
// Proceed to check if the user is logged in
13+
checkUserLogin();
14+
}
15+
16+
// Load the current configuration
17+
$config = json_decode(file_get_contents('config.json'), true);
18+
19+
// Handle form submission to update the configuration
20+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
21+
$config['LOGIN_ENABLED'] = isset($_POST['login_enabled']);
22+
23+
// Save the updated configuration back to the JSON file
24+
file_put_contents('config.json', json_encode($config, JSON_PRETTY_PRINT));
25+
}
26+
27+
?>
28+
29+
<!DOCTYPE html>
30+
<html lang="en">
31+
<head>
32+
<meta charset="UTF-8">
33+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
34+
<title>Enable/disable login</title>
35+
<!-- Include Materialize CSS -->
36+
<link href="css/materialize.min.css" rel="stylesheet">
37+
<!-- Custom Dark Mode CSS -->
38+
<style>
39+
body {
40+
background-color: #121212;
41+
color: #ffffff;
42+
}
43+
.container {
44+
margin-top: 50px;
45+
}
46+
h3 {
47+
color: #ffffff;
48+
}
49+
.switch label {
50+
color: #ffffff;
51+
}
52+
.btn {
53+
background-color: #26a69a;
54+
}
55+
.btn:hover {
56+
background-color: #2bbbad;
57+
}
58+
.switch label .lever {
59+
background-color: #26a69a;
60+
}
61+
.switch label input[type=checkbox]:checked+.lever {
62+
background-color: #64ffda;
63+
}
64+
</style>
65+
</head>
66+
<body>
67+
<div class="container">
68+
<h3>Enable / Disable Login</h3>
69+
<form method="POST">
70+
<div class="switch">
71+
<label>
72+
Login Disabled
73+
<input type="checkbox" name="login_enabled" <?php echo $config['LOGIN_ENABLED'] ? 'checked' : ''; ?>>
74+
<span class="lever"></span>
75+
Login Enabled
76+
</label>
77+
</div>
78+
<br>
79+
<button type="submit" class="btn">Save Changes</button>
80+
</form>
81+
</div>
82+
83+
<!-- Include Materialize JS -->
84+
<script src="js/materialize.min.js"></script>
85+
</body>
86+
</html>

files/www/blackbox.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
<?php
2+
3+
require_once '/data/adb/php7/files/www/auth/auth_functions.php';
4+
5+
// If login is disabled, set the current page but do not redirect to login
6+
if (isset($_SESSION['login_disabled']) && $_SESSION['login_disabled'] === true) {
7+
// Login is disabled, handle accordingly
8+
// You can show a message or just let the user stay on the page
9+
//echo "<p>Login is currently disabled.</p>";
10+
} else {
11+
// Proceed to check if the user is logged in
12+
checkUserLogin();
13+
}
14+
215
$clashlogs = "/data/adb/box/run/runs.log";
316
$pid = "/data/adb/box/run/box.pid";
417
$moduledir = "../modules/box_for_magisk";

files/www/index.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
'cookie_lifetime' => 31536000, // 1 year
88
]);
99

10-
if (!isset($_SESSION['user_id'])) {
10+
// Include the config file
11+
include 'auth/config.php';
12+
13+
// Check if login is enabled and if the user is not logged in
14+
if (LOGIN_ENABLED && !isset($_SESSION['user_id'])) {
1115
header('Location: /auth/login.php');
1216
exit;
1317
}
@@ -204,6 +208,11 @@ class="navbar-brand"
204208
<span class="sub-item">Airplane Pilot</span>
205209
</a>
206210
</li>
211+
<li>
212+
<a href="#" onclick="loadIframe('/tools/vnstat.php')">
213+
<span class="sub-item">Vnstat Bandwith</span>
214+
</a>
215+
</li>
207216
</ul>
208217
</div>
209218
</li>
@@ -236,6 +245,11 @@ class="navbar-brand"
236245
<span class="sub-item">Reset Password</span>
237246
</a>
238247
</li>
248+
<li>
249+
<a href="#" onclick="loadIframe('/auth/manage_login.php')">
250+
<span class="sub-item">enable/disable login</span>
251+
</a>
252+
</li>
239253
<!--<li>
240254
<a href="#">
241255
<span class="sub-item">Icon Menu</span>

files/www/tiny/index.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
<?php
2+
3+
require_once '/data/adb/php7/files/www/auth/auth_functions.php';
4+
5+
// If login is disabled, set the current page but do not redirect to login
6+
if (isset($_SESSION['login_disabled']) && $_SESSION['login_disabled'] === true) {
7+
// Login is disabled, handle accordingly
8+
// You can show a message or just let the user stay on the page
9+
//echo "<p>Login is currently disabled.</p>";
10+
} else {
11+
// Proceed to check if the user is logged in
12+
checkUserLogin();
13+
}
14+
215
//Default Configuration
316
$CONFIG = '{"lang":"en","error_reporting":false,"show_hidden":true,"hide_Cols":false,"theme":"dark"}';
417

files/www/tools/executed.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
<?php
2+
require_once '/data/adb/php7/files/www/auth/auth_functions.php';
3+
4+
// If login is disabled, set the current page but do not redirect to login
5+
if (isset($_SESSION['login_disabled']) && $_SESSION['login_disabled'] === true) {
6+
// Login is disabled, handle accordingly
7+
// You can show a message or just let the user stay on the page
8+
//echo "<p>Login is currently disabled.</p>";
9+
} else {
10+
// Proceed to check if the user is logged in
11+
checkUserLogin();
12+
}
13+
214
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
315
if (isset($_POST['action'])) {
416
$action = $_POST['action'];

files/www/tools/reboot.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
<?php
2+
3+
require_once '/data/adb/php7/files/www/auth/auth_functions.php';
4+
5+
// If login is disabled, set the current page but do not redirect to login
6+
if (isset($_SESSION['login_disabled']) && $_SESSION['login_disabled'] === true) {
7+
// Login is disabled, handle accordingly
8+
// You can show a message or just let the user stay on the page
9+
//echo "<p>Login is currently disabled.</p>";
10+
} else {
11+
// Proceed to check if the user is logged in
12+
checkUserLogin();
13+
}
14+
215
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
316
if (isset($_POST['action'])) {
417
$action = $_POST['action'];

0 commit comments

Comments
 (0)