Added (Check Duplicates Before Adding New User)
This commit is contained in:
		
							parent
							
								
									10c9c24742
								
							
						
					
					
						commit
						6b441c7be0
					
				
							
								
								
									
										127
									
								
								index.php
									
									
									
									
									
								
							
							
						
						
									
										127
									
								
								index.php
									
									
									
									
									
								
							@ -1,7 +1,24 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
 | 
					session_start();
 | 
				
			||||||
 | 
					// Define default config values
 | 
				
			||||||
 | 
					$defaultConfig = [
 | 
				
			||||||
 | 
					    'chap_secrets_path' => '/etc/ppp/chap-secrets',
 | 
				
			||||||
 | 
					    'admin_username' => 'admin',
 | 
				
			||||||
 | 
					    'admin_password' => password_hash('change@me', PASSWORD_DEFAULT),
 | 
				
			||||||
 | 
					];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$file = "/etc/ppp/chap-secrets";
 | 
					// Create config.php if it doesn't exist
 | 
				
			||||||
//$file = "./chap-secrets";
 | 
					if (!file_exists('config.php')) {
 | 
				
			||||||
 | 
					    file_put_contents('config.php', '<?php return ' . var_export($defaultConfig, true) . ';');
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
 | 
				
			||||||
 | 
					    header('Location: login.php');
 | 
				
			||||||
 | 
					    exit();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$config = include 'config.php';
 | 
				
			||||||
 | 
					$file = $config['chap_secrets_path'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Read the file into an array
 | 
					// Read the file into an array
 | 
				
			||||||
function readUsers($file) {
 | 
					function readUsers($file) {
 | 
				
			||||||
@ -88,6 +105,14 @@ function getNextIp($users) {
 | 
				
			|||||||
$users = readUsers($file);
 | 
					$users = readUsers($file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
					if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
				
			||||||
 | 
					    // Handle logout
 | 
				
			||||||
 | 
					    if (isset($_POST['logout'])) {
 | 
				
			||||||
 | 
					        session_destroy();
 | 
				
			||||||
 | 
					        header('Location: login.php');
 | 
				
			||||||
 | 
					        exit();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Handle add user
 | 
				
			||||||
    if (isset($_POST['add'])) {
 | 
					    if (isset($_POST['add'])) {
 | 
				
			||||||
        $client = $_POST['client'];
 | 
					        $client = $_POST['client'];
 | 
				
			||||||
        $ip = $_POST['ip'] ?? getNextIp($users);
 | 
					        $ip = $_POST['ip'] ?? getNextIp($users);
 | 
				
			||||||
@ -161,13 +186,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $users = array_merge($users, $newUsers);
 | 
					        $users = array_merge($users, $newUsers);
 | 
				
			||||||
        writeUsers($file, $users);
 | 
					        writeUsers($file, $users);
 | 
				
			||||||
 | 
					        echo json_encode(['success' => true]);
 | 
				
			||||||
 | 
					        exit();
 | 
				
			||||||
 | 
					    } elseif (isset($_POST['changePassword'])) {
 | 
				
			||||||
 | 
					        $newPassword = password_hash($_POST['newPassword'], PASSWORD_DEFAULT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Read the config file
 | 
				
			||||||
 | 
					        $config = include 'config.php';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Update the password in the config file
 | 
				
			||||||
 | 
					        $config['admin_password'] = $newPassword;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Write the updated config back to the file
 | 
				
			||||||
 | 
					        file_put_contents('config.php', '<?php return ' . var_export($config, true) . ';');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        echo json_encode(['success' => true]);
 | 
					        echo json_encode(['success' => true]);
 | 
				
			||||||
        exit();
 | 
					        exit();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
?>
 | 
					?>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
<!DOCTYPE html>
 | 
					<!DOCTYPE html>
 | 
				
			||||||
<html>
 | 
					<html>
 | 
				
			||||||
<head>
 | 
					<head>
 | 
				
			||||||
@ -208,7 +246,29 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
				
			|||||||
    </style>
 | 
					    </style>
 | 
				
			||||||
</head>
 | 
					</head>
 | 
				
			||||||
<body>
 | 
					<body>
 | 
				
			||||||
 | 
					<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
 | 
				
			||||||
 | 
					    <div class="container-fluid">
 | 
				
			||||||
 | 
					        <a class="navbar-brand" href="#">L2TP User Manager</a>
 | 
				
			||||||
 | 
					        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
 | 
				
			||||||
 | 
					            <span class="navbar-toggler-icon"></span>
 | 
				
			||||||
 | 
					        </button>
 | 
				
			||||||
 | 
					        <div class="collapse navbar-collapse" id="navbarNav">
 | 
				
			||||||
 | 
					            <ul class="navbar-nav ms-auto">
 | 
				
			||||||
 | 
					                <li class="nav-item">
 | 
				
			||||||
 | 
					                    <button class="btn btn-primary nav-link" data-bs-toggle="modal" data-bs-target="#changePasswordModal">Change Password</button>
 | 
				
			||||||
 | 
					                </li>
 | 
				
			||||||
 | 
					                <li class="nav-item">
 | 
				
			||||||
 | 
					                    <form method="post" action="index.php" class="d-inline">
 | 
				
			||||||
 | 
					                        <button type="submit" name="logout" class="btn btn-danger nav-link">Logout</button>
 | 
				
			||||||
 | 
					                    </form>
 | 
				
			||||||
 | 
					                </li>
 | 
				
			||||||
 | 
					            </ul>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					</nav>
 | 
				
			||||||
<div class="container mt-5">
 | 
					<div class="container mt-5">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <h2 class="mb-4 text-center">Manage L2TP Users</h2>
 | 
					    <h2 class="mb-4 text-center">Manage L2TP Users</h2>
 | 
				
			||||||
     <div class="table-responsive">
 | 
					     <div class="table-responsive">
 | 
				
			||||||
        <table class="table table-hover table-bordered text-center">
 | 
					        <table class="table table-hover table-bordered text-center">
 | 
				
			||||||
@ -354,6 +414,32 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
				
			|||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<!-- Change Password Modal -->
 | 
				
			||||||
 | 
					<div class="modal fade" id="changePasswordModal" tabindex="-1" aria-labelledby="changePasswordModalLabel" aria-hidden="true">
 | 
				
			||||||
 | 
					    <div class="modal-dialog modal-dialog-centered">
 | 
				
			||||||
 | 
					        <div class="modal-content">
 | 
				
			||||||
 | 
					            <div class="modal-header">
 | 
				
			||||||
 | 
					                <h5 class="modal-title" id="changePasswordModalLabel">Change Password</h5>
 | 
				
			||||||
 | 
					                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					            <div class="modal-body">
 | 
				
			||||||
 | 
					                <form id="changePasswordForm">
 | 
				
			||||||
 | 
					                    <div class="mb-3">
 | 
				
			||||||
 | 
					                        <label for="newPassword" class="form-label">New Password</label>
 | 
				
			||||||
 | 
					                        <input type="password" class="form-control" id="newPassword" name="newPassword" required placeholder="Enter new password">
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                    <div class="mb-3">
 | 
				
			||||||
 | 
					                        <label for="confirmPassword" class="form-label">Confirm Password</label>
 | 
				
			||||||
 | 
					                        <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required placeholder="Confirm new password">
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                    <button type="submit" class="btn btn-primary w-100">Change Password</button>
 | 
				
			||||||
 | 
					                </form>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
    document.getElementById('addUserForm').addEventListener('submit', function(event) {
 | 
					    document.getElementById('addUserForm').addEventListener('submit', function(event) {
 | 
				
			||||||
        event.preventDefault();
 | 
					        event.preventDefault();
 | 
				
			||||||
@ -478,6 +564,41 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
				
			|||||||
    function confirmDelete(index) {
 | 
					    function confirmDelete(index) {
 | 
				
			||||||
        document.getElementById('deleteIndex').value = index;
 | 
					        document.getElementById('deleteIndex').value = index;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Handle password change
 | 
				
			||||||
 | 
					    document.getElementById('changePasswordForm').addEventListener('submit', function(event) {
 | 
				
			||||||
 | 
					        event.preventDefault();
 | 
				
			||||||
 | 
					        const newPassword = document.getElementById('newPassword').value;
 | 
				
			||||||
 | 
					        const confirmPassword = document.getElementById('confirmPassword').value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (newPassword !== confirmPassword) {
 | 
				
			||||||
 | 
					            document.getElementById('errorMessage').textContent = 'Passwords do not match';
 | 
				
			||||||
 | 
					            document.getElementById('errorModal').classList.add('show');
 | 
				
			||||||
 | 
					            document.getElementById('errorModal').style.display = 'block';
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const formData = new FormData();
 | 
				
			||||||
 | 
					        formData.append('changePassword', '1');
 | 
				
			||||||
 | 
					        formData.append('newPassword', newPassword);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fetch('', {
 | 
				
			||||||
 | 
					            method: 'POST',
 | 
				
			||||||
 | 
					            body: formData
 | 
				
			||||||
 | 
					        }).then(response => response.json())
 | 
				
			||||||
 | 
					            .then(data => {
 | 
				
			||||||
 | 
					                if (data.success) {
 | 
				
			||||||
 | 
					                    alert('Password changed successfully');
 | 
				
			||||||
 | 
					                    document.getElementById('newPassword').value = '';
 | 
				
			||||||
 | 
					                    document.getElementById('confirmPassword').value = '';
 | 
				
			||||||
 | 
					                    document.querySelector('#changePasswordModal .btn-close').click();
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    document.getElementById('errorMessage').textContent = 'Failed to change password';
 | 
				
			||||||
 | 
					                    document.getElementById('errorModal').classList.add('show');
 | 
				
			||||||
 | 
					                    document.getElementById('errorModal').style.display = 'block';
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
 | 
					<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										47
									
								
								login.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								login.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					session_start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$config = include 'config.php';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
				
			||||||
 | 
					    $username = $_POST['username'];
 | 
				
			||||||
 | 
					    $password = $_POST['password'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ($username === $config['admin_username'] && password_verify($password, $config['admin_password'])) {
 | 
				
			||||||
 | 
					        $_SESSION['loggedin'] = true;
 | 
				
			||||||
 | 
					        header('Location: index.php');
 | 
				
			||||||
 | 
					        exit();
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        $error = 'Invalid username or password';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					?>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<!DOCTYPE html>
 | 
				
			||||||
 | 
					<html>
 | 
				
			||||||
 | 
					<head>
 | 
				
			||||||
 | 
					    <title>Login</title>
 | 
				
			||||||
 | 
					    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
 | 
				
			||||||
 | 
					</head>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					<div class="container mt-5">
 | 
				
			||||||
 | 
					    <h2 class="mb-4 text-center">Login</h2>
 | 
				
			||||||
 | 
					    <?php if (isset($error)): ?>
 | 
				
			||||||
 | 
					        <div class="alert alert-danger"><?php echo $error; ?></div>
 | 
				
			||||||
 | 
					    <?php endif; ?>
 | 
				
			||||||
 | 
					    <form method="post" action="login.php">
 | 
				
			||||||
 | 
					        <div class="mb-3">
 | 
				
			||||||
 | 
					            <label for="username" class="form-label">Username</label>
 | 
				
			||||||
 | 
					            <input type="text" class="form-control" id="username" name="username" required>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					        <div class="mb-3">
 | 
				
			||||||
 | 
					            <label for="password" class="form-label">Password</label>
 | 
				
			||||||
 | 
					            <input type="password" class="form-control" id="password" name="password" required>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					        <button type="submit" class="btn btn-primary w-100">Login</button>
 | 
				
			||||||
 | 
					    </form>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user