<?php
function MsAutoload($classname){
  $filename = $_SERVER['DOCUMENT_ROOT'] . '/script/' . str_replace('_', '/', $classname). '.php';
  include_once($filename);
}
spl_autoload_register('MsAutoload');
SessionMod::SessStart();
//ini_set('error_reporting', E_ALL);
//ini_set('display_errors', 'ON');
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED & ~E_WARNING);
ini_set('display_errors', 'OFF');

$ObjLangMod = new LangMod();
$MS_SP = new SitePage($ObjLangMod);
date_default_timezone_set($MS_SP->Sp_Config['TimeZone']);
//SiteLibs::GetVarDump($_SESSION);

if(true === isset($_SESSION['lang']) && 2 == strlen($_SESSION['lang'])){
  $MS_SP->Sp_Lang = $MS_SP->SetLang($_SESSION['lang']);
}
if(true === isset($_GET['UrlLang']) && 2 == strlen($_GET['UrlLang'])){
  $MS_SP->Sp_Lang = $MS_SP->SetLang($_GET['UrlLang']);
}

require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = $MS_SP->Sp_smarty_template_dir;
$smarty->compile_dir = $MS_SP->Sp_smarty_compile_dir;
$smarty->config_dir = $MS_SP->Sp_smarty_config_dir;
$smarty->cache_dir = $MS_SP->Sp_smarty_cache_dir;
$VarAuthForm = '';
$VarPagesTxt = '';
//require_once($_SERVER['DOCUMENT_ROOT'] .'/script/SiteAuth.mod.php');
if (true === isset($_GET['UrlLogout']) && 1 == $_GET['UrlLogout'] && true === isset($_SESSION)) {
  SessionMod::SessUnset();
}
if(true === isset($_POST['form_auth_login']) && '' != $_POST['form_auth_login']){
  if(true === isset($_POST['form_auth_password']) && '' != $_POST['form_auth_password']){
    $VarPass = "u.users_password = '". md5(@$_POST['form_auth_password']) ."'";
  }
  else{
    $VarPass = "(u.users_password = '". md5(@$_POST['form_auth_password']) ."' OR u.users_password = '')";
  }
  $ObjUsersMod = new UsersMod($MS_SP);
  $ArrUser = $ObjUsersMod->GetFromDb('SqlAll', 'GetRow', "u.users_name = '". $_POST['form_auth_login'] ."' AND ". $VarPass);
  
  if(true === is_array($ArrUser) && 1 == $ArrUser['users_status']){
    SessionMod::SessUnset();
    $ObjUsersMod->InsertSession($ArrUser);
//    echo "MS_SP->SP_CurrentHref: ".$MS_SP->SP_CurrentHref."<br />";
//    if('XMLHttpRequest' != @$_SERVER['HTTP_X_REQUESTED_WITH'] || (true === isset($MS_SP->SP_CurrentHref) && '/libs/site/AjaxServer/AuthCheck.ajax.php' == $MS_SP->SP_CurrentHref)){
//      if(true === isset($MS_SP->SP_CurrentHref) && '' != $MS_SP->SP_CurrentHref && '/libs/site/AjaxServer/AuthCheck.ajax.php' != $MS_SP->SP_CurrentHref){
//        $URL = $MS_SP->SP_CurrentHref;        
//      }
//      else{
//        $URL = '/index.php';
//      }
//      header("Location: $URL");
//    }
  }
}

$ObjUsersPrefMod = new UsersPrefMod($MS_SP);
$MS_SP->SP_UsersPref = $ObjUsersPrefMod->GetPref(@$_SESSION['SessionAuth']['id']);

$ObjProgrammPrefMod = new ProgrammPrefMod($MS_SP);
$MS_SP->SP_ProgrammPref = $ObjProgrammPrefMod->GetPref();
//SiteLibs::GetVarDump($MS_SP->SP_IpsPref['IpsPrefOrgName']['ipsprefvalue_setvalue']);

if(true === isset($_SESSION['SessionAuth']['id']) && true === is_numeric($_SESSION['SessionAuth']['id'])){
  $ObjUsersSettingsValueMod = new UsersSettingsValueMod($MS_SP);
  $MS_SP->SP_UsersSettingsValue = $ObjUsersSettingsValueMod->GetFromDb('SqlAssoc', 'GetAssoc', "a.users_id = '". $_SESSION['SessionAuth']['id'] ."'");
}

$ObjMenuMod = new MenuMod($MS_SP);
$ObjMenuMod->VarLang = $MS_SP->Sp_Lang;
$MS_SP->SP_CurrPage = $ObjMenuMod->GetFromDb('SqlAll', 'GetRow', "m.menu_href = '". $MS_SP->SP_CurrentHref ."'");
//SiteLibs::GetVarDump($MS_SP);

$smarty->assign('MS_SP', $MS_SP);

$ArrLangs = $ObjLangMod->GetLangs();
//SiteLibs::GetVarDump($ArrLangs);
if (true === is_array($ArrLangs) && 1 < count($ArrLangs)){
  $smarty->assign('ArrLangs', $ArrLangs);
  $VarMenuLangs = $smarty->fetch('Menu/MenuLangs.tpl');
  $smarty->assign('VarMenuLangs', $VarMenuLangs);
}
//$ObjModMenu->GetMenu('MenuMain');
$smarty->assign('ArrPagesPath', array_reverse($ObjMenuMod->GetPagesPathArray()));
//
$VarPagesPath = $smarty->fetch('PagesPath.tpl');
$smarty->assign('VarPagesPath', $VarPagesPath);

$ArrMenu = $ObjMenuMod->GetMenuAdv('MenuMain', 'JQueryMbMenu', @$_SESSION['SessionAuth']['webservices'], 'm.menu_participates = 1');
//SiteLibs::GetVarDump($ArrMenu);
$smarty->assign('ArrMenu', $ArrMenu);
$VarMenu = $smarty->fetch('Menu/MenuTop.tpl');
?>