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