source: trunk/Global.php

Last change on this file was 63, checked in by chronos, 3 years ago
  • Modified: Used explicit types where possible for better error reporting.
  • Modified: Updated Common packaged to newer version.
  • Modified: Simplified pages title.
  • Added: Simple keyword based spam filter for meet items.
File size: 13.5 KB
Line 
1<?php
2
3function HumanDate(int $Time): string
4{
5 return date('j.n.Y', $Time);
6}
7
8function HumanTime(int $Time): string
9{
10 return date('H:i:s', $Time);
11}
12
13function HumanDateTime(int $Time): string
14{
15 $Output = HumanDate($Time);
16 $Time = HumanTime($Time);
17 if ($Time != '00:00:00') $Output .= ' '.$Time;
18 return $Output;
19}
20
21function Link2(string $URL): string
22{
23 global $Config;
24
25 return $Config['BaseURL'].$URL;
26}
27
28function ProcessURL(): array
29{
30 if (array_key_exists('REDIRECT_QUERY_STRING', $_SERVER))
31 $PathString = $_SERVER['REDIRECT_QUERY_STRING'];
32 else $PathString = '';
33 if (substr($PathString, -1, 1) == '/') $PathString = substr($PathString, 0, -1);
34 $PathItems = explode('/', $PathString);
35 if (array_key_exists('REQUEST_URI', $_SERVER) and (strpos($_SERVER['REQUEST_URI'], '?') !== false))
36 $_SERVER['QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
37 else $_SERVER['QUERY_STRING'] = '';
38 parse_str($_SERVER['QUERY_STRING'], $_GET);
39 return $PathItems;
40}
41
42function GetQueryStringArray(string $QueryString): array
43{
44 $Result = array();
45 $Parts = explode('&', $QueryString);
46 foreach ($Parts as $Part)
47 {
48 if ($Part != '')
49 {
50 if (!strpos($Part, '=')) $Part .= '=';
51 $Item = explode('=', $Part);
52 $Result[$Item[0]] = $Item[1];
53 }
54 }
55 return $Result;
56}
57
58function SetQueryStringArray(array $QueryStringArray): string
59{
60 $Parts = array();
61 foreach ($QueryStringArray as $Index => $Item)
62 {
63 if ($Index != 'lvm') $Parts[] = $Index.'='.$Item;
64 }
65 return implode('&amp;', $Parts);
66}
67
68function HumanSize(int $Value): string
69{
70 global $UnitNames;
71
72 $UnitIndex = 0;
73 while ($Value > 1024)
74 {
75 $Value = round($Value / 1024, 3);
76 $UnitIndex++;
77 }
78 return $Value.' '.$UnitNames[$UnitIndex];
79}
80
81function GetPageList(int $TotalCount): array
82{
83 global $Config;
84
85 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
86
87 $ItemPerPage = (int)$Config['ItemsPerPage'];
88 $Around = round((int)$Config['VisiblePagingItems'] / 2);
89 $Result = '';
90 $PageCount = floor($TotalCount / $ItemPerPage) + 1;
91
92 if (!array_key_exists('Page', $_SESSION)) $_SESSION['Page'] = 0;
93 if (array_key_exists('page', $_GET)) $_SESSION['Page'] = $_GET['page'] * 1;
94 if ($_SESSION['Page'] < 0) $_SESSION['Page'] = 0;
95 if ($_SESSION['Page'] >= $PageCount) $_SESSION['Page'] = $PageCount - 1;
96 $CurrentPage = $_SESSION['Page'];
97
98
99 $Result .= 'Počet položek: <strong>'.$TotalCount.'</strong> &nbsp; Stránky: ';
100
101 $Result = '';
102 if ($PageCount > 1)
103 {
104 if ($CurrentPage > 0)
105 {
106 $QueryItems['page'] = 0;
107 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
108 $QueryItems['page'] = ($CurrentPage - 1);
109 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
110 }
111 $PagesMax = $PageCount - 1;
112 $PagesMin = 0;
113 if ($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
114 if ($PagesMin < ($CurrentPage - $Around))
115 {
116 $Result.= ' ... ';
117 $PagesMin = $CurrentPage - $Around;
118 }
119 for ($i = $PagesMin; $i <= $PagesMax; $i++)
120 {
121 if ($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
122 else {
123 $QueryItems['page'] = $i;
124 $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
125 }
126 }
127 if ($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
128 if ($CurrentPage < ($PageCount - 1))
129 {
130 $QueryItems['page'] = ($CurrentPage + 1);
131 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
132 $QueryItems['page'] = ($PageCount - 1);
133 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
134 }
135 }
136 $Result = '<div style="text-align: center">'.$Result.'</div>';
137 return array('SQLLimit' => ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage,
138 'Page' => $CurrentPage,
139 'Output' => $Result,
140 );
141}
142
143$OrderDirSQL = array('ASC', 'DESC');
144$OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
145
146function GetOrderTableHeader(array $Columns, string $DefaultColumn, int $DefaultOrder = 0): array
147{
148 global $OrderDirSQL, $OrderArrowImage, $Config;
149
150 if (array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
151 if (array_key_exists('OrderDir', $_GET) and (array_key_exists($_GET['OrderDir'], $OrderArrowImage)))
152 $_SESSION['OrderDir'] = $_GET['OrderDir'];
153 if (!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $DefaultColumn;
154 if (!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $DefaultOrder;
155
156 // Check OrderCol
157 $Found = false;
158 foreach ($Columns as $Column)
159 {
160 if ($Column['Name'] == $_SESSION['OrderCol'])
161 {
162 $Found = true;
163 break;
164 }
165 }
166 if ($Found == false)
167 {
168 $_SESSION['OrderCol'] = $DefaultColumn;
169 $_SESSION['OrderDir'] = $DefaultOrder;
170 }
171 // Check OrderDir
172 if (($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1)) $_SESSION['OrderDir'] = 0;
173
174 $Result = '';
175 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
176 foreach ($Columns as $Index => $Column)
177 {
178 $QueryItems['OrderCol'] = $Column['Name'];
179 $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
180 if ($Column['Name'] == $_SESSION['OrderCol']) $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.
181 Link2('/images/'.$OrderArrowImage[$_SESSION['OrderDir']]).'" alt="order arrow"/>';
182 else $ArrowImage = '';
183 if ($Column['Name'] == '') $Result .= '<th>'.$Column['Title'].'</th>';
184 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column['Title'].$ArrowImage.'</a></th>';
185 }
186 return array(
187 'SQL' => ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$OrderDirSQL[$_SESSION['OrderDir']],
188 'Output' => '<tr>'.$Result.'</tr>',
189 'Column' => $_SESSION['OrderCol'],
190 'Direction' => $_SESSION['OrderDir'],
191 );
192}
193
194function GetMicrotime(): float
195{
196 list($Usec, $Sec) = explode(' ', microtime());
197 return (float)$Usec + (float)$Sec;
198}
199
200class Filter
201{
202 var $Items = array();
203
204 function GetOutput(string $Link): string
205 {
206 $Output = '';
207 foreach ($this->Items as $Item)
208 {
209 if (($Item['Type'] == 'String') or ($Item['Type'] == 'Enumeration') or ($Item['Type'] == 'Boolean'))
210 {
211 if (!array_key_exists($Item['Name'], $_SESSION)) $_SESSION[$Item['Name']] = '';
212 } else
213 if ($Item['Type'] == 'Integer')
214 {
215 if (!array_key_exists($Item['Name'].'od', $_SESSION)) $_SESSION[$Item['Name'].'od'] = '';
216 if (!array_key_exists($Item['Name'].'do', $_SESSION)) $_SESSION[$Item['Name'].'do'] = '';
217 }
218 }
219
220 if (array_key_exists('lvm', $_GET) and ($_GET['lvm'] == 'seznam'))
221 {
222 foreach ($this->Items as $Item)
223 {
224 if ($Item['Type'] == 'String')
225 {
226 if (array_key_exists($Item['Name'], $_GET) ) $_SESSION[$Item['Name']] = $_GET[$Item['Name']];
227 } else
228 if (($Item['Type'] == 'Enumeration') or ($Item['Type'] == 'Boolean'))
229 {
230 if (array_key_exists($Item['Name'], $_GET) ) $_SESSION[$Item['Name']] = $_GET[$Item['Name']];
231 if ($_SESSION[$Item['Name']] == '0') $_SESSION[$Item['Name']] = '';
232 } else
233 if ($Item['Type'] == 'Integer')
234 {
235 if (array_key_exists($Item['Name'].'od', $_GET) ) $_SESSION[$Item['Name'].'od'] = $_GET[$Item['Name'].'od'];
236 if ($_SESSION[$Item['Name'].'od'] == '0') $_SESSION[$Item['Name'].'od'] = '';
237 if (array_key_exists($Item['Name'].'do', $_GET) ) $_SESSION[$Item['Name'].'do'] = $_GET[$Item['Name'].'do'];
238 if ($_SESSION[$Item['Name'].'do'] == '0') $_SESSION[$Item['Name'].'do'] = '';
239 }
240 }
241 } else
242 {
243 $Output .= '<script type="text/javascript">function reloadlist(){ $(\'#meetlist\').load(document.URL + \' #meetlist\');}'.
244 ' var load_timer = 100;
245 var ltimer = null;
246
247 function checkOut(kc, obj)
248 {
249 if (kc == 13) $(obj).blur();
250 }
251 function initLTimer(tm)
252 {
253 if (ltimer) clearTimeout(ltimer);
254 ltimer = setTimeout("freloader()", tm);
255 }
256 function stopLTimer()
257 {
258 if (ltimer) clearTimeout(ltimer);
259 ltimer = null;
260 }
261
262 var cf = {};
263
264 function setupc(key,val)
265 {
266 //console.log(\'setup-control-\' + key + \' = \' + val);
267 ';
268 $Keys = array();
269 foreach ($this->Items as $Item)
270 {
271 if ($Item['Type'] == 'Integer')
272 {
273 $Keys[] = 'key == \''.$Item['Name'].'od\'';
274 $Keys[] = 'key == \''.$Item['Name'].'do\'';
275 }
276 }
277 if (count($Keys) > 0)
278 {
279 $Output .= 'if ('.implode(' || ', $Keys).')
280 {
281 var ccv = $(\'#\' + key).val();
282 if (ccv != val) $(\'#\' + key).val(val == 0 ? \'\' : val);
283 }
284 ';
285 }
286
287 $Keys = array();
288 foreach ($this->Items as $Item)
289 {
290 if (($Item['Type'] == 'Enumeration') or ($Item['Type'] == 'Boolean'))
291 {
292 $Keys[] = 'key == \''.$Item['Name'].'\'';
293 }
294 }
295 if (count($Keys) > 0)
296 {
297 $Output .= 'if ('.implode(' || ', $Keys).')
298 {
299 $(\'.c\' + key).removeClass(\'active\');
300 $(\'#\' + key + val).addClass(\'active\');
301 }
302 ';
303 }
304 $Output .= '
305 }
306
307 function upf(key, val, lonreload)
308 {
309 if (key != \'first\') cf[\'first\'] = 0;
310 else st();
311 setupc(key, val);
312 cf[key] = val;
313 if (lonreload) initLTimer(1000);
314 else initLTimer(100);
315 }
316
317 function freloader()
318 {
319 ltimer = null;
320 var qp = jQuery.param(cf);
321 $(\'#list_content\').html(\'\');
322 $(\'#list_loading\').show();
323 $.get(\''.$Link.'?lvm=seznam&\' + qp, function(data)
324 {
325 $(\'#list_loading\').hide();
326 $(\'#list_content\').html(data);
327 });
328 }'.
329 '</script>
330 <div class="table-filter">';
331 foreach ($this->Items as $Item)
332 {
333 if ($Item['Type'] == 'String')
334 {
335 $Output .= '<div class="filter-num-box">'.
336 '<div class="label-box">'.$Item['Title'].'</div>'.
337 '<input value="'.$_SESSION[$Item['Name']].'" onkeyup="if (event.keyCode!=9) upf(\''.$Item['Name'].'\',$(this).val(),(event.keyCode==13?0:1)); '.
338 '" id="'.$Item['Name'].'" autocomplete="off" type="text"/>'.
339 '</div> ';
340 } else
341 if ($Item['Type'] == 'Boolean')
342 {
343 $Output .= '<div class="btn-group ma3">';
344 $Output .= '<button class="btn btn-filter';
345 $Output .= ' c'.$Item['Name'].'" onclick="upf(\''.$Item['Name'].'\',cf.'.$Item['Name'].'?0:1)" id="'.$Item['Name'].'1" data-toggle="tooltip" '.
346 'data-placement="top" title="'.$Item['Title'].'"><span>'.$Item['Title'].'</span></button>';
347 $Output .= '</div>';
348 } else
349 if ($Item['Type'] == 'Enumeration')
350 {
351 $Output .= '<div class="btn-group ma3">'.
352 '<div class="label-box">'.$Item['Title'].'</div>';
353 foreach ($Item['States'] as $EnumIndex => $EnumItem)
354 {
355 $Output .= '<button class="btn btn-filter c'.$Item['Name'].' btn-ico" onclick="upf(\''.$Item['Name'].'\','.$EnumIndex.')" id="'.
356 $Item['Name'].$EnumIndex.'" data-toggle="tooltip" '.
357 'data-placement="top" title="'.$EnumItem.'"><span class="icon-'.$EnumIndex.'"></span></button>';
358 }
359 $Output .= '</div>';
360 } else
361 if ($Item['Type'] == 'Integer')
362 {
363 $Output .= '<div class="filter-num-box">'.
364 '<div class="label-box">'.$Item['Title'].'</div>'.
365 '<input value="'.$_SESSION[$Item['Name'].'od'].'" onkeyup="if (event.keyCode!=9) upf(\''.$Item['Name'].'od\',$(this).val(),(event.keyCode==13?0:1)); '.
366 '" id="'.$Item['Name'].'od" autocomplete="off" type="text"/>'.
367 '<div class="label-box">-</div>'.
368 '<input value="'.$_SESSION[$Item['Name'].'do'].'" onkeyup="'.
369 'if (event.keyCode!=9) upf(\''.$Item['Name'].'do\',$(this).val(),(event.keyCode==13?0:1));" '.
370 'id="'.$Item['Name'].'do" autocomplete="off" type="text"/>';
371 if (array_key_exists('Units', $Item))
372 {
373 $Output .= '<div class="label-box">'.$Item['Units'].'</div>';
374 }
375 $Output .= '</div> ';
376 }
377 }
378 $Output .= '</div>';
379 }
380 return $Output;
381 }
382
383 function GetWhere(Database $Database): string
384 {
385 $Where = '';
386 foreach ($this->Items as $Item)
387 {
388 if ($Item['DbName'] != '')
389 {
390 if ($Item['Type'] == 'String')
391 {
392 if ($_SESSION[$Item['Name']] != '')
393 $Where .= ' AND ('.$Item['DbName'].' LIKE "%'.$Database->real_escape_string($_SESSION[$Item['Name']]).'%")';
394 } else
395 if ($Item['Type'] == 'Boolean')
396 {
397 if ($_SESSION[$Item['Name']] != '')
398 $Where .= ' AND ('.$Item['DbName'].' = '.$Database->real_escape_string($_SESSION[$Item['Name']]).')';
399 } else
400 if ($Item['Type'] == 'Enumeration')
401 {
402 if ($_SESSION[$Item['Name']] != '')
403 $Where .= ' AND ('.$Item['DbName'].' = '.$Database->real_escape_string($_SESSION[$Item['Name']]).')';
404 } else
405 if ($Item['Type'] == 'Integer')
406 {
407 if ($_SESSION[$Item['Name'].'od'] != '')
408 $Where .= ' AND ('.$Item['DbName'].' >= '.$Database->real_escape_string($_SESSION[$Item['Name'].'od']).')';
409 if ($_SESSION[$Item['Name'].'do'] != '')
410 $Where .= ' AND ('.$Item['DbName'].' <= '.$Database->real_escape_string($_SESSION[$Item['Name'].'do']).')';
411 }
412 }
413 }
414 if (substr($Where, 0, 4) == ' AND')
415 $Where = substr($Where, 4);
416 if ($Where == '') $Where = '1';
417 return $Where;
418 }
419}
Note: See TracBrowser for help on using the repository browser.