<?php
// ============================================================
// 解密加载器 - 自动解密 so_theme 扩展的控制器
// ============================================================

$encryption_key = 'my_secret_key_12345';

function bolt_decrypt($data, $key) {
    $data = base64_decode($data);
    $iv_len = openssl_cipher_iv_length('AES-256-CBC');
    $iv = substr($data, 0, $iv_len);
    $encrypted = substr($data, $iv_len);
    return openssl_decrypt($encrypted, 'AES-256-CBC', $key, 0, $iv);
}

// 安全加载函数
function secure_load($file) {
    global $encryption_key;
    
    if (!file_exists($file)) {
        return false;
    }
    
    // 检查是否是 so_theme 扩展的控制器
    $is_so_theme_controller = (strpos($file, 'extension/so_theme') !== false && 
                               (strpos($file, '/controller/') !== false || 
                                strpos($file, '\\controller\\') !== false));
    
    if ($is_so_theme_controller) {
        $content = file_get_contents($file);
        if ($content !== false) {
            try {
                $decrypted = bolt_decrypt($content, $encryption_key);
                if ($decrypted !== false && 
                    (strpos($decrypted, '<?php') !== false || 
                     strpos($decrypted, 'class ') !== false ||
                     strpos($decrypted, 'function ') !== false)) {
                    return eval('?>' . $decrypted);
                }
            } catch (Exception $e) {
                error_log("Decrypt failed for: $file - " . $e->getMessage());
            }
        }
        // 如果解密失败，直接包含原文件
        return require_once($file);
    }
    
    // 非控制器文件直接包含
    return require_once($file);
}

// ============================================================
// OpenCart 启动代码
// ============================================================

// Version
define('VERSION', '4.1.0.0');

// Configuration
if (is_file('config.php')) {
    require_once('config.php');
}

// Install
if (!defined('DIR_APPLICATION')) {
    header('Location: install/index.php');
    exit();
}

// Startup
if (defined('DIR_SYSTEM')) {
    $startup_file = DIR_SYSTEM . 'startup.php';
    if (file_exists($startup_file)) {
        secure_load($startup_file);
    }
}

// Framework
if (defined('DIR_SYSTEM')) {
    $framework_file = DIR_SYSTEM . 'framework.php';
    if (file_exists($framework_file)) {
        secure_load($framework_file);
    }
}