source: trunk/includes/system.php@ 836

Last change on this file since 836 was 836, checked in by chronos, 10 years ago
  • Modified: Allow to disable google translate as this service is not usable for offline development.
  • Modified: Translated rest of ReadMe file.
  • Property svn:executable set to *
File size: 19.4 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 $Output2 = '';
250 if((count($this->OnPageNotFound) == 2)
251 and method_exists($this->OnPageNotFound[0], $this->OnPageNotFound[1]))
252 $Output2 = call_user_func_array($this->OnPageNotFound, array());
253 if($Output2 != '') $Output .= $this->BaseView->ShowPage($Output2);
254 else {
255 $Output = $this->PageNotFound();
256 $this->BaseView->Title = T('Page not found');
257 $Output = $this->BaseView->ShowPage($Output);
258 }
259 }
260 echo($Output);
261 }
262}
263
264class BaseView extends View
265{
266 var $Title;
267
268 function ShowTopBar()
269 {
270 global $LocaleManager;
271
272 $Output = '<div class="Menu">';
273 if(!$this->System->User->Licence(LICENCE_USER))
274 $Output .= '<div class="advert">'.$this->System->Config['Web']['Advertisement'].'</div>';
275 $Output .= '<span class="MenuItem"></span><span class="MenuItem2">';
276 if($this->System->User->Licence(LICENCE_USER))
277 {
278 //$DbResult = $System->Database->query('SELECT `Id`, `Name` FROM `Team` WHERE `Id`='.$this->System->User->Team);
279 //$Team = $DbResult->fetch_assoc();
280 //$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>';
281 $Output .= $this->System->User->Name.' <a href="'.$this->System->Link('/?action=logout').'">'.T('Logout').'</a>'.
282 ' <a href="'.$this->System->Link('/user.php?user='.$this->System->User->Id).'">'.T('My page').'</a>'.
283 ' <a href="'.$this->System->Link('/Options.php').'">'.T('Options').'</a>'.
284 ' <a title="Vámi přeložené texty" href="'.$this->System->Link('/TranslationList.php?user='.
285 $this->System->User->Id.'&amp;group=0&amp;state=2&amp;text=&amp;entry=').'">'.T('Translated').'</a>'.
286 ' <a title="Vaše rozpracované text" href="'.$this->System->Link('/TranslationList.php?user='.
287 $this->System->User->Id.'&amp;group=0&amp;state=3&amp;text=&amp;entry=').'">'.T('Unfinished').'</a>'.
288 ' <a title="Nikým nepřeložené texty" href="'.
289 $this->System->Link('/TranslationList.php?user=0&amp;group=0&amp;state=1&amp;text=&amp;entry=').'">'.T('Untranslated').'</a>';
290 } else
291 {
292 $Output .= '<a href="'.$this->System->Link('/login/').'">'.T('Login').'</a>&nbsp;'.
293 '<a href="'.$this->System->Link('/registrace.php').'">'.T('Registration').'</a>';
294 }
295 //$Output .= ' <form action="?setlocale" method="get">';
296 $Output .= ' <select onchange="window.location=\'?locale=\'+this.value">';
297 foreach($LocaleManager->Available as $Locale)
298 {
299 if($Locale['Code'] == $LocaleManager->CurrentLocale->Texts->Code) $Selected = ' selected="selected"';
300 else $Selected = '';
301 $Output .= '<option title="" value="'.$Locale['Code'].'"'.$Selected.' onchange="this.form.submit()">'.$Locale['Title'].'</option>';
302 }
303 $Output .= '</select><noscript><span><input type="submit" value="Submit"/></span></noscript>';
304 //$Output .= '</form>';
305 $Output .= '</span></div>';
306 return($Output);
307 }
308
309 function ShowLoginBox()
310 {
311 $Output = '';
312 if($this->System->User->Licence(LICENCE_USER))
313 {
314 // $Output .= 'Jste přihlášen jako: <b>'.$tUser->Id.'</b> <a href="index.php?Logout">Odhlásit</a>';
315 } else
316 {
317 $Output .= '<strong>'.T('Login').':</strong>
318 <form action="" method="post">
319 <table>
320 <tr>
321 <td><input type="text" name="LoginUser" size="13" /></td>
322 </tr>
323 <tr>
324 <td><input type="password" name="LoginPass" size="13" /></td>
325 </tr>
326 <tr>
327 <th><input type="submit" value="'.T('Do login').'" /></th>
328 </tr>
329 </table>
330 </form>';
331 }
332 return($Output);
333 }
334
335 function ShowSearchBox()
336 {
337 $Output = '<strong>'.T('Search').':</strong>'.
338 '<form action="'.$this->System->Link('/search/').'" method="get"><div>'.
339 '<table>'.
340 '<tr>'.
341 '<td><input type="text" name="text" size="13" /></td>'.
342 '</tr>'.
343 '<tr>'.
344 '<th><input type="submit" value="'.T('Do search').'" /></th>'.
345 '</tr>'.
346 '</table></div>'.
347 '</form>';
348 return($Output);
349 }
350
351 function ShowMainMenu()
352 {
353 $Output = '<strong>'.T('Menu').':</strong>'.
354 '<div class="verticalmenu"><ul>';
355 foreach($this->System->Menu as $MenuItem)
356 if($this->System->User->Licence($MenuItem['Permission']))
357 {
358 if(isset($MenuItem['Click'])) $OnClick = ' onclick="'.$MenuItem['Click'].'"';
359 else $OnClick = '';
360 if($MenuItem['Icon'] != '') $Icon = '<img src="'.$this->System->Link('/images/menu/'.$MenuItem['Icon']).'"/>';
361 else $Icon = '';
362 $Output .= '<li>'.$Icon.'<a class="verticalmenua" title="'.$MenuItem['Hint'].'" href="'.
363 $MenuItem['Link'].'"'.$OnClick.'>'.$MenuItem['Title'].'</a></li>';
364 }
365 $Output .= '</ul></div>';
366 return($Output);
367 }
368
369 function ShowTranslatedMenu()
370 {
371 global $TranslationTree;
372
373 $Output = '<strong>'.T('Translate groups').':</strong><br /><div id="TranslationMenu">';
374 $DbResult = $this->System->Database->select('Group', '`Id`, `Name`', '1 ORDER BY `Name`');
375 while($Group = $DbResult->fetch_assoc())
376 {
377 $Output .= '<div id="menuitem-group'.$Group['Id'].'" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">'.
378 '<a href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').'">'.str_replace(' ','&nbsp;',$Group['Name']).'</a></div>'.
379 '<div id="group'.$Group['Id'].'" class="hidden-menu-item" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">';
380 $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 />'.
381 '&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 />';
382 if($this->System->User->Licence(LICENCE_USER))
383 {
384 $Output .= '&nbsp;<a title="Nedokončené překlady" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=3').'">Rozepsané</a><br />
385 &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 />';
386 }
387 $Output .= '&nbsp;<a title="Sestavit speciální filtr" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').'">Filtr</a><br />';
388 $Output .= '</div>';
389 }
390 $Output .= '</div>';
391 return($Output);
392 }
393
394 function ShowHeader()
395 {
396 $Output = ''.
397 '<!DOCTYPE html>'.
398 '<html>'.
399 '<head>'.
400 '<meta http-equiv="content-type" content="text/html; charset='.$this->System->Config['Web']['Charset'].'" />'.
401 '<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" />'.
402 '<meta name="description" content="'.$this->System->Config['Web']['Description'].'" />'.
403 '<meta name="robots" content="all" />'.
404 '<link rel="stylesheet" href="'.$this->System->Link('/style/style.css').'" type="text/css" media="all" />'.
405 '<script type="text/javascript" src="'.$this->System->Link('/style/global.js').'"></script>'.
406 '<link rel="shortcut icon" href="'.$this->System->Link('/images/favicon.ico').'" />';
407 $Output .= $this->System->ModuleManager->Modules['News']->ShowRSSHeader();
408 $Title = $this->System->Config['Web']['Title'];
409 if($this->Title != '') $Title = $this->Title.' - '.$Title;
410 $Output .= '<title>'.$Title.'</title>'.
411 '</head><body>';
412
413 $Output .= $this->ShowTopBar();
414 $Output .= '<table class="page"><tr><td class="menu">';
415 $Output .= $this->ShowMainMenu();
416 $Output .= $this->System->ModuleManager->Modules['User']->ShowOnlineList();
417 $Output .= '<br />';
418 $Output .= $this->ShowSearchBox();
419
420 $Output .= '</td><td id="border-left"></td><td class="content">';
421 return($Output);
422 }
423
424 function ShowFooter()
425 {
426 global $ScriptStartTime, $Revision, $ReleaseTime;
427
428 $ScriptGenerateDuration = round(GetMicrotime() - $ScriptStartTime, 2);
429
430 $Output = '</td>'.
431 '<td class="menu2">';
432 $Output .= $this->ShowTranslatedMenu();
433 $Output .= '</td>'.
434 '</tr><tr>'.
435 '<td colspan="4" class="page-bottom">'.T('Version').': '.$Revision.' ('.HumanDate($ReleaseTime).')'.
436 ' &nbsp; <a href="http://svn.zdechov.net/trac/wowpreklad/browser/trunk">'.T('Source code').'</a> &nbsp; '.
437 '<a href="http://svn.zdechov.net/trac/wowpreklad/log/trunk?verbose=on">'.T('Changelog').'</a> &nbsp; '.
438 $this->System->Config['Web']['WebCounter'];
439
440 $Output .= '</td></tr>';
441 if($this->System->Config['Web']['ShowRuntimeInfo'] == true)
442 $Output .= '<tr><td colspan="3" style="text-align: center;">'.T('Generating duration').': '.
443 $ScriptGenerateDuration.' s / '.ini_get('max_execution_time').' s &nbsp;&nbsp; '.T('Used memory').': '.
444 HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B &nbsp;&nbsp; <a href="http://validator.w3.org/check?uri='.
445 htmlentities('http://'.$_SERVER['HTTP_HOST'].GetRequestURI().'?'.$_SERVER['QUERY_STRING']).'">HTML validator</a></td></tr>';
446 $Output .= '</table>'.
447 '</body>'.
448 '</html>';
449 return($Output);
450 }
451
452 function ShowPage($Content)
453 {
454 $Output = $this->ShowHeader().$Content.$this->ShowFooter();
455 if($this->System->Config['Web']['FormatOutput'])
456 $Output = $this->FormatOutput($Output);
457 return($Output);
458 }
459
460 function FormatOutput($s)
461 {
462 $out = '';
463 $nn = 0;
464 $n = 0;
465 while($s != '')
466 {
467 $start = strpos($s, '<');
468 $end = strpos($s, '>');
469 if($start != 0)
470 {
471 $end = $start - 1;
472 $start = 0;
473 }
474 $line = trim(substr($s, $start, $end + 1));
475 if(strlen($line) > 0)
476 if($line[0] == '<')
477 {
478 if($s[$start + 1] == '/')
479 {
480 $n = $n - 2;
481 $nn = $n;
482 } else
483 {
484 if(strpos($line, ' ')) $cmd = substr($line, 1, strpos($line, ' ') - 1);
485 else $cmd = substr($line, 1, strlen($line) - 2);
486 //echo('['.$cmd.']');
487 if(strpos($s, '</'.$cmd.'>')) $n = $n + 2;
488 }
489 }// else $line = '['.$line.']';
490 //if($line != '') echo(htmlspecialchars(str_repeat(' ',$nn).$line."\n"));
491 if($nn < 0) $nn = 0;
492 if($line != '') $out .= (str_repeat(' ', $nn).$line."\n");
493 $s = substr($s, $end + 1, strlen($s));
494 $nn = $n;
495 }
496 return($out);
497 }
498}
Note: See TracBrowser for help on using the repository browser.