source: trunk/www/Base/System.php

Last change on this file was 95, checked in by chronos, 10 years ago
  • Upraveno: Soubory různých logických částí systému odděleny do aplikačních modulů.
  • Property svn:executable set to *
File size: 7.2 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Database.php');
4include_once(dirname(__FILE__).'/Form.php');
5include_once(dirname(__FILE__).'/Table.php');
6include_once(dirname(__FILE__).'/Error.php');
7include_once(dirname(__FILE__).'/AppModule.php');
8
9$PrefixMultipliers = array
10(
11 'Binary' => array
12 (
13 'BaseIndex' => 0,
14 'Definition' => array
15 (
16 array('', '', pow(2, 0)),
17 array('Ki', 'kibi', pow(2, 10)),
18 array('Mi', 'mebi', pow(2, 20)),
19 array('Gi', 'gibi', pow(2, 30)),
20 array('Ti', 'tebi', pow(2, 40)),
21 array('Pi', 'pebi', pow(2, 50)),
22 array('Ei', 'exbi', pow(2, 60)),
23 array('Zi', 'zebi', pow(2, 70)),
24 array('Yi', 'yobi', pow(2, 80)),
25 ),
26 ),
27 'Decimal' => array
28 (
29 'BaseIndex' => 8,
30 'Definition' => array
31 (
32 array('y', 'yocto', pow(10, -24)),
33 array('z', 'zepto', pow(10, -21)),
34 array('a', 'atto', pow(10, -18)),
35 array('f', 'femto', pow(10, -15)),
36 array('p', 'piko', pow(10, -12)),
37 array('n', 'nano', pow(10, -9)),
38 array('u', 'mikro', pow(10, -6)),
39 array('m', 'mili', pow(10, -3)),
40 array('', '', pow(10, 0)),
41 array('k', 'kilo', pow(10, 3)),
42 array('M', 'mega', pow(10, 6)),
43 array('G', 'giga', pow(10, 9)),
44 array('T', 'tera', pow(10, 12)),
45 array('P', 'peta', pow(10, 15)),
46 array('E', 'exa', pow(10, 18)),
47 array('Z', 'zetta', pow(10, 21)),
48 array('Y', 'yotta', pow(10, 24)),
49 ),
50 ),
51 'Time' => array
52 (
53 'BaseIndex' => 8,
54 'Definition' => array
55 (
56 array('ys', 'yoctosekunda', pow(10, -24)),
57 array('zs', 'zeptosekunda', pow(10, -21)),
58 array('as', 'attosekunda', pow(10, -18)),
59 array('fs', 'femtosekunda', pow(10, -15)),
60 array('ps', 'pikosekunda', pow(10, -12)),
61 array('ns', 'nanosekunda', pow(10, -9)),
62 array('us', 'mikrosekunda', pow(10, -6)),
63 array('ms', 'milisekunda', pow(10, -3)),
64 array('s', 'sekund', 1),
65 array('minut', 'minuta', 60),
66 array('hodin', 'hodina', 60 * 60) ,
67 array('dnů', 'den', 24 * 60 * 60),
68 array('týdnů', 'týden', 7 * 24 * 60 * 60),
69 array('měsíců', 'měsíc', 30 * 24 * 60 * 60),
70 array('roků', 'rok', 364 * 24 * 60 * 60),
71 array('desitiletí', 'desetiletí', 10 * 364 * 24 * 60 * 60),
72 array('staletí', 'staletí', 100 * 364 * 24 * 60 * 60),
73 array('tisíciletí', 'tisiciletí', 10000 * 364 * 24 * 60 * 60),
74 ),
75 ),
76);
77
78function ErrorHandler()
79{
80}
81
82class System
83{
84 var $Modules = array();
85 var $Database;
86 var $Config;
87 var $TimeStart;
88 var $Translation;
89
90 function __construct()
91 {
92 global $Config;
93
94 // Change current directory to script location
95 $BaseDir = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));
96 if($BaseDir != '') chdir($BaseDir);
97
98 if(array_key_exists('argv', $_SERVER))
99 $_GET = array_merge($_GET, ParseCommandLineArgs($_SERVER['argv']));
100
101 // SQL injection hack protection
102 foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
103 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
104
105 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
106
107 $FileName = dirname(__FILE__).'/../Application/Config/Config.php';
108 if(file_exists($FileName)) include_once($FileName);
109 else die('Nenalezen soubor '.$FileName.'. Vytvořte jej kopií ze souboru ConfigSample.php umístěném ve stejné složce.');
110 $this->Config = $Config;
111
112 $FileName = dirname(__FILE__).'/../Application/Localization/'.$Config['Web']['Locale'].'.php';
113 if(file_exists($FileName)) include_once($FileName);
114 else die('Nenalezen soubor '.$FileName.'. Buď máte špatně nastaven jazyk nebo vám chybí jazykový soubor.');
115 $this->Translation = $Translation;
116
117 $this->TimeStart = $this->GetMicrotime();
118 $this->Database = new Database($this->Config['Database']['Host'], $this->Config['Database']['User'], $this->Config['Database']['Password'], $this->Config['Database']['Database']);
119 $this->Database->Prefix = $this->Config['Database']['Prefix'];
120 $this->Database->charset($this->Config['Database']['Charset']);
121
122 $this->ModuleManager = new AppModuleManager($this);
123 }
124
125 function Run()
126 {
127 if(array_key_exists('Module', $_GET)) $Module = $_GET['Module'];
128 else $Module = 'HomePage';
129
130 $ControllerName = $Module.'Controller';
131 $FileName = dirname(__FILE__).'/../Module/'.$Module.'/Controller.php';
132 if(!file_exists($FileName))
133 echo('Soubor '.$FileName.' nenalezen.');
134 else include($FileName);
135 if(class_exists($ControllerName))
136 {
137 $Controller = new $ControllerName($this);
138 echo($Controller->Process());
139 } else echo('Modul '.$ControllerName.' nenalezen.');
140 }
141
142 function ModulePresent($Name)
143 {
144 return(array_key_exists($Name, $this->Modules));
145 }
146
147 function AddModule($Name)
148 {
149 $this->Modules[$Name] = new $Name($this);
150 }
151
152 function HumanDate($Time)
153 {
154 return(date('j.n.Y', $Time));
155 }
156
157 function TruncateDigits($Value, $Digits = 4)
158 {
159 for($II = 2; $II > -6; $II--)
160 {
161 if($Value >= pow(10, $II))
162 {
163 if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits;
164 $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);
165 break;
166 }
167 }
168 return($Value);
169 }
170
171 function AddPrefixMultipliers($Value, $Unit, $Digits = 4, $PrefixType = 'Decimal')
172 {
173 global $PrefixMultipliers;
174
175 $Negative = ($Value < 0);
176 $Value = abs($Value);
177 if(($Unit == '') and ($PrefixType != 'Time'))
178 return($this->TruncateDigits($Value, $Digits));
179
180 $I = $PrefixMultipliers[$PrefixType]['BaseIndex'];
181 if($Value == 0) return($Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit);
182
183 if($Value > 1)
184 {
185 while((($I + 1) <= count($PrefixMultipliers[$PrefixType]['Definition'])) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I + 1][2]) > 1))
186 $I = $I + 1;
187 } else
188 if($Value < 1)
189 {
190 while((($I - 1) >= 0) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2]) < 1))
191 $I = $I - 1;
192 }
193 $Value = $Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2];
194
195 // Truncate digits count
196 $Value = $this->TruncateDigits($Value, $Digits);
197 if($Negative) $Value = -$Value;
198 return($Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit);
199 }
200
201 function NetworkPortState($Address, $Port, $Timeout = 1)
202 {
203 set_error_handler('ErrorHandler');
204 if($Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout))
205 {
206 fclose($Socket);
207 $Result = true;
208 } else $Result = false;
209 restore_error_handler();
210 return($Result);
211 }
212
213 function GetMicrotime()
214 {
215 list($Usec, $Sec) = explode(' ', microtime());
216 return((float)$Usec + (float)$Sec);
217 }
218
219 function Translate($Text)
220 {
221 if(array_key_exists($Text, $this->Translation)) return($this->Translation[$Text]);
222 else return('#'.$Text);
223 }
224
225 function GetRemoteAddress()
226 {
227 if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
228 else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
229 else $IP = '0.0.0.0';
230 return($IP);
231 }
232}
Note: See TracBrowser for help on using the repository browser.