<?php

// error_reporting(E_ALL);
// ini_set('display_errors', 'on');

ini_set('session.cookie_secure',1);
ini_set('session.cookie_httponly',1);
ini_set('error_reporting', E_ALL);
session_start();

include('version.php');

include('../include/config.php');

include($root . '/include/domain.php');

require_once('../include/pg_connect.php');

include($root . '/include/' . $include_folder . '/function.php');

/* ==========================================================================
    Url rewriting - rules
   ========================================================================== */

include($root . '/include/' . $include_folder . '/rewriting.php');

if(isset($_GET['query'])){
    $query = $_GET['query'];
}
if(isset($_GET['p'])){
    $p = $_GET['p'];
}
if(isset($_GET['id'])){
    $id = $_GET['id'];
}
if(isset($_GET['category'])){
    $category = $_GET['category'];
}

$prim_uri_arr=explode('#',$_SERVER['REQUEST_URI']);
$prim_uri=$prim_uri_arr[0];
$uri = rtrim(dirname($_SERVER["SCRIPT_NAME"]), '/');
$uri = trim(str_replace( $uri, '', $prim_uri ), '/');
$uri = urldecode($uri);
$parseUrl = parse_url($uri);
$uri = isset($parseUrl['path']) ? htmlspecialchars($parseUrl['path']) : ""; // on ne garde que l'url sans le get

// Read entire file into string
$xmlfile = file_get_contents($root.'/www/oldsitemap.xml');

// Convert xml string into an object
$sitemapdecoded = simplexml_load_string($xmlfile);

foreach ($sitemapdecoded as $url) {
    if ($url->loc == 'http://www.jostgroup.com'.$_SERVER['REQUEST_URI']) {
        header('HTTP/1.0 301 Moved Permanently');
        header('Location: ' . $baseRoot . $default_lang);
    }
}

if(isset($parseUrl['query'])){
   parse_str($parseUrl['query'], $_GET); // on recrée le GET car il arrive vide
}

if(preg_match("~^(?'lang'[a-z]{2})(\/(?'uri'.*)?)?$~i", $uri, $params)){
    $toRedirect = false;
    $uri = isset($params['uri']) ? $params['uri'] : '';
    $langue = $params['lang'];
}
else{
    $toRedirect = true;
}


if( ! isset($langue) ){
    if(isset($_SESSION['lang']) && $_SESSION['lang'] != '' && strlen($_SESSION['lang']) == 2){
        $langue = $_SESSION['lang'];
    }
    elseif (isset($_COOKIE['lang']) && $_COOKIE['lang'] != '' && strlen($_COOKIE['lang']) == 2) {
        $langue = $_COOKIE['lang'];
    }
}


if(isset($langue) && in_array($langue, $accepted_lang)){
    $_SESSION['lang'] = $langue;
    if ($_COOKIE['fonctionalCookies'] == 1) {
        setcookie("lang", $langue, time()+60*60*24*300,'/','', $secure, 1);
    }
    if($uri != "splash"&& $uri != "sitemap.xml" && $toRedirect){
        header('Location: ' . generateDomainWithLang($baseRoot, $langue));
        exit();
    }
    $domain = generateDomainWithLang($baseRoot, $langue);
}
elseif($uri != "splash" && $uri != "sitemap.xml"){
    /* Don't comment next line if we have a Splash-Page */
    // header('Location: ' . $baseRoot . '/splash');
    /* Don't comment next line if we don't have a Splash-Page */
    //header('Location: ' . $baseRoot . '/' . $default_lang);
    header('Location: ' . generateDomainWithLang($baseRoot, $default_lang));
    exit();
}
else{
    $langue = $default_lang;
}

$url = substr($domain,0,-2);

/*if($uri == "sitemap.xml"){
    include('sitemap.php');
    exit();
}*/

/* ==========================================================================
    Inclusion trad
   ========================================================================== */



if(in_array($langue, $accepted_lang)){
    include('lang/'.$langue.'.php');
}else{
    include('lang/'.$default_lang.'.php');
}

/* ==========================================================================
    Url rewriting
   ========================================================================== */

foreach ( $rules[$langue] as $rule => $action ) {

    if(preg_match( '~^'.$rule.'$~i', $uri, $params)){
        $page = $action;
        break;
    }
}
/* ==========================================================================
    Url rewriting
   ========================================================================== */

