Changeset 42 for trunk/Base/System.php


Ignore:
Timestamp:
Nov 24, 2009, 9:13:38 AM (14 years ago)
Author:
george
Message:
  • Upraveno: Přepracován základový webový systém
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Base/System.php

    r40 r42  
    11<?php
     2
     3include_once(dirname(__FILE__).'/Database.php');
     4include_once(dirname(__FILE__).'/HTML/XHTML.php');
     5include_once(dirname(__FILE__).'/HTML/HTML.php');
     6include_once(dirname(__FILE__).'/HTTP.php');
     7include_once(dirname(__FILE__).'/Types/Type.php');
     8include_once(dirname(__FILE__).'/Output.php');
     9include_once(dirname(__FILE__).'/Permission.php');
     10
     11$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
    212
    313$PrefixMultipliers = array
     
    7686class System
    7787{
    78   var $Modules = array();
     88  var $Model = array();
     89  var $View = array();
     90  var $Controller = array();
     91  var $Config;
    7992  var $Database;
    80   var $Config;
     93  var $HTTP;
     94  var $HTML;
     95  var $Type;
     96  var $TimeStart;
     97  var $Translation;
     98  var $Output;
     99  var $DefaultModule;
     100  var $Modules = array();
     101
     102  function __construct()
     103  {
     104    global $ShowPHPError;
     105
     106    $TimeStart = $this->GetMicrotime();
     107   
     108    // Change base dir to index. Helpfull if script is executed from command line with related path.
     109    chdir(dirname(__FILE__));
     110
     111    $this->Output = new Output($this);
     112
     113    $FileName = dirname(__FILE__).'/Config/Config.php';
     114    if(file_exists($FileName)) include($FileName);
     115      else {
     116        $this->Output->Show($this->Output->SystemMessage('Configuration file "'.$FileName.'" not found.'));
     117        exit;
     118      }
     119
     120    $FileName = dirname(__FILE__).'/../Application/Config/Config.php';
     121    if(file_exists($FileName)) include($FileName);
     122   
     123    $this->Config = $Config;
     124   
     125    $ShowPHPError = $this->Config['System']['ShowPHPError'];
     126
     127    $FileName = dirname(__FILE__).'/../Application/Locale/'.$this->Config['System']['Locale'].'.php';
     128    if(file_exists($FileName)) include($FileName);
     129      else {
     130        $this->Output->Show($this->Output->SystemMessage('Translation file "'.$FileName.'" not found.'));
     131        exit;
     132      }
     133    $this->Translation = $Translation;
     134
     135    if(isset($_SERVER['REMOTE_ADDR'])) session_start();
     136    $this->Database = new Database($this->Config['Database']['Host'],
     137    $this->Config['Database']['User'], $this->Config['Database']['Password'],
     138    $this->Config['Database']['Database']);
     139    $this->Database->ShowSQLQuery = $this->Config['System']['ShowSQLQuery'];
     140    $this->Database->ShowSQLError = $this->Config['System']['ShowSQLError'];
     141    $this->Database->Prefix = $this->Config['Database']['Prefix'];
     142    $this->Database->charset($this->Config['Database']['Charset']);
     143     
     144    $this->HTTP = new HTTP();
     145    $this->HTML = new HTML($this);
     146    $this->Type = new Type($this);
     147    $this->AddModule('Permission');
     148  }
     149
     150  function Run()
     151  {
     152    $this->TimeStart = $this->GetMicrotime();
     153   
     154    // SQL injection hack protection
     155    foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
     156    foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
     157
     158    if(array_key_exists('M', $_GET)) $Module = $_GET['M'];
     159      else $Module = $this->DefaultModule;
     160
     161    $ControllerName = $Module.'Controller';
     162    $FileName = dirname(__FILE__).'/../Application/Controller/'.$Module.'.php';
     163    if(!file_exists($FileName))
     164    {
     165      $this->Output->Show($this->Output->SystemMessage('Soubor '.$FileName.' nenalezen.'));
     166      exit;
     167    } else include($FileName);
     168    if(class_exists($ControllerName))
     169    {
     170      $Controller = new $ControllerName($this);
     171      $this->Output->Show($Controller->Process());
     172    } else $this->Output->Show($this->Output->SystemMessage('Modul "'.$ControllerName.'" nenalezen.'));
     173  }
    81174
    82175  function ModulePresent($Name)
     
    85178  }
    86179
    87   function AddModule($Module)
    88   {
    89     global $Database;
    90 
    91     //echo('Přidávám modul '.get_class($Module).'<br />');
    92     $this->Modules[get_class($Module)] = $Module;
    93   }
    94  
    95   function AddEmailToQueue($Address, $Subject, $Content, $Headers = '')
    96   {
    97     $this->Database->insert('EmailQueue', array('Address' => $Address, 'Subject' => $Subject, 'Content' => $Content, 'Time' => 'NOW()', 'Headers' => $Headers));
    98   }
    99  
    100   function MailUTF8($To, $Subject = '(No subject)', $Message = '', $Header = '')
    101   {
    102     $Header = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n".$Header;
    103     mail($To, '=?UTF-8?B?'.base64_encode($Subject).'?=', $Message, $Header);
    104     //echo('mail('.$To.', =?UTF-8?B?'.base64_encode($Subject).'?=, '.$Message.', '.$Header.')<br/>');
    105   }
    106 
    107   function ProcessEmailQueue()
    108   {
    109     $Output = '';
    110     $DbResult = $this->Database->select('EmailQueue', '*', 'Archive=0');
    111     while($DbRow = $DbResult->fetch_assoc())
    112     {     
    113       $this->MailUTF8($DbRow['Address'], $DbRow['Subject'], $DbRow['Content'], $DbRow['Headers']);
    114       //echo('mail('.$DbRow['Address'].', '.$DbRow['Subject'].', '.$DbRow['Content'].', FromUTF8('.$DbRow['Headers'].', \'iso2\'));');
    115       $this->Database->update('EmailQueue', 'Id='.$DbRow['Id'], array('Archive' => 1));
    116       $this->Modules['Log']->NewRecord('System', 'SendEmail', $DbRow['Id']);
    117       $Output .= 'To: '.$DbRow['Address'].'  Subject: '.$DbRow['Subject'].'<br />';
    118     }   
    119     return($Output);
     180  function AddModule($Name)
     181  {
     182    $this->Modules[$Name] = new $Name($this);
    120183  }
    121184 
     
    172235  {
    173236    set_error_handler('ErrorHandler');
    174     if($Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout))
     237    $Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout);
     238    if($Socket)
    175239    {
    176240      fclose($Socket);
     
    180244    return($Result);
    181245  }
    182  
     246
     247  function Translate($Text)
     248  {
     249    if(array_key_exists($Text, $this->Translation)) return($this->Translation[$Text]);
     250      else return('#'.$Text);
     251  }
     252
     253  function MakeLink($Module, $Action, $Parameters = array())
     254  {
     255    $Parameters = $this->HTTP->SetQueryStringArray($Parameters);
     256    if($Parameters != '') $Parameters = '&amp;'.$Parameters;
     257    return('?M='.$Module.'&amp;A='.$Action.$Parameters);
     258  }
     259
     260  function GetRemoteAddress()
     261  {
     262    if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
     263    else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
     264    else $IP = '0.0.0.0';
     265    return($IP);
     266  }
     267
    183268  function GetMicrotime()
    184269  {
    185270    list($Usec, $Sec) = explode(' ', microtime());
    186271    return((float)$Usec + (float)$Sec);
    187   } 
    188 
    189   function Translate($Text)
    190   {
    191     global $Translation;
    192 
    193     if(array_key_exists($Text, $Translation)) return($Translation[$Text]);
    194       else return('#'.$Text);
     272  }
     273
     274  function RemoveDiacritic($Text)
     275  {
     276    return(str_replace(
     277      array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
     278      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'),
     279      $Text));
    195280  }
    196281}
Note: See TracChangeset for help on using the changeset viewer.