<?php

    // Compression Control
    // -----------------------------------------------------
    ini_set("zlib.output_compression", 1);
	Ob_Start(); // Suppress output while loading libraries

	
	// Set error monitoring
	// -----------------------------------------------------
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);
	
	
	// Load Settings
	// -----------------------------------------------------
	include_once $ROOT . '/Modules/INI_CONST.php';  // Should loaded first since libraries use const values.

	
	// Load Importer
	// -----------------------------------------------------
	Class PHPINI_IMPORT
	{
	    /**
	     * Import all .php files in the given directory.
	     * @param String $DIRECTORY <br />Directory to load.<br />
	     */
	    Public Static Function REF_IMPORT_ALL($DIRECTORY)
	    {
	        Foreach (glob($DIRECTORY) AS $IMPORT_LIBRARIES)
	        {
	            //echo $IMPORT_LIBRARIES . '<br />';
	            Include_Once $IMPORT_LIBRARIES;
	        }
	    }
	    
	    
	    /**
	     * Execute PHPLib Imports
	     * @param unknown $PHPINI_ROOT Root Address
	     */
	    Public Static Function REF_EXECUTE($PHPINI_ROOT)
	    {
	        // Use Pre-compiled libraries
	        // -----------------------------------------------------
	        if(File_Exists($PHPINI_ROOT . "/Modules/Prebuild"))
	        {
	            // Load All
	            // ............................................
	            Include_Once($PHPINI_ROOT . "/Modules/Prebuild/PHPLib.php");
	            Include_Once($PHPINI_ROOT . "/Modules/Prebuild/StoredProcedures.php");
	        }
	        
	        // Use Raw libraries
	        // -----------------------------------------------------
	        else
	        {
	            // [ Load Libraries ]
	            // ............................................
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/References/*.php");
	            //require_once $ROOT . '/Modules/References/PHPRC_EXCEL/PHPExcel.php'; // todo uncomment this to use excel functionalities
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/Variables/*.php");
	            
	            // [ CUSTOM Libraries ]
	            // ............................................
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/Communications/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/Account/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/Addon/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/Category/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/Mobile/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/Record/*.php");
	            self::REF_IMPORT_ALL($PHPINI_ROOT . "/Modules/CUSTOM_LIBS/StoredProcedures/System/*.php");
	        }
	    }
	}
	PHPINI_IMPORT::REF_EXECUTE($ROOT);
	
	
	// Load AutoRun Commands
	// -----------------------------------------------------
	Class PHPINI_AUTORUN
	{
	    Public Static Function INI_EXECUTE($PHPINI_ROOT)
	    {
	        // Act as Background Processes
	        // -----------------------------------------------------
	        if(isset($_GET['IN_BACKGROUND']) == 1 && StrToUpper($_GET['IN_BACKGROUND'] == "TRUE"))
	        {
	            // flush all output
	            Ob_end_flush();
	            Ob_flush();
	            Flush();
	            
	            // Background Requests
	            //---------------------------------------------------------------------------
	            
	            // Add background processes here
	            // Ignore_User_Abort(true);
	            
	            //---------------------------------------------------------------------------
	            
	            // Clear session
	            PHPVC_LOG::VAR_INFO("AUTORUN", "COMPLETED");
	            Exit;
	        }
	        
	        
	        // Execute Autorun
	        // -----------------------------------------------------
	        else
	        {
	            // Uncomment this to use Background processes.
	            //PHPRC_HTTP::REF_REQUEST($SITE_APP_URL . '?IN_BACKGROUND=TRUE', '', 1);
	        }
	        
	        
	        // Add pre execution processes here
	        // -----------------------------------------------------
	        PHPSET::SET_LOAD($PHPINI_ROOT); // Load Settings [$ROOT is declared in Index.php]
	        PHPVC_SESSION::VAR_BEGIN(); // Begins Session
	        try
	        {
	            // Ignore Session Verification, if requesting nonce
	            if(isset($_GET['url']) !== 'System/nonce/get/')
	            {
	                PHPCUS_Account_Login_Session::CUS_SESSION_LOAD(); // Refreshes the authentication info
	            }
	        }
	        catch(Exception $ERR)
	        {
	            // Do nothing, it should return NOT ENOUGHT ACCESS RIGHTS on session expiry or change password.
	        }
	        
	        
	        // Automatically Run when Exit is called.
	        // -----------------------------------------------------
			/*
	        Register_Shutdown_Function
	        (
	            function()
	            {
	                // Add post-execution processes here
	                PHPCUS_Account_Login_Session::CUS_SESSION_SAVE();
	                exit;
	        }
	        );
			*/
	    }
	}
	PHPINI_AUTORUN::INI_EXECUTE($ROOT);
	

	// CLIENT SIDE OUTPUT BEGIN
	// -----------------------------------------------------
	// Keep php running even if the client closed the window
	// Ignore_User_Abort(true); 	
	Ob_End_Clean(); // Clears any output
	
?>