/*$rules = array(
    "home" => 'home',
    "cases" => 'cases',
    "case-(?'id'[-_a-z0-9]*)" => 'case',
    "tracking" => 'tracking',
    "news" => 'news',
    "news-(?'id'[-_a-z0-9]*)" => 'news',
    "press" => 'press',
    "press-(?'id'[-_a-z0-9]*)" => 'press',
    "about" => 'about',
    "contact" => 'contact',
    "cookies" => 'cookies',
    "privacy" => 'privacy',
    "map" => 'map',
    "jobs" => 'jobs',
    "job/(?'id'[-_a-z0-9]*)" => 'job2',
    "job/(?'id'[-_a-z0-9]*)/(?'slug'[-_a-z0-9]*)" => 'job',
    "thanks/(?'id'[-_a-z0-9]*)" => 'thanks'
);*/

$_GET=$params;


/* ==========================================================================
    Accès direct langue
   ========================================================================== */

if(isset($uri) && $uri!=''){
    if($uri=="accueil"){
        $_SESSION['lang']="fr";
    }
    if($uri=="onthaal"){
        $_SESSION['lang']="nl";
    }
    if($uri=="home"){
        $_SESSION['lang']="en";
    }
}

$_GET['page'] = $uri;



/* ==========================================================================
    404
   ========================================================================== */
if((!isset($page) || $page=='') && $uri=='')
//if((!isset($page) || $page=='') && $uri!='')
{
     if(isset($_GET['page']) && $_GET['page']!='')
     {
        $page=$_GET['page'];
    }
    else
    {
        $page = 'home';
    }
}
elseif($uri=='')
{
    //$page='home';
    $page='404';
}
else
{
    $_GET=$params;
}

if(isset($get)){
   $_GET = array_merge($get,$_GET);
}

if(isset($query)){
    $page = 'search';
}
if(!isset($page)){
    $page = '404';
}
$_GET['page']=$page;

/* ==========================================================================
    Switch pages
   ========================================================================== */
include('includes/function.php');

$domain = $root = dirname(__DIR__);

$racine = '';

include($root . '/include/' . $include_folder . '/pages.php');

if($page != 'rss'){
    include('includes/footer.php');
}


function is_ssl($server) {
    if (isset($server['HTTPS']) ) {
        if ( 'on' == strtolower($server['HTTPS']) ){
            return true;
        }
        if ( '1' == $server['HTTPS'] ){
            return true;
        }
    } elseif ( isset($server['SERVER_PORT']) && ( '443' == $server['SERVER_PORT'] ) ) {
        return true;
    }elseif(isset($server['HTTP_X_FORWARDED_PROTO']) && $server['HTTP_X_FORWARDED_PROTO'] == "https"){
        return true;
    }
    return false;
}

function checkValidityCareer($get)
{

    if (
        !isset($get['id']) || empty(trim($get['id']))
    ) {
        return 0;
    }

    if (($career = getCareer($get['id']))) {
        return array("career" => $career);
    } else {
        return 0;
    }
}

function getCareer($code)
{
    global $db, $langue, $active;
    //$langue = 'en';
    $active = 'active';
    if($_SERVER["HTTP_HOST"] == 'aazio.fr' ){
        $active = 'active_fr';
    }

    $activeString = "AND c.".$active." = 't'";
    if(isset($_GET['preview']) && $_GET['preview'] == "stratPreviewOk"){
        $activeString = "";
    }

    $sql = "SELECT ct.*, c.id AS career_id, c.linkedin
    FROM career c INNER JOIN career_trad ct ON c.id = ct.fk_career_id
    WHERE c.id = $1";
    $reqCode = pg_query_params($db, $sql, array($code));
    if ($reqCode && $career = pg_fetch_assoc($reqCode)) {
        return isset($career['id']) ?  $career : false;
    } else {
        if (!$reqCode) {
            debug('Error pg_query_params() getCareer', $_SERVER['SCRIPT_FILENAME'], ' code: ' . $code . " sql: " . $sql . " Erreur: " . pg_last_error());
        }
        return false;
    }
}

function generateDomainWithLang($baseRoot, $langue)
{
    return $baseRoot . '/' . $langue;
}

?>
