source: trunk/global.php@ 342

Last change on this file since 342 was 342, checked in by chronos, 13 years ago
  • Upraveno: Databáze nyní hlásí chyby dotazů přes výjimky.
  • Opraveno: Inicializace modulů.
  • Property svn:executable set to *
File size: 16.8 KB
Line 
1<?php
2
3$ConfigFileName = dirname(__FILE__).'/config.php';
4
5if(file_exists($ConfigFileName)) include_once($ConfigFileName);
6 else die('Nenalezen konfigurační soubor '.$ConfigFileName.'!');
7
8include_once('database.php');
9include_once('Common/Error.php');
10include_once('Common/Code.php');
11include_once('module.php');
12include_once('forms.php');
13include_once('Modules/page.php');
14include_once('file.php');
15include_once('Modules/log.php');
16include_once('aktuality/news.php');
17include_once('webcam/webcam.php');
18include_once('finance/bills.php');
19include_once('finance/finance.php');
20include_once('Modules/Module.php');
21include_once('Model.php');
22include_once('Modules/User/user.php');
23
24$PrefixMultipliers = array
25(
26 'Binary' => array
27 (
28 'BaseIndex' => 0,
29 'Definition' => array
30 (
31 array('', '', pow(2, 0)),
32 array('Ki', 'kibi', pow(2, 10)),
33 array('Mi', 'mebi', pow(2, 20)),
34 array('Gi', 'gibi', pow(2, 30)),
35 array('Ti', 'tebi', pow(2, 40)),
36 array('Pi', 'pebi', pow(2, 50)),
37 array('Ei', 'exbi', pow(2, 60)),
38 array('Zi', 'zebi', pow(2, 70)),
39 array('Yi', 'yobi', pow(2, 80)),
40 ),
41 ),
42 'Decimal' => array
43 (
44 'BaseIndex' => 8,
45 'Definition' => array
46 (
47 array('y', 'yocto', pow(10, -24)),
48 array('z', 'zepto', pow(10, -21)),
49 array('a', 'atto', pow(10, -18)),
50 array('f', 'femto', pow(10, -15)),
51 array('p', 'piko', pow(10, -12)),
52 array('n', 'nano', pow(10, -9)),
53 array('u', 'mikro', pow(10, -6)),
54 array('m', 'mili', pow(10, -3)),
55 array('', '', pow(10, 0)),
56 array('k', 'kilo', pow(10, 3)),
57 array('M', 'mega', pow(10, 6)),
58 array('G', 'giga', pow(10, 9)),
59 array('T', 'tera', pow(10, 12)),
60 array('P', 'peta', pow(10, 15)),
61 array('E', 'exa', pow(10, 18)),
62 array('Z', 'zetta', pow(10, 21)),
63 array('Y', 'yotta', pow(10, 24)),
64 ),
65 ),
66 'Time' => array
67 (
68 'BaseIndex' => 8,
69 'Definition' => array
70 (
71 array('ys', 'yoctosekunda', pow(10, -24)),
72 array('zs', 'zeptosekunda', pow(10, -21)),
73 array('as', 'attosekunda', pow(10, -18)),
74 array('fs', 'femtosekunda', pow(10, -15)),
75 array('ps', 'pikosekunda', pow(10, -12)),
76 array('ns', 'nanosekunda', pow(10, -9)),
77 array('us', 'mikrosekunda', pow(10, -6)),
78 array('ms', 'milisekunda', pow(10, -3)),
79 array('s', 'sekunda', 1),
80 array('minut', 'minuta', 60),
81 array('hodin', 'hodina', 60 * 60) ,
82 array('dnů', 'den', 24 * 60 * 60),
83 array('týdnů', 'týden', 7 * 24 * 60 * 60),
84 array('měsíců', 'měsíc', 30 * 24 * 60 * 60),
85 array('roků', 'rok', 364 * 24 * 60 * 60),
86 array('desetiletí', 'desetiletí', 10 * 364 * 24 * 60 * 60),
87 array('stalatí', 'staletí', 100 * 364 * 24 * 60 * 60),
88 array('tisíciletí', 'tisiciletí', 10000 * 364 * 24 * 60 * 60),
89 ),
90 ),
91);
92
93class System extends OldModule
94{
95 var $Modules = array();
96
97 function ModulePresent($Name)
98 {
99 return(array_key_exists($Name, $this->Modules));
100 }
101
102 function AddModule($Module)
103 {
104 global $Database;
105
106 //echo('Přidávám modul '.get_class($Module).'<br />');
107 $Module->System = &$this;
108 $Module->Database = &$Database;
109 $this->Modules[get_class($Module)] = $Module;
110 }
111
112 function AddEmailToQueue($Address, $Subject, $Content, $Headers = '')
113 {
114 $this->Database->insert('EmailQueue', array('Address' => $Address, 'Subject' => $Subject, 'Content' => $Content, 'Time' => 'NOW()', 'Headers' => $Headers));
115 }
116
117 function MailUTF8($To, $Subject = '(No subject)', $Message = '', $Header = '')
118 {
119 $Header = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n".$Header;
120 mail($To, '=?UTF-8?B?'.base64_encode($Subject).'?=', $Message, $Header);
121 //echo('mail('.$To.', =?UTF-8?B?'.base64_encode($Subject).'?=, '.$Message.', '.$Header.')<br/>');
122 }
123
124 function ProcessEmailQueue()
125 {
126 $Output = '';
127 $DbResult = $this->Database->select('EmailQueue', '*', 'Archive=0');
128 while($DbRow = $DbResult->fetch_assoc())
129 {
130 $this->MailUTF8($DbRow['Address'], $DbRow['Subject'], $DbRow['Content'], $DbRow['Headers']);
131 //echo('mail('.$DbRow['Address'].', '.$DbRow['Subject'].', '.$DbRow['Content'].', FromUTF8('.$DbRow['Headers'].', \'iso2\'));');
132 $this->Database->update('EmailQueue', 'Id='.$DbRow['Id'], array('Archive' => 1));
133 $this->Modules['Log']->NewRecord('System', 'SendEmail', $DbRow['Id']);
134 $Output .= 'To: '.$DbRow['Address'].' Subject: '.$DbRow['Subject'].'<br />';
135 }
136 return($Output);
137 }
138
139 function HumanDate($Time)
140 {
141 return(date('j.n.Y', $Time));
142 }
143
144 function TruncateDigits($Value, $Digits = 4)
145 {
146 for($II = 2; $II > -6; $II--)
147 {
148 if($Value >= pow(10, $II))
149 {
150 if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits;
151 $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);
152 break;
153 }
154 }
155 return($Value);
156 }
157
158 function AddPrefixMultipliers($Value, $Unit, $Digits = 4, $PrefixType = 'Decimal')
159 {
160 global $PrefixMultipliers;
161
162 $Negative = ($Value < 0);
163 $Value = abs($Value);
164 if(($Unit == '') and ($PrefixType != 'Time'))
165 return($this->TruncateDigits($Value, $Digits));
166
167 $I = $PrefixMultipliers[$PrefixType]['BaseIndex'];
168 if($Value == 0) return($Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit);
169
170 if($Value > 1)
171 {
172 while((($I + 1) <= count($PrefixMultipliers[$PrefixType]['Definition'])) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I + 1][2]) > 1))
173 $I = $I + 1;
174 } else
175 if($Value < 1)
176 {
177 while((($I - 1) >= 0) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2]) < 1))
178 $I = $I - 1;
179 }
180 $Value = $Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2];
181
182 // Truncate digits count
183 $Value = $this->TruncateDigits($Value, $Digits);
184 if($Negative) $Value = -$Value;
185 return($Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit);
186 }
187
188 function Link($Target)
189 {
190 global $Config;
191
192 return($Config['Web']['RootFolder'].$Target);
193 }
194}
195
196function GlobalInit()
197{
198 global $Config, $Database, $System, $ScriptTimeStart, $ConfigFileName;
199
200 $ScriptTimeStart = GetMicrotime();
201 // SQL injection hack protection
202 foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
203 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
204
205 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
206
207 $Database = new Database($Config['Database']['Host'], $Config['Database']['User'], $Config['Database']['Password'], $Config['Database']['Database']);
208 $Database->Prefix = $Config['Database']['Prefix'];
209 $Database->charset($Config['Database']['Charset']);
210 $Database->ShowSQLError = $Config['Web']['ShowSQLError'];
211 $Database->ShowSQLQuery = $Config['Web']['ShowSQLQuery'];
212
213 $System = new System();
214 $System->Config = $Config;
215 $System->Database = &$Database;
216 $System->AddModule(new Log());
217 $System->AddModule(new User());
218 if(isset($_SERVER['REMOTE_ADDR'])) $System->Modules['User']->Check();
219 $System->AddModule(new News());
220 $System->AddModule(new Webcam());
221 $System->AddModule(new Bill());
222 $System->AddModule(new Finance());
223 $System->Modules['Finance']->LoadMonthParameters(0);
224
225 $System->ModularSystem = new ModularSystem($Database);
226 $System->ModularSystem->Install();
227 //$System->ModularSystem->ReloadList();
228 $System->ModularSystem->Init();
229}
230
231$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
232
233$UnitNames = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
234
235function HumanSize($Value)
236{
237 global $UnitNames;
238
239 $UnitIndex = 0;
240 while($Value > 1024)
241 {
242 $Value = round($Value / 1024, 3);
243 $UnitIndex++;
244 }
245 return($Value.' '.$UnitNames[$UnitIndex]);
246}
247
248function GetMicrotime()
249{
250 list($Usec, $Sec) = explode(' ', microtime());
251 return ((float)$Usec + (float)$Sec);
252}
253
254function ShowArray($Pole)
255{
256 echo('<pre style="font-size: 8pt;">');
257 print_r($Pole);
258 echo('</pre>');
259}
260
261function TimeToMysqlDateTime($Time)
262{
263 return(date('Y-m-d H:i:s', $Time));
264}
265
266function MysqlDateTimeToTime($Time)
267{
268 $Parts = explode(' ', $Time);
269 $DateParts = explode('-', $Parts[0]);
270 $TimeParts = explode(':', $Parts[1]);
271 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
272 return($Result);
273}
274
275function MysqlDateToTime($Time)
276{
277 return(MysqlDateTimeToTime($Time.' 0:0:0'));
278}
279
280function HumanDate($Time)
281{
282 $Date = explode(' ', $Time);
283 $Parts = explode('-', $Date[0]);
284 if($Date != '0000-00-00') return(($Parts[2]*1).'.'.($Parts[1]*1).'.'.$Parts[0]);
285 else return('&nbsp;');
286}
287
288// Zobrazení číselný seznamu stránek
289function PagesList($URL, $Page, $TotalCount, $CountPerPage)
290{
291 $Count = ceil($TotalCount / $CountPerPage);
292 $Around = 10;
293 $Result = '';
294 if($Count>1)
295 {
296 if($Page>0)
297 {
298 $Result.= '<a href="'.$URL.'0">&lt;&lt;</a> ';
299 $Result.= '<a href="'.$URL.($Page-1).'">&lt;</a> ';
300 }
301 $PagesMax = $Count-1;
302 $PagesMin = 0;
303 if($PagesMax>($Page+$Around)) $PagesMax = $Page+$Around;
304 if($PagesMin<($Page-$Around))
305 {
306 $Result.= ' .. ';
307 $PagesMin = $Page-$Around;
308 }
309 for($i=$PagesMin;$i<=$PagesMax;$i++)
310 {
311 if($i==$Page) $Result.= '<strong>';
312 $Result.= '<a href="'.$URL.$i.'">'.($i+1).'</a> ';
313 if($i==$Page) $Result.= '</strong>';
314 }
315 if($PagesMax<($Count-1)) $Result .= ' .. ';
316 if($Page<($Count-1))
317 {
318 $Result.= '<a href="'.$URL.($Page+1).'">&gt;</a> ';
319 $Result.= '<a href="'.$URL.($Count-1).'">&gt;&gt;</a>';
320 }
321 }
322 return($Result);
323}
324
325function GetQueryStringArray($QueryString)
326{
327 $Result = array();
328 $Parts = explode('&', $QueryString);
329 foreach($Parts as $Part)
330 {
331 if($Part != '')
332 {
333 if(!strpos($Part, '=')) $Part .= '=';
334 $Item = explode('=', $Part);
335 $Result[$Item[0]] = $Item[1];
336 }
337 }
338 return($Result);
339}
340
341function SetQueryStringArray($QueryStringArray)
342{
343 $Parts = array();
344 foreach($QueryStringArray as $Index => $Item)
345 {
346 $Parts[] = $Index.'='.$Item;
347 }
348 return(implode('&', $Parts));
349}
350
351function GetPageList($TotalCount)
352{
353 global $System;
354
355 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
356
357 $ItemPerPage = $System->Config['Web']['ItemsPerPage'];
358 $Around = round($System->Config['Web']['VisiblePagingItems'] / 2);
359 $Result = '';
360 $PageCount = floor($TotalCount / $ItemPerPage) + 1;
361
362 if(!array_key_exists('Page', $_SESSION)) $_SESSION['Page'] = 0;
363 if(array_key_exists('page', $_GET)) $_SESSION['Page'] = $_GET['page'] * 1;
364 if($_SESSION['Page'] < 0) $_SESSION['Page'] = 0;
365 if($_SESSION['Page'] >= $PageCount) $_SESSION['Page'] = $PageCount - 1;
366 $CurrentPage = $_SESSION['Page'];
367
368
369 $Result .= 'Počet položek: <strong>'.$TotalCount.'</strong> &nbsp; Stránky: ';
370
371 $Result = '';
372 if($PageCount > 1)
373 {
374 if($CurrentPage > 0)
375 {
376 $QueryItems['page'] = 0;
377 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
378 $QueryItems['page'] = ($CurrentPage - 1);
379 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
380 }
381 $PagesMax = $PageCount - 1;
382 $PagesMin = 0;
383 if($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
384 if($PagesMin < ($CurrentPage - $Around))
385 {
386 $Result.= ' ... ';
387 $PagesMin = $CurrentPage - $Around;
388 }
389 for($i = $PagesMin; $i <= $PagesMax; $i++)
390 {
391 if($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
392 else {
393 $QueryItems['page'] = $i;
394 $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
395 }
396 }
397 if($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
398 if($CurrentPage < ($PageCount - 1))
399 {
400 $QueryItems['page'] = ($CurrentPage + 1);
401 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
402 $QueryItems['page'] = ($PageCount - 1);
403 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
404 }
405 }
406 $Result = '<div style="text-align: center">'.$Result.'</div>';
407 return(array('SQLLimit' => ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage,
408 'Page' => $CurrentPage,
409 'Output' => $Result,
410 ));
411}
412
413$OrderDirSQL = array('ASC', 'DESC');
414$OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
415
416function GetOrderTableHeader($Columns, $DefaultColumn, $DefaultOrder = 0)
417{
418 global $OrderDirSQL, $OrderArrowImage, $Config, $System;
419
420 if(array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
421 if(array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
422 if(!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $DefaultColumn;
423 if(!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $DefaultOrder;
424
425 // Check OrderCol
426 $Found = false;
427 foreach($Columns as $Column)
428 {
429 if($Column['Name'] == $_SESSION['OrderCol'])
430 {
431 $Found = true;
432 break;
433 }
434 }
435 if($Found == false)
436 {
437 $_SESSION['OrderCol'] = $DefaultColumn;
438 $_SESSION['OrderDir'] = $DefaultOrder;
439 }
440 // Check OrderDir
441 if(($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1)) $_SESSION['OrderDir'] = 0;
442
443 $Result = '';
444 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
445 foreach($Columns as $Index => $Column)
446 {
447 $QueryItems['OrderCol'] = $Column['Name'];
448 $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
449 if($Column['Name'] == $_SESSION['OrderCol']) $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.$System->Link('/images/'.$OrderArrowImage[$_SESSION['OrderDir']]).'" alt="order arrow">';
450 else $ArrowImage = '';
451 if($Column['Name'] == '') $Result .= '<th>'.$Column['Title'].'</th>';
452 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column['Title'].$ArrowImage.'</a></th>';
453 }
454 return(array(
455 'SQL' => ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$OrderDirSQL[$_SESSION['OrderDir']],
456 'Output' => '<tr>'.$Result.'</tr>',
457 'Column' => $_SESSION['OrderCol'],
458 'Direction' => $_SESSION['OrderDir'],
459 ));
460}
461function GetRemoteAddress()
462{
463 if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
464 else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
465 else $IP = '0.0.0.0';
466 return($IP);
467}
468
469function IsInternetAddr()
470{
471 $RemoteAddr = GetRemoteAddress();
472 $RemoteAddr = explode('.', $RemoteAddr);
473// return(!(($RemoteAddr[0] == 10) and ($RemoteAddr[1] == 145)));
474 return(!(($RemoteAddr[0] == 10)));
475}
476
477function GetMemberByIP($IP)
478{
479 global $Database;
480
481 $DbResult = $Database->query('SELECT Id FROM Member WHERE (SELECT Member FROM NetworkDevice WHERE (SELECT Device FROM NetworkInterface WHERE LocalIP = "'.$IP.'") = NetworkDevice.Id) = Member.Id');
482 if($DbResult->num_rows > 0)
483 {
484 $DbRow = $DbResult->fetch_assoc();
485 return($DbRow['Id']);
486 } else return('');
487}
488
489function RemoveDiacritic($Text)
490{
491 return(str_replace(
492 array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
493 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'),
494 $Text));
495}
496
497function RouterOSIdent($Name)
498{
499 return(strtr(strtolower(trim($Name)), array(' ' => '-', '.' => '', '(' => '-', ')' => '-',
500 'č' => 'c', 'š' => 's', 'ě' => 'e', 'ř' => 'r', 'ž' => 'z', 'ý' => 'y', 'á' => 'a', 'í' => 'i', 'é' => 'e', 'ů' => 'u', 'ú' => 'u', 'ď' => 'd', 'ť' => 't', 'ň' => 'n', 'ó' => 'o',
501 'Č' => 'c', 'Š' => 's', 'Ě' => 'e', 'Ř' => 'r', 'Ž' => 'z', 'Ý' => 'y', 'Á' => 'a', 'Í' => 'i', 'É' => 'e', 'Ů' => 'u', 'Ú' => 'u', 'Ď' => 'd', 'Ť' => 't', 'Ň' => 'n', 'Ó' => 'o',
502)));
503}
504
505function NotBlank($Text)
506{
507 if($Text == '') return('&nbsp');
508 else return($Text);
509}
510
511function strip_cdata($string)
512{
513 preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
514 return str_replace($matches[0], $matches[1], $string);
515}
516
517function html2txt($document)
518{
519 $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
520 '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
521 '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
522 '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
523 );
524 $text = preg_replace($search, '', $document);
525 return $text;
526}
527
528function DebugLog($Text)
529{
530 global $Config;
531
532 if($Config['Web']['ShowDebug'])
533 {
534 if(isset($_SERVER['REMOTE_ADDR'])) $Ending = '<br/>'."\n";
535 else $Ending = "\n";
536 echo($Text.$Ending);
537 }
538}
539
540GlobalInit();
541
542?>
Note: See TracBrowser for help on using the repository browser.