1 | <?php
|
---|
2 |
|
---|
3 | class Core extends System
|
---|
4 | {
|
---|
5 | public array $Config;
|
---|
6 | public array $PathItems;
|
---|
7 | public array $Menu;
|
---|
8 | public bool $DoNotShowPage;
|
---|
9 | public array $OnPageNotFound;
|
---|
10 | public LocaleManager $LocaleManager;
|
---|
11 | public array $Bars;
|
---|
12 | public array $PageHeaders;
|
---|
13 | public string $BaseURL;
|
---|
14 | public array $RSSChannels;
|
---|
15 | public BaseView $BaseView;
|
---|
16 | public array $LinkLocaleExceptions;
|
---|
17 |
|
---|
18 | function __construct()
|
---|
19 | {
|
---|
20 | parent::__construct();
|
---|
21 | $this->Config = array();
|
---|
22 | $this->Menu = array();
|
---|
23 | $this->RSSChannels = array();
|
---|
24 | $this->DoNotShowPage = false;
|
---|
25 | $this->OnPageNotFound = array();
|
---|
26 | $this->Pages = array();
|
---|
27 | $this->Bars = array();
|
---|
28 | $this->PageHeaders = array();
|
---|
29 | $this->BaseURL = '';
|
---|
30 | }
|
---|
31 |
|
---|
32 | function Init()
|
---|
33 | {
|
---|
34 | global $GlobalLocaleManager, $Config;
|
---|
35 |
|
---|
36 | $this->Config = $Config;
|
---|
37 | if (isset($this->Config['Web']['Timezone']))
|
---|
38 | date_default_timezone_set($this->Config['Web']['Timezone']);
|
---|
39 | mb_internal_encoding("UTF-8");
|
---|
40 |
|
---|
41 | if (isset($this->Config['Database']))
|
---|
42 | {
|
---|
43 | $this->Database->Connect($this->Config['Database']['Host'],
|
---|
44 | $this->Config['Database']['User'], $this->Config['Database']['Password'],
|
---|
45 | $this->Config['Database']['Database']);
|
---|
46 | $this->Database->charset($this->Config['Database']['Charset']);
|
---|
47 | $this->Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
|
---|
48 | $this->Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
|
---|
49 | $this->Database->LogSQLQuery = $this->Config['Web']['LogSQLQuery'];
|
---|
50 | }
|
---|
51 |
|
---|
52 | $this->LocaleManager = new LocaleManager($this);
|
---|
53 | $this->LocaleManager->Dir = dirname(dirname(__FILE__)).'/locale';
|
---|
54 | $this->LocaleManager->Available = array(
|
---|
55 | 'cs' => array('Code' => 'cs', 'Title' => 'Česky'),
|
---|
56 | 'en' => array('Code' => 'en', 'Title' => 'English'),
|
---|
57 | );
|
---|
58 | $GlobalLocaleManager = $this->LocaleManager;
|
---|
59 | $this->LinkLocaleExceptions = array('images', 'style', 'tmp', 'files', 'aowow',
|
---|
60 | 'banners'
|
---|
61 | );
|
---|
62 |
|
---|
63 | if (GetRemoteAddress() != '')
|
---|
64 | {
|
---|
65 | $this->BaseURL = $_SERVER["CONTEXT_PREFIX"];
|
---|
66 | if (substr($this->BaseURL, -1, 1) == '/') $this->BaseURL = substr($this->BaseURL, 0, -1);
|
---|
67 | }
|
---|
68 | $this->PathItems = ProcessURL();
|
---|
69 |
|
---|
70 | // Detect interface locale
|
---|
71 | if (isset($this->Config['Web']['Locale']))
|
---|
72 | $this->LocaleManager->DefaultLangCode = $this->Config['Web']['Locale'];
|
---|
73 | $this->LocaleManager->LangCode = $this->LocaleManager->DefaultLangCode;
|
---|
74 | if (count($this->PathItems) > 0)
|
---|
75 | {
|
---|
76 | $NewLangCode = $this->PathItems[0];
|
---|
77 | if (array_key_exists($NewLangCode, $this->LocaleManager->Available)) {
|
---|
78 | array_shift($this->PathItems);
|
---|
79 | $this->LocaleManager->LangCode = $NewLangCode;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | if (array_key_exists($this->LocaleManager->LangCode, $this->LocaleManager->Available))
|
---|
83 | $this->LocaleManager->LoadLocale($this->LocaleManager->LangCode);
|
---|
84 |
|
---|
85 | $this->Menu = array
|
---|
86 | (
|
---|
87 | /* array(
|
---|
88 | 'Title' => T('Files'),
|
---|
89 | 'Hint' => 'Stahování různých pomocných souborů a programů',
|
---|
90 | 'Link' => $this->Link('/download.php'),
|
---|
91 | 'Permission' => LICENCE_ANONYMOUS,
|
---|
92 | 'Icon' => '',
|
---|
93 | ),
|
---|
94 |
|
---|
95 | array(
|
---|
96 | 'Title' => T('IRC chat'),
|
---|
97 | 'Hint' => 'IRC chat pro překladatele',
|
---|
98 | 'Link' => 'http://embed.mibbit.com/?server=game.zdechov.net%3A6667&channel=%23wowpreklad&forcePrompt=true&charset=utf-8',
|
---|
99 | 'Permission' => LICENCE_ANONYMOUS,
|
---|
100 | 'Icon' => '',
|
---|
101 | ),
|
---|
102 | */
|
---|
103 | );
|
---|
104 | }
|
---|
105 |
|
---|
106 | static function Cast(System $System): Core
|
---|
107 | {
|
---|
108 | if ($System instanceof Core)
|
---|
109 | {
|
---|
110 | return $System;
|
---|
111 | }
|
---|
112 | throw new Exception('Expected Core type but '.gettype($System));
|
---|
113 | }
|
---|
114 |
|
---|
115 | function Run(): void
|
---|
116 | {
|
---|
117 | global $ScriptStartTime, $StopAfterUpdateManager,
|
---|
118 | $UpdateManager;
|
---|
119 |
|
---|
120 | $ScriptStartTime = GetMicrotime();
|
---|
121 | //if (GetRemoteAddress() != '')
|
---|
122 | session_start();
|
---|
123 |
|
---|
124 | // SQL injection hack protection
|
---|
125 | foreach ($_POST as $Index => $Item)
|
---|
126 | {
|
---|
127 | if (is_array($_POST[$Index]))
|
---|
128 | foreach ($_POST[$Index] as $Index2 => $Item2) $_POST[$Index][$Index2] = addslashes($Item2);
|
---|
129 | else $_POST[$Index] = addslashes($_POST[$Index]);
|
---|
130 | }
|
---|
131 | foreach ($_GET as $Index => $Item) $_GET[$Index] = addslashes($_GET[$Index]);
|
---|
132 |
|
---|
133 | $this->RegisterPageBar('Top');
|
---|
134 | $this->RegisterPageBar('Left');
|
---|
135 | $this->RegisterPageBar('Right');
|
---|
136 | $this->Init();
|
---|
137 |
|
---|
138 | $this->StartModules();
|
---|
139 |
|
---|
140 | $this->BaseView = new BaseView($this);
|
---|
141 | if ($this->DoNotShowPage == false)
|
---|
142 | {
|
---|
143 | $this->ShowPage();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | function StartModules(): void
|
---|
148 | {
|
---|
149 | $ModuleSetup = $this->ModuleManager->LoadModule(dirname(__FILE__).'/../Packages/Common/Modules/Setup.php');
|
---|
150 | ModuleSetup::Cast($ModuleSetup)->UpdateManager->VersionTable = 'DbVersion';
|
---|
151 | $ModuleSetup->Install();
|
---|
152 | $ModuleSetup->Start();
|
---|
153 | $this->ModuleManager->LoadModules();
|
---|
154 | $this->ModuleManager->LoadModule(dirname(__FILE__).'/../Packages/Common/Modules/ModuleManager.php');
|
---|
155 | if (file_exists($this->ModuleManager->FileName)) $this->ModuleManager->LoadState();
|
---|
156 | if (ModuleSetup::Cast($ModuleSetup)->CheckState())
|
---|
157 | {
|
---|
158 | $this->ModuleManager->StartAll(array(ModuleCondition::Enabled));
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | function GetMicrotime()
|
---|
163 | {
|
---|
164 | list($Usec, $Sec) = explode(' ', microtime());
|
---|
165 | return (float)$Usec + (float)$Sec;
|
---|
166 | }
|
---|
167 |
|
---|
168 | function Link(string $Target): string
|
---|
169 | {
|
---|
170 | if (substr($Target, 0, strlen($this->BaseURL)) == $this->BaseURL)
|
---|
171 | $Remaining = substr($Target, strlen($this->BaseURL));
|
---|
172 | else $Remaining = $Target;
|
---|
173 | $TargetParts = explode('/', $Remaining);
|
---|
174 | if ((count($TargetParts) > 0) and ($TargetParts[0] == ''))
|
---|
175 | array_splice($TargetParts, 0, 1);
|
---|
176 | if (count($TargetParts) > 0)
|
---|
177 | {
|
---|
178 | if (in_array($TargetParts[0], $this->LinkLocaleExceptions))
|
---|
179 | {
|
---|
180 | $Result = $this->BaseURL.$Target;
|
---|
181 | } else $Result = $this->LinkLocale($Target);
|
---|
182 | } else $Result = $this->LinkLocale($Target);
|
---|
183 | return $Result;
|
---|
184 | }
|
---|
185 |
|
---|
186 | function TranslateURL($URL, $Locale)
|
---|
187 | {
|
---|
188 | // Try translate URL directory parts from current locale to target locale
|
---|
189 | $Remaining = $URL;
|
---|
190 | $RemainingParts = explode('?', $Remaining);
|
---|
191 | $Directory = $RemainingParts[0];
|
---|
192 | if (count($RemainingParts) > 1)
|
---|
193 | {
|
---|
194 | $Params = $RemainingParts[1];
|
---|
195 | } else {
|
---|
196 | $Params = '';
|
---|
197 | }
|
---|
198 | $TargetLocaleManager = new LocaleManager($this);
|
---|
199 | $TargetLocaleManager->Dir = $this->LocaleManager->Dir;
|
---|
200 | $TargetLocaleManager->Available = $this->LocaleManager->Available;
|
---|
201 | $TargetLocaleManager->LoadLocale($Locale);
|
---|
202 |
|
---|
203 | $DirectoryParts = explode('/', $Directory);
|
---|
204 | foreach ($DirectoryParts as $Index => $Item)
|
---|
205 | {
|
---|
206 | $NewText = $TargetLocaleManager->CurrentLocale->Texts->Translate($Item, 'URL');
|
---|
207 | $DirectoryParts[$Index] = $NewText;
|
---|
208 | }
|
---|
209 | $Directory = implode('/', $DirectoryParts);
|
---|
210 | $Remaining = $Directory;
|
---|
211 | if ($Params != '') $Remaining .= '?'.$Params;
|
---|
212 |
|
---|
213 | return $Remaining;
|
---|
214 | }
|
---|
215 |
|
---|
216 | function TranslateReverseURL($URL, $Locale)
|
---|
217 | {
|
---|
218 | // Try translate URL directory parts from current locale to target locale
|
---|
219 | $Remaining = $URL;
|
---|
220 | $RemainingParts = explode('?', $Remaining);
|
---|
221 | $Directory = $RemainingParts[0];
|
---|
222 | if (count($RemainingParts) > 1)
|
---|
223 | {
|
---|
224 | $Params = $RemainingParts[1];
|
---|
225 | } else {
|
---|
226 | $Params = '';
|
---|
227 | }
|
---|
228 | $TargetLocaleManager = new LocaleManager($this);
|
---|
229 | $TargetLocaleManager->Dir = $this->LocaleManager->Dir;
|
---|
230 | $TargetLocaleManager->Available = $this->LocaleManager->Available;
|
---|
231 | $TargetLocaleManager->LoadLocale($Locale);
|
---|
232 |
|
---|
233 | $DirectoryParts = explode('/', $Directory);
|
---|
234 | foreach ($DirectoryParts as $Index => $Item)
|
---|
235 | {
|
---|
236 | $NewText = $TargetLocaleManager->CurrentLocale->Texts->TranslateReverse($Item, 'URL');
|
---|
237 | $DirectoryParts[$Index] = $NewText;
|
---|
238 | }
|
---|
239 | $Directory = implode('/', $DirectoryParts);
|
---|
240 | $Remaining = $Directory;
|
---|
241 | if ($Params != '') $Remaining .= '?'.$Params;
|
---|
242 |
|
---|
243 | return $Remaining;
|
---|
244 | }
|
---|
245 |
|
---|
246 | function LinkLocale($Target, $Locale = '')
|
---|
247 | {
|
---|
248 | if ($Locale == '') $Locale = $this->LocaleManager->LangCode;
|
---|
249 |
|
---|
250 | $Target = $this->TranslateURL($Target, $Locale);
|
---|
251 |
|
---|
252 | if ($Locale == $this->LocaleManager->DefaultLangCode)
|
---|
253 | return $this->BaseURL.$Target;
|
---|
254 | return $this->BaseURL.'/'.$Locale.$Target;
|
---|
255 | }
|
---|
256 |
|
---|
257 | function RegisterMenuItem($MenuItem, $Pos = NULL)
|
---|
258 | {
|
---|
259 | if (is_null($Pos)) $this->Menu[] = $MenuItem;
|
---|
260 | else {
|
---|
261 | array_splice($this->Menu, $Pos, 0, array($MenuItem));
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | function SearchPage($PathItems, $Pages)
|
---|
266 | {
|
---|
267 | if (count($PathItems) == 0) $PathItems = array('');
|
---|
268 | $PathItem = $PathItems[0];
|
---|
269 | $PathItem = $this->LocaleManager->CurrentLocale->Texts->TranslateReverse($PathItem, 'URL');
|
---|
270 |
|
---|
271 | if (array_key_exists($PathItem, $Pages))
|
---|
272 | {
|
---|
273 | if (is_array($Pages[$PathItem]))
|
---|
274 | {
|
---|
275 | array_shift($PathItems);
|
---|
276 | return $this->SearchPage($PathItems, $Pages[$PathItem]);
|
---|
277 | } else
|
---|
278 | {
|
---|
279 | if (count($PathItems) == 1) return $Pages[$PathItem];
|
---|
280 | else return ''; // Unexpected subpages
|
---|
281 | }
|
---|
282 | } else return '';
|
---|
283 | }
|
---|
284 |
|
---|
285 | function PageNotFound()
|
---|
286 | {
|
---|
287 | // Send correct HTTP status code to signal unknown page
|
---|
288 | if (array_key_exists('SERVER_PROTOCOL', $_SERVER))
|
---|
289 | Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
|
---|
290 | if (array_key_exists('HTTP_REFERER', $_SERVER)) $Referer = ' Referer: '.$_SERVER['HTTP_REFERER'];
|
---|
291 | else $Referer = '';
|
---|
292 | if (isset($this->ModuleManager->Modules['Log']))
|
---|
293 | $this->ModuleManager->Modules['Log']->WriteLog('Stránka "'.
|
---|
294 | implode('/', $this->PathItems).'" nenalezena'.$Referer, LOG_TYPE_PAGE_NOT_FOUND);
|
---|
295 | return ShowMessage(sprintf(T('Page "%s" not found'), implode('/', $this->PathItems)), MESSAGE_CRITICAL);
|
---|
296 | }
|
---|
297 |
|
---|
298 | function ShowPage()
|
---|
299 | {
|
---|
300 | $Output = '';
|
---|
301 |
|
---|
302 | /* @var $Page Page */
|
---|
303 | $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
|
---|
304 | if (($ClassName != '') and (class_exists($ClassName)))
|
---|
305 | {
|
---|
306 | $Page = new $ClassName($this);
|
---|
307 | $Output = $Page->GetOutput();
|
---|
308 | $this->BaseView->Title = $Page->Title;
|
---|
309 | if ($Page->RawPage == false) $Output = $this->BaseView->ShowPage($Output);
|
---|
310 | } else {
|
---|
311 | $Output2 = '';
|
---|
312 | if ((count($this->OnPageNotFound) == 2)
|
---|
313 | and method_exists($this->OnPageNotFound[0], $this->OnPageNotFound[1]))
|
---|
314 | $Output2 = call_user_func_array($this->OnPageNotFound, array());
|
---|
315 | if ($Output2 != '') $Output .= $this->BaseView->ShowPage($Output2);
|
---|
316 | else {
|
---|
317 | $Output = $this->PageNotFound();
|
---|
318 | $this->BaseView->Title = T('Page not found');
|
---|
319 | $Output = $this->BaseView->ShowPage($Output);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | echo($Output);
|
---|
323 | }
|
---|
324 |
|
---|
325 | function RegisterPageBar($Name)
|
---|
326 | {
|
---|
327 | $this->Bars[$Name] = array();
|
---|
328 | }
|
---|
329 |
|
---|
330 | function UnregisterPageBar($Name)
|
---|
331 | {
|
---|
332 | unset($this->Bars[$Name]);
|
---|
333 | }
|
---|
334 |
|
---|
335 | function RegisterPageBarItem($BarName, $ItemName, $Callback)
|
---|
336 | {
|
---|
337 | $this->Bars[$BarName][$ItemName] = $Callback;
|
---|
338 | }
|
---|
339 |
|
---|
340 | function RegisterPageHeader($Name, $Callback)
|
---|
341 | {
|
---|
342 | $this->PageHeaders[$Name] = $Callback;
|
---|
343 | }
|
---|
344 |
|
---|
345 | function HumanDate($Time)
|
---|
346 | {
|
---|
347 | return date('j.n.Y', $Time);
|
---|
348 | }
|
---|
349 | }
|
---|