source: trunk/includes/system.php@ 577

Last change on this file since 577 was 577, checked in by chronos, 11 years ago
  • Modified: Moved some code from global to system file. System class is now serving as main application class. Now old files which still use ShowPage function need system initialization with $InitSystem = true; before global.php inclusion.
  • Modified: Get rid of some global reference to $System, $Config and $User variables.
  • Modified: Search result functionality moved to application module from action.php file.
  • Property svn:executable set to *
File size: 17.3 KB
Line 
1<?php
2
3include_once('Database.php');
4include_once('Application.php');
5
6class System extends Application
7{
8 var $Database;
9 var $Config;
10 var $ModuleManager;
11 var $PathItems;
12 var $Menu;
13 var $RSSChannels;
14 var $DoNotShowPage;
15
16 function __construct()
17 {
18 $this->Config = array();
19 $this->Menu = array();
20 $this->RSSChannels = array();
21 $this->DoNotShowPage = false;
22 }
23
24 function Init()
25 {
26 global $Config;
27
28 $this->Config = $Config;
29 $this->Database = new Database();
30 $this->Database->Connect($this->Config['Database']['Host'],
31 $this->Config['Database']['User'], $this->Config['Database']['Password'],
32 $this->Config['Database']['Database']);
33 $this->Database->charset($this->Config['Database']['Charset']);
34 $this->Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
35 $this->Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
36 $this->Database->LogSQLQuery = $this->Config['Web']['LogSQLQuery'];
37 $this->ModuleManager = new AppModuleManager();
38
39 $this->Menu = array
40 (
41 array(
42 'Title' => 'Stav dokončení',
43 'Hint' => 'Stav dokončení překládů',
44 'Link' => $this->Link('/statistic.php'),
45 'Permission' => LICENCE_ANONYMOUS,
46 'Icon' => '',
47 ),
48 array(
49 'Title' => 'Soubory',
50 'Hint' => 'Stahování různých pomocných souborů a programů',
51 'Link' => $this->Link('/download.php'),
52 'Permission' => LICENCE_ANONYMOUS,
53 'Icon' => '',
54 ),
55 array(
56 'Title' => 'Pokyny',
57 'Hint' => 'Informace k překladu hry',
58 'Link' => $this->Link('/info.php'),
59 'Permission' => LICENCE_ANONYMOUS,
60 'Icon' => '',
61 ),
62 array(
63 'Title' => 'Zdroje dat',
64 'Hint' => 'Informace o překladových skupinách',
65 'Link' => $this->Link('/TranslationList.php?action=grouplist'),
66 'Permission' => LICENCE_ANONYMOUS,
67 'Icon' => '',
68 ),
69 array(
70 'Title' => 'Prezentace',
71 'Hint' => 'Prezentace a motivace překladu',
72 'Link' => $this->Link('/promotion.php'),
73 'Permission' => LICENCE_ANONYMOUS,
74 'Icon' => '',
75 ),
76 array(
77 'Title' => 'IRC chat',
78 'Hint' => 'IRC chat pro překladatele',
79 'Link' => 'http://embed.mibbit.com/?server=game.zdechov.net%3A6667&amp;channel=%23wowpreklad&amp;forcePrompt=true&amp;charset=utf-8',
80 'Permission' => LICENCE_ANONYMOUS,
81 'Icon' => '',
82 ),
83 array(
84 'Title' => 'Správa',
85 'Hint' => 'Volby pro správu',
86 'Link' => $this->Link('/admin/'),
87 'Permission' => LICENCE_ADMIN,
88 'Icon' => '',
89 ),
90 );
91 }
92
93 function Run()
94 {
95 global $System, $ScriptStartTime, $TranslationTree, $User, $StopAfterUpdateManager,
96 $UpdateManager, $Config, $DatabaseRevision;
97
98 $ScriptStartTime = GetMicrotime();
99
100 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
101
102 if(!isset($Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/install.php">zde</a>.');
103 date_default_timezone_set($Config['Web']['Timezone']);
104
105 $this->Init();
106
107 // Check database persistence structure
108 $UpdateManager = new UpdateManager();
109 $UpdateManager->Database = $this->Database;
110 $UpdateManager->Revision = $DatabaseRevision;
111 if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
112 if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
113
114 // SQL injection hack protection
115 foreach($_POST as $Index => $Item)
116 {
117 if(is_array($_POST[$Index]))
118 foreach($_POST[$Index] as $Index2 => $Item2) $_POST[$Index][$Index2] = addslashes($Item2);
119 else $_POST[$Index] = addslashes($_POST[$Index]);
120 }
121 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($_GET[$Index]);
122
123 // TODO: Global initialized variable should be removed
124 $TranslationTree = GetTranslationTree();
125
126 // Initialize application modules
127 $this->ModuleManager->RegisterModule(new ModuleError($System));
128 $this->ModuleManager->Modules['Error']->ShowError = $Config['Web']['ShowPHPError'];
129 $this->ModuleManager->RegisterModule(new ModuleLog($System));
130 $this->ModuleManager->RegisterModule(new ModuleUser($System));
131 $this->ModuleManager->RegisterModule(new ModuleAoWoW($System));
132 $this->ModuleManager->RegisterModule(new ModuleReferrer($System));
133 $this->ModuleManager->Modules['Referrer']->Excluded[] = $System->Config['Web']['Host'];
134 $this->ModuleManager->RegisterModule(new ModuleTeam($System));
135 $this->ModuleManager->RegisterModule(new ModuleDictionary($System));
136 $this->ModuleManager->RegisterModule(new ModuleTranslation($System));
137 $this->ModuleManager->RegisterModule(new ModuleImport($System));
138 $this->ModuleManager->RegisterModule(new ModuleExport($System));
139 $this->ModuleManager->RegisterModule(new ModuleServer($System));
140 $this->ModuleManager->RegisterModule(new ModuleClientVersion($System));
141 $this->ModuleManager->RegisterModule(new ModuleShoutBox($System));
142 $this->ModuleManager->RegisterModule(new ModuleNews($System));
143 $this->ModuleManager->RegisterModule(new ModuleWiki($System));
144 $this->ModuleManager->RegisterModule(new ModuleSearch($System));
145 $this->ModuleManager->RegisterModule(new ModuleFrontPage($System));
146 $this->ModuleManager->StartAll();
147 $User = &$this->User; // Back compatibility, will be removed
148
149 $this->BaseView = new BaseView($this);
150 if($this->DoNotShowPage == false)
151 {
152 $this->PathItems = ProcessURL();
153 $this->ShowPage();
154 }
155 }
156
157 function GetMicrotime()
158 {
159 list($Usec, $Sec) = explode(' ', microtime());
160 return ((float)$Usec + (float)$Sec);
161 }
162
163 function Link($Target)
164 {
165 return($this->Config['Web']['BaseURL'].$Target);
166 }
167
168 function RegisterPage($Path, $Handler)
169 {
170 if(is_array($Path))
171 {
172 $Page = &$this->Pages;
173 $LastKey = array_pop($Path);
174 foreach($Path as $PathItem)
175 {
176 $Page = &$Page[$PathItem];
177 }
178 if(!is_array($Page)) $Page = array('' => $Page);
179 $Page[$LastKey] = $Handler;
180 } else $this->Pages[$Path] = $Handler;
181 }
182
183 function RegisterMenuItem($MenuItem, $Pos = NULL)
184 {
185 if(is_null($Pos)) $this->Menu[] = $MenuItem;
186 else {
187 array_splice($this->Menu, $Pos, 0, array($MenuItem));
188 }
189 }
190
191 function RegisterRSS($Channel, $Pos = NULL)
192 {
193 if(is_null($Pos)) $this->RSSChannels[] = $Channel;
194 else {
195 array_splice($this->RSSChannels, $Pos, 0, array($Channel));
196 }
197 }
198
199 function SearchPage($PathItems, $Pages)
200 {
201 if(count($PathItems) > 0) $PathItem = $PathItems[0];
202 else $PathItem = '';
203 if(array_key_exists($PathItem, $Pages))
204 {
205 if(is_array($Pages[$PathItem]))
206 {
207 array_shift($PathItems);
208 return($this->SearchPage($PathItems, $Pages[$PathItem]));
209 } else return($Pages[$PathItem]);
210 } else return('');
211 }
212
213 function PageNotFound()
214 {
215 return('Page '.implode('/', $this->PathItems).' not found.');
216 }
217
218 function ShowPage()
219 {
220 /* @var $Page Page */
221 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
222 if($ClassName != '')
223 {
224 $Page = new $ClassName($this);
225 $Output = $Page->GetOutput();
226 if($Page->RawPage == false) $Output = $this->BaseView->ShowPage($Output);
227 echo($Output);
228 } else echo($this->PageNotFound());
229 }
230}
231
232class BaseView extends View
233{
234 function ShowTopBar()
235 {
236 $Output = '<div class="Menu">';
237 if(!$this->System->User->Licence(LICENCE_USER))
238 $Output .= '<div class="advert">'.$this->System->Config['Web']['Advertisement'].'</div>';
239 $Output .= '<span class="MenuItem"></span>';
240 if($this->System->User->Licence(LICENCE_USER))
241 {
242 //$DbResult = $System->Database->query('SELECT `Id`, `Name` FROM `Team` WHERE `Id`='.$this->System->User->Team);
243 //$Team = $DbResult->fetch_assoc();
244 //$Output .= ''<span class="MenuItem">Moje překlady: <a href="">Dokončené</a> <a href="">Rozpracované</a> <a href="">Exporty</a> Tým: <a href="">'.$Team['name'].'</a></span>';
245 $Output .= '<span class="MenuItem2">'.$this->System->User->Name.' <a href="'.$this->System->Link('/?action=logout').'">Odhlášení</a>'.
246 ' <a href="'.$this->System->Link('/user.php?user='.$this->System->User->Id).'">Moje stránka</a>'.
247 ' <a href="'.$this->System->Link('/Options.php').'">Nastavení</a>'.
248 ' <a title="Vámi přeložené texty" href="'.$this->System->Link('/TranslationList.php?user='.
249 $this->System->User->Id.'&amp;group=0&amp;state=2&amp;text=&amp;entry=').'">Přeložené</a>'.
250 ' <a title="Vaše rozpracované text" href="'.$this->System->Link('/TranslationList.php?user='.
251 $this->System->User->Id.'&amp;group=0&amp;state=3&amp;text=&amp;entry=').'">Rozpracované</a>'.
252 ' <a title="Nikým nepřeložené texty" href="'.$this->System->Link('/TranslationList.php?user=0&amp;group=0&amp;state=1&amp;text=&amp;entry=').'">Nepřeložené</a>'.
253 '</span>';
254 } else
255 {
256 $Output .= '<span class="MenuItem2"><form action="'.$this->System->Link('/?action=login').'" method="post"> '.
257 'Jméno: <input type="text" name="LoginUser" size="8 " /> '.
258 'Heslo: <td><input type="password" name="LoginPass" size="8" /> '.
259 '<input type="submit" value="Přihlásit" /></form> &nbsp; '.
260 '<a href="'.$this->System->Link('/registrace.php').'">Registrace</a></span>';
261 }
262 $Output .= '</div>';
263 return($Output);
264 }
265
266 function ShowLoginBox()
267 {
268 $Output = '';
269 if($this->System->User->Licence(LICENCE_USER))
270 {
271 // $Output .= 'Jste přihlášen jako: <b>'.$tUser->Id.'</b> <a href="index.php?Logout">Odhlásit</a>';
272 } else
273 {
274 $Output .= '<strong>Přihlášení:</strong>
275 <form action="" method="post">
276 <table>
277 <tr>
278 <td><input type="text" name="LoginUser" size="13" /></td>
279 </tr>
280 <tr>
281 <td><input type="password" name="LoginPass" size="13" /></td>
282 </tr>
283 <tr>
284 <th><input type="submit" value="Přihlásit" /></th>
285 </tr>
286 </table>
287 </form>';
288 }
289 return($Output);
290 }
291
292 function ShowSearchBox()
293 {
294 $Output = '<strong>Hledání:</strong>'.
295 '<form action="'.$this->System->Link('/search/').'" method="get"><div>'.
296 '<table>'.
297 '<tr>'.
298 '<td><input type="text" name="text" size="13" /></td>'.
299 '</tr>'.
300 '<tr>'.
301 '<th><input type="submit" value="Hledat" /></th>'.
302 '</tr>'.
303 '</table></div>'.
304 '</form>';
305 return($Output);
306 }
307
308 function ShowMainMenu()
309 {
310 $Output = '<strong>Nabídka:</strong>'.
311 '<div class="verticalmenu"><ul>';
312 foreach($this->System->Menu as $MenuItem)
313 if($this->System->User->Licence($MenuItem['Permission']))
314 {
315 if(isset($MenuItem['Click'])) $OnClick = ' onclick="'.$MenuItem['Click'].'"';
316 else $OnClick = '';
317 if($MenuItem['Icon'] != '') $Icon = '<img src="'.$this->System->Link('/images/menu/'.$MenuItem['Icon']).'"/>';
318 else $Icon = '';
319 $Output .= '<li>'.$Icon.'<a class="verticalmenua" title="'.$MenuItem['Hint'].'" href="'.
320 $MenuItem['Link'].'"'.$OnClick.'>'.$MenuItem['Title'].'</a></li>';
321 }
322 $Output .= '</ul></div>';
323 return($Output);
324 }
325
326 function ShowTranslatedMenu()
327 {
328 global $TranslationTree;
329
330 $Output = '<strong>Překladové skupiny:</strong><br /><div id="TranslationMenu">';
331 $DbResult = $this->System->Database->select('Group', '`Id`, `Name`', '1 ORDER BY `Name`');
332 while($Group = $DbResult->fetch_assoc())
333 {
334 $Output .= '<div id="menuitem-group'.$Group['Id'].'" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">'.
335 '<a href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').'">'.$Group['Name'].'</a></div>'.
336 '<div id="group'.$Group['Id'].'" class="hidden-menu-item" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">';
337 $Output .= '&nbsp;<a title="Zde můžete začít překládat" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=1&amp;user=0&amp;entry=&amp;text=').'">Nepřeložené</a><br />'.
338 '&nbsp;<a title="Přeložené texty, můžete zde hlasovat, nebo opravovat překlady" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=2&amp;user=0&amp;entry=&amp;text=').'">Přeložené</a><br />';
339 if($this->System->User->Licence(LICENCE_USER))
340 {
341 $Output .= '&nbsp;<a title="Nedokončené překlady" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=3').'">Rozepsané</a><br />
342 &nbsp;<a title="Všechny překlady, které jste přeložil" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=1&amp;user='.$this->System->User->Id).'&amp;entry=&amp;text=">Vlastní</a><br />';
343 }
344 $Output .= '&nbsp;<a title="Sestavit speciální filtr" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').'">Filtr</a><br />';
345 $Output .= '</div>';
346 }
347 $Output .= '</div>';
348 return($Output);
349 }
350
351 function ShowHeader()
352 {
353 $Output = '<?xml version="1.0" encoding="'.$this->System->Config['Web']['Charset'].'"?>'.
354 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'.
355 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cz">'.
356 '<head>'.
357 '<meta http-equiv="content-type" content="application/xhtml+xml; charset='.$this->System->Config['Web']['Charset'].'" />'.
358 '<meta name="keywords" content="wow, quest, questy, questů, preklad, mangos, překlad, překládání, přeložený, přeložení, čeština, world of warcraft, open source, free, addon" />'.
359 '<meta name="description" content="'.$this->System->Config['Web']['Title'].'" />'.
360 '<meta name="robots" content="all" />'.
361 '<link rel="stylesheet" href="'.$this->System->Link('/style/style.css').'" type="text/css" media="all" />'.
362 '<script type="text/javascript" src="'.$this->System->Link('/style/global.js').'"></script>'.
363 '<link rel="shortcut icon" href="'.$this->System->Link('/images/favicon.ico').'" />';
364 foreach($this->System->RSSChannels as $Channel)
365 {
366 $Output .= ' <link rel="alternate" title="'.$Channel['Title'].'" href="'.
367 $this->System->Link('/rss.php?channel='.$Channel['Channel']).'" type="application/rss+xml" />';
368 }
369 $Output .= '<title>'.$this->System->Config['Web']['Title'].'</title>'.
370 '</head><body>';
371
372 $Output .= $this->ShowTopBar();
373 $Output .= '<table class="page"><tr><td class="menu">';
374 $Output .= $this->ShowMainMenu();
375 $Output .= $this->System->ModuleManager->Modules['User']->ShowOnlineList();
376 $Output .= '<br />';
377 $Output .= $this->ShowSearchBox();
378 $Output .= '</td><td id="border-left"></td><td class="content">';
379 return($Output);
380 }
381
382 function ShowFooter()
383 {
384 global $ScriptStartTime, $Revision, $ReleaseTime;
385
386 $ScriptGenerateDuration = round(GetMicrotime() - $ScriptStartTime, 2);
387
388 $Output = '</td>'.
389 '<td class="menu2">';
390 $Output .= $this->ShowTranslatedMenu();
391 $Output .= '</td>'.
392 '</tr><tr>'.
393 '<td colspan="4" class="page-bottom">Verze: '.$Revision.' ('.HumanDate($ReleaseTime).')'.
394 ' &nbsp; <a href="http://svn.zdechov.net/trac/wowpreklad/browser/trunk">Zdrojové soubory</a> &nbsp; '.
395 '<a href="http://svn.zdechov.net/trac/wowpreklad/log/trunk?verbose=on">Novinky</a> &nbsp; '.
396 $this->System->Config['Web']['WebCounter'];
397
398 $Output .= '</td></tr>';
399 if($this->System->Config['Web']['ShowRuntimeInfo'] == true)
400 $Output .= '<tr><td colspan="3" style="text-align: center;">Doba generování: '.
401 $ScriptGenerateDuration.' s / '.ini_get('max_execution_time').' s &nbsp;&nbsp; Použitá paměť: '.
402 HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B &nbsp;&nbsp; <a href="http://validator.w3.org/check?uri='.
403 htmlentities('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING']).'">HTML validator</a></td></tr>';
404 $Output .= '</table>'.
405 '</body>'.
406 '</html>';
407 $this->System->User->Store();
408 return($Output);
409 }
410
411 function ShowPage($Content)
412 {
413 $Output = $this->ShowHeader().$Content.$this->ShowFooter();
414 //if($this->System->Config['Web']['FormatOutput']) $Output = $this->FormatOutput($Output);
415 return($Output);
416 }
417
418 function FormatOutput($s)
419 {
420 $out = '';
421 $nn = 0;
422 $n = 0;
423 while($s != '')
424 {
425 $start = strpos($s, '<');
426 $end = strpos($s, '>');
427 if($start != 0)
428 {
429 $end = $start - 1;
430 $start = 0;
431 }
432 $line = trim(substr($s, $start, $end + 1));
433 if(strlen($line) > 0)
434 if($line[0] == '<')
435 {
436 if($s[$start + 1] == '/')
437 {
438 $n = $n - 2;
439 $nn = $n;
440 } else
441 {
442 if(strpos($line, ' ')) $cmd = substr($line, 1, strpos($line, ' ') - 1);
443 else $cmd = substr($line, 1, strlen($line) - 2);
444 //echo('['.$cmd.']');
445 if(strpos($s, '</'.$cmd.'>')) $n = $n + 2;
446 }
447 }// else $line = '['.$line.']';
448 //if($line != '') echo(htmlspecialchars(str_repeat(' ',$nn).$line."\n"));
449 if($line != '') $out .= (str_repeat(' ', $nn).$line."\n");
450 $s = substr($s, $end + 1, strlen($s));
451 $nn = $n;
452 }
453 return($out);
454 }
455}
Note: See TracBrowser for help on using the repository browser.