<?php

// 检查ENV环境
$_ENV = (isset($_ENV) && is_array($_ENV)) ? $_ENV : [];
$_ENV = array_merge($_ENV, load_env());
if(! get_env('ENVIRONMENT')){
    if (isset($_SERVER['SERVER_ADDR']) && in_array($_SERVER['SERVER_ADDR'], ['127.0.0.1', '::1'])) {
        $_ENV['ENVIRONMENT'] = 'development';
    } else {
        $_ENV['ENVIRONMENT'] = 'production';
    }
}
define('ENVIRONMENT', isset($_ENV['ENVIRONMENT']) ? $_ENV['ENVIRONMENT'] : 'development');

// 获取实时域名
$domain = '';
if (isset($_SERVER['HTTP_HOST'])) {
    $domain = $_SERVER['HTTP_HOST'];
} elseif (isset($_SERVER['SERVER_NAME'])){
    $domain = $_SERVER['SERVER_NAME'];
}
define('CI_DOMAIN', $domain);

switch (ENVIRONMENT) {
    case 'development':
        error_reporting(-1);
        ini_set('display_errors', 1);
        break;
    case 'production':
        ini_set('display_errors', 0);
        if (version_compare(PHP_VERSION, '5.3', '>=')) {
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        } else {
            error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
        }
        break;
    default:
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'The application environment is not set correctly.';
        exit(1); // EXIT_ERROR
}

// 是否启用调试模式
if(isset($_ENV['APP_DEBUG'])){
    if($_ENV['APP_DEBUG']){ // 调试模式
        error_reporting(-1);
        ini_set('display_errors', 1);
    }else{
        ini_set('display_errors', 0);
        if (version_compare(PHP_VERSION, '5.3', '>=')) {
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        } else {
            error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
        }
    }
}

$system_path = '../system';

$application_folder = '../application';

$view_folder = '';

if (defined('STDIN')) {
    chdir(dirname(__FILE__));
}

if (($_temp = realpath($system_path)) !== FALSE) {
    $system_path = $_temp . DIRECTORY_SEPARATOR;
} else {
    $system_path = strtr(
            rtrim($system_path, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR
        ) . DIRECTORY_SEPARATOR;
}

if (!is_dir($system_path)) {
    header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: ' . pathinfo(__FILE__, PATHINFO_BASENAME);
    exit(3);
}

define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));

define('BASEPATH', $system_path);

define('FCPATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);

define('SYSDIR', basename(BASEPATH));

if (is_dir($application_folder)) {
    if (($_temp = realpath($application_folder)) !== FALSE) {
        $application_folder = $_temp;
    } else {
        $application_folder = strtr(
            rtrim($application_folder, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR
        );
    }
} elseif (is_dir(BASEPATH . $application_folder . DIRECTORY_SEPARATOR)) {
    $application_folder = BASEPATH . strtr(
            trim($application_folder, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR
        );
} else {
    header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: ' . SELF;
    exit(3); // EXIT_CONFIG
}

define('APPPATH', $application_folder . DIRECTORY_SEPARATOR);

if (!isset($view_folder[0]) && is_dir(APPPATH . 'views' . DIRECTORY_SEPARATOR)) {
    $view_folder = APPPATH . 'views';
} elseif (is_dir($view_folder)) {
    if (($_temp = realpath($view_folder)) !== FALSE) {
        $view_folder = $_temp;
    } else {
        $view_folder = strtr(
            rtrim($view_folder, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR
        );
    }
} elseif (is_dir(APPPATH . $view_folder . DIRECTORY_SEPARATOR)) {
    $view_folder = APPPATH . strtr(
            trim($view_folder, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR
        );
} else {
    header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: ' . SELF;
    exit(3); // EXIT_CONFIG
}

define('VIEWPATH', $view_folder . DIRECTORY_SEPARATOR);

// 验证https是否开启
$https = false;
if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
    $https = true;
}elseif (isset($_SERVER['REQUEST_SCHEME']) && ('https' == $_SERVER['REQUEST_SCHEME'])) {
    $https = true;
} elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['REQUEST_SCHEME'])) {
    $https = true;
} elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && ('https' == $_SERVER['HTTP_X_FORWARDED_PROTO'])) {
    $https = true;
} else{
    if(isset($_ENV['HTTPS'])){
        $https = $_ENV['HTTPS'];
    }
}
define('CI_HTTPS', $https);

// public目录检测
$check = isset($_GET['check']) ? trim($_GET['check']) : '';
if ($check == 'public') {
    exit("true");
}

// 安装锁检测
$uri = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
if (strpos($uri, 'rewrite_check') === false) {
    if (!file_exists(APPPATH . 'cache/install.lock')) {
        header('Location:/install/index.php');
        die;
    }
}

// 检测PHP版本
if(version_compare(PHP_VERSION, '7.2', '<') || version_compare(PHP_VERSION, '7.3', '>=')){
    if((PHP_SAPI === 'cli' OR defined('STDIN'))){}else{
        echo '当前PHP版本为'.PHP_VERSION.'，仅支持PHP7.2.x';
        exit(3);
    }
}

// 加载EVN文件
function load_env($ini = "../.env"){
    // 绝对路径(兼容CLI)
    if((PHP_SAPI === 'cli' OR defined('STDIN'))){
        $path = __FILE__;
        $ini = str_replace(["/public","/index.php"],"",$path)."/.env";
    }
    if (is_file($ini) == false) { return []; }
    return parse_ini_file($ini, true);
}
// 获取ENV字段
function get_env($key, $default = null){
    if (isset($_ENV[$key])){ return $_ENV[$key]; }
    else { return $default; }
}
require_once BASEPATH . 'core/CodeIgniter.php';
