<?php
//ini_set('display_errors', 'on');
//error_reporting(E_ALL);
@session_start();
ini_set('display_errors', 'off');
error_reporting(0);

//response array
$response_request = array();
//fields with errors
$errors = array();
//response from validations
$validations_errors = array();
function AutomaticAssignment($source, $class){
  foreach($source as $key => $value){
    $class->$key = $value;
  }
}
function SanitizeRequest($rules, $exclude){
  $utils = new Utils();
  foreach($_POST as $key => $value){
    if(!in_array($key, $exclude)){
      $_POST[$key]= $utils->SanitizeString($value);
    }
    if(isset($rules[$key])){
      $response = $utils->Validate($_POST[$key],$rules[$key][0], $rules[$key][1]);
      if(count($response)>0){
        array_push($GLOBALS['errors'],$key);
        $GLOBALS['validations_errors'] = array_merge($GLOBALS['validations_errors'], $response);
      }
    }
  }
}


if (!function_exists('_'))
{
  function _($data)
  {
    return $data;
  }
}

function __autoload($class_name)
{
  //class directories
  $directories = array(
    '../core/entity/',
    '../../core/entity/',
    '../../../core/entity/',
    '../core/utils/',
    '../../core/utils/',
    '../../../core/utils/'
  );
  //for each directory
  foreach($directories as $directory)
  {
    //see if the file exsists
    if(file_exists($directory.$class_name . '.php'))
    {
      require_once($directory.$class_name . '.php');
      //only require the class once, so quit after to save effort (if you got more, then name them something else
      return;
    }
  }
}

$utils = new Utils();
$tkn = $utils->ReturnNewTokenInForm();


if (!function_exists('getallheaders'))
{
  function getallheaders()
  {
    $headers = '';
    foreach ($_SERVER as $name => $value)
    {
      if (substr($name, 0, 5) == 'HTTP_')
      {
        $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
      }
    }
    return $headers;
  }
}

if($_COOKIE["ADMIN-LOGGED"]!=""){
  $utils->SetSessionVar("LOGGED-USER", $_COOKIE["ADMIN-LOGGED"]);
  $utils->SetSessionVar("PROFILE", $_COOKIE["ADMIN-PROFILE"]);
}

$LoggedUser = $utils->ReturnSessionVar("LOGGED-USER");

function checkLogin(){
  $utils = new Utils();
  if($utils->ReturnSessionVar("LOGGED-USER")==""){
    echo "<meta content='0;URL=login.php' http-equiv='REFRESH'> </meta>";
    exit();
  }
}


$u = new UserAccount();
$u->Id = $LoggedUser;
$in = $u->GetRecord();
$AVATAR = $in["Avatar"];
$PROFILE = $in["Profile"];
$FULL_NAME = $in["FirstName"]." ".$in["FirstName2"]." ".$in["LastName"]." ".$in["LastName2"];


//default alert values:
$response_request = array();
$response_request["type"] = "error";
$response_request["title"] = _("Unexpected error");
$response_request["content"] = _("Error 500, unexpected error input");
//echo $LoggedUser;


if($_SESSION["languagecurrent"]=="es" || $_GET["lang"]=="es"){
  $curr_language = "es";
}else{
  $curr_language = "en";
}
if($_GET["lang"]=="en"){
    $curr_language = "en";
}
$_SESSION["languagecurrent"] = $curr_language;

$language = ($curr_language=="es"?"es_MX.utf8":"en_US.utf8");
putenv("LC_ALL=$language");
setlocale(LC_ALL, $language);
bindtextdomain('es_MX', '/home/roc/public_html/precisetranslations/Locale');
textdomain('es_MX');