1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008 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 | include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' ); |
---|
25 | include_once( PHPWG_ROOT_PATH .'include/functions_cookie.inc.php' ); |
---|
26 | include_once( PHPWG_ROOT_PATH .'include/functions_session.inc.php' ); |
---|
27 | include_once( PHPWG_ROOT_PATH .'include/functions_category.inc.php' ); |
---|
28 | include_once( PHPWG_ROOT_PATH .'include/functions_xml.inc.php' ); |
---|
29 | include_once( PHPWG_ROOT_PATH .'include/functions_group.inc.php' ); |
---|
30 | include_once( PHPWG_ROOT_PATH .'include/functions_html.inc.php' ); |
---|
31 | include_once( PHPWG_ROOT_PATH .'include/functions_tag.inc.php' ); |
---|
32 | include_once( PHPWG_ROOT_PATH .'include/functions_url.inc.php' ); |
---|
33 | include_once( PHPWG_ROOT_PATH .'include/functions_plugins.inc.php' ); |
---|
34 | |
---|
35 | //----------------------------------------------------------- generic functions |
---|
36 | |
---|
37 | /** |
---|
38 | * returns an array containing the possible values of an enum field |
---|
39 | * |
---|
40 | * @param string tablename |
---|
41 | * @param string fieldname |
---|
42 | */ |
---|
43 | function get_enums($table, $field) |
---|
44 | { |
---|
45 | // retrieving the properties of the table. Each line represents a field : |
---|
46 | // columns are 'Field', 'Type' |
---|
47 | $result = pwg_query('desc '.$table); |
---|
48 | while ($row = mysql_fetch_array($result)) |
---|
49 | { |
---|
50 | // we are only interested in the the field given in parameter for the |
---|
51 | // function |
---|
52 | if ($row['Field'] == $field) |
---|
53 | { |
---|
54 | // retrieving possible values of the enum field |
---|
55 | // enum('blue','green','black') |
---|
56 | $options = explode(',', substr($row['Type'], 5, -1)); |
---|
57 | foreach ($options as $i => $option) |
---|
58 | { |
---|
59 | $options[$i] = str_replace("'", '',$option); |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | mysql_free_result($result); |
---|
64 | return $options; |
---|
65 | } |
---|
66 | |
---|
67 | // get_boolean transforms a string to a boolean value. If the string is |
---|
68 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
69 | // any other case, true is returned. |
---|
70 | function get_boolean( $string ) |
---|
71 | { |
---|
72 | $boolean = true; |
---|
73 | if ( preg_match( '/^false$/i', $string ) ) |
---|
74 | { |
---|
75 | $boolean = false; |
---|
76 | } |
---|
77 | return $boolean; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
82 | * |
---|
83 | * @param mixed $var |
---|
84 | * @return mixed |
---|
85 | */ |
---|
86 | function boolean_to_string($var) |
---|
87 | { |
---|
88 | if (is_bool($var)) |
---|
89 | { |
---|
90 | if ($var) |
---|
91 | { |
---|
92 | return 'true'; |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | return 'false'; |
---|
97 | } |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | return $var; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | // The function get_moment returns a float value coresponding to the number |
---|
106 | // of seconds since the unix epoch (1st January 1970) and the microseconds |
---|
107 | // are precised : e.g. 1052343429.89276600 |
---|
108 | function get_moment() |
---|
109 | { |
---|
110 | $t1 = explode( ' ', microtime() ); |
---|
111 | $t2 = explode( '.', $t1[0] ); |
---|
112 | $t2 = $t1[1].'.'.$t2[1]; |
---|
113 | return $t2; |
---|
114 | } |
---|
115 | |
---|
116 | // The function get_elapsed_time returns the number of seconds (with 3 |
---|
117 | // decimals precision) between the start time and the end time given. |
---|
118 | function get_elapsed_time( $start, $end ) |
---|
119 | { |
---|
120 | return number_format( $end - $start, 3, '.', ' ').' s'; |
---|
121 | } |
---|
122 | |
---|
123 | // - The replace_space function replaces space and '-' characters |
---|
124 | // by their HTML equivalent &nbsb; and − |
---|
125 | // - The function does not replace characters in HTML tags |
---|
126 | // - This function was created because IE5 does not respect the |
---|
127 | // CSS "white-space: nowrap;" property unless space and minus |
---|
128 | // characters are replaced like this function does. |
---|
129 | // - Example : |
---|
130 | // <div class="foo">My friend</div> |
---|
131 | // ( 01234567891111111111222222222233 ) |
---|
132 | // ( 0123456789012345678901 ) |
---|
133 | // becomes : |
---|
134 | // <div class="foo">My friend</div> |
---|
135 | function replace_space( $string ) |
---|
136 | { |
---|
137 | //return $string; |
---|
138 | $return_string = ''; |
---|
139 | // $remaining is the rest of the string where to replace spaces characters |
---|
140 | $remaining = $string; |
---|
141 | // $start represents the position of the next '<' character |
---|
142 | // $end represents the position of the next '>' character |
---|
143 | $start = 0; |
---|
144 | $end = 0; |
---|
145 | $start = strpos ( $remaining, '<' ); // -> 0 |
---|
146 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
147 | // as long as a '<' and his friend '>' are found, we loop |
---|
148 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
149 | { |
---|
150 | // $treatment is the part of the string to treat |
---|
151 | // In the first loop of our example, this variable is empty, but in the |
---|
152 | // second loop, it equals 'My friend' |
---|
153 | $treatment = substr ( $remaining, 0, $start ); |
---|
154 | // Replacement of ' ' by his equivalent ' ' |
---|
155 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
156 | $treatment = str_replace( '-', '−', $treatment ); |
---|
157 | // composing the string to return by adding the treated string and the |
---|
158 | // following HTML tag -> 'My friend</div>' |
---|
159 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
160 | // the remaining string is deplaced to the part after the '>' of this |
---|
161 | // loop |
---|
162 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
163 | $start = strpos ( $remaining, '<' ); |
---|
164 | $end = strpos ( $remaining, '>' ); |
---|
165 | } |
---|
166 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
167 | $treatment = str_replace( '-', '−', $treatment ); |
---|
168 | $return_string.= $treatment; |
---|
169 | |
---|
170 | return $return_string; |
---|
171 | } |
---|
172 | |
---|
173 | // get_extension returns the part of the string after the last "." |
---|
174 | function get_extension( $filename ) |
---|
175 | { |
---|
176 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
177 | } |
---|
178 | |
---|
179 | // get_filename_wo_extension returns the part of the string before the last |
---|
180 | // ".". |
---|
181 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
182 | function get_filename_wo_extension( $filename ) |
---|
183 | { |
---|
184 | $pos = strrpos( $filename, '.' ); |
---|
185 | return ($pos===false) ? $filename : substr( $filename, 0, $pos); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * returns an array contening sub-directories, excluding "CVS" |
---|
190 | * |
---|
191 | * @param string $dir |
---|
192 | * @return array |
---|
193 | */ |
---|
194 | function get_dirs($directory) |
---|
195 | { |
---|
196 | $sub_dirs = array(); |
---|
197 | |
---|
198 | if ($opendir = opendir($directory)) |
---|
199 | { |
---|
200 | while ($file = readdir($opendir)) |
---|
201 | { |
---|
202 | if ($file != '.' |
---|
203 | and $file != '..' |
---|
204 | and is_dir($directory.'/'.$file) |
---|
205 | and $file != 'CVS' |
---|
206 | and $file != '.svn') |
---|
207 | { |
---|
208 | array_push($sub_dirs, $file); |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | return $sub_dirs; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * returns thumbnail directory name of input diretoty name |
---|
217 | * make thumbnail directory is necessary |
---|
218 | * set error messages on array messages |
---|
219 | * |
---|
220 | * @param: |
---|
221 | * string $dirname |
---|
222 | * arrayy $errors |
---|
223 | * @return bool false on error else string directory name |
---|
224 | */ |
---|
225 | function mkget_thumbnail_dir($dirname, &$errors) |
---|
226 | { |
---|
227 | $tndir = $dirname.'/thumbnail'; |
---|
228 | if (!is_dir($tndir)) |
---|
229 | { |
---|
230 | if (!is_writable($dirname)) |
---|
231 | { |
---|
232 | array_push($errors, |
---|
233 | '['.$dirname.'] : '.l10n('no_write_access')); |
---|
234 | return false; |
---|
235 | } |
---|
236 | umask(0000); |
---|
237 | mkdir($tndir, 0777); |
---|
238 | } |
---|
239 | |
---|
240 | return $tndir; |
---|
241 | } |
---|
242 | |
---|
243 | // The get_picture_size function return an array containing : |
---|
244 | // - $picture_size[0] : final width |
---|
245 | // - $picture_size[1] : final height |
---|
246 | // The final dimensions are calculated thanks to the original dimensions and |
---|
247 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
248 | // the width/height ratio |
---|
249 | function get_picture_size( $original_width, $original_height, |
---|
250 | $max_width, $max_height ) |
---|
251 | { |
---|
252 | $width = $original_width; |
---|
253 | $height = $original_height; |
---|
254 | $is_original_size = true; |
---|
255 | |
---|
256 | if ( $max_width != "" ) |
---|
257 | { |
---|
258 | if ( $original_width > $max_width ) |
---|
259 | { |
---|
260 | $width = $max_width; |
---|
261 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
262 | } |
---|
263 | } |
---|
264 | if ( $max_height != "" ) |
---|
265 | { |
---|
266 | if ( $original_height > $max_height ) |
---|
267 | { |
---|
268 | $height = $max_height; |
---|
269 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
270 | $is_original_size = false; |
---|
271 | } |
---|
272 | } |
---|
273 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
274 | and $max_width != 0 and $max_height != 0 ) |
---|
275 | { |
---|
276 | $ratioWidth = $original_width / $max_width; |
---|
277 | $ratioHeight = $original_height / $max_height; |
---|
278 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
279 | { |
---|
280 | if ( $ratioWidth < $ratioHeight ) |
---|
281 | { |
---|
282 | $width = floor( $original_width / $ratioHeight ); |
---|
283 | $height = $max_height; |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | $width = $max_width; |
---|
288 | $height = floor( $original_height / $ratioWidth ); |
---|
289 | } |
---|
290 | $is_original_size = false; |
---|
291 | } |
---|
292 | } |
---|
293 | $picture_size = array(); |
---|
294 | $picture_size[0] = $width; |
---|
295 | $picture_size[1] = $height; |
---|
296 | return $picture_size; |
---|
297 | } |
---|
298 | |
---|
299 | /* Returns true if the string appears to be encoded in UTF-8. (from wordpress) |
---|
300 | * @param string Str |
---|
301 | */ |
---|
302 | function seems_utf8($Str) { # by bmorel at ssi dot fr |
---|
303 | for ($i=0; $i<strlen($Str); $i++) { |
---|
304 | if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb |
---|
305 | elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb |
---|
306 | elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb |
---|
307 | elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb |
---|
308 | elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb |
---|
309 | elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b |
---|
310 | else return false; # Does not match any model |
---|
311 | for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? |
---|
312 | if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) |
---|
313 | return false; |
---|
314 | } |
---|
315 | } |
---|
316 | return true; |
---|
317 | } |
---|
318 | |
---|
319 | /* Remove accents from a UTF-8 or ISO-859-1 string (from wordpress) |
---|
320 | * @param string sstring - an UTF-8 or ISO-8859-1 string |
---|
321 | */ |
---|
322 | function remove_accents($string) |
---|
323 | { |
---|
324 | if ( !preg_match('/[\x80-\xff]/', $string) ) |
---|
325 | return $string; |
---|
326 | |
---|
327 | if (seems_utf8($string)) { |
---|
328 | $chars = array( |
---|
329 | // Decompositions for Latin-1 Supplement |
---|
330 | chr(195).chr(128) => 'A', chr(195).chr(129) => 'A', |
---|
331 | chr(195).chr(130) => 'A', chr(195).chr(131) => 'A', |
---|
332 | chr(195).chr(132) => 'A', chr(195).chr(133) => 'A', |
---|
333 | chr(195).chr(135) => 'C', chr(195).chr(136) => 'E', |
---|
334 | chr(195).chr(137) => 'E', chr(195).chr(138) => 'E', |
---|
335 | chr(195).chr(139) => 'E', chr(195).chr(140) => 'I', |
---|
336 | chr(195).chr(141) => 'I', chr(195).chr(142) => 'I', |
---|
337 | chr(195).chr(143) => 'I', chr(195).chr(145) => 'N', |
---|
338 | chr(195).chr(146) => 'O', chr(195).chr(147) => 'O', |
---|
339 | chr(195).chr(148) => 'O', chr(195).chr(149) => 'O', |
---|
340 | chr(195).chr(150) => 'O', chr(195).chr(153) => 'U', |
---|
341 | chr(195).chr(154) => 'U', chr(195).chr(155) => 'U', |
---|
342 | chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y', |
---|
343 | chr(195).chr(159) => 's', chr(195).chr(160) => 'a', |
---|
344 | chr(195).chr(161) => 'a', chr(195).chr(162) => 'a', |
---|
345 | chr(195).chr(163) => 'a', chr(195).chr(164) => 'a', |
---|
346 | chr(195).chr(165) => 'a', chr(195).chr(167) => 'c', |
---|
347 | chr(195).chr(168) => 'e', chr(195).chr(169) => 'e', |
---|
348 | chr(195).chr(170) => 'e', chr(195).chr(171) => 'e', |
---|
349 | chr(195).chr(172) => 'i', chr(195).chr(173) => 'i', |
---|
350 | chr(195).chr(174) => 'i', chr(195).chr(175) => 'i', |
---|
351 | chr(195).chr(177) => 'n', chr(195).chr(178) => 'o', |
---|
352 | chr(195).chr(179) => 'o', chr(195).chr(180) => 'o', |
---|
353 | chr(195).chr(181) => 'o', chr(195).chr(182) => 'o', |
---|
354 | chr(195).chr(182) => 'o', chr(195).chr(185) => 'u', |
---|
355 | chr(195).chr(186) => 'u', chr(195).chr(187) => 'u', |
---|
356 | chr(195).chr(188) => 'u', chr(195).chr(189) => 'y', |
---|
357 | chr(195).chr(191) => 'y', |
---|
358 | // Decompositions for Latin Extended-A |
---|
359 | chr(196).chr(128) => 'A', chr(196).chr(129) => 'a', |
---|
360 | chr(196).chr(130) => 'A', chr(196).chr(131) => 'a', |
---|
361 | chr(196).chr(132) => 'A', chr(196).chr(133) => 'a', |
---|
362 | chr(196).chr(134) => 'C', chr(196).chr(135) => 'c', |
---|
363 | chr(196).chr(136) => 'C', chr(196).chr(137) => 'c', |
---|
364 | chr(196).chr(138) => 'C', chr(196).chr(139) => 'c', |
---|
365 | chr(196).chr(140) => 'C', chr(196).chr(141) => 'c', |
---|
366 | chr(196).chr(142) => 'D', chr(196).chr(143) => 'd', |
---|
367 | chr(196).chr(144) => 'D', chr(196).chr(145) => 'd', |
---|
368 | chr(196).chr(146) => 'E', chr(196).chr(147) => 'e', |
---|
369 | chr(196).chr(148) => 'E', chr(196).chr(149) => 'e', |
---|
370 | chr(196).chr(150) => 'E', chr(196).chr(151) => 'e', |
---|
371 | chr(196).chr(152) => 'E', chr(196).chr(153) => 'e', |
---|
372 | chr(196).chr(154) => 'E', chr(196).chr(155) => 'e', |
---|
373 | chr(196).chr(156) => 'G', chr(196).chr(157) => 'g', |
---|
374 | chr(196).chr(158) => 'G', chr(196).chr(159) => 'g', |
---|
375 | chr(196).chr(160) => 'G', chr(196).chr(161) => 'g', |
---|
376 | chr(196).chr(162) => 'G', chr(196).chr(163) => 'g', |
---|
377 | chr(196).chr(164) => 'H', chr(196).chr(165) => 'h', |
---|
378 | chr(196).chr(166) => 'H', chr(196).chr(167) => 'h', |
---|
379 | chr(196).chr(168) => 'I', chr(196).chr(169) => 'i', |
---|
380 | chr(196).chr(170) => 'I', chr(196).chr(171) => 'i', |
---|
381 | chr(196).chr(172) => 'I', chr(196).chr(173) => 'i', |
---|
382 | chr(196).chr(174) => 'I', chr(196).chr(175) => 'i', |
---|
383 | chr(196).chr(176) => 'I', chr(196).chr(177) => 'i', |
---|
384 | chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij', |
---|
385 | chr(196).chr(180) => 'J', chr(196).chr(181) => 'j', |
---|
386 | chr(196).chr(182) => 'K', chr(196).chr(183) => 'k', |
---|
387 | chr(196).chr(184) => 'k', chr(196).chr(185) => 'L', |
---|
388 | chr(196).chr(186) => 'l', chr(196).chr(187) => 'L', |
---|
389 | chr(196).chr(188) => 'l', chr(196).chr(189) => 'L', |
---|
390 | chr(196).chr(190) => 'l', chr(196).chr(191) => 'L', |
---|
391 | chr(197).chr(128) => 'l', chr(197).chr(129) => 'L', |
---|
392 | chr(197).chr(130) => 'l', chr(197).chr(131) => 'N', |
---|
393 | chr(197).chr(132) => 'n', chr(197).chr(133) => 'N', |
---|
394 | chr(197).chr(134) => 'n', chr(197).chr(135) => 'N', |
---|
395 | chr(197).chr(136) => 'n', chr(197).chr(137) => 'N', |
---|
396 | chr(197).chr(138) => 'n', chr(197).chr(139) => 'N', |
---|
397 | chr(197).chr(140) => 'O', chr(197).chr(141) => 'o', |
---|
398 | chr(197).chr(142) => 'O', chr(197).chr(143) => 'o', |
---|
399 | chr(197).chr(144) => 'O', chr(197).chr(145) => 'o', |
---|
400 | chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe', |
---|
401 | chr(197).chr(148) => 'R',chr(197).chr(149) => 'r', |
---|
402 | chr(197).chr(150) => 'R',chr(197).chr(151) => 'r', |
---|
403 | chr(197).chr(152) => 'R',chr(197).chr(153) => 'r', |
---|
404 | chr(197).chr(154) => 'S',chr(197).chr(155) => 's', |
---|
405 | chr(197).chr(156) => 'S',chr(197).chr(157) => 's', |
---|
406 | chr(197).chr(158) => 'S',chr(197).chr(159) => 's', |
---|
407 | chr(197).chr(160) => 'S', chr(197).chr(161) => 's', |
---|
408 | chr(197).chr(162) => 'T', chr(197).chr(163) => 't', |
---|
409 | chr(197).chr(164) => 'T', chr(197).chr(165) => 't', |
---|
410 | chr(197).chr(166) => 'T', chr(197).chr(167) => 't', |
---|
411 | chr(197).chr(168) => 'U', chr(197).chr(169) => 'u', |
---|
412 | chr(197).chr(170) => 'U', chr(197).chr(171) => 'u', |
---|
413 | chr(197).chr(172) => 'U', chr(197).chr(173) => 'u', |
---|
414 | chr(197).chr(174) => 'U', chr(197).chr(175) => 'u', |
---|
415 | chr(197).chr(176) => 'U', chr(197).chr(177) => 'u', |
---|
416 | chr(197).chr(178) => 'U', chr(197).chr(179) => 'u', |
---|
417 | chr(197).chr(180) => 'W', chr(197).chr(181) => 'w', |
---|
418 | chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y', |
---|
419 | chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z', |
---|
420 | chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z', |
---|
421 | chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z', |
---|
422 | chr(197).chr(190) => 'z', chr(197).chr(191) => 's', |
---|
423 | // Euro Sign |
---|
424 | chr(226).chr(130).chr(172) => 'E', |
---|
425 | // GBP (Pound) Sign |
---|
426 | chr(194).chr(163) => ''); |
---|
427 | |
---|
428 | $string = strtr($string, $chars); |
---|
429 | } else { |
---|
430 | // Assume ISO-8859-1 if not UTF-8 |
---|
431 | $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158) |
---|
432 | .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194) |
---|
433 | .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202) |
---|
434 | .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210) |
---|
435 | .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218) |
---|
436 | .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227) |
---|
437 | .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235) |
---|
438 | .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243) |
---|
439 | .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251) |
---|
440 | .chr(252).chr(253).chr(255); |
---|
441 | |
---|
442 | $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy"; |
---|
443 | |
---|
444 | $string = strtr($string, $chars['in'], $chars['out']); |
---|
445 | $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254)); |
---|
446 | $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th'); |
---|
447 | $string = str_replace($double_chars['in'], $double_chars['out'], $string); |
---|
448 | } |
---|
449 | |
---|
450 | return $string; |
---|
451 | } |
---|
452 | |
---|
453 | /** |
---|
454 | * simplify a string to insert it into an URL |
---|
455 | * |
---|
456 | * @param string |
---|
457 | * @return string |
---|
458 | */ |
---|
459 | function str2url($str) |
---|
460 | { |
---|
461 | $str = remove_accents($str); |
---|
462 | $str = preg_replace('/[^a-z0-9_\s\'\:\/\[\],-]/','',strtolower($str)); |
---|
463 | $str = preg_replace('/[\s\'\:\/\[\],-]+/',' ',trim($str)); |
---|
464 | $res = str_replace(' ','_',$str); |
---|
465 | |
---|
466 | return $res; |
---|
467 | } |
---|
468 | |
---|
469 | //-------------------------------------------- PhpWebGallery specific functions |
---|
470 | |
---|
471 | /** |
---|
472 | * returns an array with a list of {language_code => language_name} |
---|
473 | * |
---|
474 | * @returns array |
---|
475 | */ |
---|
476 | function get_languages($target_charset = null) |
---|
477 | { |
---|
478 | if ( empty($target_charset) ) |
---|
479 | { |
---|
480 | $target_charset = get_pwg_charset(); |
---|
481 | } |
---|
482 | $target_charset = strtolower($target_charset); |
---|
483 | |
---|
484 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
485 | $languages = array(); |
---|
486 | |
---|
487 | while ($file = readdir($dir)) |
---|
488 | { |
---|
489 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
490 | if (is_dir($path) and !is_link($path) and file_exists($path.'/iso.txt')) |
---|
491 | { |
---|
492 | list($language_name) = @file($path.'/iso.txt'); |
---|
493 | |
---|
494 | $langdef = explode('.',$file); |
---|
495 | if (count($langdef)>1) // (langCode,encoding) |
---|
496 | { |
---|
497 | $langdef[1] = strtolower($langdef[1]); |
---|
498 | |
---|
499 | if ( |
---|
500 | $target_charset==$langdef[1] |
---|
501 | or |
---|
502 | ($target_charset=='utf-8' and $langdef[1]=='iso-8859-1') |
---|
503 | or |
---|
504 | ($target_charset=='iso-8859-1' and |
---|
505 | in_array( substr($langdef[0],2), array('en','fr','de','es','it','nl'))) |
---|
506 | ) |
---|
507 | { |
---|
508 | $language_name = convert_charset($language_name, |
---|
509 | $langdef[1], $target_charset); |
---|
510 | $languages[ $langdef[0] ] = $language_name; |
---|
511 | } |
---|
512 | else |
---|
513 | continue; // the language encoding is not compatible with our charset |
---|
514 | } |
---|
515 | else |
---|
516 | { // UTF-8 |
---|
517 | $language_name = convert_charset($language_name, |
---|
518 | 'utf-8', $target_charset); |
---|
519 | $languages[$file] = $language_name; |
---|
520 | } |
---|
521 | } |
---|
522 | } |
---|
523 | closedir($dir); |
---|
524 | @asort($languages); |
---|
525 | @reset($languages); |
---|
526 | |
---|
527 | return $languages; |
---|
528 | } |
---|
529 | |
---|
530 | function pwg_log($image_id = null, $image_type = null) |
---|
531 | { |
---|
532 | global $conf, $user, $page; |
---|
533 | |
---|
534 | $do_log = true; |
---|
535 | if (!$conf['log']) |
---|
536 | { |
---|
537 | $do_log = false; |
---|
538 | } |
---|
539 | if (is_admin() and !$conf['history_admin']) |
---|
540 | { |
---|
541 | $do_log = false; |
---|
542 | } |
---|
543 | if (is_a_guest() and !$conf['history_guest']) |
---|
544 | { |
---|
545 | $do_log = false; |
---|
546 | } |
---|
547 | |
---|
548 | $do_log = trigger_event('pwg_log_allowed', $do_log, $image_id, $image_type); |
---|
549 | |
---|
550 | if (!$do_log) |
---|
551 | { |
---|
552 | return false; |
---|
553 | } |
---|
554 | |
---|
555 | $tags_string = null; |
---|
556 | if (isset($page['section']) and $page['section'] == 'tags') |
---|
557 | { |
---|
558 | $tag_ids = array(); |
---|
559 | foreach ($page['tags'] as $tag) |
---|
560 | { |
---|
561 | array_push($tag_ids, $tag['id']); |
---|
562 | } |
---|
563 | |
---|
564 | $tags_string = implode(',', $tag_ids); |
---|
565 | } |
---|
566 | |
---|
567 | $query = ' |
---|
568 | INSERT INTO '.HISTORY_TABLE.' |
---|
569 | ( |
---|
570 | date, |
---|
571 | time, |
---|
572 | year, |
---|
573 | month, |
---|
574 | day, |
---|
575 | hour, |
---|
576 | user_id, |
---|
577 | IP, |
---|
578 | section, |
---|
579 | category_id, |
---|
580 | image_id, |
---|
581 | image_type, |
---|
582 | tag_ids |
---|
583 | ) |
---|
584 | VALUES |
---|
585 | ( |
---|
586 | CURDATE(), |
---|
587 | CURTIME(), |
---|
588 | YEAR( CURDATE() ), |
---|
589 | MONTH( CURDATE() ), |
---|
590 | DAYOFMONTH( CURDATE() ), |
---|
591 | HOUR( CURTIME() ), |
---|
592 | '.$user['id'].', |
---|
593 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
594 | '.(isset($page['section']) ? "'".$page['section']."'" : 'NULL').', |
---|
595 | '.(isset($page['category']['id']) ? $page['category']['id'] : 'NULL').', |
---|
596 | '.(isset($image_id) ? $image_id : 'NULL').', |
---|
597 | '.(isset($image_type) ? "'".$image_type."'" : 'NULL').', |
---|
598 | '.(isset($tags_string) ? "'".$tags_string."'" : 'NULL').' |
---|
599 | ) |
---|
600 | ;'; |
---|
601 | pwg_query($query); |
---|
602 | |
---|
603 | return true; |
---|
604 | } |
---|
605 | |
---|
606 | // format_date returns a formatted date for display. The date given in |
---|
607 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
608 | // american format (2003-09-15). By option, you can show the time. The |
---|
609 | // output is internationalized. |
---|
610 | // |
---|
611 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
612 | function format_date($date, $type = 'us', $show_time = false) |
---|
613 | { |
---|
614 | global $lang; |
---|
615 | |
---|
616 | list($year,$month,$day,$hour,$minute,$second) = array(0,0,0,0,0,0); |
---|
617 | |
---|
618 | switch ( $type ) |
---|
619 | { |
---|
620 | case 'us' : |
---|
621 | { |
---|
622 | list($year,$month,$day) = explode('-', $date); |
---|
623 | break; |
---|
624 | } |
---|
625 | case 'unix' : |
---|
626 | { |
---|
627 | list($year,$month,$day,$hour,$minute) = |
---|
628 | explode('.', date('Y.n.j.G.i', $date)); |
---|
629 | break; |
---|
630 | } |
---|
631 | case 'mysql_datetime' : |
---|
632 | { |
---|
633 | preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
634 | $date, $out); |
---|
635 | list($year,$month,$day,$hour,$minute,$second) = |
---|
636 | array($out[1],$out[2],$out[3],$out[4],$out[5],$out[6]); |
---|
637 | break; |
---|
638 | } |
---|
639 | } |
---|
640 | $formated_date = ''; |
---|
641 | // before 1970, Microsoft Windows can't mktime |
---|
642 | if ($year >= 1970) |
---|
643 | { |
---|
644 | // we ask midday because Windows think it's prior to midnight with a |
---|
645 | // zero and refuse to work |
---|
646 | $formated_date.= $lang['day'][date('w', mktime(12,0,0,$month,$day,$year))]; |
---|
647 | } |
---|
648 | $formated_date.= ' '.$day; |
---|
649 | $formated_date.= ' '.$lang['month'][(int)$month]; |
---|
650 | $formated_date.= ' '.$year; |
---|
651 | if ($show_time) |
---|
652 | { |
---|
653 | $formated_date.= ' '.$hour.':'.$minute; |
---|
654 | } |
---|
655 | |
---|
656 | return $formated_date; |
---|
657 | } |
---|
658 | |
---|
659 | function pwg_query($query) |
---|
660 | { |
---|
661 | global $conf,$page,$debug,$t2; |
---|
662 | |
---|
663 | $start = get_moment(); |
---|
664 | $result = mysql_query($query) or my_error($query."\n"); |
---|
665 | |
---|
666 | $time = get_moment() - $start; |
---|
667 | |
---|
668 | if (!isset($page['count_queries'])) |
---|
669 | { |
---|
670 | $page['count_queries'] = 0; |
---|
671 | $page['queries_time'] = 0; |
---|
672 | } |
---|
673 | |
---|
674 | $page['count_queries']++; |
---|
675 | $page['queries_time']+= $time; |
---|
676 | |
---|
677 | if ($conf['show_queries']) |
---|
678 | { |
---|
679 | $output = ''; |
---|
680 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
681 | $output.= "\n".$query; |
---|
682 | $output.= "\n".'(this query time : '; |
---|
683 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
684 | $output.= "\n".'(total SQL time : '; |
---|
685 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
686 | $output.= "\n".'(total time : '; |
---|
687 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
688 | if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) ) |
---|
689 | { |
---|
690 | $output.= "\n".'(num rows : '; |
---|
691 | $output.= mysql_num_rows($result).' )'; |
---|
692 | } |
---|
693 | elseif ( $result!=null |
---|
694 | and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) ) |
---|
695 | { |
---|
696 | $output.= "\n".'(affected rows : '; |
---|
697 | $output.= mysql_affected_rows().' )'; |
---|
698 | } |
---|
699 | $output.= "</pre>\n"; |
---|
700 | |
---|
701 | $debug .= $output; |
---|
702 | } |
---|
703 | |
---|
704 | return $result; |
---|
705 | } |
---|
706 | |
---|
707 | function pwg_debug( $string ) |
---|
708 | { |
---|
709 | global $debug,$t2,$page; |
---|
710 | |
---|
711 | $now = explode( ' ', microtime() ); |
---|
712 | $now2 = explode( '.', $now[0] ); |
---|
713 | $now2 = $now[1].'.'.$now2[1]; |
---|
714 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
715 | $debug .= '<p>'; |
---|
716 | $debug.= '['.$time.', '; |
---|
717 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
718 | $debug.= "</p>\n"; |
---|
719 | } |
---|
720 | |
---|
721 | /** |
---|
722 | * Redirects to the given URL (HTTP method) |
---|
723 | * |
---|
724 | * Note : once this function called, the execution doesn't go further |
---|
725 | * (presence of an exit() instruction. |
---|
726 | * |
---|
727 | * @param string $url |
---|
728 | * @return void |
---|
729 | */ |
---|
730 | function redirect_http( $url ) |
---|
731 | { |
---|
732 | if (ob_get_length () !== FALSE) |
---|
733 | { |
---|
734 | ob_clean(); |
---|
735 | } |
---|
736 | // default url is on html format |
---|
737 | $url = html_entity_decode($url); |
---|
738 | header('Request-URI: '.$url); |
---|
739 | header('Content-Location: '.$url); |
---|
740 | header('Location: '.$url); |
---|
741 | exit(); |
---|
742 | } |
---|
743 | |
---|
744 | /** |
---|
745 | * Redirects to the given URL (HTML method) |
---|
746 | * |
---|
747 | * Note : once this function called, the execution doesn't go further |
---|
748 | * (presence of an exit() instruction. |
---|
749 | * |
---|
750 | * @param string $url |
---|
751 | * @param string $title_msg |
---|
752 | * @param integer $refreh_time |
---|
753 | * @return void |
---|
754 | */ |
---|
755 | function redirect_html( $url , $msg = '', $refresh_time = 0) |
---|
756 | { |
---|
757 | global $user, $template, $lang_info, $conf, $lang, $t2, $page, $debug; |
---|
758 | |
---|
759 | if (!isset($lang_info)) |
---|
760 | { |
---|
761 | $user = build_user( $conf['guest_id'], true); |
---|
762 | load_language('common.lang'); |
---|
763 | trigger_action('loading_lang'); |
---|
764 | load_language('local.lang'); |
---|
765 | list($tmpl, $thm) = explode('/', get_default_template()); |
---|
766 | $template = new Template(PHPWG_ROOT_PATH.'template/'.$tmpl, $thm); |
---|
767 | } |
---|
768 | else |
---|
769 | { |
---|
770 | $template = new Template(PHPWG_ROOT_PATH.'template/'.$user['template'], $user['theme']); |
---|
771 | } |
---|
772 | |
---|
773 | if (empty($msg)) |
---|
774 | { |
---|
775 | $redirect_msg = l10n('redirect_msg'); |
---|
776 | } |
---|
777 | else |
---|
778 | { |
---|
779 | $redirect_msg = $msg; |
---|
780 | } |
---|
781 | $redirect_msg = nl2br($redirect_msg); |
---|
782 | |
---|
783 | $refresh = $refresh_time; |
---|
784 | $url_link = $url; |
---|
785 | $title = 'redirection'; |
---|
786 | |
---|
787 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
788 | |
---|
789 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
790 | |
---|
791 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
792 | $template->parse('redirect'); |
---|
793 | |
---|
794 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
795 | |
---|
796 | exit(); |
---|
797 | } |
---|
798 | |
---|
799 | /** |
---|
800 | * Redirects to the given URL (Switch to HTTP method or HTML method) |
---|
801 | * |
---|
802 | * Note : once this function called, the execution doesn't go further |
---|
803 | * (presence of an exit() instruction. |
---|
804 | * |
---|
805 | * @param string $url |
---|
806 | * @param string $title_msg |
---|
807 | * @param integer $refreh_time |
---|
808 | * @return void |
---|
809 | */ |
---|
810 | function redirect( $url , $msg = '', $refresh_time = 0) |
---|
811 | { |
---|
812 | global $conf; |
---|
813 | |
---|
814 | // with RefeshTime <> 0, only html must be used |
---|
815 | if ($conf['default_redirect_method']=='http' |
---|
816 | and $refresh_time==0 |
---|
817 | and !headers_sent() |
---|
818 | ) |
---|
819 | { |
---|
820 | redirect_http($url); |
---|
821 | } |
---|
822 | else |
---|
823 | { |
---|
824 | redirect_html($url, $msg, $refresh_time); |
---|
825 | } |
---|
826 | } |
---|
827 | |
---|
828 | /** |
---|
829 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
830 | * |
---|
831 | * @param array $rejects |
---|
832 | * @param boolean $escape - if true escape & to & (for html) |
---|
833 | * @returns string |
---|
834 | */ |
---|
835 | function get_query_string_diff($rejects=array(), $escape=true) |
---|
836 | { |
---|
837 | $query_string = ''; |
---|
838 | |
---|
839 | $str = $_SERVER['QUERY_STRING']; |
---|
840 | parse_str($str, $vars); |
---|
841 | |
---|
842 | $is_first = true; |
---|
843 | foreach ($vars as $key => $value) |
---|
844 | { |
---|
845 | if (!in_array($key, $rejects)) |
---|
846 | { |
---|
847 | $query_string.= $is_first ? '?' : ($escape ? '&' : '&' ); |
---|
848 | $is_first = false; |
---|
849 | $query_string.= $key.'='.$value; |
---|
850 | } |
---|
851 | } |
---|
852 | |
---|
853 | return $query_string; |
---|
854 | } |
---|
855 | |
---|
856 | function url_is_remote($url) |
---|
857 | { |
---|
858 | if ( strncmp($url, 'http://', 7)==0 |
---|
859 | or strncmp($url, 'https://', 8)==0 ) |
---|
860 | { |
---|
861 | return true; |
---|
862 | } |
---|
863 | return false; |
---|
864 | } |
---|
865 | |
---|
866 | /** |
---|
867 | * returns available template/theme |
---|
868 | */ |
---|
869 | function get_pwg_themes() |
---|
870 | { |
---|
871 | global $conf; |
---|
872 | $themes = array(); |
---|
873 | |
---|
874 | $template_dir = PHPWG_ROOT_PATH.'template'; |
---|
875 | |
---|
876 | foreach (get_dirs($template_dir) as $template) |
---|
877 | { |
---|
878 | foreach (get_dirs($template_dir.'/'.$template.'/theme') as $theme) |
---|
879 | { |
---|
880 | if ( ($template.'/'.$theme) != $conf['admin_layout'] ) |
---|
881 | array_push($themes, $template.'/'.$theme); |
---|
882 | } |
---|
883 | } |
---|
884 | |
---|
885 | return $themes; |
---|
886 | } |
---|
887 | |
---|
888 | /* Returns the PATH to the thumbnail to be displayed. If the element does not |
---|
889 | * have a thumbnail, the default mime image path is returned. The PATH can be |
---|
890 | * used in the php script, but not sent to the browser. |
---|
891 | * @param array element_info assoc array containing element info from db |
---|
892 | * at least 'path', 'tn_ext' and 'id' should be present |
---|
893 | */ |
---|
894 | function get_thumbnail_path($element_info) |
---|
895 | { |
---|
896 | $path = get_thumbnail_location($element_info); |
---|
897 | if ( !url_is_remote($path) ) |
---|
898 | { |
---|
899 | $path = PHPWG_ROOT_PATH.$path; |
---|
900 | } |
---|
901 | return $path; |
---|
902 | } |
---|
903 | |
---|
904 | /* Returns the URL of the thumbnail to be displayed. If the element does not |
---|
905 | * have a thumbnail, the default mime image url is returned. The URL can be |
---|
906 | * sent to the browser, but not used in the php script. |
---|
907 | * @param array element_info assoc array containing element info from db |
---|
908 | * at least 'path', 'tn_ext' and 'id' should be present |
---|
909 | */ |
---|
910 | function get_thumbnail_url($element_info) |
---|
911 | { |
---|
912 | $path = get_thumbnail_location($element_info); |
---|
913 | if ( !url_is_remote($path) ) |
---|
914 | { |
---|
915 | $path = embellish_url(get_root_url().$path); |
---|
916 | } |
---|
917 | |
---|
918 | // plugins want another url ? |
---|
919 | $path = trigger_event('get_thumbnail_url', $path, $element_info); |
---|
920 | return $path; |
---|
921 | } |
---|
922 | |
---|
923 | /* returns the relative path of the thumnail with regards to to the root |
---|
924 | of phpwebgallery (not the current page!).This function is not intended to be |
---|
925 | called directly from code.*/ |
---|
926 | function get_thumbnail_location($element_info) |
---|
927 | { |
---|
928 | global $conf; |
---|
929 | if ( !empty( $element_info['tn_ext'] ) ) |
---|
930 | { |
---|
931 | $path = substr_replace( |
---|
932 | get_filename_wo_extension($element_info['path']), |
---|
933 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
934 | strrpos($element_info['path'],'/'), |
---|
935 | 1 |
---|
936 | ); |
---|
937 | $path.= '.'.$element_info['tn_ext']; |
---|
938 | } |
---|
939 | else |
---|
940 | { |
---|
941 | $path = get_themeconf('mime_icon_dir') |
---|
942 | .strtolower(get_extension($element_info['path'])).'.png'; |
---|
943 | } |
---|
944 | |
---|
945 | // plugins want another location ? |
---|
946 | $path = trigger_event( 'get_thumbnail_location', $path, $element_info); |
---|
947 | return $path; |
---|
948 | } |
---|
949 | |
---|
950 | /* returns the title of the thumnail */ |
---|
951 | function get_thumbnail_title($element_info) |
---|
952 | { |
---|
953 | // message in title for the thumbnail |
---|
954 | if (isset($element_info['file'])) |
---|
955 | { |
---|
956 | $thumbnail_title = $element_info['file']; |
---|
957 | } |
---|
958 | else |
---|
959 | { |
---|
960 | $thumbnail_title = ''; |
---|
961 | } |
---|
962 | |
---|
963 | if (!empty($element_info['filesize'])) |
---|
964 | { |
---|
965 | $thumbnail_title .= ' : '.l10n_dec('%d Kb', '%d Kb', $element_info['filesize']); |
---|
966 | } |
---|
967 | |
---|
968 | return $thumbnail_title; |
---|
969 | } |
---|
970 | |
---|
971 | // my_error returns (or send to standard output) the message concerning the |
---|
972 | // error occured for the last mysql query. |
---|
973 | function my_error($header) |
---|
974 | { |
---|
975 | global $conf; |
---|
976 | |
---|
977 | $error = '<pre>'; |
---|
978 | $error.= $header; |
---|
979 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
980 | $error.= mysql_error(); |
---|
981 | $error.= '</pre>'; |
---|
982 | |
---|
983 | if ($conf['die_on_sql_error']) |
---|
984 | { |
---|
985 | die($error); |
---|
986 | } |
---|
987 | else |
---|
988 | { |
---|
989 | echo $error; |
---|
990 | } |
---|
991 | } |
---|
992 | |
---|
993 | /** |
---|
994 | * creates an array based on a query, this function is a very common pattern |
---|
995 | * used here |
---|
996 | * |
---|
997 | * @param string $query |
---|
998 | * @param string $fieldname |
---|
999 | * @return array |
---|
1000 | */ |
---|
1001 | function array_from_query($query, $fieldname) |
---|
1002 | { |
---|
1003 | $array = array(); |
---|
1004 | |
---|
1005 | $result = pwg_query($query); |
---|
1006 | while ($row = mysql_fetch_array($result)) |
---|
1007 | { |
---|
1008 | array_push($array, $row[$fieldname]); |
---|
1009 | } |
---|
1010 | |
---|
1011 | return $array; |
---|
1012 | } |
---|
1013 | |
---|
1014 | /** |
---|
1015 | * fill the current user caddie with given elements, if not already in |
---|
1016 | * caddie |
---|
1017 | * |
---|
1018 | * @param array elements_id |
---|
1019 | */ |
---|
1020 | function fill_caddie($elements_id) |
---|
1021 | { |
---|
1022 | global $user; |
---|
1023 | |
---|
1024 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1025 | |
---|
1026 | $query = ' |
---|
1027 | SELECT element_id |
---|
1028 | FROM '.CADDIE_TABLE.' |
---|
1029 | WHERE user_id = '.$user['id'].' |
---|
1030 | ;'; |
---|
1031 | $in_caddie = array_from_query($query, 'element_id'); |
---|
1032 | |
---|
1033 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
1034 | |
---|
1035 | $datas = array(); |
---|
1036 | |
---|
1037 | foreach ($caddiables as $caddiable) |
---|
1038 | { |
---|
1039 | array_push($datas, array('element_id' => $caddiable, |
---|
1040 | 'user_id' => $user['id'])); |
---|
1041 | } |
---|
1042 | |
---|
1043 | if (count($caddiables) > 0) |
---|
1044 | { |
---|
1045 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
1046 | } |
---|
1047 | } |
---|
1048 | |
---|
1049 | /** |
---|
1050 | * returns the element name from its filename |
---|
1051 | * |
---|
1052 | * @param string filename |
---|
1053 | * @return string name |
---|
1054 | */ |
---|
1055 | function get_name_from_file($filename) |
---|
1056 | { |
---|
1057 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
1058 | } |
---|
1059 | |
---|
1060 | /** |
---|
1061 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
1062 | * returned |
---|
1063 | * |
---|
1064 | * @param string key |
---|
1065 | * @return string |
---|
1066 | */ |
---|
1067 | function l10n($key) |
---|
1068 | { |
---|
1069 | global $lang, $conf; |
---|
1070 | |
---|
1071 | if ($conf['debug_l10n'] and !isset($lang[$key]) and !empty($key)) |
---|
1072 | { |
---|
1073 | trigger_error('[l10n] language key "'.$key.'" is not defined', E_USER_NOTICE); |
---|
1074 | } |
---|
1075 | |
---|
1076 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
1077 | } |
---|
1078 | |
---|
1079 | /** |
---|
1080 | * returns the prinft value for strings including %d |
---|
1081 | * return is concorded with decimal value (singular, plural) |
---|
1082 | * |
---|
1083 | * @param singular string key |
---|
1084 | * @param plural string key |
---|
1085 | * @param decimal value |
---|
1086 | * @return string |
---|
1087 | */ |
---|
1088 | function l10n_dec($singular_fmt_key, $plural_fmt_key, $decimal) |
---|
1089 | { |
---|
1090 | global $lang_info; |
---|
1091 | |
---|
1092 | return |
---|
1093 | sprintf( |
---|
1094 | l10n(( |
---|
1095 | (($decimal > 1) or ($decimal == 0 and $lang_info['zero_plural'])) |
---|
1096 | ? $plural_fmt_key |
---|
1097 | : $singular_fmt_key |
---|
1098 | )), $decimal); |
---|
1099 | } |
---|
1100 | /* |
---|
1101 | * returns a single element to use with l10n_args |
---|
1102 | * |
---|
1103 | * @param string key: translation key |
---|
1104 | * @param array/string/../number args: |
---|
1105 | * arguments to use on sprintf($key, args) |
---|
1106 | * if args is a array, each values are used on sprintf |
---|
1107 | * @return string |
---|
1108 | */ |
---|
1109 | function get_l10n_args($key, $args) |
---|
1110 | { |
---|
1111 | if (is_array($args)) |
---|
1112 | { |
---|
1113 | $key_arg = array_merge(array($key), $args); |
---|
1114 | } |
---|
1115 | else |
---|
1116 | { |
---|
1117 | $key_arg = array($key, $args); |
---|
1118 | } |
---|
1119 | return array('key_args' => $key_arg); |
---|
1120 | } |
---|
1121 | |
---|
1122 | /* |
---|
1123 | * returns a string with formated with l10n_args elements |
---|
1124 | * |
---|
1125 | * @param element/array $key_args: element or array of l10n_args elements |
---|
1126 | * @param $sep: if $key_args is array, |
---|
1127 | * separator is used when translated l10n_args elements are concated |
---|
1128 | * @return string |
---|
1129 | */ |
---|
1130 | function l10n_args($key_args, $sep = "\n") |
---|
1131 | { |
---|
1132 | if (is_array($key_args)) |
---|
1133 | { |
---|
1134 | foreach ($key_args as $key => $element) |
---|
1135 | { |
---|
1136 | if (isset($result)) |
---|
1137 | { |
---|
1138 | $result .= $sep; |
---|
1139 | } |
---|
1140 | else |
---|
1141 | { |
---|
1142 | $result = ''; |
---|
1143 | } |
---|
1144 | |
---|
1145 | if ($key === 'key_args') |
---|
1146 | { |
---|
1147 | array_unshift($element, l10n(array_shift($element))); |
---|
1148 | $result .= call_user_func_array('sprintf', $element); |
---|
1149 | } |
---|
1150 | else |
---|
1151 | { |
---|
1152 | $result .= l10n_args($element, $sep); |
---|
1153 | } |
---|
1154 | } |
---|
1155 | } |
---|
1156 | else |
---|
1157 | { |
---|
1158 | die('l10n_args: Invalid arguments'); |
---|
1159 | } |
---|
1160 | |
---|
1161 | return $result; |
---|
1162 | } |
---|
1163 | |
---|
1164 | /** |
---|
1165 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
1166 | * key is returned |
---|
1167 | * |
---|
1168 | * @param string key |
---|
1169 | * @return string |
---|
1170 | */ |
---|
1171 | function get_themeconf($key) |
---|
1172 | { |
---|
1173 | global $template; |
---|
1174 | |
---|
1175 | return $template->get_themeconf($key); |
---|
1176 | } |
---|
1177 | |
---|
1178 | /** |
---|
1179 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
1180 | * |
---|
1181 | * @return string |
---|
1182 | */ |
---|
1183 | function get_webmaster_mail_address() |
---|
1184 | { |
---|
1185 | global $conf; |
---|
1186 | |
---|
1187 | $query = ' |
---|
1188 | SELECT '.$conf['user_fields']['email'].' |
---|
1189 | FROM '.USERS_TABLE.' |
---|
1190 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
1191 | ;'; |
---|
1192 | list($email) = mysql_fetch_array(pwg_query($query)); |
---|
1193 | |
---|
1194 | return $email; |
---|
1195 | } |
---|
1196 | |
---|
1197 | /** |
---|
1198 | * which upgrades are available ? |
---|
1199 | * |
---|
1200 | * @return array |
---|
1201 | */ |
---|
1202 | function get_available_upgrade_ids() |
---|
1203 | { |
---|
1204 | $upgrades_path = PHPWG_ROOT_PATH.'install/db'; |
---|
1205 | |
---|
1206 | $available_upgrade_ids = array(); |
---|
1207 | |
---|
1208 | if ($contents = opendir($upgrades_path)) |
---|
1209 | { |
---|
1210 | while (($node = readdir($contents)) !== false) |
---|
1211 | { |
---|
1212 | if (is_file($upgrades_path.'/'.$node) |
---|
1213 | and preg_match('/^(.*?)-database\.php$/', $node, $match)) |
---|
1214 | { |
---|
1215 | array_push($available_upgrade_ids, $match[1]); |
---|
1216 | } |
---|
1217 | } |
---|
1218 | } |
---|
1219 | natcasesort($available_upgrade_ids); |
---|
1220 | |
---|
1221 | return $available_upgrade_ids; |
---|
1222 | } |
---|
1223 | |
---|
1224 | /** |
---|
1225 | * Add configuration parameters from database to global $conf array |
---|
1226 | * |
---|
1227 | * @return void |
---|
1228 | */ |
---|
1229 | function load_conf_from_db($condition = '') |
---|
1230 | { |
---|
1231 | global $conf; |
---|
1232 | |
---|
1233 | $query = ' |
---|
1234 | SELECT param, value |
---|
1235 | FROM '.CONFIG_TABLE.' |
---|
1236 | '.(!empty($condition) ? 'WHERE '.$condition : '').' |
---|
1237 | ;'; |
---|
1238 | $result = pwg_query($query); |
---|
1239 | |
---|
1240 | if ((mysql_num_rows($result) == 0) and !empty($condition)) |
---|
1241 | { |
---|
1242 | die('No configuration data'); |
---|
1243 | } |
---|
1244 | |
---|
1245 | while ($row = mysql_fetch_array($result)) |
---|
1246 | { |
---|
1247 | $conf[ $row['param'] ] = isset($row['value']) ? $row['value'] : ''; |
---|
1248 | |
---|
1249 | // If the field is true or false, the variable is transformed into a |
---|
1250 | // boolean value. |
---|
1251 | if ($conf[$row['param']] == 'true' or $conf[$row['param']] == 'false') |
---|
1252 | { |
---|
1253 | $conf[ $row['param'] ] = get_boolean($conf[ $row['param'] ]); |
---|
1254 | } |
---|
1255 | } |
---|
1256 | } |
---|
1257 | |
---|
1258 | /** |
---|
1259 | * Prepends and appends a string at each value of the given array. |
---|
1260 | * |
---|
1261 | * @param array |
---|
1262 | * @param string prefix to each array values |
---|
1263 | * @param string suffix to each array values |
---|
1264 | */ |
---|
1265 | function prepend_append_array_items($array, $prepend_str, $append_str) |
---|
1266 | { |
---|
1267 | array_walk( |
---|
1268 | $array, |
---|
1269 | create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";') |
---|
1270 | ); |
---|
1271 | |
---|
1272 | return $array; |
---|
1273 | } |
---|
1274 | |
---|
1275 | /** |
---|
1276 | * creates an hashed based on a query, this function is a very common |
---|
1277 | * pattern used here. Among the selected columns fetched, choose one to be |
---|
1278 | * the key, another one to be the value. |
---|
1279 | * |
---|
1280 | * @param string $query |
---|
1281 | * @param string $keyname |
---|
1282 | * @param string $valuename |
---|
1283 | * @return array |
---|
1284 | */ |
---|
1285 | function simple_hash_from_query($query, $keyname, $valuename) |
---|
1286 | { |
---|
1287 | $array = array(); |
---|
1288 | |
---|
1289 | $result = pwg_query($query); |
---|
1290 | while ($row = mysql_fetch_array($result)) |
---|
1291 | { |
---|
1292 | $array[ $row[$keyname] ] = $row[$valuename]; |
---|
1293 | } |
---|
1294 | |
---|
1295 | return $array; |
---|
1296 | } |
---|
1297 | |
---|
1298 | /** |
---|
1299 | * creates an hashed based on a query, this function is a very common |
---|
1300 | * pattern used here. The key is given as parameter, the value is an associative |
---|
1301 | * array. |
---|
1302 | * |
---|
1303 | * @param string $query |
---|
1304 | * @param string $keyname |
---|
1305 | * @return array |
---|
1306 | */ |
---|
1307 | function hash_from_query($query, $keyname) |
---|
1308 | { |
---|
1309 | $array = array(); |
---|
1310 | $result = pwg_query($query); |
---|
1311 | while ($row = mysql_fetch_assoc($result)) |
---|
1312 | { |
---|
1313 | $array[ $row[$keyname] ] = $row; |
---|
1314 | } |
---|
1315 | return $array; |
---|
1316 | } |
---|
1317 | |
---|
1318 | /** |
---|
1319 | * Return basename of the current script |
---|
1320 | * Lower case convertion is applied on return value |
---|
1321 | * Return value is without file extention ".php" |
---|
1322 | * |
---|
1323 | * @param void |
---|
1324 | * |
---|
1325 | * @return script basename |
---|
1326 | */ |
---|
1327 | function script_basename() |
---|
1328 | { |
---|
1329 | global $conf; |
---|
1330 | |
---|
1331 | foreach (array('SCRIPT_NAME', 'SCRIPT_FILENAME', 'PHP_SELF') as $value) |
---|
1332 | { |
---|
1333 | $continue = !empty($_SERVER[$value]); |
---|
1334 | if ($continue) |
---|
1335 | { |
---|
1336 | $filename = strtolower($_SERVER[$value]); |
---|
1337 | |
---|
1338 | if ($conf['php_extension_in_urls']) |
---|
1339 | { |
---|
1340 | $continue = get_extension($filename) === 'php'; |
---|
1341 | } |
---|
1342 | |
---|
1343 | if ($continue) |
---|
1344 | { |
---|
1345 | $basename = basename($filename, '.php'); |
---|
1346 | $continue = !empty($basename); |
---|
1347 | } |
---|
1348 | |
---|
1349 | if ($continue) |
---|
1350 | { |
---|
1351 | return $basename; |
---|
1352 | } |
---|
1353 | } |
---|
1354 | } |
---|
1355 | |
---|
1356 | return ''; |
---|
1357 | } |
---|
1358 | |
---|
1359 | /** |
---|
1360 | * Return value for the current page define on $conf['filter_pages'] |
---|
1361 | * Îf value is not defined, default value are returned |
---|
1362 | * |
---|
1363 | * @param value name |
---|
1364 | * |
---|
1365 | * @return filter page value |
---|
1366 | */ |
---|
1367 | function get_filter_page_value($value_name) |
---|
1368 | { |
---|
1369 | global $conf; |
---|
1370 | |
---|
1371 | $page_name = script_basename(); |
---|
1372 | |
---|
1373 | if (isset($conf['filter_pages'][$page_name][$value_name])) |
---|
1374 | { |
---|
1375 | return $conf['filter_pages'][$page_name][$value_name]; |
---|
1376 | } |
---|
1377 | else if (isset($conf['filter_pages']['default'][$value_name])) |
---|
1378 | { |
---|
1379 | return $conf['filter_pages']['default'][$value_name]; |
---|
1380 | } |
---|
1381 | else |
---|
1382 | { |
---|
1383 | return null; |
---|
1384 | } |
---|
1385 | } |
---|
1386 | |
---|
1387 | /** |
---|
1388 | * returns the character set of data sent to browsers / received from forms |
---|
1389 | */ |
---|
1390 | function get_pwg_charset() |
---|
1391 | { |
---|
1392 | defined('PWG_CHARSET') or die('load_language PWG_CHARSET undefined'); |
---|
1393 | return PWG_CHARSET; |
---|
1394 | } |
---|
1395 | |
---|
1396 | /** |
---|
1397 | * includes a language file or returns the content of a language file |
---|
1398 | * availability of the file |
---|
1399 | * |
---|
1400 | * in descending order of preference: |
---|
1401 | * param language, user language, default language |
---|
1402 | * PhpWebGallery default language. |
---|
1403 | * |
---|
1404 | * @param string filename |
---|
1405 | * @param string dirname |
---|
1406 | * @param string language |
---|
1407 | * @param bool return_content - if true the file content is returned otherwise |
---|
1408 | * the file is evaluated as php |
---|
1409 | * @return boolean success status or a string if return_content is true |
---|
1410 | */ |
---|
1411 | function load_language($filename, $dirname = '', $language = '', |
---|
1412 | $return_content=false, $target_charset=null) |
---|
1413 | { |
---|
1414 | global $user; |
---|
1415 | |
---|
1416 | if (!$return_content) |
---|
1417 | { |
---|
1418 | $filename .= '.php'; //MAYBE to do .. load .po and .mo localization files |
---|
1419 | } |
---|
1420 | if (empty($dirname)) |
---|
1421 | { |
---|
1422 | $dirname = PHPWG_ROOT_PATH; |
---|
1423 | } |
---|
1424 | $dirname .= 'language/'; |
---|
1425 | |
---|
1426 | $languages = array(); |
---|
1427 | if ( !empty($language) ) |
---|
1428 | { |
---|
1429 | $languages[] = $language; |
---|
1430 | } |
---|
1431 | if ( !empty($user['language']) ) |
---|
1432 | { |
---|
1433 | $languages[] = $user['language']; |
---|
1434 | } |
---|
1435 | if ( defined('PHPWG_INSTALLED') ) |
---|
1436 | { |
---|
1437 | $languages[] = get_default_language(); |
---|
1438 | } |
---|
1439 | $languages[] = PHPWG_DEFAULT_LANGUAGE; |
---|
1440 | $languages = array_unique($languages); |
---|
1441 | |
---|
1442 | if ( empty($target_charset) ) |
---|
1443 | { |
---|
1444 | $target_charset = get_pwg_charset(); |
---|
1445 | } |
---|
1446 | $target_charset = strtolower($target_charset); |
---|
1447 | $source_charset = ''; |
---|
1448 | $source_file = ''; |
---|
1449 | foreach ($languages as $language) |
---|
1450 | { |
---|
1451 | $dir = $dirname.$language; |
---|
1452 | |
---|
1453 | // exact charset match - no conversion required |
---|
1454 | $f = $dir.'.'.$target_charset.'/'.$filename; |
---|
1455 | if (file_exists($f)) |
---|
1456 | { |
---|
1457 | $source_file = $f; |
---|
1458 | break; |
---|
1459 | } |
---|
1460 | |
---|
1461 | // UTF-8 ? |
---|
1462 | $f = $dir.'/'.$filename; |
---|
1463 | if (file_exists($f)) |
---|
1464 | { |
---|
1465 | $source_charset = 'utf-8'; |
---|
1466 | $source_file = $f; |
---|
1467 | break; |
---|
1468 | } |
---|
1469 | |
---|
1470 | if ($target_charset=='utf-8') |
---|
1471 | { // we accept conversion from ISO-8859-1 to UTF-8 |
---|
1472 | $f = $dir.'.iso-8859-1/'.$filename; |
---|
1473 | if (file_exists($f)) |
---|
1474 | { |
---|
1475 | $source_charset = 'iso-8859-1'; |
---|
1476 | $source_file = $f; |
---|
1477 | break; |
---|
1478 | } |
---|
1479 | } |
---|
1480 | } |
---|
1481 | |
---|
1482 | if ( !empty($source_file) ) |
---|
1483 | { |
---|
1484 | if (!$return_content) |
---|
1485 | { |
---|
1486 | @include($source_file); |
---|
1487 | $load_lang = @$lang; |
---|
1488 | $load_lang_info = @$lang_info; |
---|
1489 | |
---|
1490 | global $lang, $lang_info; |
---|
1491 | if ( !isset($lang) ) $lang=array(); |
---|
1492 | if ( !isset($lang_info) ) $lang_info=array(); |
---|
1493 | |
---|
1494 | if ( !empty($source_charset) and $source_charset!=$target_charset) |
---|
1495 | { |
---|
1496 | if ( is_array($load_lang) ) |
---|
1497 | { |
---|
1498 | foreach ($load_lang as $k => $v) |
---|
1499 | { |
---|
1500 | if ( is_array($v) ) |
---|
1501 | { |
---|
1502 | $func = create_function('$v', 'return convert_charset($v, "'.$source_charset.'","'.$target_charset.'");' ); |
---|
1503 | $lang[$k] = array_map($func, $v); |
---|
1504 | } |
---|
1505 | else |
---|
1506 | $lang[$k] = convert_charset($v, $source_charset, $target_charset); |
---|
1507 | } |
---|
1508 | } |
---|
1509 | if ( is_array($load_lang_info) ) |
---|
1510 | { |
---|
1511 | foreach ($load_lang_info as $k => $v) |
---|
1512 | { |
---|
1513 | $lang_info[$k] = convert_charset($v, $source_charset, $target_charset); |
---|
1514 | } |
---|
1515 | } |
---|
1516 | } |
---|
1517 | else |
---|
1518 | { |
---|
1519 | $lang = array_merge( $lang, (array)$load_lang ); |
---|
1520 | $lang_info = array_merge( $lang_info, (array)$load_lang_info ); |
---|
1521 | } |
---|
1522 | return true; |
---|
1523 | } |
---|
1524 | else |
---|
1525 | { |
---|
1526 | $content = @file_get_contents($source_file); |
---|
1527 | if ( !empty($source_charset) and $source_charset!=$target_charset) |
---|
1528 | { |
---|
1529 | $content = convert_charset($content, $source_charset, $target_charset); |
---|
1530 | } |
---|
1531 | return $content; |
---|
1532 | } |
---|
1533 | } |
---|
1534 | return false; |
---|
1535 | } |
---|
1536 | |
---|
1537 | /** |
---|
1538 | * converts a string from a character set to another character set |
---|
1539 | * @param string str the string to be converted |
---|
1540 | * @param string source_charset the character set in which the string is encoded |
---|
1541 | * @param string dest_charset the destination character set |
---|
1542 | */ |
---|
1543 | function convert_charset($str, $source_charset, $dest_charset) |
---|
1544 | { |
---|
1545 | if ($source_charset==$dest_charset) |
---|
1546 | return $str; |
---|
1547 | if ($source_charset=='iso-8859-1' and $dest_charset=='utf-8') |
---|
1548 | { |
---|
1549 | return utf8_encode($str); |
---|
1550 | } |
---|
1551 | if ($source_charset=='utf-8' and $dest_charset=='iso-8859-1') |
---|
1552 | { |
---|
1553 | return utf8_decode($str); |
---|
1554 | } |
---|
1555 | if (function_exists('iconv')) |
---|
1556 | { |
---|
1557 | return iconv($source_charset, $dest_charset, $str); |
---|
1558 | } |
---|
1559 | if (function_exists('mb_convert_encoding')) |
---|
1560 | { |
---|
1561 | return mb_convert_encoding( $str, $dest_charset, $source_charset ); |
---|
1562 | } |
---|
1563 | return $str; //??? |
---|
1564 | } |
---|
1565 | ?> |
---|