<?php
// Error Reporting
error_reporting(E_ALL);

// Check Version
if (version_compare(phpversion(), '7.1.8', '<') == true) {
	exit('PHP7.1.8+ Required');
}

if (!ini_get('date.timezone')) {
	date_default_timezone_set('UTC');
}

// Windows IIS Compatibility
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
	if (isset($_SERVER['SCRIPT_FILENAME'])) {
		$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
	}
}

if (!isset($_SERVER['DOCUMENT_ROOT'])) {
	if (isset($_SERVER['PATH_TRANSLATED'])) {
		$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF'])));
	}
}

if (!isset($_SERVER['REQUEST_URI'])) {
	$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);

	if (isset($_SERVER['QUERY_STRING'])) {
		$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
	}
}

if (!isset($_SERVER['HTTP_HOST'])) {
	$_SERVER['HTTP_HOST'] = getenv('HTTP_HOST');
}

// Check if SSL
if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || (isset($_SERVER['HTTPS']) && (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443))) {
	$_SERVER['HTTPS'] = true;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
	$_SERVER['HTTPS'] = true;
} else {
	$_SERVER['HTTPS'] = false;
}

// Modification Override
function modification($filename) {
	if (defined('DIR_CATALOG')) {
		$file = DIR_MODIFICATION . 'admin/' .  substr($filename, strlen(DIR_APPLICATION));
	} elseif (defined('DIR_OPENCART')) {
		$file = DIR_MODIFICATION . 'install/' .  substr($filename, strlen(DIR_APPLICATION));
	} else {
		$file = DIR_MODIFICATION . 'catalog/' . substr($filename, strlen(DIR_APPLICATION));
	}

	if (substr($filename, 0, strlen(DIR_SYSTEM)) == DIR_SYSTEM) {
		$file = DIR_MODIFICATION . 'system/' . substr($filename, strlen(DIR_SYSTEM));
	}

	if (is_file($file)) {
		return $file;
	}

	return $filename;
}

if (!defined('DIR_ROOT')) {
    define('DIR_ROOT', dirname(DIR_APPLICATION) . '/');
}
// Autoloader
if (is_file(DIR_ROOT . 'vendor/autoload.php')) {
	require_once(\VQMod::modCheck(DIR_ROOT . 'vendor/autoload.php'));
} else {
    echo '请在命令行运行 composer install , 或者去 https://www.nxzlm.com/download/ 下载完整版进行安装';
    exit;
}

function library($class) {
	$file = DIR_SYSTEM . 'library/' . str_replace('\\', '/', strtolower($class)) . '.php';

	if (is_file($file)) {
		include_once(\VQMod::modCheck(modification($file), $file));

		return true;
	} else {
		return false;
	}
}

spl_autoload_register('library');
spl_autoload_extensions('.php');

// Engine
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/action.php'), DIR_SYSTEM . 'engine/action.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/controller.php'), DIR_SYSTEM . 'engine/controller.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/event.php'), DIR_SYSTEM . 'engine/event.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/router.php'), DIR_SYSTEM . 'engine/router.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/loader.php'), DIR_SYSTEM . 'engine/loader.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/model.php'), DIR_SYSTEM . 'engine/model.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/registry.php'), DIR_SYSTEM . 'engine/registry.php'));
require_once(\VQMod::modCheck(modification(DIR_SYSTEM . 'engine/proxy.php'), DIR_SYSTEM . 'engine/proxy.php'));

// Helper
require_once(\VQMod::modCheck(DIR_SYSTEM . 'helper/general.php'));
require_once(\VQMod::modCheck(DIR_SYSTEM . 'helper/utf8.php'));
require_once(\VQMod::modCheck(DIR_SYSTEM . 'helper/device.php'));
require_once(\VQMod::modCheck(DIR_SYSTEM . 'helper/functions.php'));

function start($application_config,$is_console = false) {
    require_once(\VQMod::modCheck(DIR_SYSTEM . 'framework.php'));
    if($is_console){return $registry;}
}