Files
pk10/App/Core/WebBaseController.php
T

143 lines
4.4 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Core;
class WebBaseController extends BaseController {
protected function render($viewPath, $data = []) {
// 如果是绝对路径,直接使用
if (strpos($viewPath, '/') === 0 || preg_match('/^[a-zA-Z]:\\\\/', $viewPath)) {
$fullPath = $viewPath;
} else {
// 相对路径,按默认视图目录拼接
$viewsDir = __DIR__ . '/../views/';
$fullPath = $viewsDir . $viewPath;
}
if (!file_exists($fullPath)) {
throw new Exception("视图文件不存在: $fullPath");
}
extract($data);
if (
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
) {
include $fullPath;
} else {
ob_start();
include $fullPath;
$Content = ob_get_clean();
include __DIR__ . '/../views/Web/index.php'; // 主后台模板
}
}
/**
* 检查前台用户登录状态
* 如果未登录,重定向到登录页面
*/
protected function checkWebLogin() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$timeout = isset($_SESSION['web_remember']) && $_SESSION['web_remember'] ? 604800 : 7200; // 记住我:7天,否则2小时
// 检查是否有用户ID
if (!isset($_SESSION['web_user_id'])) {
$this->redirectToLogin();
return;
}
// 提取 IP 前三段(IPv4
function get_ip_prefix($ip, $segments = 3) {
if (empty($ip)) return '';
$parts = explode('.', $ip);
if (count($parts) < 4) return $ip; // IPv6 或其他格式,直接返回
return implode('.', array_slice($parts, 0, $segments));
}
// 宽松 IP 检查(只比对前三段,例如 192.168.1.xxx
$currentIp = $_SERVER['REMOTE_ADDR'] ?? '';
$sessionIp = $_SESSION['web_ip'] ?? '';
if (!empty($currentIp) && !empty($sessionIp)) {
$current_ip_prefix = get_ip_prefix($currentIp, 3);
$session_ip_prefix = get_ip_prefix($sessionIp, 3);
if ($current_ip_prefix !== $session_ip_prefix) {
session_destroy();
$this->redirectToLogin();
return;
}
}
// User Agent 检查
$currentUa = $_SERVER['HTTP_USER_AGENT'] ?? '';
$sessionUa = $_SESSION['web_ua'] ?? '';
if (!empty($currentUa) && !empty($sessionUa) && $currentUa !== $sessionUa) {
session_destroy();
$this->redirectToLogin();
return;
}
// 会话超时检查
$lastActivity = $_SESSION['web_last_activity'] ?? 0;
if (time() - $lastActivity > $timeout) {
session_destroy();
$this->redirectToLogin();
return;
}
// 更新最后活动时间
$_SESSION['web_last_activity'] = time();
}
/**
* 重定向到登录页面
*/
private function redirectToLogin() {
$currentUrl = $_SERVER['REQUEST_URI'] ?? '/';
$loginUrl = '/login?redirect=' . urlencode($currentUrl);
if (
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
) {
// AJAX 请求,返回 JSON
header('Content-Type: application/json');
echo json_encode([
'success' => false,
'redirect' => $loginUrl,
'message' => 'Phiên đăng nhập đã hết hạn, vui lòng đăng nhập lại!'
]);
} else {
// 普通请求,重定向
header('Location: ' . $loginUrl);
}
exit;
}
/**
* 获取当前登录用户ID
*/
protected function getCurrentUserId() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
return $_SESSION['web_user_id'] ?? null;
}
/**
* 获取当前登录用户名
*/
protected function getCurrentUsername() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
return $_SESSION['web_username'] ?? null;
}
}