source: trunk/Common/Global.php@ 506

Last change on this file since 506 was 506, checked in by chronos, 12 years ago
  • Upraveno: Aktuality přepracovány na aplikační modul.
  • Property svn:executable set to *
File size: 17.7 KB
Line 
1<?php
2
3$Revision =
4
5/* @var $System System */
6$System = NULL;
7/* @var $Database Database */
8$Database = NULL;
9
10$ConfigFileName = dirname(__FILE__).'/../config.php';
11if(file_exists($ConfigFileName)) include_once($ConfigFileName);
12include_once(dirname(__FILE__).'/Version.php');
13include_once(dirname(__FILE__).'/Update.php');
14include_once(dirname(__FILE__).'/Module.php');
15include_once(dirname(__FILE__).'/AppModule.php');
16include_once(dirname(__FILE__).'/Database.php');
17include_once(dirname(__FILE__).'/Error.php');
18include_once(dirname(__FILE__).'/Code.php');
19include_once(dirname(__FILE__).'/Mail.php');
20include_once(dirname(__FILE__).'/Log.php');
21include_once(dirname(__FILE__).'/User.php');
22include_once(dirname(__FILE__).'/Page.php');
23include_once(dirname(__FILE__).'/Form/Form.php');
24include_once(dirname(__FILE__).'/../finance/bills.php');
25include_once(dirname(__FILE__).'/../finance/finance.php');
26include_once(dirname(__FILE__).'/../form_classes.php');
27
28
29// Application modules
30include_once(dirname(__FILE__).'/../Modules/File/File.php');
31include_once(dirname(__FILE__).'/../Modules/Meteostation/Meteostation.php');
32include_once(dirname(__FILE__).'/../Modules/Portal/Portal.php');
33include_once(dirname(__FILE__).'/../Modules/IS/IS.php');
34include_once(dirname(__FILE__).'/../Modules/Network/Network.php');
35include_once(dirname(__FILE__).'/../Modules/TV/TV.php');
36include_once(dirname(__FILE__).'/../Modules/OpeningHours/OpeningHours.php');
37include_once(dirname(__FILE__).'/../Modules/Map/Map.php');
38include_once(dirname(__FILE__).'/../Modules/Chat/Chat.php');
39include_once(dirname(__FILE__).'/../Modules/WebCam/WebCam.php');
40include_once(dirname(__FILE__).'/../Modules/User/User.php');
41include_once(dirname(__FILE__).'/../Modules/Finance/Finance.php');
42include_once(dirname(__FILE__).'/../Modules/FinanceBankAPI/FinanceBankAPI.php');
43include_once(dirname(__FILE__).'/../Modules/NetworkShare/NetworkShare.php');
44include_once(dirname(__FILE__).'/../Modules/News/News.php');
45
46class System extends Module
47{
48 var $Modules;
49 /** @var Type */
50 var $Type;
51 var $Pages;
52 /** @var AppModuleManager */
53 var $ModuleManager;
54 var $PathItems;
55
56 function __construct()
57 {
58 parent::__construct();
59 $this->Modules = array();
60 $this->Pages = array();
61 $this->ModuleManager = new AppModuleManager();
62 $this->Database = new Database();
63 $this->FormManager = new FormManager($this->Database);
64 }
65
66 function RegisterPage($Path, $Handler)
67 {
68 $this->Pages[$Path] = $Handler;
69 }
70
71 function SearchPage($PathItems, $Pages)
72 {
73 $PathItem = $PathItems[0];
74 if(array_key_exists($PathItem, $Pages))
75 {
76 if(is_array($Pages[$PathItem]))
77 {
78 array_shift($PathItems);
79 return($this->SearchPage($PathItems, $Pages[$PathItem]));
80 } else return($Pages[$PathItem]);
81 } else return('');
82 }
83
84 function ShowPage()
85 {
86 /* @var $Page Page */
87 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
88 if($ClassName != '')
89 {
90 $Page = new $ClassName();
91 $Page->System = &$this;
92 $Page->Database = &$this->Database;
93 $Page->GetOutput();
94 } else echo('Page '.implode('/', $this->PathItems).' not found.');
95 }
96
97 function ModulePresent($Name)
98 {
99 return(array_key_exists($Name, $this->Modules));
100 }
101
102 function AddModule($Module)
103 {
104 //echo('Přidávám modul '.get_class($Module).'<br />');
105 $Module->System = &$this;
106 $Module->Database = &$this->Database;
107 $this->Modules[get_class($Module)] = $Module;
108 }
109
110 function AddEmailToQueue($To, $Subject, $Content, $From, $AttachmentFileId = '')
111 {
112 $Values = array('To' => $To,
113 'Subject' => $Subject, 'Content' => $Content, 'Time' => 'NOW()',
114 'From' => $From);
115 if($AttachmentFileId != '') $Values['AttachmentFile'] = $AttachmentFileId;
116 $this->Database->insert('EmailQueue', $Values);
117 }
118
119 function ProcessEmailQueue()
120 {
121 $Output = '';
122 $DbResult = $this->Database->select('EmailQueue', '*', 'Archive=0');
123 while($DbRow = $DbResult->fetch_assoc())
124 {
125 $Mail = new Mail();
126 $Mail->AddToCombined($DbRow['To']);
127 $Mail->Subject = $DbRow['Subject'];
128 $Mail->From = $DbRow['From'];
129 $Mail->AddBody(strip_tags($DbRow['Content']), 'text/plain');
130 $Mail->AddBody($DbRow['Content'], 'text/html');
131 if($DbRow['AttachmentFile'] != '')
132 {
133 $DbResult2 = $this->Database->select('File', '*', 'Id='.$DbRow['AttachmentFile']);
134 while($File = $DbResult2->fetch_assoc())
135 $Mail->AttachFile($this->Config['Web']['FileRootFolder'].$File['DrivePath'], $File['MimeType']);
136 }
137 $Mail->Send();
138 $this->Database->update('EmailQueue', 'Id='.$DbRow['Id'], array('Archive' => 1));
139 $this->Modules['Log']->NewRecord('System', 'SendEmail', $DbRow['Id']);
140 $Output .= 'To: '.$DbRow['To'].' Subject: '.$DbRow['Subject'].'<br />';
141 }
142 return($Output);
143 }
144
145 function HumanDate($Time)
146 {
147 return(date('j.n.Y', $Time));
148 }
149
150 function Link($Target)
151 {
152 global $Config;
153
154 return($Config['Web']['RootFolder'].$Target);
155 }
156}
157
158function GlobalInit()
159{
160 global $Config, $Database, $System, $ScriptTimeStart, $ConfigFileName, $Mail, $Type,
161 $DatabaseRevision;
162
163 date_default_timezone_set('Europe/Prague');
164 $ScriptTimeStart = GetMicrotime();
165
166 if(!isset($Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/install.php">zde</a>.');
167
168 // SQL injection hack protection
169 foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
170 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
171
172 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
173
174 $System = new System();
175 $System->Config = $Config;
176 $System->Database->Connect($Config['Database']['Host'], $Config['Database']['User'], $Config['Database']['Password'], $Config['Database']['Database']);
177 $System->Database->Prefix = $Config['Database']['Prefix'];
178 $System->Database->charset($Config['Database']['Charset']);
179 $System->Database->ShowSQLError = $Config['Web']['ShowSQLError'];
180 $System->Database->ShowSQLQuery = $Config['Web']['ShowSQLQuery'];
181
182 // Check database persistence structure
183 $UpdateManager = new UpdateManager();
184 $UpdateManager->Database = &$System->Database;
185 $UpdateManager->Revision = $DatabaseRevision;
186 if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
187 if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
188
189 // Init old modules
190 $System->AddModule(new Log());
191 $System->AddModule(new ErrorHandler());
192 $System->Modules['ErrorHandler']->ShowErrors = $Config['Web']['ShowPHPError'];
193 $System->Modules['ErrorHandler']->Init();
194 $System->AddModule(new User());
195 if(isset($_SERVER['REMOTE_ADDR'])) $System->Modules['User']->Check();
196 $System->AddModule(new Bill());
197 $System->AddModule(new Finance());
198 $System->Modules['Finance']->LoadMonthParameters(0);
199 RegisterFormClasses($System->FormManager);
200
201 // Register new modules
202 $System->ModuleManager->RegisterModule(new ModuleFile($System));
203 $System->ModuleManager->RegisterModule(new ModuleMeteostation($System));
204 $System->ModuleManager->RegisterModule(new ModulePortal($System));
205 $System->ModuleManager->RegisterModule(new ModuleIS($System));
206 $System->ModuleManager->RegisterModule(new ModuleNetwork($System));
207 $System->ModuleManager->RegisterModule(new ModuleTV($System));
208 $System->ModuleManager->RegisterModule(new ModuleOpeningHours($System));
209 $System->ModuleManager->RegisterModule(new ModuleMap($System));
210 $System->ModuleManager->RegisterModule(new ModuleChat($System));
211 $System->ModuleManager->RegisterModule(new ModuleWebCam($System));
212 $System->ModuleManager->RegisterModule(new ModuleUser($System));
213 $System->ModuleManager->RegisterModule(new ModuleFinance($System));
214 $System->ModuleManager->RegisterModule(new ModuleFinanceBankAPI($System));
215 $System->ModuleManager->RegisterModule(new ModuleNetworkShare($System));
216 $System->ModuleManager->RegisterModule(new ModuleNews($System));
217 $System->ModuleManager->StartAll();
218}
219
220$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
221
222$UnitNames = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
223
224function HumanSize($Value)
225{
226 global $UnitNames;
227
228 $UnitIndex = 0;
229 while($Value > 1024)
230 {
231 $Value = round($Value / 1024, 3);
232 $UnitIndex++;
233 }
234 return($Value.' '.$UnitNames[$UnitIndex]);
235}
236
237function GetMicrotime()
238{
239 list($Usec, $Sec) = explode(' ', microtime());
240 return ((float)$Usec + (float)$Sec);
241}
242
243function ShowArray($Pole)
244{
245 echo('<pre style="font-size: 8pt;">');
246 print_r($Pole);
247 echo('</pre>');
248}
249
250function HumanDate($Time)
251{
252 $Date = explode(' ', $Time);
253 $Parts = explode('-', $Date[0]);
254 if($Date != '0000-00-00') return(($Parts[2]*1).'.'.($Parts[1]*1).'.'.$Parts[0]);
255 else return('&nbsp;');
256}
257
258// Zobrazení číselný seznamu stránek
259function PagesList($URL, $Page, $TotalCount, $CountPerPage)
260{
261 $Count = ceil($TotalCount / $CountPerPage);
262 $Around = 10;
263 $Result = '';
264 if($Count>1)
265 {
266 if($Page>0)
267 {
268 $Result.= '<a href="'.$URL.'0">&lt;&lt;</a> ';
269 $Result.= '<a href="'.$URL.($Page-1).'">&lt;</a> ';
270 }
271 $PagesMax = $Count-1;
272 $PagesMin = 0;
273 if($PagesMax>($Page+$Around)) $PagesMax = $Page+$Around;
274 if($PagesMin<($Page-$Around))
275 {
276 $Result.= ' .. ';
277 $PagesMin = $Page-$Around;
278 }
279 for($i=$PagesMin;$i<=$PagesMax;$i++)
280 {
281 if($i==$Page) $Result.= '<strong>';
282 $Result.= '<a href="'.$URL.$i.'">'.($i+1).'</a> ';
283 if($i==$Page) $Result.= '</strong>';
284 }
285 if($PagesMax<($Count-1)) $Result .= ' .. ';
286 if($Page<($Count-1))
287 {
288 $Result.= '<a href="'.$URL.($Page+1).'">&gt;</a> ';
289 $Result.= '<a href="'.$URL.($Count-1).'">&gt;&gt;</a>';
290 }
291 }
292 return($Result);
293}
294
295function ExtractTime($Time)
296{
297 return(array(
298 'Year' => date('Y', $Time),
299 'Month' => date('n', $Time),
300 'Day' => date('j', $Time),
301 'Hour' => date('h', $Time),
302 'Minute' => date('i', $Time),
303 'Second' => date('s', $Time)
304 ));
305}
306
307function GetQueryStringArray($QueryString)
308{
309 $Result = array();
310 $Parts = explode('&', $QueryString);
311 foreach($Parts as $Part)
312 {
313 if($Part != '')
314 {
315 if(!strpos($Part, '=')) $Part .= '=';
316 $Item = explode('=', $Part);
317 $Result[$Item[0]] = $Item[1];
318 }
319 }
320 return($Result);
321}
322
323function SetQueryStringArray($QueryStringArray)
324{
325 $Parts = array();
326 foreach($QueryStringArray as $Index => $Item)
327 {
328 $Parts[] = $Index.'='.$Item;
329 }
330 return(implode('&amp;', $Parts));
331}
332
333function GetPageList($TotalCount)
334{
335 global $System;
336
337 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
338
339 $Result = '';
340 if(array_key_exists('all', $QueryItems))
341 {
342 $PageCount = 1;
343 $ItemPerPage = $TotalCount;
344 } else
345 {
346 $ItemPerPage = $System->Config['Web']['ItemsPerPage'];
347 $Around = round($System->Config['Web']['VisiblePagingItems'] / 2);
348 $PageCount = floor($TotalCount / $ItemPerPage) + 1;
349 }
350
351 if(!array_key_exists('Page', $_SESSION)) $_SESSION['Page'] = 0;
352 if(array_key_exists('page', $_GET)) $_SESSION['Page'] = $_GET['page'] * 1;
353 if($_SESSION['Page'] < 0) $_SESSION['Page'] = 0;
354 if($_SESSION['Page'] >= $PageCount) $_SESSION['Page'] = $PageCount - 1;
355 $CurrentPage = $_SESSION['Page'];
356
357
358 $Result .= 'Počet položek: <strong>'.$TotalCount.'</strong> &nbsp; Stránky: ';
359
360 $Result = '';
361 if($PageCount > 1)
362 {
363 if($CurrentPage > 0)
364 {
365 $QueryItems['page'] = 0;
366 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
367 $QueryItems['page'] = ($CurrentPage - 1);
368 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
369 }
370 $PagesMax = $PageCount - 1;
371 $PagesMin = 0;
372 if($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
373 if($PagesMin < ($CurrentPage - $Around))
374 {
375 $Result.= ' ... ';
376 $PagesMin = $CurrentPage - $Around;
377 }
378 for($i = $PagesMin; $i <= $PagesMax; $i++)
379 {
380 if($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
381 else {
382 $QueryItems['page'] = $i;
383 $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
384 }
385 }
386 if($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
387 if($CurrentPage < ($PageCount - 1))
388 {
389 $QueryItems['page'] = ($CurrentPage + 1);
390 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
391 $QueryItems['page'] = ($PageCount - 1);
392 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
393 }
394 }
395 $QueryItems['all'] = '1';
396 if($PageCount > 1) $Result.= ' <a href="?'.SetQueryStringArray($QueryItems).'">Vše</a>';
397
398 $Result = '<div style="text-align: center">'.$Result.'</div>';
399 return(array('SQLLimit' => ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage,
400 'Page' => $CurrentPage,
401 'Output' => $Result,
402 ));
403}
404
405$OrderDirSQL = array('ASC', 'DESC');
406$OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
407
408function GetOrderTableHeader($Columns, $DefaultColumn, $DefaultOrder = 0)
409{
410 global $OrderDirSQL, $OrderArrowImage, $Config, $System;
411
412 if(array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
413 if(array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
414 if(!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $DefaultColumn;
415 if(!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $DefaultOrder;
416
417 // Check OrderCol
418 $Found = false;
419 foreach($Columns as $Column)
420 {
421 if($Column['Name'] == $_SESSION['OrderCol'])
422 {
423 $Found = true;
424 break;
425 }
426 }
427 if($Found == false)
428 {
429 $_SESSION['OrderCol'] = $DefaultColumn;
430 $_SESSION['OrderDir'] = $DefaultOrder;
431 }
432 // Check OrderDir
433 if(($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1)) $_SESSION['OrderDir'] = 0;
434
435 $Result = '';
436 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
437 foreach($Columns as $Index => $Column)
438 {
439 $QueryItems['OrderCol'] = $Column['Name'];
440 $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
441 if($Column['Name'] == $_SESSION['OrderCol']) $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.$System->Link('/images/'.$OrderArrowImage[$_SESSION['OrderDir']]).'" alt="order arrow">';
442 else $ArrowImage = '';
443 if($Column['Name'] == '') $Result .= '<th>'.$Column['Title'].'</th>';
444 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column['Title'].$ArrowImage.'</a></th>';
445 }
446 return(array(
447 'SQL' => ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$OrderDirSQL[$_SESSION['OrderDir']],
448 'Output' => '<tr>'.$Result.'</tr>',
449 'Column' => $_SESSION['OrderCol'],
450 'Direction' => $_SESSION['OrderDir'],
451 ));
452}
453
454function GetRemoteAddress()
455{
456 if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
457 else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
458 else $IP = '0.0.0.0';
459 return($IP);
460}
461
462function IsInternetAddr()
463{
464 $RemoteAddr = GetRemoteAddress();
465 $RemoteAddr = explode('.', $RemoteAddr);
466// return(!(($RemoteAddr[0] == 10) and ($RemoteAddr[1] == 145)));
467 return(!(($RemoteAddr[0] == 10)));
468}
469
470function GetMemberByIP($IP)
471{
472 global $Database;
473
474 $DbResult = $Database->query('SELECT Id FROM Member WHERE (SELECT Member FROM NetworkDevice WHERE (SELECT Device FROM NetworkInterface WHERE LocalIP = "'.$IP.'") = NetworkDevice.Id) = Member.Id');
475 if($DbResult->num_rows > 0)
476 {
477 $DbRow = $DbResult->fetch_assoc();
478 return($DbRow['Id']);
479 } else return('');
480}
481
482function RemoveDiacritic($Text)
483{
484 return(str_replace(
485 array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
486 array('a', 'c', 'd', 'e', 'e', 'i', 'l', 'n', 'o', 'r', 's', 't', 'u', 'u', 'y', 'z', 'A', 'C', 'D', 'E', 'E', 'I', 'L', 'N', 'O', 'R', 'S', 'T', 'U', 'U', 'Y', 'Z'),
487 $Text));
488}
489
490function RouterOSIdent($Name)
491{
492 return(strtr(strtolower(trim($Name)), array(' ' => '-', '.' => '', '(' => '-', ')' => '-',
493 'č' => 'c', 'š' => 's', 'ě' => 'e', 'ř' => 'r', 'ž' => 'z', 'ý' => 'y', 'á' => 'a', 'í' => 'i', 'é' => 'e', 'ů' => 'u', 'ú' => 'u', 'ď' => 'd', 'ť' => 't', 'ň' => 'n', 'ó' => 'o',
494 'Č' => 'c', 'Š' => 's', 'Ě' => 'e', 'Ř' => 'r', 'Ž' => 'z', 'Ý' => 'y', 'Á' => 'a', 'Í' => 'i', 'É' => 'e', 'Ů' => 'u', 'Ú' => 'u', 'Ď' => 'd', 'Ť' => 't', 'Ň' => 'n', 'Ó' => 'o',
495)));
496}
497
498function NotBlank($Text)
499{
500 if($Text == '') return('&nbsp');
501 else return($Text);
502}
503
504function strip_cdata($string)
505{
506 preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
507 return str_replace($matches[0], $matches[1], $string);
508}
509
510function html2txt($document)
511{
512 $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
513 '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
514 '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
515 '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
516 );
517 $text = preg_replace($search, '', $document);
518 return $text;
519}
520
521function ProcessURL()
522{
523 if(array_key_exists('REDIRECT_QUERY_STRING', $_SERVER))
524 $PathString = $_SERVER['REDIRECT_QUERY_STRING'];
525 else $PathString = '';
526 if(substr($PathString, -1, 1) == '/') $PathString = substr($PathString, 0, -1);
527 $PathItems = explode('/', $PathString);
528 if(strpos($_SERVER['REQUEST_URI'], '?') !== false)
529 $_SERVER['QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
530 else $_SERVER['QUERY_STRING'] = '';
531 parse_str($_SERVER['QUERY_STRING'], $_GET);
532 return($PathItems);
533}
534
535GlobalInit();
536
537?>
Note: See TracBrowser for help on using the repository browser.