source: trunk/Common/Global.php@ 592

Last change on this file since 592 was 592, checked in by chronos, 11 years ago
  • Upraveno: Aplikačně závislé soubory přesunuty do adresáře Application.
  • Upraveno: Modul Setup nemůže vystupovat jako aplikační modul neboť připravuje prostředí pro instalaci těchto modulů.
  • Přidáno: Třída AppModuleRepository pro správu dostupných aplikačních balíků k instalaci. Ty se následně instalují do seznamu Modules v třídě AppModuleManager.
  • Property svn:executable set to *
File size: 10.4 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__).'/VarDumper.php');
11include_once(dirname(__FILE__).'/Base.php');
12include_once(dirname(__FILE__).'/AppModule.php');
13include_once(dirname(__FILE__).'/Database.php');
14include_once(dirname(__FILE__).'/UTF8.php');
15include_once(dirname(__FILE__).'/Mail.php');
16include_once(dirname(__FILE__).'/Page.php');
17include_once(dirname(__FILE__).'/Form/Form.php');
18include_once(dirname(__FILE__).'/Config.php');
19include_once(dirname(__FILE__).'/Setup/Setup.php');
20
21$MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen',
22 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
23
24$UnitNames = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
25
26function HumanSize($Value)
27{
28 global $UnitNames;
29
30 $UnitIndex = 0;
31 while($Value > 1024)
32 {
33 $Value = round($Value / 1024, 3);
34 $UnitIndex++;
35 }
36 return($Value.' '.$UnitNames[$UnitIndex]);
37}
38
39function GetMicrotime()
40{
41 list($Usec, $Sec) = explode(' ', microtime());
42 return ((float)$Usec + (float)$Sec);
43}
44
45function ShowArray($Pole)
46{
47 echo('<pre style="font-size: 8pt;">');
48 print_r($Pole);
49 echo('</pre>');
50}
51
52function HumanDate($Time)
53{
54 $Date = explode(' ', $Time);
55 $Parts = explode('-', $Date[0]);
56 if($Date != '0000-00-00') return(($Parts[2]*1).'.'.($Parts[1]*1).'.'.$Parts[0]);
57 else return('&nbsp;');
58}
59
60// Show page listing numbers
61function PagesList($URL, $Page, $TotalCount, $CountPerPage)
62{
63 $Count = ceil($TotalCount / $CountPerPage);
64 $Around = 10;
65 $Result = '';
66 if($Count>1)
67 {
68 if($Page>0)
69 {
70 $Result.= '<a href="'.$URL.'0">&lt;&lt;</a> ';
71 $Result.= '<a href="'.$URL.($Page-1).'">&lt;</a> ';
72 }
73 $PagesMax = $Count-1;
74 $PagesMin = 0;
75 if($PagesMax>($Page+$Around)) $PagesMax = $Page+$Around;
76 if($PagesMin<($Page-$Around))
77 {
78 $Result.= ' .. ';
79 $PagesMin = $Page-$Around;
80 }
81 for($i=$PagesMin;$i<=$PagesMax;$i++)
82 {
83 if($i==$Page) $Result.= '<strong>';
84 $Result.= '<a href="'.$URL.$i.'">'.($i+1).'</a> ';
85 if($i==$Page) $Result.= '</strong>';
86 }
87 if($PagesMax<($Count-1)) $Result .= ' .. ';
88 if($Page<($Count-1))
89 {
90 $Result.= '<a href="'.$URL.($Page+1).'">&gt;</a> ';
91 $Result.= '<a href="'.$URL.($Count-1).'">&gt;&gt;</a>';
92 }
93 }
94 return($Result);
95}
96
97function ExtractTime($Time)
98{
99 return(array(
100 'Year' => date('Y', $Time),
101 'Month' => date('n', $Time),
102 'Day' => date('j', $Time),
103 'Hour' => date('h', $Time),
104 'Minute' => date('i', $Time),
105 'Second' => date('s', $Time)
106 ));
107}
108
109function GetQueryStringArray($QueryString)
110{
111 $Result = array();
112 $Parts = explode('&', $QueryString);
113 foreach($Parts as $Part)
114 {
115 if($Part != '')
116 {
117 if(!strpos($Part, '=')) $Part .= '=';
118 $Item = explode('=', $Part);
119 $Result[$Item[0]] = $Item[1];
120 }
121 }
122 return($Result);
123}
124
125function SetQueryStringArray($QueryStringArray)
126{
127 $Parts = array();
128 foreach($QueryStringArray as $Index => $Item)
129 {
130 $Parts[] = $Index.'='.$Item;
131 }
132 return(implode('&amp;', $Parts));
133}
134
135function GetPageList($TotalCount)
136{
137 global $System;
138
139 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
140
141 $Result = '';
142 if(array_key_exists('all', $QueryItems))
143 {
144 $PageCount = 1;
145 $ItemPerPage = $TotalCount;
146 } else
147 {
148 $ItemPerPage = $System->Config['Web']['ItemsPerPage'];
149 $Around = round($System->Config['Web']['VisiblePagingItems'] / 2);
150 $PageCount = floor($TotalCount / $ItemPerPage) + 1;
151 }
152
153 if(!array_key_exists('Page', $_SESSION)) $_SESSION['Page'] = 0;
154 if(array_key_exists('page', $_GET)) $_SESSION['Page'] = $_GET['page'] * 1;
155 if($_SESSION['Page'] < 0) $_SESSION['Page'] = 0;
156 if($_SESSION['Page'] >= $PageCount) $_SESSION['Page'] = $PageCount - 1;
157 $CurrentPage = $_SESSION['Page'];
158
159
160 $Result .= 'Počet položek: <strong>'.$TotalCount.'</strong> &nbsp; Stránky: ';
161
162 $Result = '';
163 if($PageCount > 1)
164 {
165 if($CurrentPage > 0)
166 {
167 $QueryItems['page'] = 0;
168 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
169 $QueryItems['page'] = ($CurrentPage - 1);
170 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
171 }
172 $PagesMax = $PageCount - 1;
173 $PagesMin = 0;
174 if($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
175 if($PagesMin < ($CurrentPage - $Around))
176 {
177 $Result.= ' ... ';
178 $PagesMin = $CurrentPage - $Around;
179 }
180 for($i = $PagesMin; $i <= $PagesMax; $i++)
181 {
182 if($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
183 else {
184 $QueryItems['page'] = $i;
185 $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
186 }
187 }
188 if($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
189 if($CurrentPage < ($PageCount - 1))
190 {
191 $QueryItems['page'] = ($CurrentPage + 1);
192 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
193 $QueryItems['page'] = ($PageCount - 1);
194 $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
195 }
196 }
197 $QueryItems['all'] = '1';
198 if($PageCount > 1) $Result.= ' <a href="?'.SetQueryStringArray($QueryItems).'">Vše</a>';
199
200 $Result = '<div style="text-align: center">'.$Result.'</div>';
201 return(array('SQLLimit' => ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage,
202 'Page' => $CurrentPage,
203 'Output' => $Result,
204 ));
205}
206
207$OrderDirSQL = array('ASC', 'DESC');
208$OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
209
210function GetOrderTableHeader($Columns, $DefaultColumn, $DefaultOrder = 0)
211{
212 global $OrderDirSQL, $OrderArrowImage, $Config, $System;
213
214 if(array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
215 if(array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
216 if(!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $DefaultColumn;
217 if(!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $DefaultOrder;
218
219 // Check OrderCol
220 $Found = false;
221 foreach($Columns as $Column)
222 {
223 if($Column['Name'] == $_SESSION['OrderCol'])
224 {
225 $Found = true;
226 break;
227 }
228 }
229 if($Found == false)
230 {
231 $_SESSION['OrderCol'] = $DefaultColumn;
232 $_SESSION['OrderDir'] = $DefaultOrder;
233 }
234 // Check OrderDir
235 if(($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1)) $_SESSION['OrderDir'] = 0;
236
237 $Result = '';
238 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
239 foreach($Columns as $Index => $Column)
240 {
241 $QueryItems['OrderCol'] = $Column['Name'];
242 $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
243 if($Column['Name'] == $_SESSION['OrderCol']) $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.$System->Link('/images/'.$OrderArrowImage[$_SESSION['OrderDir']]).'" alt="order arrow">';
244 else $ArrowImage = '';
245 if($Column['Name'] == '') $Result .= '<th>'.$Column['Title'].'</th>';
246 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column['Title'].$ArrowImage.'</a></th>';
247 }
248 return(array(
249 'SQL' => ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$OrderDirSQL[$_SESSION['OrderDir']],
250 'Output' => '<tr>'.$Result.'</tr>',
251 'Column' => $_SESSION['OrderCol'],
252 'Direction' => $_SESSION['OrderDir'],
253 ));
254}
255
256function GetRemoteAddress()
257{
258 if(array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) $IP = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
259 else if(array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
260 else $IP = '0.0.0.0';
261 return($IP);
262}
263
264function IsInternetAddr()
265{
266 $RemoteAddr = GetRemoteAddress();
267 $RemoteAddr = explode('.', $RemoteAddr);
268// return(!(($RemoteAddr[0] == 10) and ($RemoteAddr[1] == 145)));
269 return(!(($RemoteAddr[0] == 10)));
270}
271
272function GetMemberByIP($IP)
273{
274 global $Database;
275
276 $DbResult = $Database->query('SELECT Id FROM Member WHERE (SELECT Member FROM NetworkDevice WHERE (SELECT Device FROM NetworkInterface WHERE LocalIP = "'.$IP.'") = NetworkDevice.Id) = Member.Id');
277 if($DbResult->num_rows > 0)
278 {
279 $DbRow = $DbResult->fetch_assoc();
280 return($DbRow['Id']);
281 } else return('');
282}
283
284function RemoveDiacritic($Text)
285{
286 return(str_replace(
287 array('á', 'č', 'ď', 'é', 'ě', 'í', 'ľ', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů',
288 'ý', 'ž', 'Á', 'Č', 'Ď', 'É', 'Ě', 'Í', 'Ľ', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů', 'Ý', 'Ž'),
289 array('a', 'c', 'd', 'e', 'e', 'i', 'l', 'n', 'o', 'r', 's', 't', 'u', 'u',
290 'y', 'z', 'A', 'C', 'D', 'E', 'E', 'I', 'L', 'N', 'O', 'R', 'S', 'T', 'U', 'U', 'Y', 'Z'),
291 $Text));
292}
293
294function RouterOSIdent($Name)
295{
296 return(strtr(strtolower(trim($Name)), array(' ' => '-', '.' => '', '(' => '-', ')' => '-',
297 'č' => 'c', 'š' => 's', 'ě' => 'e', 'ř' => 'r', 'ž' => 'z', 'ý' => 'y',
298 'á' => 'a', 'í' => 'i', 'é' => 'e', 'ů' => 'u', 'ú' => 'u', 'ď' => 'd',
299 'ť' => 't', 'ň' => 'n', 'ó' => 'o',
300 'Č' => 'c', 'Š' => 's', 'Ě' => 'e', 'Ř' => 'r', 'Ž' => 'z', 'Ý' => 'y',
301 'Á' => 'a', 'Í' => 'i', 'É' => 'e', 'Ů' => 'u', 'Ú' => 'u', 'Ď' => 'd',
302 'Ť' => 't', 'Ň' => 'n', 'Ó' => 'o',
303)));
304}
305
306function NotBlank($Text)
307{
308 if($Text == '') return('&nbsp');
309 else return($Text);
310}
311
312function strip_cdata($string)
313{
314 preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
315 return str_replace($matches[0], $matches[1], $string);
316}
317
318function html2txt($document)
319{
320 $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
321 '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
322 '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
323 '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
324 );
325 $text = preg_replace($search, '', $document);
326 return $text;
327}
328
329function ProcessURL()
330{
331 if(array_key_exists('REDIRECT_QUERY_STRING', $_SERVER))
332 $PathString = $_SERVER['REDIRECT_QUERY_STRING'];
333 else $PathString = '';
334 if(substr($PathString, -1, 1) == '/') $PathString = substr($PathString, 0, -1);
335 $PathItems = explode('/', $PathString);
336 if(strpos($_SERVER['REQUEST_URI'], '?') !== false)
337 $_SERVER['QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
338 else $_SERVER['QUERY_STRING'] = '';
339 parse_str($_SERVER['QUERY_STRING'], $_GET);
340 return($PathItems);
341}
Note: See TracBrowser for help on using the repository browser.