Files
FamilyBook/app/bootstrap.php
T

69 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/**
* 应用引导:配置加载、会话、自动加载、全局辅助函数。
* 每个页面/入口只需 require 这一个文件。
*/
declare(strict_types=1);
// ---------------- 自动加载(避免「新增类忘了 require」这类事故)----------------
spl_autoload_register(function ($class) {
if (strpos($class, 'App\\') !== 0) {
return;
}
$rel = str_replace('\\', '/', substr($class, 4));
$file = __DIR__ . '/' . $rel . '.php';
if (is_file($file)) {
require $file;
}
});
define('FB_ROOT', dirname(__DIR__));
define('FB_APP', __DIR__);
define('FB_UPLOADS', FB_ROOT . '/uploads');
// ---------------- 配置 ----------------
$configFile = FB_ROOT . '/config.php';
if (!is_file($configFile)) {
http_response_code(500);
echo '<!doctype html><meta charset="utf-8"><title>家书 · 未配置</title>'
. '<div style="max-width:520px;margin:12vh auto;padding:28px 32px;'
. 'background:#fff;border:1px solid rgba(47,36,30,.1);border-radius:16px;'
. 'font:15px/1.8 -apple-system,\'PingFang SC\',\'Microsoft YaHei\',sans-serif;color:#2F241E">'
. '<h1 style="font-size:18px;margin:0 0 12px">还差一步:没有找到 config.php</h1>'
. '<p style="color:#7C6A5B;margin:0 0 16px">'
. '请复制同目录下的 <code>config.example.php</code> 为 <code>config.php</code>,'
. '填好数据库连接与 app_key 后再访问。</p>'
. '</div>';
exit;
}
$GLOBALS['config'] = require $configFile;
// ---------------- 错误报告(线上不显示错误,避免泄露路径与库信息)----------------
$debug = !empty($GLOBALS['config']['debug']);
error_reporting(E_ALL);
ini_set('display_errors', $debug ? '1' : '0');
ini_set('log_errors', '1');
// ---------------- 时区:日期是家书的核心数据,一开始就必须明确 ----------------
date_default_timezone_set($GLOBALS['config']['timezone'] ?? 'Asia/Shanghai');
// ---------------- 会话 ----------------
$isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');
session_name('fbsid');
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'secure' => $isHttps,
'httponly' => true,
'samesite' => 'Lax',
]);
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
require FB_APP . '/helpers.php';