source: trunk/Application/Core.php

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