source: trunk/includes/system.php@ 809

Last change on this file since 809 was 809, checked in by chronos, 11 years ago
  • Property svn:executable set to *
File size: 19.1 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 $DoNotShowPage;
14 var $OnPageNotFound;
15
16 function __construct()
17 {
18 $this->Config = array();
19 $this->Menu = array();
20 $this->RSSChannels = array();
21 $this->DoNotShowPage = false;
22 $this->OnPageNotFound = array();
23 }
24
25 function Init()
26 {
27 global $Config, $LocaleManager;
28
29 $this->Config = $Config;
30 $this->Database = new Database();
31 $this->Database->Connect($this->Config['Database']['Host'],
32 $this->Config['Database']['User'], $this->Config['Database']['Password'],
33 $this->Config['Database']['Database']);
34 $this->Database->charset($this->Config['Database']['Charset']);
35 $this->Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
36 $this->Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
37 $this->Database->LogSQLQuery = $this->Config['Web']['LogSQLQuery'];
38 $this->ModuleManager = new AppModuleManager();
39
40 $this->PathItems = ProcessURL();
41 // Initialize locale
42 $LocaleManager = new LocaleManager($this);
43 $LocaleManager->Dir = dirname(dirname(__FILE__)).'/locale';
44 $LocaleManager->Available = array(
45 'cs' => array('Code' => 'cs', 'Title' => 'Česky'),
46 'en' => array('Code' => 'en', 'Title' => 'English'),
47 );
48 $Code = $Config['Web']['Locale'];
49 if (isset($_SESSION))
50 if(array_key_exists('Locale', $_SESSION)) $Code = $_SESSION['Locale'];
51 if (isset($_GET))
52 if(array_key_exists('locale', $_GET)) $Code = $_GET['locale'];
53
54 //echo(phpinfo());
55 if(!array_key_exists($Code, $LocaleManager->Available)) $Code = 'en';
56 $_SESSION['Locale'] = $Code;
57 $LocaleManager->LoadLocale($Code);
58
59 $this->Menu = array
60 (
61 /* array(
62 'Title' => T('Files'),
63 'Hint' => 'Stahování různých pomocných souborů a programů',
64 'Link' => $this->Link('/download.php'),
65 'Permission' => LICENCE_ANONYMOUS,
66 'Icon' => '',
67 ),
68 */
69 array(
70 'Title' => T('Instructions'),
71 'Hint' => 'Informace k překladu hry',
72 'Link' => $this->Link('/info.php'),
73 'Permission' => LICENCE_ANONYMOUS,
74 'Icon' => '',
75 ),
76 array(
77 'Title' => T('Data source'),
78 'Hint' => 'Informace o překladových skupinách',
79 'Link' => $this->Link('/TranslationList.php?action=grouplist'),
80 'Permission' => LICENCE_ANONYMOUS,
81 'Icon' => '',
82 ),
83 array(
84 'Title' => T('Presentation'),
85 'Hint' => 'Prezentace a motivace překladu',
86 'Link' => $this->Link('/promotion.php'),
87 'Permission' => LICENCE_ANONYMOUS,
88 'Icon' => '',
89 ),
90 array(
91 'Title' => T('IRC chat'),
92 'Hint' => 'IRC chat pro překladatele',
93 'Link' => 'http://embed.mibbit.com/?server=game.zdechov.net%3A6667&amp;channel=%23wowpreklad&amp;forcePrompt=true&amp;charset=utf-8',
94 'Permission' => LICENCE_ANONYMOUS,
95 'Icon' => '',
96 ),
97 array(
98 'Title' => T('Administration'),
99 'Hint' => 'Volby pro správu',
100 'Link' => $this->Link('/admin/'),
101 'Permission' => LICENCE_ADMIN,
102 'Icon' => '',
103 ),
104 );
105 }
106
107 function Run()
108 {
109 global $System, $ScriptStartTime, $TranslationTree, $User, $StopAfterUpdateManager,
110 $UpdateManager, $Config, $DatabaseRevision;
111
112 $ScriptStartTime = GetMicrotime();
113
114 if(GetRemoteAddress() != '') session_start();
115
116 if(!isset($Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/install.php">zde</a>.');
117 date_default_timezone_set($Config['Web']['Timezone']);
118
119 $this->Init();
120
121 // Check database persistence structure
122 $UpdateManager = new UpdateManager();
123 $UpdateManager->Database = $this->Database;
124 $UpdateManager->Revision = $DatabaseRevision;
125 if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
126 if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
127
128 // SQL injection hack protection
129 foreach($_POST as $Index => $Item)
130 {
131 if(is_array($_POST[$Index]))
132 foreach($_POST[$Index] as $Index2 => $Item2) $_POST[$Index][$Index2] = addslashes($Item2);
133 else $_POST[$Index] = addslashes($_POST[$Index]);
134 }
135 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($_GET[$Index]);
136
137 // TODO: Global initialized variable should be removed
138 $TranslationTree = GetTranslationTree();
139
140 // Initialize application modules
141 $this->ModuleManager->RegisterModule(new ModuleError($System));
142 $this->ModuleManager->Modules['Error']->ShowError = $Config['Web']['ShowPHPError'];
143 $this->ModuleManager->RegisterModule(new ModuleLog($System));
144 $this->ModuleManager->RegisterModule(new ModuleUser($System));
145 $this->ModuleManager->RegisterModule(new ModuleAoWoW($System));
146 $this->ModuleManager->RegisterModule(new ModuleReferrer($System));
147 $this->ModuleManager->Modules['Referrer']->Excludes[] = $System->Config['Web']['Host'];
148 $this->ModuleManager->RegisterModule(new ModuleTeam($System));
149 $this->ModuleManager->RegisterModule(new ModuleDictionary($System));
150 $this->ModuleManager->RegisterModule(new ModuleTranslation($System));
151 $this->ModuleManager->RegisterModule(new ModuleImport($System));
152 $this->ModuleManager->RegisterModule(new ModuleExport($System));
153 $this->ModuleManager->RegisterModule(new ModuleServer($System));
154 $this->ModuleManager->RegisterModule(new ModuleClientVersion($System));
155 $this->ModuleManager->RegisterModule(new ModuleShoutBox($System));
156 $this->ModuleManager->RegisterModule(new ModuleNews($System));
157 $this->ModuleManager->RegisterModule(new ModuleWiki($System));
158 $this->ModuleManager->RegisterModule(new ModuleSearch($System));
159 $this->ModuleManager->RegisterModule(new ModuleFrontPage($System));
160 $this->ModuleManager->RegisterModule(new ModuleDownload($System));
161 $this->ModuleManager->RegisterModule(new ModuleForum($System));
162 $this->ModuleManager->RegisterModule(new ModuleRedirection($System));
163 $this->ModuleManager->StartAll();
164
165 $this->BaseView = new BaseView($this);
166 if($this->DoNotShowPage == false)
167 {
168 $this->ShowPage();
169 }
170 }
171
172 function GetMicrotime()
173 {
174 list($Usec, $Sec) = explode(' ', microtime());
175 return ((float)$Usec + (float)$Sec);
176 }
177
178 function Link($Target)
179 {
180 return($this->Config['Web']['BaseURL'].$Target);
181 }
182
183 function RegisterPage($Path, $Handler)
184 {
185 if(is_array($Path))
186 {
187 $Page = &$this->Pages;
188 $LastKey = array_pop($Path);
189 foreach($Path as $PathItem)
190 {
191 $Page = &$Page[$PathItem];
192 }
193 if(!is_array($Page)) $Page = array('' => $Page);
194 $Page[$LastKey] = $Handler;
195 } else $this->Pages[$Path] = $Handler;
196 }
197
198 function RegisterMenuItem($MenuItem, $Pos = NULL)
199 {
200 if(is_null($Pos)) $this->Menu[] = $MenuItem;
201 else {
202 array_splice($this->Menu, $Pos, 0, array($MenuItem));
203 }
204 }
205
206 function SearchPage($PathItems, $Pages)
207 {
208 if(count($PathItems) == 0) $PathItems = array('');
209 $PathItem = $PathItems[0];
210 if(array_key_exists($PathItem, $Pages))
211 {
212 if(is_array($Pages[$PathItem]))
213 {
214 array_shift($PathItems);
215 return($this->SearchPage($PathItems, $Pages[$PathItem]));
216 } else
217 {
218 if(count($PathItems) == 1) return($Pages[$PathItem]);
219 else return(''); // Unexpected subpages
220 }
221 } else return('');
222 }
223
224 function PageNotFound()
225 {
226 // Send correct HTTP status code to signal unknown page
227 if(array_key_exists('SERVER_PROTOCOL', $_SERVER))
228 Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
229 if(array_key_exists('HTTP_REFERER', $_SERVER)) $Referer = ' Referer: '.$_SERVER['HTTP_REFERER'];
230 else $Referer = '';
231 if(isset($this->ModuleManager->Modules['Log']))
232 $this->ModuleManager->Modules['Log']->WriteLog('Stránka "'.
233 implode('/', $this->PathItems).'" nenalezena'.$Referer, LOG_TYPE_PAGE_NOT_FOUND);
234 return(ShowMessage(sprintf(T('Page "%s" not found'), implode('/', $this->PathItems)), MESSAGE_CRITICAL));
235 }
236
237 function ShowPage()
238 {
239 $Output = '';
240 /* @var $Page Page */
241 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
242 if(($ClassName != '') and (class_exists($ClassName)))
243 {
244 $Page = new $ClassName($this);
245 $Output = $Page->GetOutput();
246 $this->BaseView->Title = $Page->Title;
247 if($Page->RawPage == false) $Output = $this->BaseView->ShowPage($Output);
248 } else {
249 if((count($this->OnPageNotFound) == 2)
250 and method_exists($this->OnPageNotFound[0], $this->OnPageNotFound[1]))
251 $Output = $this->BaseView->ShowPage(call_user_func_array($this->OnPageNotFound, array()));
252 else
253 {
254 $Output = $this->PageNotFound();
255 $this->BaseView->Title = T('Page not found');
256 $Output = $this->BaseView->ShowPage($Output);
257 }
258 }
259 echo($Output);
260 }
261}
262
263class BaseView extends View
264{
265 var $Title;
266
267 function ShowTopBar()
268 {
269 global $LocaleManager;
270
271 $Output = '<div class="Menu">';
272 if(!$this->System->User->Licence(LICENCE_USER))
273 $Output .= '<div class="advert">'.$this->System->Config['Web']['Advertisement'].'</div>';
274 $Output .= '<span class="MenuItem"></span><span class="MenuItem2">';
275 if($this->System->User->Licence(LICENCE_USER))
276 {
277 //$DbResult = $System->Database->query('SELECT `Id`, `Name` FROM `Team` WHERE `Id`='.$this->System->User->Team);
278 //$Team = $DbResult->fetch_assoc();
279 //$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>';
280 $Output .= $this->System->User->Name.' <a href="'.$this->System->Link('/?action=logout').'">'.T('Logout').'</a>'.
281 ' <a href="'.$this->System->Link('/user.php?user='.$this->System->User->Id).'">'.T('My page').'</a>'.
282 ' <a href="'.$this->System->Link('/Options.php').'">'.T('Options').'</a>'.
283 ' <a title="Vámi přeložené texty" href="'.$this->System->Link('/TranslationList.php?user='.
284 $this->System->User->Id.'&amp;group=0&amp;state=2&amp;text=&amp;entry=').'">'.T('Translated').'</a>'.
285 ' <a title="Vaše rozpracované text" href="'.$this->System->Link('/TranslationList.php?user='.
286 $this->System->User->Id.'&amp;group=0&amp;state=3&amp;text=&amp;entry=').'">'.T('Unfinished').'</a>'.
287 ' <a title="Nikým nepřeložené texty" href="'.
288 $this->System->Link('/TranslationList.php?user=0&amp;group=0&amp;state=1&amp;text=&amp;entry=').'">'.T('Untranslated').'</a>';
289 } else
290 {
291 $Output .= '<a href="'.$this->System->Link('/login/').'">'.T('Login').'</a>&nbsp;'.
292 '<a href="'.$this->System->Link('/registrace.php').'">'.T('Registration').'</a>';
293 }
294 //$Output .= ' <form action="?setlocale" method="get">';
295 $Output .= ' <select onchange="window.location=\'?locale=\'+this.value">';
296 foreach($LocaleManager->Available as $Locale)
297 {
298 if($Locale['Code'] == $LocaleManager->CurrentLocale->Texts->Code) $Selected = ' selected="selected"';
299 else $Selected = '';
300 $Output .= '<option title="" value="'.$Locale['Code'].'"'.$Selected.' onchange="this.form.submit()">'.$Locale['Title'].'</option>';
301 }
302 $Output .= '</select><noscript><span><input type="submit" value="Submit"/></span></noscript>';
303 //$Output .= '</form>';
304 $Output .= '</span></div>';
305 return($Output);
306 }
307
308 function ShowLoginBox()
309 {
310 $Output = '';
311 if($this->System->User->Licence(LICENCE_USER))
312 {
313 // $Output .= 'Jste přihlášen jako: <b>'.$tUser->Id.'</b> <a href="index.php?Logout">Odhlásit</a>';
314 } else
315 {
316 $Output .= '<strong>'.T('Login').':</strong>
317 <form action="" method="post">
318 <table>
319 <tr>
320 <td><input type="text" name="LoginUser" size="13" /></td>
321 </tr>
322 <tr>
323 <td><input type="password" name="LoginPass" size="13" /></td>
324 </tr>
325 <tr>
326 <th><input type="submit" value="'.T('Do login').'" /></th>
327 </tr>
328 </table>
329 </form>';
330 }
331 return($Output);
332 }
333
334 function ShowSearchBox()
335 {
336 $Output = '<strong>'.T('Search').':</strong>'.
337 '<form action="'.$this->System->Link('/search/').'" method="get"><div>'.
338 '<table>'.
339 '<tr>'.
340 '<td><input type="text" name="text" size="13" /></td>'.
341 '</tr>'.
342 '<tr>'.
343 '<th><input type="submit" value="'.T('Do search').'" /></th>'.
344 '</tr>'.
345 '</table></div>'.
346 '</form>';
347 return($Output);
348 }
349
350 function ShowMainMenu()
351 {
352 $Output = '<strong>'.T('Menu').':</strong>'.
353 '<div class="verticalmenu"><ul>';
354 foreach($this->System->Menu as $MenuItem)
355 if($this->System->User->Licence($MenuItem['Permission']))
356 {
357 if(isset($MenuItem['Click'])) $OnClick = ' onclick="'.$MenuItem['Click'].'"';
358 else $OnClick = '';
359 if($MenuItem['Icon'] != '') $Icon = '<img src="'.$this->System->Link('/images/menu/'.$MenuItem['Icon']).'"/>';
360 else $Icon = '';
361 $Output .= '<li>'.$Icon.'<a class="verticalmenua" title="'.$MenuItem['Hint'].'" href="'.
362 $MenuItem['Link'].'"'.$OnClick.'>'.$MenuItem['Title'].'</a></li>';
363 }
364 $Output .= '</ul></div>';
365 return($Output);
366 }
367
368 function ShowTranslatedMenu()
369 {
370 global $TranslationTree;
371
372 $Output = '<strong>'.T('Translate groups').':</strong><br /><div id="TranslationMenu">';
373 $DbResult = $this->System->Database->select('Group', '`Id`, `Name`', '1 ORDER BY `Name`');
374 while($Group = $DbResult->fetch_assoc())
375 {
376 $Output .= '<div id="menuitem-group'.$Group['Id'].'" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">'.
377 '<a href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').'">'.str_replace(' ','&nbsp;',$Group['Name']).'</a></div>'.
378 '<div id="group'.$Group['Id'].'" class="hidden-menu-item" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">';
379 $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 />'.
380 '&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 />';
381 if($this->System->User->Licence(LICENCE_USER))
382 {
383 $Output .= '&nbsp;<a title="Nedokončené překlady" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=3').'">Rozepsané</a><br />
384 &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 />';
385 }
386 $Output .= '&nbsp;<a title="Sestavit speciální filtr" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').'">Filtr</a><br />';
387 $Output .= '</div>';
388 }
389 $Output .= '</div>';
390 return($Output);
391 }
392
393 function ShowHeader()
394 {
395 $Output = ''.
396 '<!DOCTYPE html>'.
397 '<html>'.
398 '<head>'.
399 '<meta http-equiv="content-type" content="text/html; charset='.$this->System->Config['Web']['Charset'].'" />'.
400 '<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" />'.
401 '<meta name="description" content="'.$this->System->Config['Web']['Description'].'" />'.
402 '<meta name="robots" content="all" />'.
403 '<link rel="stylesheet" href="'.$this->System->Link('/style/style.css').'" type="text/css" media="all" />'.
404 '<script type="text/javascript" src="'.$this->System->Link('/style/global.js').'"></script>'.
405 '<link rel="shortcut icon" href="'.$this->System->Link('/images/favicon.ico').'" />';
406 $Output .= $this->System->ModuleManager->Modules['News']->ShowRSSHeader();
407 $Title = $this->System->Config['Web']['Title'];
408 if($this->Title != '') $Title = $this->Title.' - '.$Title;
409 $Output .= '<title>'.$Title.'</title>'.
410 '</head><body>';
411
412 $Output .= $this->ShowTopBar();
413 $Output .= '<table class="page"><tr><td class="menu">';
414 $Output .= $this->ShowMainMenu();
415 $Output .= $this->System->ModuleManager->Modules['User']->ShowOnlineList();
416 $Output .= '<br />';
417 $Output .= $this->ShowSearchBox();
418
419 $Output .= '</td><td id="border-left"></td><td class="content">';
420 return($Output);
421 }
422
423 function ShowFooter()
424 {
425 global $ScriptStartTime, $Revision, $ReleaseTime;
426
427 $ScriptGenerateDuration = round(GetMicrotime() - $ScriptStartTime, 2);
428
429 $Output = '</td>'.
430 '<td class="menu2">';
431 $Output .= $this->ShowTranslatedMenu();
432 $Output .= '</td>'.
433 '</tr><tr>'.
434 '<td colspan="4" class="page-bottom">'.T('Version').': '.$Revision.' ('.HumanDate($ReleaseTime).')'.
435 ' &nbsp; <a href="http://svn.zdechov.net/trac/wowpreklad/browser/trunk">'.T('Source code').'</a> &nbsp; '.
436 '<a href="http://svn.zdechov.net/trac/wowpreklad/log/trunk?verbose=on">'.T('Changelog').'</a> &nbsp; '.
437 $this->System->Config['Web']['WebCounter'];
438
439 $Output .= '</td></tr>';
440 if($this->System->Config['Web']['ShowRuntimeInfo'] == true)
441 $Output .= '<tr><td colspan="3" style="text-align: center;">'.T('Generating duration').': '.
442 $ScriptGenerateDuration.' s / '.ini_get('max_execution_time').' s &nbsp;&nbsp; '.T('Used memory').': '.
443 HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B &nbsp;&nbsp; <a href="http://validator.w3.org/check?uri='.
444 htmlentities('http://'.$_SERVER['HTTP_HOST'].GetRequestURI().'?'.$_SERVER['QUERY_STRING']).'">HTML validator</a></td></tr>';
445 $Output .= '</table>'.
446 '</body>'.
447 '</html>';
448 return($Output);
449 }
450
451 function ShowPage($Content)
452 {
453 $Output = $this->ShowHeader().$Content.$this->ShowFooter();
454 if($this->System->Config['Web']['FormatOutput'])
455 $Output = $this->FormatOutput($Output);
456 return($Output);
457 }
458
459 function FormatOutput($s)
460 {
461 $out = '';
462 $nn = 0;
463 $n = 0;
464 while($s != '')
465 {
466 $start = strpos($s, '<');
467 $end = strpos($s, '>');
468 if($start != 0)
469 {
470 $end = $start - 1;
471 $start = 0;
472 }
473 $line = trim(substr($s, $start, $end + 1));
474 if(strlen($line) > 0)
475 if($line[0] == '<')
476 {
477 if($s[$start + 1] == '/')
478 {
479 $n = $n - 2;
480 $nn = $n;
481 } else
482 {
483 if(strpos($line, ' ')) $cmd = substr($line, 1, strpos($line, ' ') - 1);
484 else $cmd = substr($line, 1, strlen($line) - 2);
485 //echo('['.$cmd.']');
486 if(strpos($s, '</'.$cmd.'>')) $n = $n + 2;
487 }
488 }// else $line = '['.$line.']';
489 //if($line != '') echo(htmlspecialchars(str_repeat(' ',$nn).$line."\n"));
490 if($line != '') $out .= (str_repeat(' ', $nn).$line."\n");
491 $s = substr($s, $end + 1, strlen($s));
492 $nn = $n;
493 }
494 return($out);
495 }
496}
Note: See TracBrowser for help on using the repository browser.