source: trunk/Common/Global.php@ 550

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