<?php
/**
 * Name: Nxz官网系统
 * URI: https://www.nxzlm.com/
 * Description: 逆行者官网系统源码，简单好用带后台
 * Version: 1.0.0
 * Author: 逆行者联盟
 */

session_start();
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

$configFile = __DIR__ . '/config.php';
if (!file_exists($configFile)) {
    // 未安装，跳转到安装目录
    header("Location: /install/index.php");
    exit;
}

$dbConfig = require $configFile;

try {
    $dsn = "mysql:host={$dbConfig['host']};dbname={$dbConfig['name']};charset=utf8mb4";
    $pdo = new PDO($dsn, $dbConfig['user'], $dbConfig['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("数据库连接失败，请检查配置文件。");
}

// 获取网站配置
function get_config($key = null) {
    global $pdo;
    static $config = null;
    
    if ($config === null) {
        $stmt = $pdo->query("SELECT * FROM nxz_config");
        $rows = $stmt->fetchAll();
        $config = [];
        foreach ($rows as $row) {
            $config[$row['k']] = $row['v'];
        }
    }
    
    if ($key === null) {
        return $config;
    }
    return isset($config[$key]) ? $config[$key] : '';
}

// 检查后台登录状态
function check_admin_login() {
    if (!isset($_SESSION['admin_id'])) {
        header("Location: /admin/login.php");
        exit;
    }
}

// JSON返回助手
function json_success($msg = '成功', $data = []) {
    echo json_encode(['code' => 0, 'msg' => $msg, 'data' => $data]);
    exit;
}

function json_error($msg = '失败', $code = 1) {
    echo json_encode(['code' => $code, 'msg' => $msg]);
    exit;
}

// 统一的版权信息
$nxz_copyright = [
    'Name' => 'Nxz官网系统',
    'URI' => 'https://www.nxzlm.com/',
    'Description' => '逆行者官网系统源码，简单好用带后台',
    'Version' => '1.0.0',
    'Author' => '逆行者联盟',
    'Ps' => '需要多的功能欢迎联系我们扩展定制喔'
];
?>