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