source: branches/mvc/Base/System.php

Last change on this file was 59, checked in by chronos, 10 years ago
  • Moved trunk as mvc branch.
  • Property svn:executable set to *
File size: 5.7 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Version.php');
4include_once(dirname(__FILE__).'/Database.php');
5include_once(dirname(__FILE__).'/Error.php');
6include_once(dirname(__FILE__).'/HTML/XHTML.php');
7include_once(dirname(__FILE__).'/HTML/HTML.php');
8include_once(dirname(__FILE__).'/HTTP.php');
9include_once(dirname(__FILE__).'/Types/Type.php');
10include_once(dirname(__FILE__).'/Output.php');
11include_once(dirname(__FILE__).'/Permission.php');
12include_once(dirname(__FILE__).'/PrefixMultiplier.php');
13include_once(dirname(__FILE__).'/Setup.php');
14
15$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
16
17function ErrorHandler()
18{
19}
20
21class System
22{
23 var $Model = array();
24 var $View = array();
25 var $Controller = array();
26 var $Config;
27 var $Database;
28 var $HTTP;
29 var $HTML;
30 var $Type;
31 var $TimeStart;
32 var $Translation;
33 var $Output;
34 var $DefaultModule;
35 var $Modules = array();
36
37 function __construct()
38 {
39 global $ShowPHPError;
40
41 $TimeStart = $this->GetMicrotime();
42
43 // Change base dir to index. Helpfull if script is executed from command line with related path.
44 chdir(dirname(__FILE__));
45
46 $this->Output = new Output($this);
47
48 $Config = array();
49
50 $FileName = dirname(__FILE__).'/Config/Config.php';
51 if(file_exists($FileName)) include($FileName);
52 else {
53 $this->Output->Show($this->Output->SystemMessage('Configuration file "'.$FileName.'" not found.'));
54 exit;
55 }
56
57 $FileName = dirname(__FILE__).'/../Application/Config/Config.php';
58 if(file_exists($FileName)) include($FileName);
59
60 $this->Config = $Config;
61
62 $ShowPHPError = $this->Config['System']['ShowPHPError'];
63
64 $Translation = array();
65 $FileName = dirname(__FILE__).'/../Application/Locale/'.$this->Config['System']['Locale'].'.php';
66 if(file_exists($FileName)) include($FileName);
67 else {
68 $this->Output->Show($this->Output->SystemMessage('Translation file "'.$FileName.'" not found.'));
69 exit;
70 }
71 $this->Translation = $Translation;
72
73 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
74 $this->Database = new Database();
75 $this->Database->Connect($this->Config['Database']['Host'],
76 $this->Config['Database']['User'], $this->Config['Database']['Password'],
77 $this->Config['Database']['Database']);
78 $this->Database->ShowSQLQuery = $this->Config['System']['ShowSQLQuery'];
79 $this->Database->ShowSQLError = $this->Config['System']['ShowSQLError'];
80 $this->Database->Prefix = $this->Config['Database']['Prefix'];
81 $this->Database->charset($this->Config['Database']['Charset']);
82
83 $this->HTTP = new HTTP();
84 $this->HTML = new HTML($this);
85 $this->Type = new Type($this);
86 $this->AddModule('Permission');
87 }
88
89 function Run()
90 {
91 $this->TimeStart = $this->GetMicrotime();
92
93 // SQL injection hack protection
94 foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
95 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
96
97 // Register and start existing modules
98 $this->Setup = new Setup($this);
99 $this->Setup->Start();
100
101 if(array_key_exists('M', $_GET)) $Module = $_GET['M'];
102 else $Module = $this->DefaultModule;
103
104 $ControllerName = $Module.'Controller';
105 if(!class_exists($ControllerName))
106 {
107 // Try to load controller class
108 $FileName = dirname(__FILE__).'/../Application/Controller/'.$Module.'.php';
109 if(!file_exists($FileName))
110 {
111 $this->Output->Show($this->Output->SystemMessage('Soubor '.$FileName.' nenalezen.'));
112 exit;
113 } else include($FileName);
114 }
115 if(class_exists($ControllerName))
116 {
117 $Controller = new $ControllerName($this);
118 $this->Output->Show($Controller->Process());
119 } else $this->Output->Show($this->Output->SystemMessage('Modul "'.$ControllerName.'" nenalezen.'));
120 }
121
122 function ModulePresent($Name)
123 {
124 return(array_key_exists($Name, $this->Modules));
125 }
126
127 function AddModule($Name)
128 {
129 $this->Modules[$Name] = new $Name($this);
130 }
131
132 function HumanDate($Time)
133 {
134 return(date('j.n.Y', $Time));
135 }
136
137 function NetworkPortState($Address, $Port, $Timeout = 1)
138 {
139 set_error_handler('ErrorHandler');
140 $Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout);
141 if($Socket)
142 {
143 fclose($Socket);
144 $Result = true;
145 } else $Result = false;
146 restore_error_handler();
147 return($Result);
148 }
149
150 function Translate($Text)
151 {
152 if(array_key_exists($Text, $this->Translation)) return($this->Translation[$Text]);
153 else return('#'.$Text);
154 }
155
156 function MakeLink($Module, $Action, $Parameters = array())
157 {
158 $Parameters = $this->HTTP->SetQueryStringArray($Parameters);
159 if($Parameters != '') $Parameters = '&amp;'.$Parameters;
160 return('?M='.$Module.'&amp;A='.$Action.$Parameters);
161 }
162
163 function GetRemoteAddress()
164 {
165 if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
166 else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
167 else $IP = '0.0.0.0';
168 return($IP);
169 }
170
171 function GetMicrotime()
172 {
173 list($Usec, $Sec) = explode(' ', microtime());
174 return((float)$Usec + (float)$Sec);
175 }
176
177 function RemoveDiacritic($Text)
178 {
179 return(str_replace(
180 array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
181 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'),
182 $Text));
183 }
184}
Note: See TracBrowser for help on using the repository browser.