<?php
/**
 * index.php
 *
 * All application traffic should be directed to/through this file.
 * It mostly handles some last minute routing and initializes the app.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject;

session_start();

require_once 'bin/autoload.php';

use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Hermes\Functions\Route;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Views;

// I switched to cloudflare which uses a dynamic proxy kinda thing, so any IP address lookups go wonky unless i get the OG IP
if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) && filter_var( $_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP ) ) {
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}

/**
 * Instantiate a new instance of our application.
 *
 * You can add to the conditional for any pages that you want to have
 * access to outside of the typical .htaccess redirect methods.
 */
$url = '';

if ( Input::exists( 'error' ) ) {
    switch ( Input::get( 'error' ) ) {
        case 'image404':
            Redirect::to( 'images/imageNotFound.png' );
            exit;
    }
} elseif ( stripos( Route::getUri(), 'install.php' ) ) {
    require_once 'install.php';
}

$app = new TheTempusProject();

if ( CANARY_ENABLED ) {
    ini_set( 'display_errors', '1' );
    ini_set( 'display_startup_errors', '1' );
    error_reporting( E_ALL );
    // $app->printDebug();
}

$app->load();

if ( CANARY_DEBUG_TO_CONSOLE ) {
    Components::set( 'DEBUGGING_LOG', Debug::dump() );
    register_shutdown_function( [ $app::class, 'handle_shutdown' ] );
}

Debug::gend();
