92 lines
4.1 KiB
PHP
92 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* Session-based login. POST: validate against .htpasswd and set session. GET: show form.
|
|
* Deploy to /www/wwwroot/soft_download/login.php
|
|
* Requires: PHP with session support, htpasswd in PATH for validation.
|
|
*/
|
|
session_start();
|
|
|
|
$HTPASSWD_FILE = '/usr/local/nginx/conf/.htpasswd';
|
|
$HTPASSWD_CMD = '/usr/bin/htpasswd'; // or 'htpasswd' if in PATH
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$user = isset($_POST['username']) ? trim((string) $_POST['username']) : '';
|
|
$pass = isset($_POST['password']) ? (string) $_POST['password'] : '';
|
|
$return_path = isset($_POST['return']) ? trim((string) $_POST['return']) : '/';
|
|
if (!strlen($return_path) || $return_path[0] !== '/') {
|
|
$return_path = '/';
|
|
}
|
|
|
|
$error = '';
|
|
if ($user !== '' && $pass !== '' && is_readable($HTPASSWD_FILE)) {
|
|
$user_esc = escapeshellarg($user);
|
|
$pass_esc = escapeshellarg($pass);
|
|
$file_esc = escapeshellarg($HTPASSWD_FILE);
|
|
$cmd = sprintf('%s -vb %s %s %s 2>/dev/null', $HTPASSWD_CMD, $file_esc, $user_esc, $pass_esc);
|
|
exec($cmd, $out, $code);
|
|
if ($code === 0) {
|
|
$_SESSION['soft_download_user'] = $user;
|
|
header('Location: ' . $return_path);
|
|
exit;
|
|
}
|
|
$error = '用户名或密码错误';
|
|
} else {
|
|
if ($user === '' && $pass === '') {
|
|
$error = '请输入用户名和密码';
|
|
} else {
|
|
$error = '用户名或密码错误';
|
|
}
|
|
}
|
|
} else {
|
|
$error = '';
|
|
$return_path = isset($_GET['return']) ? trim((string) $_GET['return']) : '/';
|
|
if (!strlen($return_path) || $return_path[0] !== '/') {
|
|
$return_path = '/';
|
|
}
|
|
}
|
|
|
|
$return_esc = htmlspecialchars($return_path, ENT_QUOTES, 'UTF-8');
|
|
$error_esc = htmlspecialchars($error ?? '', ENT_QUOTES, 'UTF-8');
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="zh">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>登录 - 软件下载页面</title>
|
|
<style>
|
|
* { box-sizing: border-box; }
|
|
body { font-size: 18px; line-height: 1.6; font-family: sans-serif; margin: 0; padding: 2em; min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #f5f5f5; }
|
|
.login-box { background: #fff; padding: 2em 2.5em; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); max-width: 380px; width: 100%; }
|
|
h1 { font-size: 22px; margin: 0 0 1.2em 0; color: #333; border-bottom: 2px solid #ccc; padding-bottom: 0.4em; }
|
|
.form-group { margin-bottom: 1.2em; }
|
|
.form-group label { display: block; margin-bottom: 0.3em; color: #555; font-size: 16px; }
|
|
.form-group input { width: 100%; padding: 0.5em 0.8em; font-size: 16px; border: 1px solid #ccc; border-radius: 4px; }
|
|
.form-group input:focus { outline: none; border-color: #0066cc; }
|
|
.login-btn { width: 100%; padding: 0.6em 1em; font-size: 18px; background: #0066cc; color: #fff; border: none; border-radius: 4px; cursor: pointer; }
|
|
.login-btn:hover { background: #0052a3; }
|
|
.error-msg { color: #c00; font-size: 14px; margin-top: 0.5em; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="login-box">
|
|
<h1>登录</h1>
|
|
<form method="post" action="login.php">
|
|
<input type="hidden" name="return" value="<?php echo $return_esc; ?>">
|
|
<div class="form-group">
|
|
<label for="username">用户名</label>
|
|
<input type="text" id="username" name="username" required autocomplete="username" value="<?php echo htmlspecialchars($_POST['username'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">密码</label>
|
|
<input type="password" id="password" name="password" required autocomplete="current-password">
|
|
</div>
|
|
<?php if ($error_esc !== ''): ?>
|
|
<div class="error-msg"><?php echo $error_esc; ?></div>
|
|
<?php endif; ?>
|
|
<button type="submit" class="login-btn">登录</button>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|