<?php

    session_cache_limiter('private,must-revalidate');
    session_start();

	

    $Sess_UserId=$_SESSION['Sess_UserId'];

    $link=mysql_connect($CONST_DB_HOST, $CONST_DB_USER, $CONST_DB_PASS);
    if (!$link) die ("Database connection failure");
    mysql_select_db($CONST_DB_NAME) or die("Failure in connection ".mysql_error() );


################################################################
##  Set user language                                         ##

    if (!$_SESSION['lang_id']) $_SESSION['lang_id'] = $CONST_DEFAULT_LANGUAGE;
    if ($_REQUEST['lang_id']) {
        $_SESSION['lang_id'] = $_REQUEST['lang_id'];
        if (isset ($Sess_UserId)) {
           echo $query="UPDATE members SET lang_id='".$_REQUEST['lang_id']."' WHERE mem_userid='$Sess_UserId'";
            $result=mysql_query($query,$link) or die(mysql_error());
        }
    }
	echo $_SESSION['lang_id'];
    if (isset ($Sess_UserId)) {
       echo $query="UPDATE members SET mem_timeout=NOW() WHERE mem_userid='$Sess_UserId'";
        $result=mysql_query($query,$link) or die(mysql_error());
        $query="SELECT * FROM members WHERE mem_userid='$Sess_UserId'";
        $result=mysql_query($query,$link) or die(mysql_error());
        $row = mysql_fetch_assoc($result);
        $_SESSION['lang_id'] = $row['lang_id'];
    }
  echo  $query="SELECT * FROM langfile WHERE lang_active=1";
    $result=mysql_query($query,$link) or die(mysql_error());
    $arr_languages = array();
    while ($lang = mysql_fetch_assoc($result)) {
        $arr_languages[] = $lang;
        if ($lang['lang_id'] == $_SESSION['lang_id'])
            $language = $lang;
    }
    $CONST_IMAGE_LANG = $language['lang_id'];
    $CONST_LANG_CHARSET = $language['lang_charset'];
    $CONST_FILE_LANG = $language['lang_filename'];

##################################################################
##  Loading text constants                                      ##
    include $CONST_INCLUDE_ROOT."/languages/".$language['lang_filename'].".php";

    $CONST_LINK_LANG_SWITCHER = include $CONST_INCLUDE_ROOT."/langswitcher.inc.php";

//  var_dump($_SESSION['HISTORY_PAGE']);
    include $CONST_INCLUDE_ROOT.'/nav_history.php';

################################################################
##  Handling extensions                                       ##

$extensionsList=array(
    "generatedEvents"    =>"/speeddating/ext_events.php",
    "generatedStories"   =>"/speeddating/ext_stories.php",
    "generatedLogin"     =>"/speeddating/ext_login.php"
);

foreach ($extensionsList as $extension=>$generator) {
    if (file_exists("$CONST_INCLUDE_ROOT/$generator"))
        ${$extension} = include($CONST_INCLUDE_ROOT."/$generator");
}

header("Content-Type: text/html; charset='$CONST_LANG_CHARSET'");
?>