1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | /** |
---|
25 | * @package functions\___ |
---|
26 | */ |
---|
27 | |
---|
28 | include_once( PHPWG_ROOT_PATH .'include/functions_plugins.inc.php' ); |
---|
29 | include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' ); |
---|
30 | include_once( PHPWG_ROOT_PATH .'include/functions_cookie.inc.php' ); |
---|
31 | include_once( PHPWG_ROOT_PATH .'include/functions_session.inc.php' ); |
---|
32 | include_once( PHPWG_ROOT_PATH .'include/functions_category.inc.php' ); |
---|
33 | include_once( PHPWG_ROOT_PATH .'include/functions_html.inc.php' ); |
---|
34 | include_once( PHPWG_ROOT_PATH .'include/functions_tag.inc.php' ); |
---|
35 | include_once( PHPWG_ROOT_PATH .'include/functions_url.inc.php' ); |
---|
36 | include_once( PHPWG_ROOT_PATH .'include/derivative_params.inc.php'); |
---|
37 | include_once( PHPWG_ROOT_PATH .'include/derivative_std_params.inc.php'); |
---|
38 | include_once( PHPWG_ROOT_PATH .'include/derivative.inc.php'); |
---|
39 | include_once( PHPWG_ROOT_PATH .'include/template.class.php'); |
---|
40 | include_once( PHPWG_ROOT_PATH .'include/cache.class.php'); |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | * returns the current microsecond since Unix epoch |
---|
45 | * |
---|
46 | * @return int |
---|
47 | */ |
---|
48 | function micro_seconds() |
---|
49 | { |
---|
50 | $t1 = explode(' ', microtime()); |
---|
51 | $t2 = explode('.', $t1[0]); |
---|
52 | $t2 = $t1[1].substr($t2[1], 0, 6); |
---|
53 | return $t2; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * returns a float value coresponding to the number of seconds since |
---|
58 | * the unix epoch (1st January 1970) and the microseconds are precised |
---|
59 | * e.g. 1052343429.89276600 |
---|
60 | * |
---|
61 | * @return float |
---|
62 | */ |
---|
63 | function get_moment() |
---|
64 | { |
---|
65 | return microtime(true); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * returns the number of seconds (with 3 decimals precision) |
---|
70 | * between the start time and the end time given |
---|
71 | * |
---|
72 | * @param float $start |
---|
73 | * @param float $end |
---|
74 | * @return string "$TIME s" |
---|
75 | */ |
---|
76 | function get_elapsed_time($start, $end) |
---|
77 | { |
---|
78 | return number_format($end - $start, 3, '.', ' ').' s'; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * returns the part of the string after the last "." |
---|
83 | * |
---|
84 | * @param string $filename |
---|
85 | * @return string |
---|
86 | */ |
---|
87 | function get_extension( $filename ) |
---|
88 | { |
---|
89 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * returns the part of the string before the last ".". |
---|
94 | * get_filename_wo_extension( 'test.tar.gz' ) = 'test.tar' |
---|
95 | * |
---|
96 | * @param string $filename |
---|
97 | * @return string |
---|
98 | */ |
---|
99 | function get_filename_wo_extension( $filename ) |
---|
100 | { |
---|
101 | $pos = strrpos( $filename, '.' ); |
---|
102 | return ($pos===false) ? $filename : substr( $filename, 0, $pos); |
---|
103 | } |
---|
104 | |
---|
105 | /** no option for mkgetdir() */ |
---|
106 | define('MKGETDIR_NONE', 0); |
---|
107 | /** sets mkgetdir() recursive */ |
---|
108 | define('MKGETDIR_RECURSIVE', 1); |
---|
109 | /** sets mkgetdir() exit script on error */ |
---|
110 | define('MKGETDIR_DIE_ON_ERROR', 2); |
---|
111 | /** sets mkgetdir() add a index.htm file */ |
---|
112 | define('MKGETDIR_PROTECT_INDEX', 4); |
---|
113 | /** sets mkgetdir() add a .htaccess file*/ |
---|
114 | define('MKGETDIR_PROTECT_HTACCESS', 8); |
---|
115 | /** default options for mkgetdir() = MKGETDIR_RECURSIVE | MKGETDIR_DIE_ON_ERROR | MKGETDIR_PROTECT_INDEX */ |
---|
116 | define('MKGETDIR_DEFAULT', MKGETDIR_RECURSIVE | MKGETDIR_DIE_ON_ERROR | MKGETDIR_PROTECT_INDEX); |
---|
117 | |
---|
118 | /** |
---|
119 | * creates directory if not exists and ensures that directory is writable |
---|
120 | * |
---|
121 | * @param string $dir |
---|
122 | * @param int $flags combination of MKGETDIR_xxx |
---|
123 | * @return bool |
---|
124 | */ |
---|
125 | function mkgetdir($dir, $flags=MKGETDIR_DEFAULT) |
---|
126 | { |
---|
127 | if ( !is_dir($dir) ) |
---|
128 | { |
---|
129 | global $conf; |
---|
130 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
131 | { |
---|
132 | $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); |
---|
133 | } |
---|
134 | $umask = umask(0); |
---|
135 | $mkd = @mkdir($dir, $conf['chmod_value'], ($flags&MKGETDIR_RECURSIVE) ? true:false ); |
---|
136 | umask($umask); |
---|
137 | if ($mkd==false) |
---|
138 | { |
---|
139 | !($flags&MKGETDIR_DIE_ON_ERROR) or fatal_error( "$dir ".l10n('no write access')); |
---|
140 | return false; |
---|
141 | } |
---|
142 | if( $flags&MKGETDIR_PROTECT_HTACCESS ) |
---|
143 | { |
---|
144 | $file = $dir.'/.htaccess'; |
---|
145 | file_exists($file) or @file_put_contents( $file, 'deny from all' ); |
---|
146 | } |
---|
147 | if( $flags&MKGETDIR_PROTECT_INDEX ) |
---|
148 | { |
---|
149 | $file = $dir.'/index.htm'; |
---|
150 | file_exists($file) or @file_put_contents( $file, 'Not allowed!' ); |
---|
151 | } |
---|
152 | } |
---|
153 | if ( !is_writable($dir) ) |
---|
154 | { |
---|
155 | !($flags&MKGETDIR_DIE_ON_ERROR) or fatal_error( "$dir ".l10n('no write access')); |
---|
156 | return false; |
---|
157 | } |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * finds out if a string is in ASCII, UTF-8 or other encoding |
---|
163 | * |
---|
164 | * @param string $str |
---|
165 | * @return int *0* if _$str_ is ASCII, *1* if UTF-8, *-1* otherwise |
---|
166 | */ |
---|
167 | function qualify_utf8($Str) |
---|
168 | { |
---|
169 | $ret = 0; |
---|
170 | for ($i=0; $i<strlen($Str); $i++) |
---|
171 | { |
---|
172 | if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb |
---|
173 | $ret = 1; |
---|
174 | if ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb |
---|
175 | elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb |
---|
176 | elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb |
---|
177 | elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb |
---|
178 | elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b |
---|
179 | else return -1; # Does not match any model |
---|
180 | for ($j=0; $j<$n; $j++) |
---|
181 | { # n bytes matching 10bbbbbb follow ? |
---|
182 | if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) |
---|
183 | return -1; |
---|
184 | } |
---|
185 | } |
---|
186 | return $ret; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Remove accents from a UTF-8 or ISO-8859-1 string (from wordpress) |
---|
191 | * |
---|
192 | * @param string $string |
---|
193 | * @return string |
---|
194 | */ |
---|
195 | function remove_accents($string) |
---|
196 | { |
---|
197 | $utf = qualify_utf8($string); |
---|
198 | if ( $utf == 0 ) |
---|
199 | { |
---|
200 | return $string; // ascii |
---|
201 | } |
---|
202 | |
---|
203 | if ( $utf > 0 ) |
---|
204 | { |
---|
205 | $chars = array( |
---|
206 | // Decompositions for Latin-1 Supplement |
---|
207 | "\xc3\x80"=>'A', "\xc3\x81"=>'A', |
---|
208 | "\xc3\x82"=>'A', "\xc3\x83"=>'A', |
---|
209 | "\xc3\x84"=>'A', "\xc3\x85"=>'A', |
---|
210 | "\xc3\x87"=>'C', "\xc3\x88"=>'E', |
---|
211 | "\xc3\x89"=>'E', "\xc3\x8a"=>'E', |
---|
212 | "\xc3\x8b"=>'E', "\xc3\x8c"=>'I', |
---|
213 | "\xc3\x8d"=>'I', "\xc3\x8e"=>'I', |
---|
214 | "\xc3\x8f"=>'I', "\xc3\x91"=>'N', |
---|
215 | "\xc3\x92"=>'O', "\xc3\x93"=>'O', |
---|
216 | "\xc3\x94"=>'O', "\xc3\x95"=>'O', |
---|
217 | "\xc3\x96"=>'O', "\xc3\x99"=>'U', |
---|
218 | "\xc3\x9a"=>'U', "\xc3\x9b"=>'U', |
---|
219 | "\xc3\x9c"=>'U', "\xc3\x9d"=>'Y', |
---|
220 | "\xc3\x9f"=>'s', "\xc3\xa0"=>'a', |
---|
221 | "\xc3\xa1"=>'a', "\xc3\xa2"=>'a', |
---|
222 | "\xc3\xa3"=>'a', "\xc3\xa4"=>'a', |
---|
223 | "\xc3\xa5"=>'a', "\xc3\xa7"=>'c', |
---|
224 | "\xc3\xa8"=>'e', "\xc3\xa9"=>'e', |
---|
225 | "\xc3\xaa"=>'e', "\xc3\xab"=>'e', |
---|
226 | "\xc3\xac"=>'i', "\xc3\xad"=>'i', |
---|
227 | "\xc3\xae"=>'i', "\xc3\xaf"=>'i', |
---|
228 | "\xc3\xb1"=>'n', "\xc3\xb2"=>'o', |
---|
229 | "\xc3\xb3"=>'o', "\xc3\xb4"=>'o', |
---|
230 | "\xc3\xb5"=>'o', "\xc3\xb6"=>'o', |
---|
231 | "\xc3\xb9"=>'u', "\xc3\xba"=>'u', |
---|
232 | "\xc3\xbb"=>'u', "\xc3\xbc"=>'u', |
---|
233 | "\xc3\xbd"=>'y', "\xc3\xbf"=>'y', |
---|
234 | // Decompositions for Latin Extended-A |
---|
235 | "\xc4\x80"=>'A', "\xc4\x81"=>'a', |
---|
236 | "\xc4\x82"=>'A', "\xc4\x83"=>'a', |
---|
237 | "\xc4\x84"=>'A', "\xc4\x85"=>'a', |
---|
238 | "\xc4\x86"=>'C', "\xc4\x87"=>'c', |
---|
239 | "\xc4\x88"=>'C', "\xc4\x89"=>'c', |
---|
240 | "\xc4\x8a"=>'C', "\xc4\x8b"=>'c', |
---|
241 | "\xc4\x8c"=>'C', "\xc4\x8d"=>'c', |
---|
242 | "\xc4\x8e"=>'D', "\xc4\x8f"=>'d', |
---|
243 | "\xc4\x90"=>'D', "\xc4\x91"=>'d', |
---|
244 | "\xc4\x92"=>'E', "\xc4\x93"=>'e', |
---|
245 | "\xc4\x94"=>'E', "\xc4\x95"=>'e', |
---|
246 | "\xc4\x96"=>'E', "\xc4\x97"=>'e', |
---|
247 | "\xc4\x98"=>'E', "\xc4\x99"=>'e', |
---|
248 | "\xc4\x9a"=>'E', "\xc4\x9b"=>'e', |
---|
249 | "\xc4\x9c"=>'G', "\xc4\x9d"=>'g', |
---|
250 | "\xc4\x9e"=>'G', "\xc4\x9f"=>'g', |
---|
251 | "\xc4\xa0"=>'G', "\xc4\xa1"=>'g', |
---|
252 | "\xc4\xa2"=>'G', "\xc4\xa3"=>'g', |
---|
253 | "\xc4\xa4"=>'H', "\xc4\xa5"=>'h', |
---|
254 | "\xc4\xa6"=>'H', "\xc4\xa7"=>'h', |
---|
255 | "\xc4\xa8"=>'I', "\xc4\xa9"=>'i', |
---|
256 | "\xc4\xaa"=>'I', "\xc4\xab"=>'i', |
---|
257 | "\xc4\xac"=>'I', "\xc4\xad"=>'i', |
---|
258 | "\xc4\xae"=>'I', "\xc4\xaf"=>'i', |
---|
259 | "\xc4\xb0"=>'I', "\xc4\xb1"=>'i', |
---|
260 | "\xc4\xb2"=>'IJ', "\xc4\xb3"=>'ij', |
---|
261 | "\xc4\xb4"=>'J', "\xc4\xb5"=>'j', |
---|
262 | "\xc4\xb6"=>'K', "\xc4\xb7"=>'k', |
---|
263 | "\xc4\xb8"=>'k', "\xc4\xb9"=>'L', |
---|
264 | "\xc4\xba"=>'l', "\xc4\xbb"=>'L', |
---|
265 | "\xc4\xbc"=>'l', "\xc4\xbd"=>'L', |
---|
266 | "\xc4\xbe"=>'l', "\xc4\xbf"=>'L', |
---|
267 | "\xc5\x80"=>'l', "\xc5\x81"=>'L', |
---|
268 | "\xc5\x82"=>'l', "\xc5\x83"=>'N', |
---|
269 | "\xc5\x84"=>'n', "\xc5\x85"=>'N', |
---|
270 | "\xc5\x86"=>'n', "\xc5\x87"=>'N', |
---|
271 | "\xc5\x88"=>'n', "\xc5\x89"=>'N', |
---|
272 | "\xc5\x8a"=>'n', "\xc5\x8b"=>'N', |
---|
273 | "\xc5\x8c"=>'O', "\xc5\x8d"=>'o', |
---|
274 | "\xc5\x8e"=>'O', "\xc5\x8f"=>'o', |
---|
275 | "\xc5\x90"=>'O', "\xc5\x91"=>'o', |
---|
276 | "\xc5\x92"=>'OE', "\xc5\x93"=>'oe', |
---|
277 | "\xc5\x94"=>'R', "\xc5\x95"=>'r', |
---|
278 | "\xc5\x96"=>'R', "\xc5\x97"=>'r', |
---|
279 | "\xc5\x98"=>'R', "\xc5\x99"=>'r', |
---|
280 | "\xc5\x9a"=>'S', "\xc5\x9b"=>'s', |
---|
281 | "\xc5\x9c"=>'S', "\xc5\x9d"=>'s', |
---|
282 | "\xc5\x9e"=>'S', "\xc5\x9f"=>'s', |
---|
283 | "\xc5\xa0"=>'S', "\xc5\xa1"=>'s', |
---|
284 | "\xc5\xa2"=>'T', "\xc5\xa3"=>'t', |
---|
285 | "\xc5\xa4"=>'T', "\xc5\xa5"=>'t', |
---|
286 | "\xc5\xa6"=>'T', "\xc5\xa7"=>'t', |
---|
287 | "\xc5\xa8"=>'U', "\xc5\xa9"=>'u', |
---|
288 | "\xc5\xaa"=>'U', "\xc5\xab"=>'u', |
---|
289 | "\xc5\xac"=>'U', "\xc5\xad"=>'u', |
---|
290 | "\xc5\xae"=>'U', "\xc5\xaf"=>'u', |
---|
291 | "\xc5\xb0"=>'U', "\xc5\xb1"=>'u', |
---|
292 | "\xc5\xb2"=>'U', "\xc5\xb3"=>'u', |
---|
293 | "\xc5\xb4"=>'W', "\xc5\xb5"=>'w', |
---|
294 | "\xc5\xb6"=>'Y', "\xc5\xb7"=>'y', |
---|
295 | "\xc5\xb8"=>'Y', "\xc5\xb9"=>'Z', |
---|
296 | "\xc5\xba"=>'z', "\xc5\xbb"=>'Z', |
---|
297 | "\xc5\xbc"=>'z', "\xc5\xbd"=>'Z', |
---|
298 | "\xc5\xbe"=>'z', "\xc5\xbf"=>'s', |
---|
299 | // Decompositions for Latin Extended-B |
---|
300 | "\xc8\x98"=>'S', "\xc8\x99"=>'s', |
---|
301 | "\xc8\x9a"=>'T', "\xc8\x9b"=>'t', |
---|
302 | // Euro Sign |
---|
303 | "\xe2\x82\xac"=>'E', |
---|
304 | // GBP (Pound) Sign |
---|
305 | "\xc2\xa3"=>''); |
---|
306 | |
---|
307 | $string = strtr($string, $chars); |
---|
308 | } |
---|
309 | else |
---|
310 | { |
---|
311 | // Assume ISO-8859-1 if not UTF-8 |
---|
312 | $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158) |
---|
313 | .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194) |
---|
314 | .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202) |
---|
315 | .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210) |
---|
316 | .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218) |
---|
317 | .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227) |
---|
318 | .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235) |
---|
319 | .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243) |
---|
320 | .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251) |
---|
321 | .chr(252).chr(253).chr(255); |
---|
322 | |
---|
323 | $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy"; |
---|
324 | |
---|
325 | $string = strtr($string, $chars['in'], $chars['out']); |
---|
326 | $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254)); |
---|
327 | $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th'); |
---|
328 | $string = str_replace($double_chars['in'], $double_chars['out'], $string); |
---|
329 | } |
---|
330 | |
---|
331 | return $string; |
---|
332 | } |
---|
333 | |
---|
334 | if (function_exists('mb_strtolower') && defined('PWG_CHARSET')) |
---|
335 | { |
---|
336 | /** |
---|
337 | * removes accents from a string and converts it to lower case |
---|
338 | * |
---|
339 | * @param string $term |
---|
340 | * @return string |
---|
341 | */ |
---|
342 | function transliterate($term) |
---|
343 | { |
---|
344 | return remove_accents( mb_strtolower($term, PWG_CHARSET) ); |
---|
345 | } |
---|
346 | } |
---|
347 | else |
---|
348 | { |
---|
349 | /** |
---|
350 | * @ignore |
---|
351 | */ |
---|
352 | function transliterate($term) |
---|
353 | { |
---|
354 | return remove_accents( strtolower($term) ); |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | /** |
---|
359 | * simplify a string to insert it into an URL |
---|
360 | * |
---|
361 | * @param string $str |
---|
362 | * @return string |
---|
363 | */ |
---|
364 | function str2url($str) |
---|
365 | { |
---|
366 | $str = $safe = transliterate($str); |
---|
367 | $str = preg_replace('/[^\x80-\xffa-z0-9_\s\'\:\/\[\],-]/','',$str); |
---|
368 | $str = preg_replace('/[\s\'\:\/\[\],-]+/',' ',trim($str)); |
---|
369 | $res = str_replace(' ','_',$str); |
---|
370 | |
---|
371 | if (empty($res)) |
---|
372 | { |
---|
373 | $res = str_replace(' ','_', $safe); |
---|
374 | } |
---|
375 | |
---|
376 | return $res; |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * returns an array with a list of {language_code => language_name} |
---|
381 | * |
---|
382 | * @return string[] |
---|
383 | */ |
---|
384 | function get_languages() |
---|
385 | { |
---|
386 | $query = ' |
---|
387 | SELECT id, name |
---|
388 | FROM '.LANGUAGES_TABLE.' |
---|
389 | ORDER BY name ASC |
---|
390 | ;'; |
---|
391 | $result = pwg_query($query); |
---|
392 | |
---|
393 | $languages = array(); |
---|
394 | while ($row = pwg_db_fetch_assoc($result)) |
---|
395 | { |
---|
396 | if (is_dir(PHPWG_ROOT_PATH.'language/'.$row['id'])) |
---|
397 | { |
---|
398 | $languages[ $row['id'] ] = $row['name']; |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | return $languages; |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | * log the visit into history table |
---|
407 | * |
---|
408 | * @param int $image_id |
---|
409 | * @param string $image_type |
---|
410 | * @return bool |
---|
411 | */ |
---|
412 | function pwg_log($image_id = null, $image_type = null) |
---|
413 | { |
---|
414 | global $conf, $user, $page; |
---|
415 | |
---|
416 | $do_log = $conf['log']; |
---|
417 | if (is_admin()) |
---|
418 | { |
---|
419 | $do_log = $conf['history_admin']; |
---|
420 | } |
---|
421 | if (is_a_guest()) |
---|
422 | { |
---|
423 | $do_log = $conf['history_guest']; |
---|
424 | } |
---|
425 | |
---|
426 | $do_log = trigger_change('pwg_log_allowed', $do_log, $image_id, $image_type); |
---|
427 | |
---|
428 | if (!$do_log) |
---|
429 | { |
---|
430 | return false; |
---|
431 | } |
---|
432 | |
---|
433 | $tags_string = null; |
---|
434 | if ('tags'==@$page['section']) |
---|
435 | { |
---|
436 | $tags_string = implode(',', $page['tag_ids']); |
---|
437 | } |
---|
438 | |
---|
439 | $query = ' |
---|
440 | INSERT INTO '.HISTORY_TABLE.' |
---|
441 | ( |
---|
442 | date, |
---|
443 | time, |
---|
444 | user_id, |
---|
445 | IP, |
---|
446 | section, |
---|
447 | category_id, |
---|
448 | image_id, |
---|
449 | image_type, |
---|
450 | tag_ids |
---|
451 | ) |
---|
452 | VALUES |
---|
453 | ( |
---|
454 | CURRENT_DATE, |
---|
455 | CURRENT_TIME, |
---|
456 | '.$user['id'].', |
---|
457 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
458 | '.(isset($page['section']) ? "'".$page['section']."'" : 'NULL').', |
---|
459 | '.(isset($page['category']['id']) ? $page['category']['id'] : 'NULL').', |
---|
460 | '.(isset($image_id) ? $image_id : 'NULL').', |
---|
461 | '.(isset($image_type) ? "'".$image_type."'" : 'NULL').', |
---|
462 | '.(isset($tags_string) ? "'".$tags_string."'" : 'NULL').' |
---|
463 | ) |
---|
464 | ;'; |
---|
465 | pwg_query($query); |
---|
466 | |
---|
467 | return true; |
---|
468 | } |
---|
469 | |
---|
470 | /** |
---|
471 | * Computes the difference between two dates. |
---|
472 | * returns a DateInterval object or a stdClass with the same attributes |
---|
473 | * http://stephenharris.info/date-intervals-in-php-5-2 |
---|
474 | * |
---|
475 | * @param DateTime $date1 |
---|
476 | * @param DateTime $date2 |
---|
477 | * @return DateInterval|stdClass |
---|
478 | */ |
---|
479 | function dateDiff($date1, $date2) |
---|
480 | { |
---|
481 | if (version_compare(PHP_VERSION, '5.3.0') >= 0) |
---|
482 | { |
---|
483 | return $date1->diff($date2); |
---|
484 | } |
---|
485 | |
---|
486 | $diff = new stdClass(); |
---|
487 | |
---|
488 | //Make sure $date1 is ealier |
---|
489 | $diff->invert = $date2 < $date1; |
---|
490 | if ($diff->invert) |
---|
491 | { |
---|
492 | list($date1, $date2) = array($date2, $date1); |
---|
493 | } |
---|
494 | |
---|
495 | //Calculate R values |
---|
496 | $R = ($date1 <= $date2 ? '+' : '-'); |
---|
497 | $r = ($date1 <= $date2 ? '' : '-'); |
---|
498 | |
---|
499 | //Calculate total days |
---|
500 | $diff->days = round(abs($date1->format('U') - $date2->format('U'))/86400); |
---|
501 | |
---|
502 | //A leap year work around - consistent with DateInterval |
---|
503 | $leap_year = $date1->format('m-d') == '02-29'; |
---|
504 | if ($leap_year) |
---|
505 | { |
---|
506 | $date1->modify('-1 day'); |
---|
507 | } |
---|
508 | |
---|
509 | //Years, months, days, hours |
---|
510 | $periods = array('years'=>-1, 'months'=>-1, 'days'=>-1, 'hours'=>-1); |
---|
511 | |
---|
512 | foreach ($periods as $period => &$i) |
---|
513 | { |
---|
514 | if ($period == 'days' && $leap_year) |
---|
515 | { |
---|
516 | $date1->modify('+1 day'); |
---|
517 | } |
---|
518 | |
---|
519 | while ($date1 <= $date2 ) |
---|
520 | { |
---|
521 | $date1->modify('+1 '.$period); |
---|
522 | $i++; |
---|
523 | } |
---|
524 | |
---|
525 | //Reset date and record increments |
---|
526 | $date1->modify('-1 '.$period); |
---|
527 | } |
---|
528 | |
---|
529 | list($diff->y, $diff->m, $diff->d, $diff->h) = array_values($periods); |
---|
530 | |
---|
531 | //Minutes, seconds |
---|
532 | $diff->s = round(abs($date1->format('U') - $date2->format('U'))); |
---|
533 | $diff->i = floor($diff->s/60); |
---|
534 | $diff->s = $diff->s - $diff->i*60; |
---|
535 | |
---|
536 | return $diff; |
---|
537 | } |
---|
538 | |
---|
539 | /** |
---|
540 | * converts a string into a DateTime object |
---|
541 | * |
---|
542 | * @param int|string timestamp or datetime string |
---|
543 | * @param string $format input format respecting date() syntax |
---|
544 | * @return DateTime|false |
---|
545 | */ |
---|
546 | function str2DateTime($original, $format=null) |
---|
547 | { |
---|
548 | if (empty($original)) |
---|
549 | { |
---|
550 | return false; |
---|
551 | } |
---|
552 | |
---|
553 | if (!empty($format) && version_compare(PHP_VERSION, '5.3.0') >= 0)// from known date format |
---|
554 | { |
---|
555 | return DateTime::createFromFormat('!'.$format, $original); // ! char to reset fields to UNIX epoch |
---|
556 | } |
---|
557 | else |
---|
558 | { |
---|
559 | $t = trim($original, '0123456789'); |
---|
560 | if (empty($t)) // from timestamp |
---|
561 | { |
---|
562 | return new DateTime('@'.$original); |
---|
563 | } |
---|
564 | else // from unknown date format (assuming something like Y-m-d H:i:s) |
---|
565 | { |
---|
566 | $ymdhms = array(); |
---|
567 | $tok = strtok($original, '- :/'); |
---|
568 | while ($tok !== false) |
---|
569 | { |
---|
570 | $ymdhms[] = $tok; |
---|
571 | $tok = strtok('- :/'); |
---|
572 | } |
---|
573 | |
---|
574 | if (count($ymdhms)<3) return false; |
---|
575 | if (!isset($ymdhms[3])) $ymdhms[3] = 0; |
---|
576 | if (!isset($ymdhms[4])) $ymdhms[4] = 0; |
---|
577 | if (!isset($ymdhms[5])) $ymdhms[5] = 0; |
---|
578 | |
---|
579 | $date = new DateTime(); |
---|
580 | $date->setDate($ymdhms[0], $ymdhms[1], $ymdhms[2]); |
---|
581 | $date->setTime($ymdhms[3], $ymdhms[4], $ymdhms[5]); |
---|
582 | return $date; |
---|
583 | } |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | /** |
---|
588 | * returns a formatted and localized date for display |
---|
589 | * |
---|
590 | * @param int|string timestamp or datetime string |
---|
591 | * @param bool $show_time |
---|
592 | * @param bool $show_day_name |
---|
593 | * @param string $format input format respecting date() syntax |
---|
594 | * @return string |
---|
595 | */ |
---|
596 | function format_date($original, $show_time=false, $show_day_name=true, $format=null) |
---|
597 | { |
---|
598 | global $lang; |
---|
599 | |
---|
600 | $date = str2DateTime($original, $format); |
---|
601 | |
---|
602 | if (!$date) |
---|
603 | { |
---|
604 | return l10n('N/A'); |
---|
605 | } |
---|
606 | |
---|
607 | $print = ''; |
---|
608 | if ($show_day_name) |
---|
609 | { |
---|
610 | $print.= $lang['day'][ $date->format('w') ].' '; |
---|
611 | } |
---|
612 | |
---|
613 | $print.= $date->format('j'); |
---|
614 | $print.= ' '.$lang['month'][ $date->format('n') ]; |
---|
615 | $print.= ' '.$date->format('Y'); |
---|
616 | |
---|
617 | if ($show_time) |
---|
618 | { |
---|
619 | $temp = $date->format('H:i'); |
---|
620 | if ($temp != '00:00') |
---|
621 | { |
---|
622 | $print.= ' '.$temp; |
---|
623 | } |
---|
624 | } |
---|
625 | |
---|
626 | return trim($print); |
---|
627 | } |
---|
628 | |
---|
629 | /** |
---|
630 | * Works out the time since the given date |
---|
631 | * |
---|
632 | * @param int|string timestamp or datetime string |
---|
633 | * @param string $stop year,month,week,day,hour,minute,second |
---|
634 | * @param string $format input format respecting date() syntax |
---|
635 | * @param bool $with_text append "ago" or "in the future" |
---|
636 | * @param bool $with_weeks |
---|
637 | * @return string |
---|
638 | */ |
---|
639 | function time_since($original, $stop='minute', $format=null, $with_text=true, $with_week=true) |
---|
640 | { |
---|
641 | $date = str2DateTime($original, $format); |
---|
642 | |
---|
643 | if (!$date) |
---|
644 | { |
---|
645 | return l10n('N/A'); |
---|
646 | } |
---|
647 | |
---|
648 | $now = new DateTime(); |
---|
649 | $diff = dateDiff($now, $date); |
---|
650 | |
---|
651 | $chunks = array( |
---|
652 | 'year' => $diff->y, |
---|
653 | 'month' => $diff->m, |
---|
654 | 'week' => 0, |
---|
655 | 'day' => $diff->d, |
---|
656 | 'hour' => $diff->h, |
---|
657 | 'minute' => $diff->i, |
---|
658 | 'second' => $diff->s, |
---|
659 | ); |
---|
660 | |
---|
661 | // DateInterval does not contain the number of weeks |
---|
662 | if ($with_week) |
---|
663 | { |
---|
664 | $chunks['week'] = (int)floor($chunks['day']/7); |
---|
665 | $chunks['day'] = $chunks['day'] - $chunks['week']*7; |
---|
666 | } |
---|
667 | |
---|
668 | $j = array_search($stop, array_keys($chunks)); |
---|
669 | |
---|
670 | $print = ''; $i=0; |
---|
671 | foreach ($chunks as $name => $value) |
---|
672 | { |
---|
673 | if ($value != 0) |
---|
674 | { |
---|
675 | $print.= ' '.l10n_dec('%d '.$name, '%d '.$name.'s', $value); |
---|
676 | } |
---|
677 | if (!empty($print) && $i >= $j) |
---|
678 | { |
---|
679 | break; |
---|
680 | } |
---|
681 | $i++; |
---|
682 | } |
---|
683 | |
---|
684 | $print = trim($print); |
---|
685 | |
---|
686 | if ($with_text) |
---|
687 | { |
---|
688 | if ($diff->invert) |
---|
689 | { |
---|
690 | $print = l10n('%s ago', $print); |
---|
691 | } |
---|
692 | else |
---|
693 | { |
---|
694 | $print = l10n('%s in the future', $print); |
---|
695 | } |
---|
696 | } |
---|
697 | |
---|
698 | return $print; |
---|
699 | } |
---|
700 | |
---|
701 | /** |
---|
702 | * transform a date string from a format to another (MySQL to d/M/Y for instance) |
---|
703 | * |
---|
704 | * @param string $original |
---|
705 | * @param string $format_in respecting date() syntax |
---|
706 | * @param string $format_out respecting date() syntax |
---|
707 | * @param string $default if _$original_ is empty |
---|
708 | * @return string |
---|
709 | */ |
---|
710 | function transform_date($original, $format_in, $format_out, $default=null) |
---|
711 | { |
---|
712 | if (empty($original)) return $default; |
---|
713 | $date = str2DateTime($original, $format_in); |
---|
714 | return $date->format($format_out); |
---|
715 | } |
---|
716 | |
---|
717 | /** |
---|
718 | * append a variable to _$debug_ global |
---|
719 | * |
---|
720 | * @param string $string |
---|
721 | */ |
---|
722 | function pwg_debug( $string ) |
---|
723 | { |
---|
724 | global $debug,$t2,$page; |
---|
725 | |
---|
726 | $now = explode( ' ', microtime() ); |
---|
727 | $now2 = explode( '.', $now[0] ); |
---|
728 | $now2 = $now[1].'.'.$now2[1]; |
---|
729 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
730 | $debug .= '<p>'; |
---|
731 | $debug.= '['.$time.', '; |
---|
732 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
733 | $debug.= "</p>\n"; |
---|
734 | } |
---|
735 | |
---|
736 | /** |
---|
737 | * Redirects to the given URL (HTTP method). |
---|
738 | * once this function called, the execution doesn't go further |
---|
739 | * (presence of an exit() instruction. |
---|
740 | * |
---|
741 | * @param string $url |
---|
742 | * @return void |
---|
743 | */ |
---|
744 | function redirect_http( $url ) |
---|
745 | { |
---|
746 | if (ob_get_length () !== FALSE) |
---|
747 | { |
---|
748 | ob_clean(); |
---|
749 | } |
---|
750 | // default url is on html format |
---|
751 | $url = html_entity_decode($url); |
---|
752 | header('Request-URI: '.$url); |
---|
753 | header('Content-Location: '.$url); |
---|
754 | header('Location: '.$url); |
---|
755 | exit(); |
---|
756 | } |
---|
757 | |
---|
758 | /** |
---|
759 | * Redirects to the given URL (HTML method). |
---|
760 | * once this function called, the execution doesn't go further |
---|
761 | * (presence of an exit() instruction. |
---|
762 | * |
---|
763 | * @param string $url |
---|
764 | * @param string $msg |
---|
765 | * @param integer $refresh_time |
---|
766 | * @return void |
---|
767 | */ |
---|
768 | function redirect_html( $url , $msg = '', $refresh_time = 0) |
---|
769 | { |
---|
770 | global $user, $template, $lang_info, $conf, $lang, $t2, $page, $debug; |
---|
771 | |
---|
772 | if (!isset($lang_info) || !isset($template) ) |
---|
773 | { |
---|
774 | $user = build_user( $conf['guest_id'], true); |
---|
775 | load_language('common.lang'); |
---|
776 | trigger_notify('loading_lang'); |
---|
777 | load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, array('no_fallback'=>true, 'local'=>true) ); |
---|
778 | $template = new Template(PHPWG_ROOT_PATH.'themes', get_default_theme()); |
---|
779 | } |
---|
780 | elseif (defined('IN_ADMIN') and IN_ADMIN) |
---|
781 | { |
---|
782 | $template = new Template(PHPWG_ROOT_PATH.'themes', get_default_theme()); |
---|
783 | } |
---|
784 | |
---|
785 | if (empty($msg)) |
---|
786 | { |
---|
787 | $msg = nl2br(l10n('Redirection...')); |
---|
788 | } |
---|
789 | |
---|
790 | $refresh = $refresh_time; |
---|
791 | $url_link = $url; |
---|
792 | $title = 'redirection'; |
---|
793 | |
---|
794 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
795 | |
---|
796 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
797 | |
---|
798 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
799 | $template->assign('REDIRECT_MSG', $msg); |
---|
800 | |
---|
801 | $template->parse('redirect'); |
---|
802 | |
---|
803 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
804 | |
---|
805 | exit(); |
---|
806 | } |
---|
807 | |
---|
808 | /** |
---|
809 | * Redirects to the given URL (automatically choose HTTP or HTML method). |
---|
810 | * once this function called, the execution doesn't go further |
---|
811 | * (presence of an exit() instruction. |
---|
812 | * |
---|
813 | * @param string $url |
---|
814 | * @param string $msg |
---|
815 | * @param integer $refresh_time |
---|
816 | * @return void |
---|
817 | */ |
---|
818 | function redirect( $url , $msg = '', $refresh_time = 0) |
---|
819 | { |
---|
820 | global $conf; |
---|
821 | |
---|
822 | // with RefeshTime <> 0, only html must be used |
---|
823 | if ($conf['default_redirect_method']=='http' |
---|
824 | and $refresh_time==0 |
---|
825 | and !headers_sent() |
---|
826 | ) |
---|
827 | { |
---|
828 | redirect_http($url); |
---|
829 | } |
---|
830 | else |
---|
831 | { |
---|
832 | redirect_html($url, $msg, $refresh_time); |
---|
833 | } |
---|
834 | } |
---|
835 | |
---|
836 | /** |
---|
837 | * returns available themes |
---|
838 | * |
---|
839 | * @param bool $show_mobile |
---|
840 | * @return array |
---|
841 | */ |
---|
842 | function get_pwg_themes($show_mobile=false) |
---|
843 | { |
---|
844 | global $conf; |
---|
845 | |
---|
846 | $themes = array(); |
---|
847 | |
---|
848 | $query = ' |
---|
849 | SELECT |
---|
850 | id, |
---|
851 | name |
---|
852 | FROM '.THEMES_TABLE.' |
---|
853 | ORDER BY name ASC |
---|
854 | ;'; |
---|
855 | $result = pwg_query($query); |
---|
856 | while ($row = pwg_db_fetch_assoc($result)) |
---|
857 | { |
---|
858 | if ($row['id'] == $conf['mobile_theme']) |
---|
859 | { |
---|
860 | if (!$show_mobile) |
---|
861 | { |
---|
862 | continue; |
---|
863 | } |
---|
864 | $row['name'] .= ' ('.l10n('Mobile').')'; |
---|
865 | } |
---|
866 | if (check_theme_installed($row['id'])) |
---|
867 | { |
---|
868 | $themes[ $row['id'] ] = $row['name']; |
---|
869 | } |
---|
870 | } |
---|
871 | |
---|
872 | // plugins want remove some themes based on user status maybe? |
---|
873 | $themes = trigger_change('get_pwg_themes', $themes); |
---|
874 | |
---|
875 | return $themes; |
---|
876 | } |
---|
877 | |
---|
878 | /** |
---|
879 | * check if a theme is installed (directory exsists) |
---|
880 | * |
---|
881 | * @param string $theme_id |
---|
882 | * @return bool |
---|
883 | */ |
---|
884 | function check_theme_installed($theme_id) |
---|
885 | { |
---|
886 | global $conf; |
---|
887 | |
---|
888 | return file_exists($conf['themes_dir'].'/'.$theme_id.'/'.'themeconf.inc.php'); |
---|
889 | } |
---|
890 | |
---|
891 | /** |
---|
892 | * Transforms an original path to its pwg representative |
---|
893 | * |
---|
894 | * @param string $path |
---|
895 | * @param string $representative_ext |
---|
896 | * @return string |
---|
897 | */ |
---|
898 | function original_to_representative($path, $representative_ext) |
---|
899 | { |
---|
900 | $pos = strrpos($path, '/'); |
---|
901 | $path = substr_replace($path, 'pwg_representative/', $pos+1, 0); |
---|
902 | $pos = strrpos($path, '.'); |
---|
903 | return substr_replace($path, $representative_ext, $pos+1); |
---|
904 | } |
---|
905 | |
---|
906 | /** |
---|
907 | * get the full path of an image |
---|
908 | * |
---|
909 | * @param array $element_info element information from db (at least 'path') |
---|
910 | * @return string |
---|
911 | */ |
---|
912 | function get_element_path($element_info) |
---|
913 | { |
---|
914 | $path = $element_info['path']; |
---|
915 | if ( !url_is_remote($path) ) |
---|
916 | { |
---|
917 | $path = PHPWG_ROOT_PATH.$path; |
---|
918 | } |
---|
919 | return $path; |
---|
920 | } |
---|
921 | |
---|
922 | |
---|
923 | /** |
---|
924 | * fill the current user caddie with given elements, if not already in caddie |
---|
925 | * |
---|
926 | * @param int[] $elements_id |
---|
927 | */ |
---|
928 | function fill_caddie($elements_id) |
---|
929 | { |
---|
930 | global $user; |
---|
931 | |
---|
932 | $query = ' |
---|
933 | SELECT element_id |
---|
934 | FROM '.CADDIE_TABLE.' |
---|
935 | WHERE user_id = '.$user['id'].' |
---|
936 | ;'; |
---|
937 | $in_caddie = query2array($query, null, 'element_id'); |
---|
938 | |
---|
939 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
940 | |
---|
941 | $datas = array(); |
---|
942 | |
---|
943 | foreach ($caddiables as $caddiable) |
---|
944 | { |
---|
945 | $datas[] = array( |
---|
946 | 'element_id' => $caddiable, |
---|
947 | 'user_id' => $user['id'], |
---|
948 | ); |
---|
949 | } |
---|
950 | |
---|
951 | if (count($caddiables) > 0) |
---|
952 | { |
---|
953 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
954 | } |
---|
955 | } |
---|
956 | |
---|
957 | /** |
---|
958 | * returns the element name from its filename. |
---|
959 | * removes file extension and replace underscores by spaces |
---|
960 | * |
---|
961 | * @param string $filename |
---|
962 | * @return string name |
---|
963 | */ |
---|
964 | function get_name_from_file($filename) |
---|
965 | { |
---|
966 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
967 | } |
---|
968 | |
---|
969 | /** |
---|
970 | * translation function. |
---|
971 | * returns the corresponding value from _$lang_ if existing else the key is returned |
---|
972 | * if more than one parameter is provided sprintf is applied |
---|
973 | * |
---|
974 | * @param string $key |
---|
975 | * @param mixed $args,... optional arguments |
---|
976 | * @return string |
---|
977 | */ |
---|
978 | function l10n($key) |
---|
979 | { |
---|
980 | global $lang, $conf; |
---|
981 | |
---|
982 | if ( ($val=@$lang[$key]) === null) |
---|
983 | { |
---|
984 | if ($conf['debug_l10n'] and !isset($lang[$key]) and !empty($key)) |
---|
985 | { |
---|
986 | trigger_error('[l10n] language key "'. $key .'" not defined', E_USER_WARNING); |
---|
987 | } |
---|
988 | $val = $key; |
---|
989 | } |
---|
990 | |
---|
991 | if (func_num_args() > 1) |
---|
992 | { |
---|
993 | $args = func_get_args(); |
---|
994 | $val = vsprintf($val, array_slice($args, 1)); |
---|
995 | } |
---|
996 | |
---|
997 | return $val; |
---|
998 | } |
---|
999 | |
---|
1000 | /** |
---|
1001 | * returns the printf value for strings including %d |
---|
1002 | * returned value is concorded with decimal value (singular, plural) |
---|
1003 | * |
---|
1004 | * @param string $singular_key |
---|
1005 | * @param string $plural_key |
---|
1006 | * @param int $decimal |
---|
1007 | * @return string |
---|
1008 | */ |
---|
1009 | function l10n_dec($singular_key, $plural_key, $decimal) |
---|
1010 | { |
---|
1011 | global $lang_info; |
---|
1012 | |
---|
1013 | return |
---|
1014 | sprintf( |
---|
1015 | l10n(( |
---|
1016 | (($decimal > 1) or ($decimal == 0 and $lang_info['zero_plural'])) |
---|
1017 | ? $plural_key |
---|
1018 | : $singular_key |
---|
1019 | )), $decimal); |
---|
1020 | } |
---|
1021 | |
---|
1022 | /** |
---|
1023 | * returns a single element to use with l10n_args |
---|
1024 | * |
---|
1025 | * @param string $key translation key |
---|
1026 | * @param mixed $args arguments to use on sprintf($key, args) |
---|
1027 | * if args is a array, each values are used on sprintf |
---|
1028 | * @return string |
---|
1029 | */ |
---|
1030 | function get_l10n_args($key, $args='') |
---|
1031 | { |
---|
1032 | if (is_array($args)) |
---|
1033 | { |
---|
1034 | $key_arg = array_merge(array($key), $args); |
---|
1035 | } |
---|
1036 | else |
---|
1037 | { |
---|
1038 | $key_arg = array($key, $args); |
---|
1039 | } |
---|
1040 | return array('key_args' => $key_arg); |
---|
1041 | } |
---|
1042 | |
---|
1043 | /** |
---|
1044 | * returns a string formated with l10n elements. |
---|
1045 | * it is usefull to "prepare" a text and translate it later |
---|
1046 | * @see get_l10n_args() |
---|
1047 | * |
---|
1048 | * @param array $key_args one l10n_args element or array of l10n_args elements |
---|
1049 | * @param string $sep used when translated elements are concatened |
---|
1050 | * @return string |
---|
1051 | */ |
---|
1052 | function l10n_args($key_args, $sep = "\n") |
---|
1053 | { |
---|
1054 | if (is_array($key_args)) |
---|
1055 | { |
---|
1056 | foreach ($key_args as $key => $element) |
---|
1057 | { |
---|
1058 | if (isset($result)) |
---|
1059 | { |
---|
1060 | $result .= $sep; |
---|
1061 | } |
---|
1062 | else |
---|
1063 | { |
---|
1064 | $result = ''; |
---|
1065 | } |
---|
1066 | |
---|
1067 | if ($key === 'key_args') |
---|
1068 | { |
---|
1069 | array_unshift($element, l10n(array_shift($element))); // translate the key |
---|
1070 | $result .= call_user_func_array('sprintf', $element); |
---|
1071 | } |
---|
1072 | else |
---|
1073 | { |
---|
1074 | $result .= l10n_args($element, $sep); |
---|
1075 | } |
---|
1076 | } |
---|
1077 | } |
---|
1078 | else |
---|
1079 | { |
---|
1080 | fatal_error('l10n_args: Invalid arguments'); |
---|
1081 | } |
---|
1082 | |
---|
1083 | return $result; |
---|
1084 | } |
---|
1085 | |
---|
1086 | /** |
---|
1087 | * returns the corresponding value from $themeconf if existing or an empty string |
---|
1088 | * |
---|
1089 | * @param string $key |
---|
1090 | * @return string |
---|
1091 | */ |
---|
1092 | function get_themeconf($key) |
---|
1093 | { |
---|
1094 | global $template; |
---|
1095 | |
---|
1096 | return $template->get_themeconf($key); |
---|
1097 | } |
---|
1098 | |
---|
1099 | /** |
---|
1100 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
1101 | * |
---|
1102 | * @return string |
---|
1103 | */ |
---|
1104 | function get_webmaster_mail_address() |
---|
1105 | { |
---|
1106 | global $conf; |
---|
1107 | |
---|
1108 | $query = ' |
---|
1109 | SELECT '.$conf['user_fields']['email'].' |
---|
1110 | FROM '.USERS_TABLE.' |
---|
1111 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
1112 | ;'; |
---|
1113 | list($email) = pwg_db_fetch_row(pwg_query($query)); |
---|
1114 | |
---|
1115 | $email = trigger_change('get_webmaster_mail_address', $email); |
---|
1116 | |
---|
1117 | return $email; |
---|
1118 | } |
---|
1119 | |
---|
1120 | /** |
---|
1121 | * Add configuration parameters from database to global $conf array |
---|
1122 | * |
---|
1123 | * @param string $condition SQL condition |
---|
1124 | * @return void |
---|
1125 | */ |
---|
1126 | function load_conf_from_db($condition = '') |
---|
1127 | { |
---|
1128 | global $conf; |
---|
1129 | |
---|
1130 | $query = ' |
---|
1131 | SELECT param, value |
---|
1132 | FROM '.CONFIG_TABLE.' |
---|
1133 | '.(!empty($condition) ? 'WHERE '.$condition : '').' |
---|
1134 | ;'; |
---|
1135 | $result = pwg_query($query); |
---|
1136 | |
---|
1137 | if ((pwg_db_num_rows($result) == 0) and !empty($condition)) |
---|
1138 | { |
---|
1139 | fatal_error('No configuration data'); |
---|
1140 | } |
---|
1141 | |
---|
1142 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1143 | { |
---|
1144 | $val = isset($row['value']) ? $row['value'] : ''; |
---|
1145 | // If the field is true or false, the variable is transformed into a boolean value. |
---|
1146 | if ($val == 'true') |
---|
1147 | { |
---|
1148 | $val = true; |
---|
1149 | } |
---|
1150 | elseif ($val == 'false') |
---|
1151 | { |
---|
1152 | $val = false; |
---|
1153 | } |
---|
1154 | $conf[ $row['param'] ] = $val; |
---|
1155 | } |
---|
1156 | |
---|
1157 | trigger_notify('load_conf', $condition); |
---|
1158 | } |
---|
1159 | |
---|
1160 | /** |
---|
1161 | * Add or update a config parameter |
---|
1162 | * |
---|
1163 | * @param string $param |
---|
1164 | * @param string $value |
---|
1165 | * @param boolean $updateGlobal update global *$conf* variable |
---|
1166 | * @param callable $parser function to apply to the value before save in database |
---|
1167 | (eg: serialize, json_encode) will not be applied to *$conf* if *$parser* is *true* |
---|
1168 | */ |
---|
1169 | function conf_update_param($param, $value, $updateGlobal=false, $parser=null) |
---|
1170 | { |
---|
1171 | if ($parser != null) |
---|
1172 | { |
---|
1173 | $dbValue = call_user_func($parser, $value); |
---|
1174 | } |
---|
1175 | else if (is_array($value) || is_object($value)) |
---|
1176 | { |
---|
1177 | $dbValue = addslashes(serialize($value)); |
---|
1178 | } |
---|
1179 | else |
---|
1180 | { |
---|
1181 | $dbValue = boolean_to_string($value); |
---|
1182 | } |
---|
1183 | |
---|
1184 | $query = ' |
---|
1185 | INSERT INTO |
---|
1186 | '.CONFIG_TABLE.' (param, value) |
---|
1187 | VALUES(\''.$param.'\', \''.$dbValue.'\') |
---|
1188 | ON DUPLICATE KEY UPDATE value = \''.$dbValue.'\' |
---|
1189 | ;'; |
---|
1190 | |
---|
1191 | pwg_query($query); |
---|
1192 | |
---|
1193 | if ($updateGlobal) |
---|
1194 | { |
---|
1195 | global $conf; |
---|
1196 | $conf[$param] = $value; |
---|
1197 | } |
---|
1198 | } |
---|
1199 | |
---|
1200 | /** |
---|
1201 | * Delete one or more config parameters |
---|
1202 | * @since 2.6 |
---|
1203 | * |
---|
1204 | * @param string|string[] $params |
---|
1205 | */ |
---|
1206 | function conf_delete_param($params) |
---|
1207 | { |
---|
1208 | global $conf; |
---|
1209 | |
---|
1210 | if (!is_array($params)) |
---|
1211 | { |
---|
1212 | $params = array($params); |
---|
1213 | } |
---|
1214 | if (empty($params)) |
---|
1215 | { |
---|
1216 | return; |
---|
1217 | } |
---|
1218 | |
---|
1219 | $query = ' |
---|
1220 | DELETE FROM '.CONFIG_TABLE.' |
---|
1221 | WHERE param IN(\''. implode('\',\'', $params) .'\') |
---|
1222 | ;'; |
---|
1223 | pwg_query($query); |
---|
1224 | |
---|
1225 | foreach ($params as $param) |
---|
1226 | { |
---|
1227 | unset($conf[$param]); |
---|
1228 | } |
---|
1229 | } |
---|
1230 | |
---|
1231 | /** |
---|
1232 | * Apply *unserialize* on a value only if it is a string |
---|
1233 | * @since 2.7 |
---|
1234 | * |
---|
1235 | * @param array|string $value |
---|
1236 | * @return array |
---|
1237 | */ |
---|
1238 | function safe_unserialize($value) |
---|
1239 | { |
---|
1240 | if (is_string($value)) |
---|
1241 | { |
---|
1242 | return unserialize($value); |
---|
1243 | } |
---|
1244 | return $value; |
---|
1245 | } |
---|
1246 | |
---|
1247 | /** |
---|
1248 | * Apply *json_decode* on a value only if it is a string |
---|
1249 | * @since 2.7 |
---|
1250 | * |
---|
1251 | * @param array|string $value |
---|
1252 | * @return array |
---|
1253 | */ |
---|
1254 | function safe_json_decode($value) |
---|
1255 | { |
---|
1256 | if (is_string($value)) |
---|
1257 | { |
---|
1258 | return json_decode($value, true); |
---|
1259 | } |
---|
1260 | return $value; |
---|
1261 | } |
---|
1262 | |
---|
1263 | /** |
---|
1264 | * Prepends and appends strings at each value of the given array. |
---|
1265 | * |
---|
1266 | * @param array $array |
---|
1267 | * @param string $prepend_str |
---|
1268 | * @param string $append_str |
---|
1269 | * @return array |
---|
1270 | */ |
---|
1271 | function prepend_append_array_items($array, $prepend_str, $append_str) |
---|
1272 | { |
---|
1273 | array_walk( |
---|
1274 | $array, |
---|
1275 | create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";') |
---|
1276 | ); |
---|
1277 | |
---|
1278 | return $array; |
---|
1279 | } |
---|
1280 | |
---|
1281 | /** |
---|
1282 | * creates an simple hashmap based on a SQL query. |
---|
1283 | * choose one to be the key, another one to be the value. |
---|
1284 | * @deprecated 2.6 |
---|
1285 | * |
---|
1286 | * @param string $query |
---|
1287 | * @param string $keyname |
---|
1288 | * @param string $valuename |
---|
1289 | * @return array |
---|
1290 | */ |
---|
1291 | function simple_hash_from_query($query, $keyname, $valuename) |
---|
1292 | { |
---|
1293 | return query2array($query, $keyname, $valuename); |
---|
1294 | } |
---|
1295 | |
---|
1296 | /** |
---|
1297 | * creates an associative array based on a SQL query. |
---|
1298 | * choose one to be the key |
---|
1299 | * @deprecated 2.6 |
---|
1300 | * |
---|
1301 | * @param string $query |
---|
1302 | * @param string $keyname |
---|
1303 | * @return array |
---|
1304 | */ |
---|
1305 | function hash_from_query($query, $keyname) |
---|
1306 | { |
---|
1307 | return query2array($query, $keyname); |
---|
1308 | } |
---|
1309 | |
---|
1310 | /** |
---|
1311 | * creates a numeric array based on a SQL query. |
---|
1312 | * if _$fieldname_ is empty the returned value will be an array of arrays |
---|
1313 | * if _$fieldname_ is provided the returned value will be a one dimension array |
---|
1314 | * @deprecated 2.6 |
---|
1315 | * |
---|
1316 | * @param string $query |
---|
1317 | * @param string $fieldname |
---|
1318 | * @return array |
---|
1319 | */ |
---|
1320 | function array_from_query($query, $fieldname=false) |
---|
1321 | { |
---|
1322 | if (false === $fieldname) |
---|
1323 | { |
---|
1324 | return query2array($query); |
---|
1325 | } |
---|
1326 | else |
---|
1327 | { |
---|
1328 | return query2array($query, null, $fieldname); |
---|
1329 | } |
---|
1330 | } |
---|
1331 | |
---|
1332 | /** |
---|
1333 | * Return the basename of the current script. |
---|
1334 | * The lowercase case filename of the current script without extension |
---|
1335 | * |
---|
1336 | * @return string |
---|
1337 | */ |
---|
1338 | function script_basename() |
---|
1339 | { |
---|
1340 | global $conf; |
---|
1341 | |
---|
1342 | foreach (array('SCRIPT_NAME', 'SCRIPT_FILENAME', 'PHP_SELF') as $value) |
---|
1343 | { |
---|
1344 | if (!empty($_SERVER[$value])) |
---|
1345 | { |
---|
1346 | $filename = strtolower($_SERVER[$value]); |
---|
1347 | if ($conf['php_extension_in_urls'] and get_extension($filename)!=='php') |
---|
1348 | continue; |
---|
1349 | $basename = basename($filename, '.php'); |
---|
1350 | if (!empty($basename)) |
---|
1351 | { |
---|
1352 | return $basename; |
---|
1353 | } |
---|
1354 | } |
---|
1355 | } |
---|
1356 | return ''; |
---|
1357 | } |
---|
1358 | |
---|
1359 | /** |
---|
1360 | * Return $conf['filter_pages'] value for the current page |
---|
1361 | * |
---|
1362 | * @param string $value_name |
---|
1363 | * @return mixed |
---|
1364 | */ |
---|
1365 | function get_filter_page_value($value_name) |
---|
1366 | { |
---|
1367 | global $conf; |
---|
1368 | |
---|
1369 | $page_name = script_basename(); |
---|
1370 | |
---|
1371 | if (isset($conf['filter_pages'][$page_name][$value_name])) |
---|
1372 | { |
---|
1373 | return $conf['filter_pages'][$page_name][$value_name]; |
---|
1374 | } |
---|
1375 | elseif (isset($conf['filter_pages']['default'][$value_name])) |
---|
1376 | { |
---|
1377 | return $conf['filter_pages']['default'][$value_name]; |
---|
1378 | } |
---|
1379 | else |
---|
1380 | { |
---|
1381 | return null; |
---|
1382 | } |
---|
1383 | } |
---|
1384 | |
---|
1385 | /** |
---|
1386 | * return the character set used by Piwigo |
---|
1387 | * @return string |
---|
1388 | */ |
---|
1389 | function get_pwg_charset() |
---|
1390 | { |
---|
1391 | $pwg_charset = 'utf-8'; |
---|
1392 | if (defined('PWG_CHARSET')) |
---|
1393 | { |
---|
1394 | $pwg_charset = PWG_CHARSET; |
---|
1395 | } |
---|
1396 | return $pwg_charset; |
---|
1397 | } |
---|
1398 | |
---|
1399 | /** |
---|
1400 | * returns the parent (fallback) language of a language. |
---|
1401 | * if _$lang_id_ is null it applies to the current language |
---|
1402 | * @since 2.6 |
---|
1403 | * |
---|
1404 | * @param string $lang_id |
---|
1405 | * @return string|null |
---|
1406 | */ |
---|
1407 | function get_parent_language($lang_id=null) |
---|
1408 | { |
---|
1409 | if (empty($lang_id)) |
---|
1410 | { |
---|
1411 | global $lang_info; |
---|
1412 | return !empty($lang_info['parent']) ? $lang_info['parent'] : null; |
---|
1413 | } |
---|
1414 | else |
---|
1415 | { |
---|
1416 | $f = PHPWG_ROOT_PATH.'language/'.$lang_id.'/common.lang.php'; |
---|
1417 | if (file_exists($f)) |
---|
1418 | { |
---|
1419 | include($f); |
---|
1420 | return !empty($lang_info['parent']) ? $lang_info['parent'] : null; |
---|
1421 | } |
---|
1422 | } |
---|
1423 | return null; |
---|
1424 | } |
---|
1425 | |
---|
1426 | /** |
---|
1427 | * includes a language file or returns the content of a language file |
---|
1428 | * |
---|
1429 | * tries to load in descending order: |
---|
1430 | * param language, user language, default language |
---|
1431 | * |
---|
1432 | * @param string $filename |
---|
1433 | * @param string $dirname |
---|
1434 | * @param mixed options can contain |
---|
1435 | * @option string language - language to load |
---|
1436 | * @option bool return - if true the file content is returned |
---|
1437 | * @option bool no_fallback - if true do not load default language |
---|
1438 | * @option bool|string force_fallback - force pre-loading of another language |
---|
1439 | * default language if *true* or specified language |
---|
1440 | * @option bool local - if true load file from local directory |
---|
1441 | * @return boolean|string |
---|
1442 | */ |
---|
1443 | function load_language($filename, $dirname = '', $options = array()) |
---|
1444 | { |
---|
1445 | global $user, $language_files; |
---|
1446 | |
---|
1447 | // keep trace of plugins loaded files for switch_lang_to() function |
---|
1448 | if (!empty($dirname) && !empty($filename) && !@$options['return'] |
---|
1449 | && !isset($language_files[$dirname][$filename])) |
---|
1450 | { |
---|
1451 | $language_files[$dirname][$filename] = $options; |
---|
1452 | } |
---|
1453 | |
---|
1454 | if (!@$options['return']) |
---|
1455 | { |
---|
1456 | $filename .= '.php'; |
---|
1457 | } |
---|
1458 | if (empty($dirname)) |
---|
1459 | { |
---|
1460 | $dirname = PHPWG_ROOT_PATH; |
---|
1461 | } |
---|
1462 | $dirname .= 'language/'; |
---|
1463 | |
---|
1464 | $default_language = defined('PHPWG_INSTALLED') ? |
---|
1465 | get_default_language() : PHPWG_DEFAULT_LANGUAGE; |
---|
1466 | |
---|
1467 | // construct list of potential languages |
---|
1468 | $languages = array(); |
---|
1469 | if (!empty($options['language'])) |
---|
1470 | { // explicit language |
---|
1471 | $languages[] = $options['language']; |
---|
1472 | } |
---|
1473 | if (!empty($user['language'])) |
---|
1474 | { // use language |
---|
1475 | $languages[] = $user['language']; |
---|
1476 | } |
---|
1477 | if (($parent = get_parent_language()) != null) |
---|
1478 | { // parent language |
---|
1479 | // this is only for when the "child" language is missing |
---|
1480 | $languages[] = $parent; |
---|
1481 | } |
---|
1482 | if (isset($options['force_fallback'])) |
---|
1483 | { // fallback language |
---|
1484 | // this is only for when the main language is missing |
---|
1485 | if ($options['force_fallback'] === true) |
---|
1486 | { |
---|
1487 | $options['force_fallback'] = $default_language; |
---|
1488 | } |
---|
1489 | $languages[] = $options['force_fallback']; |
---|
1490 | } |
---|
1491 | if (!@$options['no_fallback']) |
---|
1492 | { // default language |
---|
1493 | $languages[] = $default_language; |
---|
1494 | } |
---|
1495 | |
---|
1496 | $languages = array_unique($languages); |
---|
1497 | |
---|
1498 | // find first existing |
---|
1499 | $source_file = ''; |
---|
1500 | $selected_language = ''; |
---|
1501 | foreach ($languages as $language) |
---|
1502 | { |
---|
1503 | $f = @$options['local'] ? |
---|
1504 | $dirname.$language.'.'.$filename: |
---|
1505 | $dirname.$language.'/'.$filename; |
---|
1506 | |
---|
1507 | if (file_exists($f)) |
---|
1508 | { |
---|
1509 | $selected_language = $language; |
---|
1510 | $source_file = $f; |
---|
1511 | break; |
---|
1512 | } |
---|
1513 | } |
---|
1514 | |
---|
1515 | if (!empty($source_file)) |
---|
1516 | { |
---|
1517 | if (!@$options['return']) |
---|
1518 | { |
---|
1519 | // load forced fallback |
---|
1520 | if (isset($options['force_fallback']) && $options['force_fallback'] != $selected_language) |
---|
1521 | { |
---|
1522 | @include(str_replace($selected_language, $options['force_fallback'], $source_file)); |
---|
1523 | } |
---|
1524 | |
---|
1525 | // load language content |
---|
1526 | @include($source_file); |
---|
1527 | $load_lang = @$lang; |
---|
1528 | $load_lang_info = @$lang_info; |
---|
1529 | |
---|
1530 | // access already existing values |
---|
1531 | global $lang, $lang_info; |
---|
1532 | if (!isset($lang)) $lang = array(); |
---|
1533 | if (!isset($lang_info)) $lang_info = array(); |
---|
1534 | |
---|
1535 | // load parent language content directly in global |
---|
1536 | if (!empty($load_lang_info['parent'])) |
---|
1537 | $parent_language = $load_lang_info['parent']; |
---|
1538 | else if (!empty($lang_info['parent'])) |
---|
1539 | $parent_language = $lang_info['parent']; |
---|
1540 | else |
---|
1541 | $parent_language = null; |
---|
1542 | |
---|
1543 | if (!empty($parent_language) && $parent_language != $selected_language) |
---|
1544 | { |
---|
1545 | @include(str_replace($selected_language, $parent_language, $source_file)); |
---|
1546 | } |
---|
1547 | |
---|
1548 | // merge contents |
---|
1549 | $lang = array_merge($lang, (array)$load_lang); |
---|
1550 | $lang_info = array_merge($lang_info, (array)$load_lang_info); |
---|
1551 | return true; |
---|
1552 | } |
---|
1553 | else |
---|
1554 | { |
---|
1555 | $content = @file_get_contents($source_file); |
---|
1556 | //Note: target charset is always utf-8 $content = convert_charset($content, 'utf-8', $target_charset); |
---|
1557 | return $content; |
---|
1558 | } |
---|
1559 | } |
---|
1560 | |
---|
1561 | return false; |
---|
1562 | } |
---|
1563 | |
---|
1564 | /** |
---|
1565 | * converts a string from a character set to another character set |
---|
1566 | * |
---|
1567 | * @param string $str |
---|
1568 | * @param string $source_charset |
---|
1569 | * @param string $dest_charset |
---|
1570 | */ |
---|
1571 | function convert_charset($str, $source_charset, $dest_charset) |
---|
1572 | { |
---|
1573 | if ($source_charset==$dest_charset) |
---|
1574 | return $str; |
---|
1575 | if ($source_charset=='iso-8859-1' and $dest_charset=='utf-8') |
---|
1576 | { |
---|
1577 | return utf8_encode($str); |
---|
1578 | } |
---|
1579 | if ($source_charset=='utf-8' and $dest_charset=='iso-8859-1') |
---|
1580 | { |
---|
1581 | return utf8_decode($str); |
---|
1582 | } |
---|
1583 | if (function_exists('iconv')) |
---|
1584 | { |
---|
1585 | return iconv($source_charset, $dest_charset, $str); |
---|
1586 | } |
---|
1587 | if (function_exists('mb_convert_encoding')) |
---|
1588 | { |
---|
1589 | return mb_convert_encoding( $str, $dest_charset, $source_charset ); |
---|
1590 | } |
---|
1591 | return $str; // TODO |
---|
1592 | } |
---|
1593 | |
---|
1594 | /** |
---|
1595 | * makes sure a index.htm protects the directory from browser file listing |
---|
1596 | * |
---|
1597 | * @param string $dir |
---|
1598 | */ |
---|
1599 | function secure_directory($dir) |
---|
1600 | { |
---|
1601 | $file = $dir.'/index.htm'; |
---|
1602 | if (!file_exists($file)) |
---|
1603 | { |
---|
1604 | @file_put_contents($file, 'Not allowed!'); |
---|
1605 | } |
---|
1606 | } |
---|
1607 | |
---|
1608 | /** |
---|
1609 | * returns a "secret key" that is to be sent back when a user posts a form |
---|
1610 | * |
---|
1611 | * @param int $valid_after_seconds - key validity start time from now |
---|
1612 | * @param string $aditionnal_data_to_hash |
---|
1613 | * @return string |
---|
1614 | */ |
---|
1615 | function get_ephemeral_key($valid_after_seconds, $aditionnal_data_to_hash = '') |
---|
1616 | { |
---|
1617 | global $conf; |
---|
1618 | $time = round(microtime(true), 1); |
---|
1619 | return $time.':'.$valid_after_seconds.':' |
---|
1620 | .hash_hmac( |
---|
1621 | 'md5', |
---|
1622 | $time.substr($_SERVER['REMOTE_ADDR'],0,5).$valid_after_seconds.$aditionnal_data_to_hash, |
---|
1623 | $conf['secret_key']); |
---|
1624 | } |
---|
1625 | |
---|
1626 | /** |
---|
1627 | * verify a key sent back with a form |
---|
1628 | * |
---|
1629 | * @param string $key |
---|
1630 | * @param string $aditionnal_data_to_hash |
---|
1631 | * @return bool |
---|
1632 | */ |
---|
1633 | function verify_ephemeral_key($key, $aditionnal_data_to_hash = '') |
---|
1634 | { |
---|
1635 | global $conf; |
---|
1636 | $time = microtime(true); |
---|
1637 | $key = explode( ':', @$key ); |
---|
1638 | if ( count($key)!=3 |
---|
1639 | or $key[0]>$time-(float)$key[1] // page must have been retrieved more than X sec ago |
---|
1640 | or $key[0]<$time-3600 // 60 minutes expiration |
---|
1641 | or hash_hmac( |
---|
1642 | 'md5', $key[0].substr($_SERVER['REMOTE_ADDR'],0,5).$key[1].$aditionnal_data_to_hash, $conf['secret_key'] |
---|
1643 | ) != $key[2] |
---|
1644 | ) |
---|
1645 | { |
---|
1646 | return false; |
---|
1647 | } |
---|
1648 | return true; |
---|
1649 | } |
---|
1650 | |
---|
1651 | /** |
---|
1652 | * return an array which will be sent to template to display navigation bar |
---|
1653 | * |
---|
1654 | * @param string $url base url of all links |
---|
1655 | * @param int $nb_elements |
---|
1656 | * @param int $start |
---|
1657 | * @param int $nb_element_page |
---|
1658 | * @param bool $clean_url |
---|
1659 | * @param string $param_name |
---|
1660 | * @return array |
---|
1661 | */ |
---|
1662 | function create_navigation_bar($url, $nb_element, $start, $nb_element_page, $clean_url = false, $param_name='start') |
---|
1663 | { |
---|
1664 | global $conf; |
---|
1665 | |
---|
1666 | $navbar = array(); |
---|
1667 | $pages_around = $conf['paginate_pages_around']; |
---|
1668 | $start_str = $clean_url ? '/'.$param_name.'-' : (strpos($url, '?')===false ? '?':'&').$param_name.'='; |
---|
1669 | |
---|
1670 | if (!isset($start) or !is_numeric($start) or (is_numeric($start) and $start < 0)) |
---|
1671 | { |
---|
1672 | $start = 0; |
---|
1673 | } |
---|
1674 | |
---|
1675 | // navigation bar useful only if more than one page to display ! |
---|
1676 | if ($nb_element > $nb_element_page) |
---|
1677 | { |
---|
1678 | $url_start = $url.$start_str; |
---|
1679 | |
---|
1680 | $cur_page = $navbar['CURRENT_PAGE'] = $start / $nb_element_page + 1; |
---|
1681 | $maximum = ceil($nb_element / $nb_element_page); |
---|
1682 | |
---|
1683 | $start = $nb_element_page * round( $start / $nb_element_page ); |
---|
1684 | $previous = $start - $nb_element_page; |
---|
1685 | $next = $start + $nb_element_page; |
---|
1686 | $last = ($maximum - 1) * $nb_element_page; |
---|
1687 | |
---|
1688 | // link to first page and previous page? |
---|
1689 | if ($cur_page != 1) |
---|
1690 | { |
---|
1691 | $navbar['URL_FIRST'] = $url; |
---|
1692 | $navbar['URL_PREV'] = $previous > 0 ? $url_start.$previous : $url; |
---|
1693 | } |
---|
1694 | // link on next page and last page? |
---|
1695 | if ($cur_page != $maximum) |
---|
1696 | { |
---|
1697 | $navbar['URL_NEXT'] = $url_start.($next < $last ? $next : $last); |
---|
1698 | $navbar['URL_LAST'] = $url_start.$last; |
---|
1699 | } |
---|
1700 | |
---|
1701 | // pages to display |
---|
1702 | $navbar['pages'] = array(); |
---|
1703 | $navbar['pages'][1] = $url; |
---|
1704 | for ($i = max( floor($cur_page) - $pages_around , 2), $stop = min( ceil($cur_page) + $pages_around + 1, $maximum); |
---|
1705 | $i < $stop; $i++) |
---|
1706 | { |
---|
1707 | $navbar['pages'][$i] = $url.$start_str.(($i - 1) * $nb_element_page); |
---|
1708 | } |
---|
1709 | $navbar['pages'][$maximum] = $url_start.$last; |
---|
1710 | $navbar['NB_PAGE']=$maximum; |
---|
1711 | } |
---|
1712 | return $navbar; |
---|
1713 | } |
---|
1714 | |
---|
1715 | /** |
---|
1716 | * return an array which will be sent to template to display recent icon |
---|
1717 | * |
---|
1718 | * @param string $date |
---|
1719 | * @param bool $is_child_date |
---|
1720 | * @return array |
---|
1721 | */ |
---|
1722 | function get_icon($date, $is_child_date = false) |
---|
1723 | { |
---|
1724 | global $cache, $user; |
---|
1725 | |
---|
1726 | if (empty($date)) |
---|
1727 | { |
---|
1728 | return false; |
---|
1729 | } |
---|
1730 | |
---|
1731 | if (!isset($cache['get_icon']['title'])) |
---|
1732 | { |
---|
1733 | $cache['get_icon']['title'] = l10n( |
---|
1734 | 'photos posted during the last %d days', |
---|
1735 | $user['recent_period'] |
---|
1736 | ); |
---|
1737 | } |
---|
1738 | |
---|
1739 | $icon = array( |
---|
1740 | 'TITLE' => $cache['get_icon']['title'], |
---|
1741 | 'IS_CHILD_DATE' => $is_child_date, |
---|
1742 | ); |
---|
1743 | |
---|
1744 | if (isset($cache['get_icon'][$date])) |
---|
1745 | { |
---|
1746 | return $cache['get_icon'][$date] ? $icon : array(); |
---|
1747 | } |
---|
1748 | |
---|
1749 | if (!isset($cache['get_icon']['sql_recent_date'])) |
---|
1750 | { |
---|
1751 | // Use MySql date in order to standardize all recent "actions/queries" |
---|
1752 | $cache['get_icon']['sql_recent_date'] = pwg_db_get_recent_period($user['recent_period']); |
---|
1753 | } |
---|
1754 | |
---|
1755 | $cache['get_icon'][$date] = $date > $cache['get_icon']['sql_recent_date']; |
---|
1756 | |
---|
1757 | return $cache['get_icon'][$date] ? $icon : array(); |
---|
1758 | } |
---|
1759 | |
---|
1760 | /** |
---|
1761 | * check token comming from form posted or get params to prevent csrf attacks. |
---|
1762 | * if pwg_token is empty action doesn't require token |
---|
1763 | * else pwg_token is compare to server token |
---|
1764 | * |
---|
1765 | * @return void access denied if token given is not equal to server token |
---|
1766 | */ |
---|
1767 | function check_pwg_token() |
---|
1768 | { |
---|
1769 | if (!empty($_REQUEST['pwg_token'])) |
---|
1770 | { |
---|
1771 | if (get_pwg_token() != $_REQUEST['pwg_token']) |
---|
1772 | { |
---|
1773 | access_denied(); |
---|
1774 | } |
---|
1775 | } |
---|
1776 | else |
---|
1777 | { |
---|
1778 | bad_request('missing token'); |
---|
1779 | } |
---|
1780 | } |
---|
1781 | |
---|
1782 | /** |
---|
1783 | * get pwg_token used to prevent csrf attacks |
---|
1784 | * |
---|
1785 | * @return string |
---|
1786 | */ |
---|
1787 | function get_pwg_token() |
---|
1788 | { |
---|
1789 | global $conf; |
---|
1790 | |
---|
1791 | return hash_hmac('md5', session_id(), $conf['secret_key']); |
---|
1792 | } |
---|
1793 | |
---|
1794 | /* |
---|
1795 | * breaks the script execution if the given value doesn't match the given |
---|
1796 | * pattern. This should happen only during hacking attempts. |
---|
1797 | * |
---|
1798 | * @param string $param_name |
---|
1799 | * @param array $param_array |
---|
1800 | * @param boolean $is_array |
---|
1801 | * @param string $pattern |
---|
1802 | * @param boolean $mandatory |
---|
1803 | */ |
---|
1804 | function check_input_parameter($param_name, $param_array, $is_array, $pattern, $mandatory=false) |
---|
1805 | { |
---|
1806 | $param_value = null; |
---|
1807 | if (isset($param_array[$param_name])) |
---|
1808 | { |
---|
1809 | $param_value = $param_array[$param_name]; |
---|
1810 | } |
---|
1811 | |
---|
1812 | // it's ok if the input parameter is null |
---|
1813 | if (empty($param_value)) |
---|
1814 | { |
---|
1815 | if ($mandatory) |
---|
1816 | { |
---|
1817 | fatal_error('[Hacking attempt] the input parameter "'.$param_name.'" is not valid'); |
---|
1818 | } |
---|
1819 | return true; |
---|
1820 | } |
---|
1821 | |
---|
1822 | if ($is_array) |
---|
1823 | { |
---|
1824 | if (!is_array($param_value)) |
---|
1825 | { |
---|
1826 | fatal_error('[Hacking attempt] the input parameter "'.$param_name.'" should be an array'); |
---|
1827 | } |
---|
1828 | |
---|
1829 | foreach ($param_value as $item_to_check) |
---|
1830 | { |
---|
1831 | if (!preg_match($pattern, $item_to_check)) |
---|
1832 | { |
---|
1833 | fatal_error('[Hacking attempt] an item is not valid in input parameter "'.$param_name.'"'); |
---|
1834 | } |
---|
1835 | } |
---|
1836 | } |
---|
1837 | else |
---|
1838 | { |
---|
1839 | if (!preg_match($pattern, $param_value)) |
---|
1840 | { |
---|
1841 | fatal_error('[Hacking attempt] the input parameter "'.$param_name.'" is not valid'); |
---|
1842 | } |
---|
1843 | } |
---|
1844 | } |
---|
1845 | |
---|
1846 | /** |
---|
1847 | * get localized privacy level values |
---|
1848 | * |
---|
1849 | * @return string[] |
---|
1850 | */ |
---|
1851 | function get_privacy_level_options() |
---|
1852 | { |
---|
1853 | global $conf; |
---|
1854 | |
---|
1855 | $options = array(); |
---|
1856 | $label = ''; |
---|
1857 | foreach (array_reverse($conf['available_permission_levels']) as $level) |
---|
1858 | { |
---|
1859 | if (0 == $level) |
---|
1860 | { |
---|
1861 | $label = l10n('Everybody'); |
---|
1862 | } |
---|
1863 | else |
---|
1864 | { |
---|
1865 | if (strlen($label)) |
---|
1866 | { |
---|
1867 | $label .= ', '; |
---|
1868 | } |
---|
1869 | $label .= l10n( sprintf('Level %d', $level) ); |
---|
1870 | } |
---|
1871 | $options[$level] = $label; |
---|
1872 | } |
---|
1873 | return $options; |
---|
1874 | } |
---|
1875 | |
---|
1876 | |
---|
1877 | /** |
---|
1878 | * return the branch from the version. For example version 2.2.4 is for branch 2.2 |
---|
1879 | * |
---|
1880 | * @param string $version |
---|
1881 | * @return string |
---|
1882 | */ |
---|
1883 | function get_branch_from_version($version) |
---|
1884 | { |
---|
1885 | return implode('.', array_slice(explode('.', $version), 0, 2)); |
---|
1886 | } |
---|
1887 | |
---|
1888 | /** |
---|
1889 | * return the device type: mobile, tablet or desktop |
---|
1890 | * |
---|
1891 | * @return string |
---|
1892 | */ |
---|
1893 | function get_device() |
---|
1894 | { |
---|
1895 | $device = pwg_get_session_var('device'); |
---|
1896 | |
---|
1897 | if (is_null($device)) |
---|
1898 | { |
---|
1899 | include_once(PHPWG_ROOT_PATH.'include/mdetect.php'); |
---|
1900 | $uagent_obj = new uagent_info(); |
---|
1901 | if ($uagent_obj->DetectSmartphone()) |
---|
1902 | { |
---|
1903 | $device = 'mobile'; |
---|
1904 | } |
---|
1905 | elseif ($uagent_obj->DetectTierTablet()) |
---|
1906 | { |
---|
1907 | $device = 'tablet'; |
---|
1908 | } |
---|
1909 | else |
---|
1910 | { |
---|
1911 | $device = 'desktop'; |
---|
1912 | } |
---|
1913 | pwg_set_session_var('device', $device); |
---|
1914 | } |
---|
1915 | |
---|
1916 | return $device; |
---|
1917 | } |
---|
1918 | |
---|
1919 | /** |
---|
1920 | * return true if mobile theme should be loaded |
---|
1921 | * |
---|
1922 | * @return bool |
---|
1923 | */ |
---|
1924 | function mobile_theme() |
---|
1925 | { |
---|
1926 | global $conf; |
---|
1927 | |
---|
1928 | if (empty($conf['mobile_theme'])) |
---|
1929 | { |
---|
1930 | return false; |
---|
1931 | } |
---|
1932 | |
---|
1933 | if (isset($_GET['mobile'])) |
---|
1934 | { |
---|
1935 | $is_mobile_theme = get_boolean($_GET['mobile']); |
---|
1936 | pwg_set_session_var('mobile_theme', $is_mobile_theme); |
---|
1937 | } |
---|
1938 | else |
---|
1939 | { |
---|
1940 | $is_mobile_theme = pwg_get_session_var('mobile_theme'); |
---|
1941 | } |
---|
1942 | |
---|
1943 | if (is_null($is_mobile_theme)) |
---|
1944 | { |
---|
1945 | $is_mobile_theme = (get_device() == 'mobile'); |
---|
1946 | pwg_set_session_var('mobile_theme', $is_mobile_theme); |
---|
1947 | } |
---|
1948 | |
---|
1949 | return $is_mobile_theme; |
---|
1950 | } |
---|
1951 | |
---|
1952 | /** |
---|
1953 | * check url format |
---|
1954 | * |
---|
1955 | * @param string $url |
---|
1956 | * @return bool |
---|
1957 | */ |
---|
1958 | function url_check_format($url) |
---|
1959 | { |
---|
1960 | return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED)!==false; |
---|
1961 | } |
---|
1962 | |
---|
1963 | /** |
---|
1964 | * check email format |
---|
1965 | * |
---|
1966 | * @param string $mail_address |
---|
1967 | * @return bool |
---|
1968 | */ |
---|
1969 | function email_check_format($mail_address) |
---|
1970 | { |
---|
1971 | return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false; |
---|
1972 | } |
---|
1973 | |
---|
1974 | /** |
---|
1975 | * returns the number of available comments for the connected user |
---|
1976 | * |
---|
1977 | * @return int |
---|
1978 | */ |
---|
1979 | function get_nb_available_comments() |
---|
1980 | { |
---|
1981 | global $user; |
---|
1982 | if (!isset($user['nb_available_comments'])) |
---|
1983 | { |
---|
1984 | $where = array(); |
---|
1985 | if ( !is_admin() ) |
---|
1986 | $where[] = 'validated=\'true\''; |
---|
1987 | $where[] = get_sql_condition_FandF |
---|
1988 | ( |
---|
1989 | array |
---|
1990 | ( |
---|
1991 | 'forbidden_categories' => 'category_id', |
---|
1992 | 'forbidden_images' => 'ic.image_id' |
---|
1993 | ), |
---|
1994 | '', true |
---|
1995 | ); |
---|
1996 | |
---|
1997 | $query = ' |
---|
1998 | SELECT COUNT(DISTINCT(com.id)) |
---|
1999 | FROM '.IMAGE_CATEGORY_TABLE.' AS ic |
---|
2000 | INNER JOIN '.COMMENTS_TABLE.' AS com |
---|
2001 | ON ic.image_id = com.image_id |
---|
2002 | WHERE '.implode(' |
---|
2003 | AND ', $where); |
---|
2004 | list($user['nb_available_comments']) = pwg_db_fetch_row(pwg_query($query)); |
---|
2005 | |
---|
2006 | single_update(USER_CACHE_TABLE, |
---|
2007 | array('nb_available_comments'=>$user['nb_available_comments']), |
---|
2008 | array('user_id'=>$user['id']) |
---|
2009 | ); |
---|
2010 | } |
---|
2011 | return $user['nb_available_comments']; |
---|
2012 | } |
---|
2013 | |
---|
2014 | /** |
---|
2015 | * Compare two versions with version_compare after having converted |
---|
2016 | * single chars to their decimal values. |
---|
2017 | * Needed because version_compare does not understand versions like '2.5.c'. |
---|
2018 | * @since 2.6 |
---|
2019 | * |
---|
2020 | * @param string $a |
---|
2021 | * @param string $b |
---|
2022 | * @param string $op |
---|
2023 | */ |
---|
2024 | function safe_version_compare($a, $b, $op=null) |
---|
2025 | { |
---|
2026 | $replace_chars = create_function('$m', 'return ord(strtolower($m[1]));'); |
---|
2027 | |
---|
2028 | // add dot before groups of letters (version_compare does the same thing) |
---|
2029 | $a = preg_replace('#([0-9]+)([a-z]+)#i', '$1.$2', $a); |
---|
2030 | $b = preg_replace('#([0-9]+)([a-z]+)#i', '$1.$2', $b); |
---|
2031 | |
---|
2032 | // apply ord() to any single letter |
---|
2033 | $a = preg_replace_callback('#\b([a-z]{1})\b#i', $replace_chars, $a); |
---|
2034 | $b = preg_replace_callback('#\b([a-z]{1})\b#i', $replace_chars, $b); |
---|
2035 | |
---|
2036 | if (empty($op)) |
---|
2037 | { |
---|
2038 | return version_compare($a, $b); |
---|
2039 | } |
---|
2040 | else |
---|
2041 | { |
---|
2042 | return version_compare($a, $b, $op); |
---|
2043 | } |
---|
2044 | } |
---|
2045 | |
---|
2046 | ?> |
---|