<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();

//smc_error_reporting();
//this function check if custom path exist then include it else do nothing
function include_custom_path($path, $is_include = true) {
    if (!defined('CUSTOM_FOLDER') || empty(CUSTOM_FOLDER))
        return false;
    $full_path = __DIR__ . DIRECTORY_SEPARATOR . '_custom' . DIRECTORY_SEPARATOR . CUSTOM_FOLDER . DIRECTORY_SEPARATOR . $path;
    if (!file_exists($full_path) || !is_readable($full_path))
        return false;
    if ($is_include)
        include $full_path;
    else
        return $full_path;
    return true;
}

/**
 * Debug class|variable|etc..
 * @param type $param
 */
function smc_debug($param) {
    echo "<table><tr><td bgcolor=c0c0c0 width=2000 style='direction:ltr'><pre>";
    print_r($param);
    echo "</pre></td></tr></table>";
}

function smc_error_reporting() {
    error_reporting(E_ALL);
    ini_set('display_errors', 'Off');
}

/**
 * Debug class|variable|etc..
 * @param type $param
 */
function smc_escape($param) {
    if (!empty($param) && is_string($param))
        return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $param);
    return $param;
}

/*
 * if constants.php is not included, include it
 */
//but we do below since in case of symbolic link since __DIR__ resolve the symbolic link
if (!empty($_SERVER['SCRIPT_FILENAME'])) //if called from cmd like cron it will be empty
    $cms__website_root_path = $_SERVER['SCRIPT_FILENAME'];
else {
    $cms__tmp_included_files = get_included_files();    //script originally called is considered an included file
    $cms__website_root_path = $cms__tmp_included_files[0];
    unset($cms__tmp_included_files);
}
$cms__tmp_i = 0;
while (true && !empty($cms__website_root_path)) {
    $cms__tmp_website_root_path = $cms__website_root_path;
    $cms__website_root_path = dirname($cms__website_root_path);
    if ($cms__website_root_path == $cms__tmp_website_root_path)    //if no more folders exist to get parent, stop
        break;
    if (file_exists($cms__website_root_path . '/' . basename(__FILE__)) || ++$cms__tmp_i > 50)
        break;
}
if (empty($cms__website_root_path))
    $cms__website_root_path = __DIR__;  //by default
//make rtrim not trim since maybe path is '/var/www/..' to don't remove first '/'
$cms__website_root_path = rtrim(str_replace('\\', '/', $cms__website_root_path), '/');
$cms__website_root_path .= (empty($cms__website_root_path) ? '' : '/');
include_once $cms__website_root_path . 'Includes/constants.php';
//include_once __DIR__ . '/Includes/constants.php';
//
if (!defined('dirBase'))
    define('dirBase', $cms__website_root_path);
unset($cms__website_root_path);

//die(SITE_LINK);
/**
 * 
 */
function &smc_session() {
    if (!isset($_SESSION['trusto__' . DB_NAME]))
        $_SESSION['trusto__' . DB_NAME] = array();
    return $_SESSION['trusto__' . DB_NAME];
}

function smc_destroy_session() {
    unset($_SESSION['trusto__' . DB_NAME]);
}

/*
 * iincludes->autoloader
 */
spl_autoload_register(function ($class) {
    $directories = array(
        'System/Core/',
        'System/Database/',
        'System/Database/Adapters/',
        'System/Database/Drivers/',
        'Controllers/',
        'Models/',
        'libs/',
        'libs/Common/',
        'libs/Inventory/',
        'libs/HRM/',
        'libs/Maintenance/',
        'libs/Accounting/',
        'libs/Inventory/',
        'libs/Stock/',
        'libs/crm/',
        'libs/Garage/',
        'libs/General/',
        'libs/GeneralFunctions/',
        'libs/PHPExcel/',
        'libs/TCPDF/'
    );
    $is_exist = false;
    $file_name = $class;
    if ($class == 'TCPDFBarcode')
        $file_name = 'tcpdf_barcodes_1d';
    $is_smc_custom = (substr($file_name, -9) == 'SMCCustom');
    $file_noncustom = $is_smc_custom ? substr($file_name, 0, -9) : $file_name;
    foreach ($directories as $directory) {
        $file_path = dirBase . $directory . $file_name . '.php';
        $file_noncustom_sub_path = $directory . $file_noncustom . '.php';
        $file_noncustom_path = dirBase . $file_noncustom_sub_path;
        if (file_exists($file_path) && is_readable($file_path)) {
            include $file_path;
            $is_exist = true;
        }
        //if not exis as normal file and it is smc custom file then try if it is really exist in custom folder if not exist
        //then we make check if it is already exist as class non custom then we create a custom class that just extends the normal class.
        if (!$is_exist && $is_smc_custom) {
            if (include_custom_path($file_noncustom_sub_path)) {
                $is_exist = true;
            } elseif (file_exists($file_noncustom_path) && is_readable($file_noncustom_path)) {
                if (!class_exists($class, false))
                    eval(" class {$class} extends {$file_noncustom}{} ");
                $is_exist = true;
            }
        }
        if ($is_exist)
            return;
    }
    //
    if (strtolower($class) == 'dompdf') {
//        require'libs/dompdf/autoload.inc.php';
//        include_once __DIR__.'/libs/dompdf/src/Dompdf.php';
    }
});

//use libs\Dompdf\Dompdf;


if (!empty(smc_session()['default_setting']['default_time_zone'])) {
    date_default_timezone_set(smc_session()['default_setting']['default_time_zone']);
}

//for datatable if we are send them as post, we don't put it inside datatable class since here the search fields that we checking in controller and add conditions
if (!empty($_GET) && !empty($_GET['smc__is_datatable_method']) && $_GET['smc__is_datatable_method'] == 'post' && !empty($_POST) && !empty($_POST['smc__datatable_post_data']))
    if (!empty($_POST['smc__datatable_request_method']) && strtolower($_POST['smc__datatable_request_method']) == 'post') {
        $_POST = array_merge($_POST, json_decode($_POST['smc__datatable_post_data'], true));
        $_POST['smc__is_export_csv'] = 1;
    } else
        $_GET = array_merge($_GET, json_decode($_POST['smc__datatable_post_data'], true));
