<?php

include_once(dirname(__FILE__).'/Version.php');
include_once(dirname(__FILE__).'/Database.php');
include_once(dirname(__FILE__).'/Error.php');
include_once(dirname(__FILE__).'/HTML/XHTML.php');
include_once(dirname(__FILE__).'/HTML/HTML.php');
include_once(dirname(__FILE__).'/HTTP.php');
include_once(dirname(__FILE__).'/Types/Type.php');
include_once(dirname(__FILE__).'/Output.php');
include_once(dirname(__FILE__).'/Permission.php');
include_once(dirname(__FILE__).'/PrefixMultiplier.php');
include_once(dirname(__FILE__).'/Setup.php');

$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');

function ErrorHandler()
{
}

class System
{
  var $Model = array();
  var $View = array();
  var $Controller = array();
  var $Config;
  var $Database;
  var $HTTP;
  var $HTML;
  var $Type;
  var $TimeStart;
  var $Translation;
  var $Output;
  var $DefaultModule;
  var $Modules = array();

  function __construct()
  {
    global $ShowPHPError;

    $TimeStart = $this->GetMicrotime();

    // Change base dir to index. Helpfull if script is executed from command line with related path.
    chdir(dirname(__FILE__));

    $this->Output = new Output($this);

    $Config = array();

    $FileName = dirname(__FILE__).'/Config/Config.php';
    if(file_exists($FileName)) include($FileName);
      else {
        $this->Output->Show($this->Output->SystemMessage('Configuration file "'.$FileName.'" not found.'));
        exit;
      }

    $FileName = dirname(__FILE__).'/../Application/Config/Config.php';
    if(file_exists($FileName)) include($FileName);

    $this->Config = $Config;

    $ShowPHPError = $this->Config['System']['ShowPHPError'];

    $Translation = array();
    $FileName = dirname(__FILE__).'/../Application/Locale/'.$this->Config['System']['Locale'].'.php';
    if(file_exists($FileName)) include($FileName);
      else {
        $this->Output->Show($this->Output->SystemMessage('Translation file "'.$FileName.'" not found.'));
        exit;
      }
    $this->Translation = $Translation;

    if(isset($_SERVER['REMOTE_ADDR'])) session_start();
    $this->Database = new Database();
    $this->Database->Connect($this->Config['Database']['Host'],
      $this->Config['Database']['User'], $this->Config['Database']['Password'],
      $this->Config['Database']['Database']);
    $this->Database->ShowSQLQuery = $this->Config['System']['ShowSQLQuery'];
    $this->Database->ShowSQLError = $this->Config['System']['ShowSQLError'];
    $this->Database->Prefix = $this->Config['Database']['Prefix'];
    $this->Database->charset($this->Config['Database']['Charset']);

    $this->HTTP = new HTTP();
    $this->HTML = new HTML($this);
    $this->Type = new Type($this);
    $this->AddModule('Permission');
  }

  function Run()
  {
    $this->TimeStart = $this->GetMicrotime();

    // SQL injection hack protection
    foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
    foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);

    // Register and start existing modules
    $this->Setup = new Setup($this);
    $this->Setup->Start();

    if(array_key_exists('M', $_GET)) $Module = $_GET['M'];
      else $Module = $this->DefaultModule;

    $ControllerName = $Module.'Controller';
    if(!class_exists($ControllerName))
    {
    	// Try to load controller class
      $FileName = dirname(__FILE__).'/../Application/Controller/'.$Module.'.php';
      if(!file_exists($FileName))
      {
        $this->Output->Show($this->Output->SystemMessage('Soubor '.$FileName.' nenalezen.'));
        exit;
      } else include($FileName);
    }
    if(class_exists($ControllerName))
    {
      $Controller = new $ControllerName($this);
      $this->Output->Show($Controller->Process());
    } else $this->Output->Show($this->Output->SystemMessage('Modul "'.$ControllerName.'" nenalezen.'));
  }

  function ModulePresent($Name)
  {
    return(array_key_exists($Name, $this->Modules));
  }

  function AddModule($Name)
  {
    $this->Modules[$Name] = new $Name($this);
  }

  function HumanDate($Time)
  {
    return(date('j.n.Y', $Time));
  }

  function NetworkPortState($Address, $Port, $Timeout = 1)
  {
    set_error_handler('ErrorHandler');
    $Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout);
    if($Socket)
    {
      fclose($Socket);
      $Result = true;
    } else $Result = false;
    restore_error_handler();
    return($Result);
  }

  function Translate($Text)
  {
    if(array_key_exists($Text, $this->Translation)) return($this->Translation[$Text]);
      else return('#'.$Text);
  }

  function MakeLink($Module, $Action, $Parameters = array())
  {
    $Parameters = $this->HTTP->SetQueryStringArray($Parameters);
    if($Parameters != '') $Parameters = '&amp;'.$Parameters;
    return('?M='.$Module.'&amp;A='.$Action.$Parameters);
  }

  function GetRemoteAddress()
  {
    if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
    else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
    else $IP = '0.0.0.0';
    return($IP);
  }

  function GetMicrotime()
  {
    list($Usec, $Sec) = explode(' ', microtime());
    return((float)$Usec + (float)$Sec);
  }

  function RemoveDiacritic($Text)
  {
    return(str_replace(
      array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
      array('a', 'c', 'd', 'e', 'e', 'i', 'l', 'n', 'o', 'r', 's', 't', 'u', 'u', 'y', 'z', 'A', 'C', 'D', 'E', 'E', 'I', 'L', 'N', 'O', 'R', 'S', 'T', 'U', 'U', 'Y', 'Z'),
      $Text));
  }
}
