<?php
/**
 * 后台管理公共函数
 */
require_once '../includes/functions.php';
// 保存配置函数
function save_config($config) {
    $config_content = "<?php\n// 网站基础配置\n\$config = " . var_export($config, true) . ";\n\n// 全局常量定义\ndefine('BASE_PATH', dirname(__FILE__));\ndefine('CACHE_PATH', BASE_PATH . '/cache');\ndefine('TEMPLATE_PATH', BASE_PATH . '/templates/' . \$config['site']['theme']);\ndefine('INCLUDE_PATH', BASE_PATH . '/includes');\ndefine('UPLOAD_PATH', BASE_PATH . '/uploads');\ndefine('ADMIN_PATH', BASE_PATH . '/admin');\n?>";
    
    $result = file_put_contents('../config.php', $config_content);
    
    if ($result === false) {
        // 记录错误日志
        error_log("保存配置文件失败: " . date('Y-m-d H:i:s'));
        return false;
    }
    
    return true;
}
// 检查文件权限
function check_config_permission() {
    $config_file = '../config.php';
    if (!is_writable($config_file)) {
        return "配置文件不可写，请检查文件权限";
    }
    return true;
}

// 生成随机验证码
function generate_captcha_code($length = 4) {
    $chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
    $code = '';
    for ($i = 0; $i < $length; $i++) {
        $code .= $chars[rand(0, strlen($chars) - 1)];
    }
    return $code;
}

// 验证配置数据
function validate_settings($data, $type) {
    $errors = [];
    
    switch ($type) {
        case 'basic':
            if (empty($data['site_name'])) {
                $errors[] = '网站名称不能为空';
            }
            if (empty($data['site_theme'])) {
                $errors[] = '网站主题不能为空';
            }
            break;
            
        case 'display':
            if (!is_numeric($data['index_posts_count']) || $data['index_posts_count'] < 1) {
                $errors[] = '首页文章显示数量必须大于0';
            }
            if (!is_numeric($data['announcements_count']) || $data['announcements_count'] < 0) {
                $errors[] = '公告显示数量不能为负数';
            }
            break;
            
        case 'api':
            if (empty($data['api_url'])) {
                $errors[] = 'API地址不能为空';
            }
            if (empty($data['api_skid'])) {
                $errors[] = 'SKID不能为空';
            }
            if (empty($data['api_skey'])) {
                $errors[] = 'SKEY不能为空';
            }
            break;
    }
    
    return $errors;
}

// 获取默认配置值
function get_default_value($key, $type) {
    $defaults = [
        'basic' => [
            'site_name' => '我的网站',
            'site_description' => '',
            'site_keywords' => '',
            'site_theme' => 'default',
            'site_weburl' => '',
            'category_show_count' => 10
        ],
        'display' => [
            'index_posts_count' => 10,
            'announcements_count' => 5,
            'category_posts_count' => 10,
            'hot_posts_count' => 5,
            'hot_posts_days' => 30,
            'random_posts_count' => 5,
            'pagination_pages_count' => 10
        ],
        'api' => [
            'apiurl' => '',
            'skid' => '',
            'skey' => '',
            'stype' => 'md5'
        ]
    ];
    
    return $defaults[$type][$key] ?? '';
}
?>