source: trunk/Application/Core.php@ 914

Last change on this file since 914 was 912, checked in by chronos, 4 years ago
  • Modified: Updated Common package.
File size: 7.7 KB
Line 
1<?php
2
3$ConfigFileName = dirname(__FILE__).'/../Config/Config.php';
4if (file_exists($ConfigFileName)) include_once($ConfigFileName);
5
6include_once(dirname(__FILE__).'/Version.php');
7include_once(dirname(__FILE__).'/../Common/Global.php');
8include_once(dirname(__FILE__).'/FormClasses.php');
9include_once(dirname(__FILE__).'/FullInstall.php');
10include_once(dirname(__FILE__).'/UpdateTrace.php');
11include_once(dirname(__FILE__).'/BaseView.php');
12include_once(dirname(__FILE__).'/DefaultConfig.php');
13
14class Core extends System
15{
16 public Type $Type;
17 public array $Bars;
18 public FormManager $FormManager;
19 public array $Config;
20 public Config $ConfigManager;
21 public array $PathItems;
22 public string $RootURLFolder;
23 public bool $ShowPage;
24 public array $PageHeaders;
25 public BaseView $BaseView;
26 public LocaleManager $LocaleManager;
27 public array $LinkLocaleExceptions;
28
29 function __construct()
30 {
31 parent::__construct();
32 $this->Config = array();
33 $this->ModuleManager->FileName = dirname(__FILE__).'/../Config/ModulesConfig.php';
34 $this->FormManager = new FormManager($this->Database);
35 $this->ShowPage = true;
36 $this->ConfigManager = new Config();
37 $this->RootURLFolder = $_SERVER['SCRIPT_NAME'];
38 if (substr($this->RootURLFolder, -10, 10) == '/index.php')
39 $this->RootURLFolder = substr($this->RootURLFolder, 0, -10);
40 $this->PageHeaders = array();
41 $this->LinkLocaleExceptions = array();
42 }
43
44 function SearchPage(array $PathItems, array $Pages): ?string
45 {
46 if (count($PathItems) > 0) $PathItem = $PathItems[0];
47 else $PathItem = '';
48 if (array_key_exists($PathItem, $Pages))
49 {
50 if (is_array($Pages[$PathItem]))
51 {
52 array_shift($PathItems);
53 return $this->SearchPage($PathItems, $Pages[$PathItem]);
54 } else return $Pages[$PathItem];
55 } else return '';
56 }
57
58 function ShowPage(): void
59 {
60 $this->BaseView = new BaseView($this);
61
62 /* @var $Page Page */
63 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
64 if ($ClassName != '')
65 {
66 $Page = new $ClassName($this);
67 } else
68 {
69 $Page = new PageMissing($this);
70 }
71 echo($this->BaseView->GetOutput($Page));
72 }
73
74 function ModulePresent(string $Name): bool
75 {
76 return array_key_exists($Name, $this->ModuleManager->Modules);
77 }
78
79 function HumanDate(int $Time): string
80 {
81 return date('j.n.Y', $Time);
82 }
83
84 function Link(string $Target): string
85 {
86 return $this->RootURLFolder.$Target;
87 }
88
89 function RunCommon(): void
90 {
91 global $Database, $ScriptTimeStart, $ConfigFileName, $Config, $GlobalLocaleManager;
92
93 date_default_timezone_set('Europe/Prague');
94 mb_internal_encoding("UTF-8");
95 $ScriptTimeStart = GetMicrotime();
96
97 // SQL injection hack protection
98 foreach ($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
99 foreach ($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
100
101 if (isset($_SERVER['REMOTE_ADDR'])) session_start();
102
103 $ConfigFileName = dirname(dirname(__FILE__)).'/config.php';
104 if (file_exists($ConfigFileName))
105 $this->ConfigManager->LoadFromFile($ConfigFileName);
106 //$this->Config = $this->ConfigManager->GetAsArray();
107 $this->Config = &$Config;
108
109 try
110 {
111 $this->Database->Connect($this->Config['Database']['Host'], $this->Config['Database']['User'],
112 $this->Config['Database']['Password'], $this->Config['Database']['Database']);
113 $this->Database->Prefix = $this->Config['Database']['Prefix'];
114 $this->Database->charset($this->Config['Database']['Charset']);
115 $this->Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
116 $this->Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
117 if (isset($this->Config['Web']['LogSQLQuery']))
118 $this->Database->LogSQLQuery = $this->Config['Web']['LogSQLQuery'];
119 } catch (Exception $E)
120 {
121 //$Output .= 'Nelze se připojit k databázi.';
122 }
123
124 $this->LocaleManager = new LocaleManager($this);
125 $this->LocaleManager->Dir = dirname(dirname(__FILE__)).'/locale';
126 $this->LocaleManager->Available = array(
127 'cs' => array('Code' => 'cs', 'Title' => 'Česky'),
128 'en' => array('Code' => 'en', 'Title' => 'English'),
129 );
130 $GlobalLocaleManager = $this->LocaleManager;
131 $this->LinkLocaleExceptions = array('images', 'style', 'files');
132
133 if (isset($this->Config['Web']['RootFolder']))
134 $this->RootURLFolder = $this->Config['Web']['RootFolder'];
135 $this->FormManager->Root = $this->RootURLFolder;
136
137 $this->RegisterPageBar('Top');
138
139 $Database = $this->Database;
140 RegisterFormClasses($this->FormManager);
141
142 $this->StartModules();
143 }
144
145 function StartModules(): void
146 {
147 $ModuleSetup = $this->ModuleManager->LoadModule(dirname(__FILE__).'/../Packages/Common/Modules/Setup.php');
148 $ModuleSetup->Install();
149 $ModuleSetup->Start();
150 $this->ModuleManager->LoadModules();
151 $this->ModuleManager->LoadModule(dirname(__FILE__).'/../Packages/Common/Modules/ModuleManager.php');
152 if (file_exists($this->ModuleManager->FileName)) $this->ModuleManager->LoadState();
153 if (ModuleSetup::Cast($ModuleSetup)->CheckState())
154 {
155 $this->ModuleManager->StartAll(array(ModuleCondition::Enabled));
156 }
157 }
158
159 function Run(): void
160 {
161 $this->RunCommon();
162 if ($this->ShowPage)
163 {
164 $this->PathItems = ProcessURL();
165
166 // Detect interface locale
167 if (isset($this->Config['Web']['Locale']))
168 $this->LocaleManager->DefaultLangCode = $this->Config['Web']['Locale'];
169 $this->LocaleManager->LangCode = $this->LocaleManager->DefaultLangCode;
170 if (count($this->PathItems) > 0)
171 {
172 $NewLangCode = $this->PathItems[0];
173 if (array_key_exists($NewLangCode, $this->LocaleManager->Available))
174 {
175 array_shift($this->PathItems);
176 $this->LocaleManager->LangCode = $NewLangCode;
177 }
178 }
179 if (array_key_exists($this->LocaleManager->LangCode, $this->LocaleManager->Available))
180 $this->LocaleManager->LoadLocale($this->LocaleManager->LangCode);
181
182 $this->ShowPage();
183 }
184 }
185
186 function RunCommandLine(): void
187 {
188 global $argv;
189
190 $this->RunCommon();
191 if (count($argv) > 1)
192 {
193 if (array_key_exists($argv[1], $this->CommandLine))
194 {
195 $Command = $this->CommandLine[$argv[1]];
196 $Output = call_user_func($Command->Callback, $argv);
197 } else $Output = 'Command "'.$argv[1].'" not supported.'."\n";
198 } else $Output = 'No command was given as parameter. Use "list" to show available commands.'."\n";
199 echo($Output);
200 }
201
202 function RegisterPageBar(string $Name): void
203 {
204 $this->Bars[$Name] = array();
205 }
206
207 function UnregisterPageBar(string $Name): void
208 {
209 unset($this->Bars[$Name]);
210 }
211
212 function RegisterPageBarItem(string $BarName, string $ItemName, callable $Callback): void
213 {
214 $this->Bars[$BarName][$ItemName] = $Callback;
215 }
216
217 function UnregisterPageBarItem(string $BarName, string $ItemName): void
218 {
219 unset($this->Bars[$BarName][$ItemName]);
220 }
221
222 function RegisterPageHeader(string $Name, callable $Callback): void
223 {
224 $this->PageHeaders[$Name] = $Callback;
225 }
226
227 function UnregisterPageHeader(string $Name): void
228 {
229 unset($this->PageHeaders[$Name]);
230 }
231
232 static function Cast(System $System): Core
233 {
234 if ($System instanceof Core)
235 {
236 return $System;
237 }
238 throw new Exception('Expected Core type but '.gettype($System));
239 }
240}
241
242class PageMissing extends Page
243{
244 function __construct(System $System)
245 {
246 parent::__construct($System);
247 $this->Title = 'Stránka nenalezena';
248 $this->ParentClass = 'PagePortal';
249 }
250
251 function Show(): string
252 {
253 Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
254 return '<h3 align="center">Požadovaná stránka neexistuje.</h3>';
255 }
256}
Note: See TracBrowser for help on using the repository browser.