<?php
/**
 * @author David DRAPEAU <david.drapeau@jmdn-solutions.com>
 * @author Benjamin LECHALUPE <benjamin.lechalupe@jmdn-solutions.com>
 * @version 0.1
 */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Send page content into a buffer instead of draw it directly.
// Required for file downloads and redirections from modules!!
ob_start();

session_start();

if(!isset($_COOKIE['firstvisit'])){
  setcookie("firstvisit", "false", time()+60*60*24*60);
}

// Reporte toutes les erreurs PHP (Voir l'historique des modifications)
if(isset($_SESSION['config']['framework_environment']) && $_SESSION['config']['framework_environment']=="dev"){
  ini_set("display_errors", "stdout");
  error_reporting(E_ALL & E_NOTICE);
}

require_once './modules/InterfaceConfig.php';
require_once './modules/InterfaceController.php';
require_once './modules/InterfacePostgreSQL.php';
require_once './modules/InterfaceTranslation.php';
require_once './modules/InterfaceEmail.php';

require_once './modules/ClassConfig.php';
require_once './modules/ClassTranslation.php';
require_once './modules/ClassController.php';
require_once './modules/ClassLang.php';
require_once './modules/ClassFeature.php';
require_once './modules/ClassFeatureTranslation.php';
require_once './modules/ClassCountry.php';
require_once './modules/ClassCurrency.php';
require_once './modules/ClassUser.php';
require_once './modules/ClassEmail.php';
require_once './modules/ClassForm.php';

$oConf = new ClassConfig();
$oTrans = new ClassTranslation();
$oController = new ClassController($_REQUEST);
$oLang = new ClassLang();
$oFeat = new ClassFeature();
$oFeatTrans = new ClassFeatureTranslation();
$oUser = new ClassUser();
$oCountry = new ClassCountry();
$oCurrency = new ClassCurrency();


try {
  $oPDOLink = $oConf->databaseConnect();
} catch(PDOException $e){
    print "Error:". $e->getMessage()."<br/>";
    die("If you are seeing this message, please send urgently a message to contact@jmdn-solutions.com");
}



//IMPORT MODULES FROM CUSTOM DIRECTORY
if(file_exists($oConf->getPathCustom("modules.php"))){
  include($oConf->getPathCustom("modules.php"));
}


// Load configuration in _SESSION
//unset($_SESSION['config']); //uncomment this line, even for test, creates a bug for translations for logged users

if (!isset($_SESSION['config'])) {
    $_SESSION['config'] = $oConf->getConfig();

    //////
    //// The code below creates a bug in Chromium "Too many redirections
    //$find_lang = "/[a-zA-Z][a-zA-Z]-[a-zA-Z][a-zA-Z]/";
    //preg_match($find_lang, $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang);
    //$lang = str_replace('-', '_', strtolower($lang[0]));
    //$langs = $oLang->listLanguages();
    //
    //foreach ($langs as $k=>$v) {
    //  if ($langs[$k]['code'] == $lang) {
    //    $_SESSION['config']['default_lang_code'] = $lang;
    //  }
    //}
    
    $_SESSION['translations'] = $oTrans->listTranslations($_SESSION['config']['default_lang_code']);
    $_SESSION['countries'] = $oCountry->listCountries('priority ASC, name ASC');
    $_SESSION['activeCountries'] = $oCountry->listActiveCountries('priority ASC, name ASC');
    $_SESSION['currencies'] = $oCurrency->listCurrencies('priority ASC, name ASC');
    $_SESSION['languages'] = $oLang->listLanguages();
    $_SESSION['activeLanguages'] = $oLang->listActiveLanguages();
}

if(!isset($_SESSION['translations'])){
    $_SESSION['translations'] = $oTrans->listTranslations('fr_fr');
}
if(!isset($_SESSION['countries'])){
    $_SESSION['countries'] = $oCountry->listCountries('priority ASC, name ASC');
}
if(!isset($_SESSION['activeCountries'])){
    $_SESSION['activeCountries'] = $oCountry->listActiveCountries('priority ASC, name ASC');
}
if(!isset($_SESSION['currencies'])){
    $_SESSION['currencies'] = $oCurrency->listCurrencies('priority ASC, name ASC');
}
if(!isset($_SESSION['languages'])){
    $_SESSION['languages'] = $oLang->listLanguages();
}
if(!isset($_SESSION['activeLanguages'])){
    $_SESSION['activeLanguages'] = $oLang->listActiveLanguages();
}

//$data_updated = array();
//$user = array();
//if(isset($_SESSION['user']) && isset($_POST['submit_personal_data'])){
//    $_SESSION['data_updated'] = $oUser->updatePersonalData($_SESSION['user']['id'], $_POST);
//    //$data_updated = $oUser->updatePersonalData($_SESSION['user']['id'], $_POST);
//    $user = $oUser->getFullProfile($_SESSION['user']['id']);
//    $_SESSION['user']['core_lang_id'] = $user['core_lang_id'];
//    $_SESSION['user']['core_country_id'] = $user['core_country_id'];
//    $_SESSION['user']['lang_code'] = $user['lang_code'];
//    $_SESSION['translations'] = $oTrans->listTranslations($user['lang_code']);
//    $_SESSION['features'] = $oFeat->getFeaturesOfUser($_SESSION['user']['id'], $_SESSION['user']['lang_code']);
//    unset($_POST);
//    unset($user);
//    //$_SESSION['data_updated'] = $data_updated;
//    //unset($data_updated);
//}

// If no requested page defined, load default page in default lang code
if (!isset($_REQUEST['page'])) header("Location: ".$oConf->getURLRoot($oFeatTrans->getFeatureTranslation('menu_home_url', $_SESSION['config']['default_lang_code'])));

// Get features for menu
//unset($_SESSION['features']);
if (!isset($_SESSION['features'])) {
    if(isset($_SESSION['user']['id'])) {
        $_SESSION['features'] = $oFeat->getFeaturesOfUser($_SESSION['user']['id']);
    } else {
        $_SESSION['features'] = $oFeat->getFeatures($_SESSION['config']['default_lang_code']);
    }
}

if(file_exists($oConf->getPathCustom("themes/".$_SESSION['config']['theme']."/index.php"))){
  include($oConf->getPathCustom("themes/".$_SESSION['config']['theme']."/index.php"));
} elseif(file_exists("./themes/".$_SESSION['config']['theme']."/index.php")) {
  include("themes/".$_SESSION['config']['theme']."/index.php");  
} else {
  include("themes/default/index.php");  
}

// Commit the buffer (display the page)
ob_end_flush();
?>