source: trunk/includes/system.php@ 695

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