<?php
// ============================================================
// 解密加载器 - 使用唯一函数名
// ============================================================

// 检查函数是否已存在，避免重复声明
if (!function_exists('so_theme_decrypt')) {
    function so_theme_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);
    }
}

if (!function_exists('so_theme_secure_load')) {
    function so_theme_secure_load($file) {
        global $encryption_key;
        
        if (!file_exists($file)) {
            return false;
        }
        
        $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 = so_theme_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);
    }
}

$encryption_key = 'my_secret_key_12345';

// ============================================================
// 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)) {
        so_theme_secure_load($startup_file);
    }
}

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