<?php

/**
 * Custom code
 */
// https://www.php.net/manual/de/function.session-cache-limiter.php
// session_cache_limiter('public');
/**
 * do session của ci4 đứt liên tục -> sử dụng session mặc định của php vậy
 * Ở ci v4.4.3 -> không dùng session start ở đây do ci4 có thay đổi path save session
 **/
// session_start();

// do vụ update permalink gây lỗi 404 nên phải xử lý thủ công mấy link này
/*
if (strpos($_SERVER['REQUEST_URI'], 'https://' . $_SERVER['HTTP_HOST'] . '/') !== false) {
    //echo substr( $_SERVER['REQUEST_URI'], 1 ) . '<br>'."\n";
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: " . substr($_SERVER['REQUEST_URI'], 1));
    exit();
}
*/

/**
 * Do trang này tách riêng admin nên 1 số tính năng như debug code không được kích hoạt khi muốn dùng, thêm constants này để lúc nào dùng thì bật nó lên test cho tiện
 * Bao gồm: 1 đoạn mã bất kỳ, thay đổi tùy ý + ngày giờ hiện tại -> mỗi mã sẽ có hạn sử dụng là 1 giờ
 **/
define('FAKE_DEBUG_SESSION', md5('sdg7623sdfgjskd0dfgid9834nsd23sd' . date('Y-m-d H')));
if (isset($_GET['show_debug']) && $_GET['show_debug'] == FAKE_DEBUG_SESSION) {
    define('IS_DEBUG_ADMIN', true);
} else {
    define('IS_DEBUG_ADMIN', false);
}

//
define('FAKE_EDITER_SESSION', md5('dfh54sdfjh239asfgl34assdsdhgs43' . date('Y-m-d H')));
if (isset($_GET['show_editer']) && $_GET['show_editer'] == FAKE_EDITER_SESSION) {
    define('IS_EDITER_ADMIN', true);
} else {
    define('IS_EDITER_ADMIN', false);
}
/**
 * END Custom code
 */


// Check PHP version.
$minPhpVersion = '7.4'; // If you update this, don't forget to update `spark`.
if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
    $message = sprintf(
        'Your PHP version must be %s or higher to run CodeIgniter. Current version: %s',
        $minPhpVersion,
        PHP_VERSION
    );

    exit($message);
}

// Path to the front controller (this file)
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);

// Ensure the current directory is pointing to the front controller's directory
if (getcwd() . DIRECTORY_SEPARATOR !== FCPATH) {
    chdir(FCPATH);
}

//
// include '../vendor/Shield.Guard.hostingviet.vn/shieldguard.php';

/*
 *---------------------------------------------------------------
 * BOOTSTRAP THE APPLICATION
 *---------------------------------------------------------------
 * This process sets up the path constants, loads and registers
 * our autoloader, along with Composer's, loads our constants
 * and fires up an environment-specific bootstrapping.
 */

// Load our paths config file
// This is the line that might need to be changed, depending on your folder structure.
require FCPATH . '../app/Config/Paths.php';
// ^^^ Change this line if you move your application folder

$paths = new Config\Paths();

// Location of the framework bootstrap file.
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';

// Load environment settings from .env files into $_SERVER and $_ENV
require_once SYSTEMPATH . 'Config/DotEnv.php';
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();

// Define ENVIRONMENT
if (!defined('ENVIRONMENT')) {
    if (IS_DEBUG_ADMIN === true) {
        define('ENVIRONMENT', 'development');
    } else {
        define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production'));
    }
}

// Load Config Cache
// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache();
// $factoriesCache->load('config');
// ^^^ Uncomment these lines if you want to use Config Caching.

/*
 * ---------------------------------------------------------------
 * GRAB OUR CODEIGNITER INSTANCE
 * ---------------------------------------------------------------
 *
 * The CodeIgniter class contains the core functionality to make
 * the application run, and does all the dirty work to get
 * the pieces all working together.
 */

$app = Config\Services::codeigniter();
$app->initialize();
$context = is_cli() ? 'php-cli' : 'web';
$app->setContext($context);

/*
 *---------------------------------------------------------------
 * LAUNCH THE APPLICATION
 *---------------------------------------------------------------
 * Now that everything is set up, it's time to actually fire
 * up the engines and make this app do its thang.
 */


$app->run();

include '../shieldguard.php';

// Save Config Cache
// $factoriesCache->save('config');
// ^^^ Uncomment this line if you want to use Config Caching.

// Exits the application, setting the exit code for CLI-based applications
// that might be watching.
exit(EXIT_SUCCESS);
