source: trunk/Common/Global.php@ 541

Last change on this file since 541 was 541, checked in by chronos, 12 years ago
  • Přidáno: Modul Search pro prohledávání obsahu.
  • Property svn:executable set to *
File size: 16.1 KB
Line 
1<?php
2
3/* @var $System System */
4$System = NULL;
5/* @var $Database Database */
6$Database = NULL;
7
8$ConfigFileName = dirname(__FILE__).'/../config.php';
9if(file_exists($ConfigFileName)) include_once($ConfigFileName);
10include_once(dirname(__FILE__).'/Version.php');
11include_once(dirname(__FILE__).'/Update.php');
12include_once(dirname(__FILE__).'/VarDumper.php');
13include_once(dirname(__FILE__).'/Module.php');
14include_once(dirname(__FILE__).'/AppModule.php');
15include_once(dirname(__FILE__).'/Database.php');
16include_once(dirname(__FILE__).'/UTF8.php');
17include_once(dirname(__FILE__).'/System.php');
18include_once(dirname(__FILE__).'/Mail.php');
19include_once(dirname(__FILE__).'/Page.php');
20include_once(dirname(__FILE__).'/Form/Form.php');
21include_once(dirname(__FILE__).'/../FormClasses.php');
22
23// Application modules
24// TODO: Should be configurable
25include_once(dirname(__FILE__).'/../Modules/System/System.php');
26include_once(dirname(__FILE__).'/../Modules/Error/Error.php');
27include_once(dirname(__FILE__).'/../Modules/Log/Log.php');
28include_once(dirname(__FILE__).'/../Modules/File/File.php');
29include_once(dirname(__FILE__).'/../Modules/Meteostation/Meteostation.php');
30include_once(dirname(__FILE__).'/../Modules/Portal/Portal.php');
31include_once(dirname(__FILE__).'/../Modules/IS/IS.php');
32include_once(dirname(__FILE__).'/../Modules/Network/Network.php');
33include_once(dirname(__FILE__).'/../Modules/TV/TV.php');
34include_once(dirname(__FILE__).'/../Modules/OpeningHours/OpeningHours.php');
35include_once(dirname(__FILE__).'/../Modules/Map/Map.php');
36include_once(dirname(__FILE__).'/../Modules/Chat/Chat.php');
37include_once(dirname(__FILE__).'/../Modules/WebCam/WebCam.php');
38include_once(dirname(__FILE__).'/../Modules/User/User.php');
39include_once(dirname(__FILE__).'/../Modules/Finance/Finance.php');
40include_once(dirname(__FILE__).'/../Modules/FinanceBankAPI/FinanceBankAPI.php');
41include_once(dirname(__FILE__).'/../Modules/Customer/Customer.php');
42include_once(dirname(__FILE__).'/../Modules/NetworkShare/NetworkShare.php');
43include_once(dirname(__FILE__).'/../Modules/News/News.php');
44include_once(dirname(__FILE__).'/../Modules/Meals/Meals.php');
45include_once(dirname(__FILE__).'/../Modules/NetworkTopology/NetworkTopology.php');
46include_once(dirname(__FILE__).'/../Modules/NetworkConfig/NetworkConfig.php');
47include_once(dirname(__FILE__).'/../Modules/NetworkConfigLinux/NetworkConfigLinux.php');
48include_once(dirname(__FILE__).'/../Modules/NetworkConfigRouterOS/NetworkConfigRouterOS.php');
49include_once(dirname(__FILE__).'/../Modules/TimeMeasure/TimeMeasure.php');
50include_once(dirname(__FILE__).'/../Modules/Task/Task.php');
51include_once(dirname(__FILE__).'/../Modules/Stock/Stock.php');
52include_once(dirname(__FILE__).'/../Modules/Search/Search.php');
53
54function GlobalInit()
55{
56 global $Config, $Database, $System, $ScriptTimeStart, $ConfigFileName, $Mail, $Type,
57 $DatabaseRevision;
58
59 date_default_timezone_set('Europe/Prague');
60 $ScriptTimeStart = GetMicrotime();
61
62 if(!isset($Config)) die('Systém není nainstalován. Pokračujte v instalaci <a href="admin/">zde</a>.');
63
64 // SQL injection hack protection
65 foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
66 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
67
68 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
69
70 $System = new System();
71 // TODO: unset general global variable $Config after setting is loaded to objects
72 $System->Config = &$Config;
73 $System->Database->Connect($Config['Database']['Host'], $Config['Database']['User'], $Config['Database']['Password'], $Config['Database']['Database']);
74 $System->Database->Prefix = $Config['Database']['Prefix'];
75 $System->Database->charset($Config['Database']['Charset']);
76 $System->Database->ShowSQLError = $Config['Web']['ShowSQLError'];
77 $System->Database->ShowSQLQuery = $Config['Web']['ShowSQLQuery'];
78 $System->RootURLFolder = $Config['Web']['RootFolder'];
79 $System->FormManager->Root = $Config['Web']['RootFolder'];
80
81 // Check database persistence structure
82 $UpdateManager = new UpdateManager();
83 $UpdateManager->Database = &$System->Database;
84 $UpdateManager->Revision = $DatabaseRevision;
85 if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
86 if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
87
88 $Database = $System->Database;
89 RegisterFormClasses($System->FormManager);
90
91 // Register new modules
92 // TODO: Should be configurable
93 $System->ModuleManager->RegisterModule(new ModuleError($System));
94 $System->ModuleManager->RegisterModule(new ModuleSystem($System));
95 $System->ModuleManager->RegisterModule(new ModuleLog($System));
96 $System->ModuleManager->RegisterModule(new ModuleFile($System));
97 $System->ModuleManager->RegisterModule(new ModuleUser($System));
98 $System->ModuleManager->RegisterModule(new ModuleMeteostation($System));
99 $System->ModuleManager->RegisterModule(new ModulePortal($System));
100 $System->ModuleManager->RegisterModule(new ModuleIS($System));
101 $System->ModuleManager->RegisterModule(new ModuleNetwork($System));
102 $System->ModuleManager->RegisterModule(new ModuleTV($System));
103 $System->ModuleManager->RegisterModule(new ModuleOpeningHours($System));
104 $System->ModuleManager->RegisterModule(new ModuleMap($System));
105 $System->ModuleManager->RegisterModule(new ModuleChat($System));
106 $System->ModuleManager->RegisterModule(new ModuleWebCam($System));
107 $System->ModuleManager->RegisterModule(new ModuleFinance($System));
108 $System->ModuleManager->RegisterModule(new ModuleFinanceBankAPI($System));
109 $System->ModuleManager->RegisterModule(new ModuleCustomer($System));
110 $System->ModuleManager->RegisterModule(new ModuleNetworkShare($System));
111 $System->ModuleManager->RegisterModule(new ModuleNews($System));
112 $System->ModuleManager->RegisterModule(new ModuleMeals($System));
113 $System->ModuleManager->RegisterModule(new ModuleNetworkTopology($System));
114 $System->ModuleManager->RegisterModule(new ModuleNetworkConfig($System));
115 $System->ModuleManager->RegisterModule(new ModuleNetworkConfigRouterOS($System));
116 $System->ModuleManager->RegisterModule(new ModuleNetworkConfigLinux($System));
117 $System->ModuleManager->RegisterModule(new ModuleTimeMeasure($System));
118 $System->ModuleManager->RegisterModule(new ModuleTask($System));
119 $System->ModuleManager->RegisterModule(new ModuleStock($System));
120 $System->ModuleManager->RegisterModule(new ModuleSearch($System));
121 $System->ModuleManager->StartAll();
122}
123
124$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
125
126$UnitNames = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
127
128function HumanSize($Value)
129{
130 global $UnitNames;
131
132 $UnitIndex = 0;
133 while($Value > 1024)
134 {
135 $Value = round($Value / 1024, 3);
136 $UnitIndex++;
137 }
138 return($Value.' '.$UnitNames[$UnitIndex]);
139}
140
141function GetMicrotime()
142{
143 list($Usec, $Sec) = explode(' ', microtime());
144 return ((float)$Usec + (float)$Sec);
145}
146
147function ShowArray($Pole)
148{
149 echo('<pre style="font-size: 8pt;">');
150 print_r($Pole);
151 echo('</pre>');
152}
153
154function HumanDate($Time)
155{
156 $Date = explode(' ', $Time);
157 $Parts = explode('-', $Date[0]);
158 if($Date != '0000-00-00') return(($Parts[2]*1).'.'.($Parts[1]*1).'.'.$Parts[0]);
159 else return('&nbsp;');
160}
161
162// Show page listing numbers
163function PagesList($URL, $Page, $TotalCount, $CountPerPage)
164{
165 $Count = ceil($TotalCount / $CountPerPage);
166 $Around = 10;
167 $Result = '';
168 if($Count>1)
169 {
170 if($Page>0)
171 {
172 $Result.= '<a href="'.$URL.'0">&lt;&lt;</a> ';
173 $Result.= '<a href="'.$URL.($Page-1).'">&lt;</a> ';
174 }
175 $PagesMax = $Count-1;
176 $PagesMin = 0;
177 if($PagesMax>($Page+$Around)) $PagesMax = $Page+$Around;
178 if($PagesMin<($Page-$Around))
179 {
180 $Result.= ' .. ';
181 $PagesMin = $Page-$Around;
182 }
183 for($i=$PagesMin;$i<=$PagesMax;$i++)
184 {
185 if($i==$Page) $Result.= '<strong>';
186 $Result.= '<a href="'.$URL.$i.'">'.($i+1).'</a> ';
187 if($i==$Page) $Result.= '</strong>';
188 }
189 if($PagesMax<($Count-1)) $Result .= ' .. ';
190 if($Page<($Count-1))
191 {
192 $Result.= '<a href="'.$URL.($Page+1).'">&gt;</a> ';
193 $Result.= '<a href="'.$URL.($Count-1).'">&gt;&gt;</a>';
194 }
195 }
196 return($Result);
197}
198
199function ExtractTime($Time)
200{
201 return(array(
202 'Year' => date('Y', $Time),
203 'Month' => date('n', $Time),
204 'Day' => date('j', $Time),
205 'Hour' => date('h', $Time),
206 'Minute' => date('i', $Time),
207 'Second' => date('s', $Time)
208 ));
209}
210
211function GetQueryStringArray($QueryString)
212{
213 $Result = array();
214 $Parts = explode('&', $QueryString);
215 foreach($Parts as $Part)
216 {
217 if($Part != '')
218 {
219 if(!strpos($Part, '=')) $Part .= '=';
220 $Item = explode('=', $Part);
221 $Result[$Item[0]] = $Item[1];
222 }
223 }
224 return($Result);
225}
226
227function SetQueryStringArray($QueryStringArray)
228{
229 $Parts = array();
230 foreach($QueryStringArray as $Index => $Item)
231 {
232 $Parts[] = $Index.'='.$Item;
233 }
234 return(implode('&amp;', $Parts));
235}
236
237function GetPageList($TotalCount)
238{
239 global $System;
240
241 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
242
243 $Result = '';
244 if(array_key_exists('all', $QueryItems))
245 {
246 $PageCount = 1;
247 $ItemPerPage = $TotalCount;
248 } else
249 {
250 $ItemPerPage = $System->Config['Web']['ItemsPerPage'];
251 $Around = round($System->Config['Web']['VisiblePagingItems'] / 2);
252 $PageCount = floor($TotalCount / $ItemPerPage) + 1;
253 }
254
255 if(!array_key_exists('Page', $_SESSION)) $_SESSION['Page'] = 0;
256 if(array_key_exists('page', $_GET)) $_SESSION['Page'] = $_GET['page'] * 1;
257 if($_SESSION['Page'] < 0) $_SESSION['Page'] = 0;
258 if($_SESSION['Page'] >= $PageCount) $_SESSION['Page'] = $PageCount - 1;
259 $CurrentPage = $_SESSION['Page'];
260
261
262 $Result .= 'Počet položek: <strong>'.$TotalCount.'</strong> &nbsp; Stránky: ';
263
264 $Result = '';
265 if($PageCount > 1)
266 {
267 if($CurrentPage > 0)
268 {
269 $QueryItems['page'] = 0;
270 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
271 $QueryItems['page'] = ($CurrentPage - 1);
272 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
273 }
274 $PagesMax = $PageCount - 1;
275 $PagesMin = 0;
276 if($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
277 if($PagesMin < ($CurrentPage - $Around))
278 {
279 $Result.= ' ... ';
280 $PagesMin = $CurrentPage - $Around;
281 }
282 for($i = $PagesMin; $i <= $PagesMax; $i++)
283 {
284 if($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
285 else {
286 $QueryItems['page'] = $i;
287 $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
288 }
289 }
290 if($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
291 if($CurrentPage < ($PageCount - 1))
292 {
293 $QueryItems['page'] = ($CurrentPage + 1);
294 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
295 $QueryItems['page'] = ($PageCount - 1);
296 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
297 }
298 }
299 $QueryItems['all'] = '1';
300 if($PageCount > 1) $Result.= ' <a href="?'.SetQueryStringArray($QueryItems).'">Vše</a>';
301
302 $Result = '<div style="text-align: center">'.$Result.'</div>';
303 return(array('SQLLimit' => ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage,
304 'Page' => $CurrentPage,
305 'Output' => $Result,
306 ));
307}
308
309$OrderDirSQL = array('ASC', 'DESC');
310$OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
311
312function GetOrderTableHeader($Columns, $DefaultColumn, $DefaultOrder = 0)
313{
314 global $OrderDirSQL, $OrderArrowImage, $Config, $System;
315
316 if(array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
317 if(array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
318 if(!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $DefaultColumn;
319 if(!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $DefaultOrder;
320
321 // Check OrderCol
322 $Found = false;
323 foreach($Columns as $Column)
324 {
325 if($Column['Name'] == $_SESSION['OrderCol'])
326 {
327 $Found = true;
328 break;
329 }
330 }
331 if($Found == false)
332 {
333 $_SESSION['OrderCol'] = $DefaultColumn;
334 $_SESSION['OrderDir'] = $DefaultOrder;
335 }
336 // Check OrderDir
337 if(($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1)) $_SESSION['OrderDir'] = 0;
338
339 $Result = '';
340 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
341 foreach($Columns as $Index => $Column)
342 {
343 $QueryItems['OrderCol'] = $Column['Name'];
344 $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
345 if($Column['Name'] == $_SESSION['OrderCol']) $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.$System->Link('/images/'.$OrderArrowImage[$_SESSION['OrderDir']]).'" alt="order arrow">';
346 else $ArrowImage = '';
347 if($Column['Name'] == '') $Result .= '<th>'.$Column['Title'].'</th>';
348 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column['Title'].$ArrowImage.'</a></th>';
349 }
350 return(array(
351 'SQL' => ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$OrderDirSQL[$_SESSION['OrderDir']],
352 'Output' => '<tr>'.$Result.'</tr>',
353 'Column' => $_SESSION['OrderCol'],
354 'Direction' => $_SESSION['OrderDir'],
355 ));
356}
357
358function GetRemoteAddress()
359{
360 if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
361 else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
362 else $IP = '0.0.0.0';
363 return($IP);
364}
365
366function IsInternetAddr()
367{
368 $RemoteAddr = GetRemoteAddress();
369 $RemoteAddr = explode('.', $RemoteAddr);
370// return(!(($RemoteAddr[0] == 10) and ($RemoteAddr[1] == 145)));
371 return(!(($RemoteAddr[0] == 10)));
372}
373
374function GetMemberByIP($IP)
375{
376 global $Database;
377
378 $DbResult = $Database->query('SELECT Id FROM Member WHERE (SELECT Member FROM NetworkDevice WHERE (SELECT Device FROM NetworkInterface WHERE LocalIP = "'.$IP.'") = NetworkDevice.Id) = Member.Id');
379 if($DbResult->num_rows > 0)
380 {
381 $DbRow = $DbResult->fetch_assoc();
382 return($DbRow['Id']);
383 } else return('');
384}
385
386function RemoveDiacritic($Text)
387{
388 return(str_replace(
389 array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů', 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
390 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'),
391 $Text));
392}
393
394function RouterOSIdent($Name)
395{
396 return(strtr(strtolower(trim($Name)), array(' ' => '-', '.' => '', '(' => '-', ')' => '-',
397 'č' => 'c', 'š' => 's', 'ě' => 'e', 'ř' => 'r', 'ž' => 'z', 'ý' => 'y', 'á' => 'a', 'í' => 'i', 'é' => 'e', 'ů' => 'u', 'ú' => 'u', 'ď' => 'd', 'ť' => 't', 'ň' => 'n', 'ó' => 'o',
398 'Č' => 'c', 'Š' => 's', 'Ě' => 'e', 'Ř' => 'r', 'Ž' => 'z', 'Ý' => 'y', 'Á' => 'a', 'Í' => 'i', 'É' => 'e', 'Ů' => 'u', 'Ú' => 'u', 'Ď' => 'd', 'Ť' => 't', 'Ň' => 'n', 'Ó' => 'o',
399)));
400}
401
402function NotBlank($Text)
403{
404 if($Text == '') return('&nbsp');
405 else return($Text);
406}
407
408function strip_cdata($string)
409{
410 preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
411 return str_replace($matches[0], $matches[1], $string);
412}
413
414function html2txt($document)
415{
416 $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
417 '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
418 '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
419 '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
420 );
421 $text = preg_replace($search, '', $document);
422 return $text;
423}
424
425function ProcessURL()
426{
427 if(array_key_exists('REDIRECT_QUERY_STRING', $_SERVER))
428 $PathString = $_SERVER['REDIRECT_QUERY_STRING'];
429 else $PathString = '';
430 if(substr($PathString, -1, 1) == '/') $PathString = substr($PathString, 0, -1);
431 $PathItems = explode('/', $PathString);
432 if(strpos($_SERVER['REQUEST_URI'], '?') !== false)
433 $_SERVER['QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
434 else $_SERVER['QUERY_STRING'] = '';
435 parse_str($_SERVER['QUERY_STRING'], $_GET);
436 return($PathItems);
437}
438
439GlobalInit();
440
441?>
Note: See TracBrowser for help on using the repository browser.