source: trunk/Application/System.php@ 839

Last change on this file since 839 was 839, checked in by chronos, 9 years ago
  • Modified: Use TinyMCE editor for news content.
File size: 7.1 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__).'/View.php');
12include_once(dirname(__FILE__).'/DefaultConfig.php');
13
14class Core extends Application
15{
16 /** @var Type */
17 var $Type;
18 var $Pages;
19 var $Bars;
20 /** @var FormManager */
21 var $FormManager;
22 /** @var Config */
23 var $ConfigManager;
24 var $PathItems;
25 var $RootURLFolder;
26 var $ShowPage;
27 var $Setup;
28 var $CommandLine;
29 var $PageHeaders;
30 var $BaseView;
31
32 function __construct()
33 {
34 parent::__construct();
35 $this->Modules = array();
36 $this->Pages = array();
37 $this->ModuleManager->FileName = dirname(__FILE__).'/../Config/ModulesConfig.php';
38 $this->FormManager = new FormManager($this->Database);
39 $this->ShowPage = true;
40 $this->ConfigManager = new Config();
41 $this->RootURLFolder = $_SERVER['SCRIPT_NAME'];
42 if(substr($this->RootURLFolder, -10, 10) == '/index.php')
43 $this->RootURLFolder = substr($this->RootURLFolder, 0, -10);
44 $this->CommandLine = array();
45 $this->PageHeaders = array();
46 }
47
48 function RegisterPage($Path, $Handler)
49 {
50 if(is_array($Path))
51 {
52 $Page = &$this->Pages;
53 $LastKey = array_pop($Path);
54 foreach($Path as $PathItem)
55 {
56 $Page = &$Page[$PathItem];
57 }
58 if(!is_array($Page)) $Page = array('' => $Page);
59 $Page[$LastKey] = $Handler;
60 } else $this->Pages[$Path] = $Handler;
61 }
62
63 function UnregisterPage($Path)
64 {
65 unset($this->Pages[$Path]);
66 }
67
68 function SearchPage($PathItems, $Pages)
69 {
70 if(count($PathItems) > 0) $PathItem = $PathItems[0];
71 else $PathItem = '';
72 if(array_key_exists($PathItem, $Pages))
73 {
74 if(is_array($Pages[$PathItem]))
75 {
76 array_shift($PathItems);
77 return($this->SearchPage($PathItems, $Pages[$PathItem]));
78 } else return($Pages[$PathItem]);
79 } else return('');
80 }
81
82 function PageNotFound()
83 {
84 return('Page '.implode('/', $this->PathItems).' not found.');
85 }
86
87 function ShowPage()
88 {
89 $this->BaseView = new BaseView($this);
90
91 /* @var $Page Page */
92 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
93 if($ClassName != '')
94 {
95 $Page = new $ClassName($this);
96 } else {
97 $Page = new PageMissing($this);
98 }
99 echo($this->BaseView->GetOutput($Page));
100 }
101
102 function ModulePresent($Name)
103 {
104 return(array_key_exists($Name, $this->Modules));
105 }
106
107 function AddModule($Module)
108 {
109 $this->Modules[get_class($Module)] = $Module;
110 }
111
112 function HumanDate($Time)
113 {
114 return(date('j.n.Y', $Time));
115 }
116
117 function Link($Target)
118 {
119 return($this->RootURLFolder.$Target);
120 }
121
122 function ShowAction($Id)
123 {
124 $Output = '';
125 $DbResult = $this->Database->query('SELECT *, `ActionIcon`.`Name` AS `Icon` FROM `Action` '.
126 'LEFT JOIN `ActionIcon` ON `ActionIcon`.`Id` = `Action`.`Icon` '.
127 'WHERE (`Action`.`Id`='.$Id.')');
128 if($DbResult->num_rows > 0)
129 {
130 $Action = $DbResult->fetch_assoc();
131 if($Action['Icon'] == '') $Action['Icon'] = 'clear.png';
132 if(substr($Action['URL'], 0, 4) != 'http') $Action['URL'] = $this->Link($Action['URL']);
133 if(!defined('NEW_PERMISSION') or $this->User->CheckPermission('System', 'Read', 'Item', $Id))
134 $Output .= '<img alt="'.$Action['Title'].'" src="'.$this->Link('/images/favicons/'.$Action['Icon']).
135 '" width="16" height="16" /> <a href="'.$Action['URL'].'">'.$Action['Title'].'</a>';
136 }
137 return($Output);
138 }
139
140 function RunCommon()
141 {
142 global $Database, $ScriptTimeStart, $ConfigFileName, $Mail, $Type,
143 $DatabaseRevision, $Config;
144
145 date_default_timezone_set('Europe/Prague');
146 mb_internal_encoding("UTF-8");
147 $ScriptTimeStart = GetMicrotime();
148
149 // SQL injection hack protection
150 foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
151 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
152
153 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
154
155 $ConfigFileName = dirname(dirname(__FILE__)).'/config.php';
156 if(file_exists($ConfigFileName))
157 $this->ConfigManager->LoadFromFile($ConfigFileName);
158 //$this->Config = $this->ConfigManager->GetAsArray();
159 $this->Config = &$Config;
160
161 try {
162 $this->Database->Connect($this->Config['Database']['Host'], $this->Config['Database']['User'],
163 $this->Config['Database']['Password'], $this->Config['Database']['Database']);
164 $this->Database->Prefix = $this->Config['Database']['Prefix'];
165 $this->Database->charset($this->Config['Database']['Charset']);
166 $this->Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
167 $this->Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
168 if(isset($this->Config['Web']['LogSQLQuery']))
169 $this->Database->LogSQLQuery = $this->Config['Web']['LogSQLQuery'];
170 } catch (Exception $E) {
171 //$Output .= 'Nelze se připojit k databázi.';
172 }
173 if(isset($this->Config['Web']['RootFolder']))
174 $this->RootURLFolder = $this->Config['Web']['RootFolder'];
175 $this->FormManager->Root = $this->RootURLFolder;
176
177 $this->RegisterPageBar('Top');
178
179 $Database = $this->Database;
180 RegisterFormClasses($this->FormManager);
181
182 // Register and start existing modules
183 $this->Setup = new Setup($this);
184 $this->Setup->Start();
185 if($this->Setup->CheckState())
186 {
187 $this->ModuleManager->Start();
188 }
189 }
190
191 function Run()
192 {
193 $this->RunCommon();
194 if($this->ShowPage)
195 {
196 $this->PathItems = ProcessURL();
197 $this->ShowPage();
198 }
199 }
200
201 function RunCommandLine()
202 {
203 global $argv;
204
205 $this->RunCommon();
206 if(count($argv) > 1)
207 {
208 if(array_key_exists($argv[1], $this->CommandLine))
209 {
210 $Command = $this->CommandLine[$argv[1]];
211 $Output = call_user_func($Command['Callback'], $argv);
212 } else $Output = 'Command "'.$argv[1].'" not supported.'."\n";
213 } else $Output = 'No command was given as parameter'."\n";
214 echo($Output);
215 }
216
217 function RegisterCommandLine($Name, $Callback)
218 {
219 $this->CommandLine[$Name] = array('Name' => $Name, 'Callback' => $Callback);
220 }
221
222 function RegisterPageBar($Name)
223 {
224 $this->Bars[$Name] = array();
225 }
226
227 function UnregisterPageBar($Name)
228 {
229 unset($this->Bars[$Name]);
230 }
231
232 function RegisterPageBarItem($BarName, $ItemName, $Callback)
233 {
234 $this->Bars[$BarName][$ItemName] = $Callback;
235 }
236
237 function RegisterPageHeader($Name, $Callback)
238 {
239 $this->PageHeaders[$Name] = $Callback;
240 }
241}
242
243class PageMissing extends Page
244{
245 var $FullTitle = 'Stránka nenalezena';
246 var $ShortTitle = 'Stránka nenalezena';
247
248 function __construct($System)
249 {
250 parent::__construct($System);
251 $this->ParentClass = 'PagePortal';
252 }
253
254 function Show()
255 {
256 Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
257 return('<h3 align="center">Požadovaná stránka neexistuje.</h3>');
258 }
259}
Note: See TracBrowser for help on using the repository browser.