source: trunk/Common/Global.php@ 459

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