source: trunk/Common/Global.php

Last change on this file was 957, checked in by chronos, 8 months ago
  • Added: Store request URL into Log table.
  • Fixed: Some warnings for invalid inputs.
File size: 12.4 KB
Line 
1<?php
2
3/* @var $System System */
4$System = NULL;
5/* @var $Database Database */
6$Database = NULL;
7
8include_once(dirname(__FILE__).'/../Packages/Package.php');
9include_once(dirname(__FILE__).'/../Packages/Common/Common.php');
10include_once(dirname(__FILE__).'/Form/Form.php');
11include_once(dirname(__FILE__).'/VCL/General.php');
12include_once(dirname(__FILE__).'/VCL/Database.php');
13
14//define('NEW_PERMISSION', '1');
15
16$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen',
17 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
18
19$UnitNames = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
20$YesNo = array(false => 'Ne', true => 'Ano');
21
22function HumanSize(int $Value): string
23{
24 global $UnitNames;
25
26 $UnitIndex = 0;
27 while ($Value > 1024)
28 {
29 $Value = round($Value / 1024, 3);
30 $UnitIndex++;
31 }
32 return $Value.' '.$UnitNames[$UnitIndex];
33}
34
35function GetMicrotime(): float
36{
37 list($Usec, $Sec) = explode(' ', microtime());
38 return (float)$Usec + (float)$Sec;
39}
40
41function ShowArray(array $Array): void
42{
43 echo('<pre style="font-size: 8pt;">');
44 print_r($Array);
45 echo('</pre>');
46}
47
48function HumanDate(?string $Time): string
49{
50 if ($Time != '')
51 {
52 $Date = explode(' ', $Time);
53 $Parts = explode('-', $Date[0]);
54 if ($Date != '0000-00-00') return ($Parts[2] * 1).'.'.($Parts[1] * 1).'.'.$Parts[0];
55 else return '&nbsp;';
56 } else return '&nbsp;';
57}
58
59// Show page listing numbers
60function PagesList(string $URL, int $Page, int $TotalCount, int $CountPerPage): string
61{
62 $Count = ceil($TotalCount / $CountPerPage);
63 $Around = 10;
64 $Result = '';
65 if ($Count > 1)
66 {
67 if ($Page > 0)
68 {
69 $Result.= '<a href="'.$URL.'0">&lt;&lt;</a> ';
70 $Result.= '<a href="'.$URL.($Page - 1).'">&lt;</a> ';
71 }
72 $PagesMax = $Count - 1;
73 $PagesMin = 0;
74 if ($PagesMax > ($Page + $Around)) $PagesMax = $Page + $Around;
75 if ($PagesMin < ($Page - $Around))
76 {
77 $Result.= ' .. ';
78 $PagesMin = $Page - $Around;
79 }
80 for ($i = $PagesMin; $i <= $PagesMax; $i++)
81 {
82 if ($i == $Page) $Result.= '<strong>';
83 $Result .= '<a href="'.$URL.$i.'">'.($i + 1).'</a> ';
84 if ($i == $Page) $Result.= '</strong>';
85 }
86 if ($PagesMax < ($Count - 1)) $Result .= ' .. ';
87 if ($Page < ($Count - 1))
88 {
89 $Result .= '<a href="'.$URL.($Page + 1).'">&gt;</a> ';
90 $Result .= '<a href="'.$URL.($Count - 1).'">&gt;&gt;</a>';
91 }
92 }
93 return $Result;
94}
95
96function ExtractTime($Time): array
97{
98 return array(
99 'Year' => date('Y', $Time),
100 'Month' => date('n', $Time),
101 'Day' => date('j', $Time),
102 'Hour' => date('h', $Time),
103 'Minute' => date('i', $Time),
104 'Second' => date('s', $Time)
105 );
106}
107
108function GetQueryStringArray(string $QueryString): array
109{
110 $Result = array();
111 $Parts = explode('&', $QueryString);
112 foreach ($Parts as $Part)
113 {
114 if ($Part != '')
115 {
116 if (!strpos($Part, '=')) $Part .= '=';
117 $Item = explode('=', $Part);
118 $Result[$Item[0]] = $Item[1];
119 }
120 }
121 return $Result;
122}
123
124function SetQueryStringArray(array $QueryStringArray): string
125{
126 $Parts = array();
127 foreach ($QueryStringArray as $Index => $Item)
128 {
129 $Parts[] = $Index.'='.$Item;
130 }
131 return implode('&amp;', $Parts);
132}
133
134function GetPageList(string $ObjectName, int $TotalCount): array
135{
136 global $System;
137
138 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
139
140 $Result = '';
141 if (array_key_exists('all', $QueryItems))
142 {
143 $PageCount = 1;
144 $ItemPerPage = $TotalCount;
145 } else
146 {
147 $ItemPerPage = $System->Config['Web']['ItemsPerPage'];
148 $Around = round($System->Config['Web']['VisiblePagingItems'] / 2);
149 $PageCount = floor($TotalCount / $ItemPerPage) + 1;
150 }
151
152 if (!array_key_exists($ObjectName.'Page', $_SESSION)) $_SESSION[$ObjectName.'Page'] = 0;
153 if (array_key_exists('ObjectName', $_GET) and ($_GET['ObjectName'] == $ObjectName)) {
154 if (array_key_exists('page', $_GET)) $_SESSION[$ObjectName.'Page'] = $_GET['page'] * 1;
155 }
156 if ($_SESSION[$ObjectName.'Page'] < 0) $_SESSION[$ObjectName.'Page'] = 0;
157 if ($_SESSION[$ObjectName.'Page'] >= $PageCount) $_SESSION[$ObjectName.'Page'] = $PageCount - 1;
158 $CurrentPage = $_SESSION[$ObjectName.'Page'];
159
160 $Result .= 'Počet položek: <strong>'.$TotalCount.'</strong> &nbsp; Stránky: ';
161 $QueryItems['ObjectName'] = $ObjectName;
162
163 $Result = '';
164 if ($PageCount > 1)
165 {
166 if ($CurrentPage > 0)
167 {
168 $QueryItems['page'] = 0;
169 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
170 $QueryItems['page'] = ($CurrentPage - 1);
171 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
172 }
173 $PagesMax = $PageCount - 1;
174 $PagesMin = 0;
175 if ($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
176 if ($PagesMin < ($CurrentPage - $Around))
177 {
178 $Result.= ' ... ';
179 $PagesMin = $CurrentPage - $Around;
180 }
181 for ($i = $PagesMin; $i <= $PagesMax; $i++)
182 {
183 if ($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
184 else {
185 $QueryItems['page'] = $i;
186 $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
187 }
188 }
189 if ($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
190 if ($CurrentPage < ($PageCount - 1))
191 {
192 $QueryItems['page'] = ($CurrentPage + 1);
193 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
194 $QueryItems['page'] = ($PageCount - 1);
195 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
196 }
197 }
198 $QueryItems['all'] = '1';
199 if ($PageCount > 1) $Result.= ' <a href="?'.SetQueryStringArray($QueryItems).'">Vše</a>';
200
201 $Result = '<div style="text-align: center">'.$Result.'</div>';
202 return array('SQLLimit' => ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage,
203 'Page' => $CurrentPage,
204 'Output' => $Result,
205 );
206}
207
208$OrderDirSQL = array('ASC', 'DESC');
209$OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
210
211function GetOrderTableHeader(string $ObjectName, array $Columns, string $DefaultColumn, int $DefaultOrder = 0): array
212{
213 global $OrderDirSQL, $OrderArrowImage, $Config, $System;
214
215 if (array_key_exists('ObjectName', $_GET) and ($_GET['ObjectName'] == $ObjectName))
216 {
217 if (array_key_exists('OrderCol', $_GET)) $_SESSION[$ObjectName.'OrderCol'] = $_GET['OrderCol'];
218 if (array_key_exists('OrderDir', $_GET)) $_SESSION[$ObjectName.'OrderDir'] = $_GET['OrderDir'];
219 }
220 if (!array_key_exists($ObjectName.'OrderCol', $_SESSION)) $_SESSION[$ObjectName.'OrderCol'] = $DefaultColumn;
221 if (!array_key_exists($ObjectName.'OrderDir', $_SESSION) ) $_SESSION[$ObjectName.'OrderDir'] = $DefaultOrder;
222
223 // Check OrderCol
224 $Found = false;
225 foreach ($Columns as $Column)
226 {
227 if ($Column['Name'] == $_SESSION[$ObjectName.'OrderCol'])
228 {
229 $Found = true;
230 break;
231 }
232 }
233 if (($_SESSION[$ObjectName.'OrderCol'] == '') or ($Found == false))
234 {
235 $_SESSION[$ObjectName.'OrderCol'] = $DefaultColumn;
236 $_SESSION[$ObjectName.'OrderDir'] = $DefaultOrder;
237 }
238 // Check OrderDir
239 if (($_SESSION[$ObjectName.'OrderDir'] != 0) and ($_SESSION[$ObjectName.'OrderDir'] != 1))
240 $_SESSION[$ObjectName.'OrderDir'] = 0;
241
242 $Result = '';
243 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
244 foreach ($Columns as $Index => $Column)
245 {
246 $QueryItems['ObjectName'] = $ObjectName;
247 $QueryItems['OrderCol'] = $Column['Name'];
248 $QueryItems['OrderDir'] = 1 - $_SESSION[$ObjectName.'OrderDir'];
249 if ($Column['Name'] == $_SESSION[$ObjectName.'OrderCol'])
250 $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.
251 $System->Link('/images/'.$OrderArrowImage[$_SESSION[$ObjectName.'OrderDir']]).'" alt="order arrow">';
252 else $ArrowImage = '';
253 if ($Column['Name'] == '') $Result .= '<th>'.$Column['Title'].'</th>';
254 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column['Title'].$ArrowImage.'</a></th>';
255 }
256 return array(
257 'SQL' => ' ORDER BY `'.$_SESSION[$ObjectName.'OrderCol'].'` '.$OrderDirSQL[$_SESSION[$ObjectName.'OrderDir']],
258 'Output' => '<tr>'.$Result.'</tr>',
259 'Column' => $_SESSION[$ObjectName.'OrderCol'],
260 'Direction' => $_SESSION[$ObjectName.'OrderDir'],
261 );
262}
263
264function GetRemoteAddress(): string
265{
266 if (array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
267 else $IP = '0.0.0.0';
268 return $IP;
269}
270
271function IsInternetAddr(): bool
272{
273 global $Config;
274
275 $Result = true;
276 $RemoteAddr = GetRemoteAddress();
277 foreach ($Config['Web']['IntranetSubnets'] as $Subnet)
278 {
279 if (substr($RemoteAddr, 0, strlen($Subnet)) == $Subnet)
280 {
281 $Result = false;
282 break;
283 }
284 }
285 return $Result;
286}
287
288function GetMemberByIP(Database $Database, string $IP): string
289{
290 $DbResult = $Database->query('SELECT `Id` FROM `Member` WHERE '.
291 '(SELECT `Member` FROM `NetworkDevice` WHERE (SELECT `Device` FROM `NetworkInterface` '.
292 'WHERE `LocalIP` = "'.$IP.'") = `NetworkDevice`.`Id`) = `Member`.`Id`');
293 if ($DbResult->num_rows > 0)
294 {
295 $DbRow = $DbResult->fetch_assoc();
296 return $DbRow['Id'];
297 } else return '';
298}
299
300function CommandExist(string $Command): bool
301{
302 $Result = shell_exec('which '.$Command);
303 return !empty($Result);
304}
305
306function RemoveDiacritic(string $Text): string
307{
308 return str_replace(
309 array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů',
310 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
311 array('a', 'c', 'd', 'e', 'e', 'i', 'l', 'n', 'o', 'r', 's', 't', 'u', 'u',
312 'y', 'z', 'A', 'C', 'D', 'E', 'E', 'I', 'L', 'N', 'O', 'R', 'S', 'T', 'U', 'U', 'Y', 'Z'),
313 $Text);
314}
315
316function RouterOSIdent(string $Name): string
317{
318 return strtr(strtolower(trim($Name)), array(' ' => '-', '.' => '', '(' => '-', ')' => '-', ',' => '-',
319 'č' => 'c', 'š' => 's', 'ě' => 'e', 'ř' => 'r', 'ž' => 'z', 'ý' => 'y',
320 'á' => 'a', 'í' => 'i', 'é' => 'e', 'ů' => 'u', 'ú' => 'u', 'ď' => 'd',
321 'ť' => 't', 'ň' => 'n', 'ó' => 'o',
322 'Č' => 'c', 'Š' => 's', 'Ě' => 'e', 'Ř' => 'r', 'Ž' => 'z', 'Ý' => 'y',
323 'Á' => 'a', 'Í' => 'i', 'É' => 'e', 'Ů' => 'u', 'Ú' => 'u', 'Ď' => 'd',
324 'Ť' => 't', 'Ň' => 'n', 'Ó' => 'o',
325 ));
326}
327
328function NotBlank(string $Text): string
329{
330 if ($Text == '') return '&nbsp;';
331 else return $Text;
332}
333
334function strip_cdata(string $string): string
335{
336 preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
337 return str_replace($matches[0], $matches[1], $string);
338}
339
340function html2txt($document)
341{
342 $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
343 '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
344 '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
345 '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
346 );
347 $text = preg_replace($search, '', $document);
348 return $text;
349}
350
351function ProcessURL(): array
352{
353 if (array_key_exists('REDIRECT_QUERY_STRING', $_SERVER))
354 $PathString = $_SERVER['REDIRECT_QUERY_STRING'];
355 else $PathString = '';
356 if (substr($PathString, -1, 1) == '/') $PathString = substr($PathString, 0, -1);
357 $PathItems = explode('/', $PathString);
358 if (array_key_exists('REQUEST_URI', $_SERVER) and (strpos($_SERVER['REQUEST_URI'], '?') !== false))
359 $_SERVER['QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
360 else $_SERVER['QUERY_STRING'] = '';
361 parse_str($_SERVER['QUERY_STRING'], $_GET);
362 return $PathItems;
363}
364
365function RepeatFunction(int $Period, callable $Callback): void
366{
367 while (1)
368 {
369 $StartTime = time();
370 call_user_func($Callback);
371 $EndTime = time();
372 $Delay = $Period - ($EndTime - $StartTime);
373 if ($Delay < 0) $Delay = 0;
374
375 echo('Waiting remaining '.$Delay.' of '.$Period.' seconds period...'."\n");
376 sleep($Delay);
377 }
378}
379
380function pack_array($v, $a)
381{
382 return call_user_func_array('pack', array_merge(array($v), (array)$a));
383}
384
385function file_get_contents_curl(string $Url)
386{
387 $Ch = curl_init();
388
389 curl_setopt($Ch, CURLOPT_AUTOREFERER, TRUE);
390 curl_setopt($Ch, CURLOPT_HEADER, 0);
391 curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1);
392 curl_setopt($Ch, CURLOPT_URL, $Url);
393 curl_setopt($Ch, CURLOPT_FOLLOWLOCATION, TRUE);
394
395 $Data = curl_exec($Ch);
396 curl_close($Ch);
397 return $Data;
398}
399
400function ExecuteNoError(callable $Function): void
401{
402 try
403 {
404 $LastErrorReporting = error_reporting();
405 error_reporting(0);
406 $Function();
407 }
408 finally
409 {
410 error_reporting($LastErrorReporting);
411 }
412}
Note: See TracBrowser for help on using the repository browser.