1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $Id: functions.inc.php 1722 2007-01-15 00:09:14Z rub $ |
---|
9 | // | last update : $Date: 2007-01-15 00:09:14 +0000 (Mon, 15 Jan 2007) $ |
---|
10 | // | last modifier : $Author: rub $ |
---|
11 | // | revision : $Revision: 1722 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' ); |
---|
29 | include_once( PHPWG_ROOT_PATH .'include/functions_session.inc.php' ); |
---|
30 | include_once( PHPWG_ROOT_PATH .'include/functions_category.inc.php' ); |
---|
31 | include_once( PHPWG_ROOT_PATH .'include/functions_xml.inc.php' ); |
---|
32 | include_once( PHPWG_ROOT_PATH .'include/functions_group.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/functions_plugins.inc.php' ); |
---|
37 | include_once( PHPWG_ROOT_PATH .'include/functions_webserv.inc.php' ); |
---|
38 | |
---|
39 | //----------------------------------------------------------- generic functions |
---|
40 | |
---|
41 | /** |
---|
42 | * returns an array containing the possible values of an enum field |
---|
43 | * |
---|
44 | * @param string tablename |
---|
45 | * @param string fieldname |
---|
46 | */ |
---|
47 | function get_enums($table, $field) |
---|
48 | { |
---|
49 | // retrieving the properties of the table. Each line represents a field : |
---|
50 | // columns are 'Field', 'Type' |
---|
51 | $result = pwg_query('desc '.$table); |
---|
52 | while ($row = mysql_fetch_array($result)) |
---|
53 | { |
---|
54 | // we are only interested in the the field given in parameter for the |
---|
55 | // function |
---|
56 | if ($row['Field'] == $field) |
---|
57 | { |
---|
58 | // retrieving possible values of the enum field |
---|
59 | // enum('blue','green','black') |
---|
60 | $options = explode(',', substr($row['Type'], 5, -1)); |
---|
61 | foreach ($options as $i => $option) |
---|
62 | { |
---|
63 | $options[$i] = str_replace("'", '',$option); |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | mysql_free_result($result); |
---|
68 | return $options; |
---|
69 | } |
---|
70 | |
---|
71 | // get_boolean transforms a string to a boolean value. If the string is |
---|
72 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
73 | // any other case, true is returned. |
---|
74 | function get_boolean( $string ) |
---|
75 | { |
---|
76 | $boolean = true; |
---|
77 | if ( preg_match( '/^false$/i', $string ) ) |
---|
78 | { |
---|
79 | $boolean = false; |
---|
80 | } |
---|
81 | return $boolean; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
86 | * |
---|
87 | * @param mixed $var |
---|
88 | * @return mixed |
---|
89 | */ |
---|
90 | function boolean_to_string($var) |
---|
91 | { |
---|
92 | if (is_bool($var)) |
---|
93 | { |
---|
94 | if ($var) |
---|
95 | { |
---|
96 | return 'true'; |
---|
97 | } |
---|
98 | else |
---|
99 | { |
---|
100 | return 'false'; |
---|
101 | } |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | return $var; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | // The function get_moment returns a float value coresponding to the number |
---|
110 | // of seconds since the unix epoch (1st January 1970) and the microseconds |
---|
111 | // are precised : e.g. 1052343429.89276600 |
---|
112 | function get_moment() |
---|
113 | { |
---|
114 | $t1 = explode( ' ', microtime() ); |
---|
115 | $t2 = explode( '.', $t1[0] ); |
---|
116 | $t2 = $t1[1].'.'.$t2[1]; |
---|
117 | return $t2; |
---|
118 | } |
---|
119 | |
---|
120 | // The function get_elapsed_time returns the number of seconds (with 3 |
---|
121 | // decimals precision) between the start time and the end time given. |
---|
122 | function get_elapsed_time( $start, $end ) |
---|
123 | { |
---|
124 | return number_format( $end - $start, 3, '.', ' ').' s'; |
---|
125 | } |
---|
126 | |
---|
127 | // - The replace_space function replaces space and '-' characters |
---|
128 | // by their HTML equivalent &nbsb; and − |
---|
129 | // - The function does not replace characters in HTML tags |
---|
130 | // - This function was created because IE5 does not respect the |
---|
131 | // CSS "white-space: nowrap;" property unless space and minus |
---|
132 | // characters are replaced like this function does. |
---|
133 | // - Example : |
---|
134 | // <div class="foo">My friend</div> |
---|
135 | // ( 01234567891111111111222222222233 ) |
---|
136 | // ( 0123456789012345678901 ) |
---|
137 | // becomes : |
---|
138 | // <div class="foo">My friend</div> |
---|
139 | function replace_space( $string ) |
---|
140 | { |
---|
141 | //return $string; |
---|
142 | $return_string = ''; |
---|
143 | // $remaining is the rest of the string where to replace spaces characters |
---|
144 | $remaining = $string; |
---|
145 | // $start represents the position of the next '<' character |
---|
146 | // $end represents the position of the next '>' character |
---|
147 | $start = 0; |
---|
148 | $end = 0; |
---|
149 | $start = strpos ( $remaining, '<' ); // -> 0 |
---|
150 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
151 | // as long as a '<' and his friend '>' are found, we loop |
---|
152 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
153 | { |
---|
154 | // $treatment is the part of the string to treat |
---|
155 | // In the first loop of our example, this variable is empty, but in the |
---|
156 | // second loop, it equals 'My friend' |
---|
157 | $treatment = substr ( $remaining, 0, $start ); |
---|
158 | // Replacement of ' ' by his equivalent ' ' |
---|
159 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
160 | $treatment = str_replace( '-', '−', $treatment ); |
---|
161 | // composing the string to return by adding the treated string and the |
---|
162 | // following HTML tag -> 'My friend</div>' |
---|
163 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
164 | // the remaining string is deplaced to the part after the '>' of this |
---|
165 | // loop |
---|
166 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
167 | $start = strpos ( $remaining, '<' ); |
---|
168 | $end = strpos ( $remaining, '>' ); |
---|
169 | } |
---|
170 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
171 | $treatment = str_replace( '-', '−', $treatment ); |
---|
172 | $return_string.= $treatment; |
---|
173 | |
---|
174 | return $return_string; |
---|
175 | } |
---|
176 | |
---|
177 | // get_extension returns the part of the string after the last "." |
---|
178 | function get_extension( $filename ) |
---|
179 | { |
---|
180 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
181 | } |
---|
182 | |
---|
183 | // get_filename_wo_extension returns the part of the string before the last |
---|
184 | // ".". |
---|
185 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
186 | function get_filename_wo_extension( $filename ) |
---|
187 | { |
---|
188 | $pos = strrpos( $filename, '.' ); |
---|
189 | return ($pos===false) ? $filename : substr( $filename, 0, $pos); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * returns an array contening sub-directories, excluding "CVS" |
---|
194 | * |
---|
195 | * @param string $dir |
---|
196 | * @return array |
---|
197 | */ |
---|
198 | function get_dirs($directory) |
---|
199 | { |
---|
200 | $sub_dirs = array(); |
---|
201 | |
---|
202 | if ($opendir = opendir($directory)) |
---|
203 | { |
---|
204 | while ($file = readdir($opendir)) |
---|
205 | { |
---|
206 | if ($file != '.' |
---|
207 | and $file != '..' |
---|
208 | and is_dir($directory.'/'.$file) |
---|
209 | and $file != 'CVS' |
---|
210 | and $file != '.svn') |
---|
211 | { |
---|
212 | array_push($sub_dirs, $file); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | return $sub_dirs; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * returns thumbnail directory name of input diretoty name |
---|
221 | * make thumbnail directory is necessary |
---|
222 | * set error messages on array messages |
---|
223 | * |
---|
224 | * @param: |
---|
225 | * string $dirname |
---|
226 | * arrayy $errors |
---|
227 | * @return bool false on error else string directory name |
---|
228 | */ |
---|
229 | function mkget_thumbnail_dir($dirname, &$errors) |
---|
230 | { |
---|
231 | $tndir = $dirname.'/thumbnail'; |
---|
232 | if (!is_dir($tndir)) |
---|
233 | { |
---|
234 | if (!is_writable($dirname)) |
---|
235 | { |
---|
236 | array_push($errors, |
---|
237 | '['.$dirname.'] : '.l10n('no_write_access')); |
---|
238 | return false; |
---|
239 | } |
---|
240 | umask(0000); |
---|
241 | mkdir($tndir, 0777); |
---|
242 | } |
---|
243 | |
---|
244 | return $tndir; |
---|
245 | } |
---|
246 | |
---|
247 | // The get_picture_size function return an array containing : |
---|
248 | // - $picture_size[0] : final width |
---|
249 | // - $picture_size[1] : final height |
---|
250 | // The final dimensions are calculated thanks to the original dimensions and |
---|
251 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
252 | // the width/height ratio |
---|
253 | function get_picture_size( $original_width, $original_height, |
---|
254 | $max_width, $max_height ) |
---|
255 | { |
---|
256 | $width = $original_width; |
---|
257 | $height = $original_height; |
---|
258 | $is_original_size = true; |
---|
259 | |
---|
260 | if ( $max_width != "" ) |
---|
261 | { |
---|
262 | if ( $original_width > $max_width ) |
---|
263 | { |
---|
264 | $width = $max_width; |
---|
265 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
266 | } |
---|
267 | } |
---|
268 | if ( $max_height != "" ) |
---|
269 | { |
---|
270 | if ( $original_height > $max_height ) |
---|
271 | { |
---|
272 | $height = $max_height; |
---|
273 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
274 | $is_original_size = false; |
---|
275 | } |
---|
276 | } |
---|
277 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
278 | and $max_width != 0 and $max_height != 0 ) |
---|
279 | { |
---|
280 | $ratioWidth = $original_width / $max_width; |
---|
281 | $ratioHeight = $original_height / $max_height; |
---|
282 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
283 | { |
---|
284 | if ( $ratioWidth < $ratioHeight ) |
---|
285 | { |
---|
286 | $width = floor( $original_width / $ratioHeight ); |
---|
287 | $height = $max_height; |
---|
288 | } |
---|
289 | else |
---|
290 | { |
---|
291 | $width = $max_width; |
---|
292 | $height = floor( $original_height / $ratioWidth ); |
---|
293 | } |
---|
294 | $is_original_size = false; |
---|
295 | } |
---|
296 | } |
---|
297 | $picture_size = array(); |
---|
298 | $picture_size[0] = $width; |
---|
299 | $picture_size[1] = $height; |
---|
300 | return $picture_size; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * simplify a string to insert it into an URL |
---|
305 | * |
---|
306 | * based on str2url function from Dotclear |
---|
307 | * |
---|
308 | * @param string |
---|
309 | * @return string |
---|
310 | */ |
---|
311 | function str2url($str) |
---|
312 | { |
---|
313 | $str = strtr( |
---|
314 | $str, |
---|
315 | 'ÀÁÂÃÄÅàáâãäåÇçÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü¾ÝÿýÑñ', |
---|
316 | 'AAAAAAaaaaaaCcOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuYYyyNn' |
---|
317 | ); |
---|
318 | |
---|
319 | $str = str_replace('Æ', 'AE', $str); |
---|
320 | $str = str_replace('æ', 'ae', $str); |
---|
321 | $str = str_replace('¼', 'OE', $str); |
---|
322 | $str = str_replace('½', 'oe', $str); |
---|
323 | |
---|
324 | $str = preg_replace('/[^a-z0-9_\s\'\:\/\[\],-]/','',strtolower($str)); |
---|
325 | $str = preg_replace('/[\s\'\:\/\[\],-]+/',' ',trim($str)); |
---|
326 | $res = str_replace(' ','_',$str); |
---|
327 | |
---|
328 | return $res; |
---|
329 | } |
---|
330 | |
---|
331 | //-------------------------------------------- PhpWebGallery specific functions |
---|
332 | |
---|
333 | /** |
---|
334 | * returns an array with a list of {language_code => language_name} |
---|
335 | * |
---|
336 | * @returns array |
---|
337 | */ |
---|
338 | function get_languages() |
---|
339 | { |
---|
340 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
341 | $languages = array(); |
---|
342 | |
---|
343 | while ($file = readdir($dir)) |
---|
344 | { |
---|
345 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
346 | if (is_dir($path) and !is_link($path) and file_exists($path.'/iso.txt')) |
---|
347 | { |
---|
348 | list($language_name) = @file($path.'/iso.txt'); |
---|
349 | $languages[$file] = $language_name; |
---|
350 | } |
---|
351 | } |
---|
352 | closedir($dir); |
---|
353 | @asort($languages); |
---|
354 | @reset($languages); |
---|
355 | |
---|
356 | return $languages; |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * replaces the $search into <span style="$style">$search</span> in the |
---|
361 | * given $string. |
---|
362 | * |
---|
363 | * case insensitive replacements, does not replace characters in HTML tags |
---|
364 | * |
---|
365 | * @param string $string |
---|
366 | * @param string $search |
---|
367 | * @param string $style |
---|
368 | * @return string |
---|
369 | */ |
---|
370 | function add_style( $string, $search, $style ) |
---|
371 | { |
---|
372 | //return $string; |
---|
373 | $return_string = ''; |
---|
374 | $remaining = $string; |
---|
375 | |
---|
376 | $start = 0; |
---|
377 | $end = 0; |
---|
378 | $start = strpos ( $remaining, '<' ); |
---|
379 | $end = strpos ( $remaining, '>' ); |
---|
380 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
381 | { |
---|
382 | $treatment = substr ( $remaining, 0, $start ); |
---|
383 | $treatment = preg_replace( '/('.$search.')/i', |
---|
384 | '<span style="'.$style.'">\\0</span>', |
---|
385 | $treatment ); |
---|
386 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
387 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
388 | $start = strpos ( $remaining, '<' ); |
---|
389 | $end = strpos ( $remaining, '>' ); |
---|
390 | } |
---|
391 | $treatment = preg_replace( '/('.$search.')/i', |
---|
392 | '<span style="'.$style.'">\\0</span>', |
---|
393 | $remaining ); |
---|
394 | $return_string.= $treatment; |
---|
395 | |
---|
396 | return $return_string; |
---|
397 | } |
---|
398 | |
---|
399 | // replace_search replaces a searched words array string by the search in |
---|
400 | // another style for the given $string. |
---|
401 | function replace_search( $string, $search ) |
---|
402 | { |
---|
403 | // FIXME : with new advanced search, this function needs a rewrite |
---|
404 | return $string; |
---|
405 | |
---|
406 | $words = explode( ',', $search ); |
---|
407 | $style = 'background-color:white;color:red;'; |
---|
408 | foreach ( $words as $word ) { |
---|
409 | $string = add_style( $string, $word, $style ); |
---|
410 | } |
---|
411 | return $string; |
---|
412 | } |
---|
413 | |
---|
414 | function pwg_log( $file, $category, $picture = '' ) |
---|
415 | { |
---|
416 | global $conf, $user; |
---|
417 | |
---|
418 | if ( is_admin() ) |
---|
419 | { |
---|
420 | $doit=$conf['history_admin']; |
---|
421 | } |
---|
422 | elseif ( $user['is_the_guest'] ) |
---|
423 | { |
---|
424 | $doit=$conf['history_guest']; |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | $doit = $conf['log']; |
---|
429 | } |
---|
430 | |
---|
431 | if ($doit) |
---|
432 | { |
---|
433 | $login = ($user['id'] == $conf['guest_id']) |
---|
434 | ? 'guest' : addslashes($user['username']); |
---|
435 | insert_into_history($login, $file, $category, $picture); |
---|
436 | } |
---|
437 | } |
---|
438 | |
---|
439 | function pwg_log_login( $username ) |
---|
440 | { |
---|
441 | global $conf; |
---|
442 | if ( $conf['login_history'] ) |
---|
443 | { |
---|
444 | insert_into_history($username, 'login', '', ''); |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | // inserts a row in the history table |
---|
449 | function insert_into_history( $login, $file, $category, $picture) |
---|
450 | { |
---|
451 | $query = ' |
---|
452 | INSERT INTO '.HISTORY_TABLE.' |
---|
453 | (date,login,IP,file,category,picture) |
---|
454 | VALUES |
---|
455 | (NOW(), |
---|
456 | \''.$login.'\', |
---|
457 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
458 | \''.addslashes($file).'\', |
---|
459 | \''.addslashes(strip_tags($category)).'\', |
---|
460 | \''.addslashes($picture).'\') |
---|
461 | ;'; |
---|
462 | pwg_query($query); |
---|
463 | } |
---|
464 | |
---|
465 | // format_date returns a formatted date for display. The date given in |
---|
466 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
467 | // american format (2003-09-15). By option, you can show the time. The |
---|
468 | // output is internationalized. |
---|
469 | // |
---|
470 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
471 | function format_date($date, $type = 'us', $show_time = false) |
---|
472 | { |
---|
473 | global $lang; |
---|
474 | |
---|
475 | list($year,$month,$day,$hour,$minute,$second) = array(0,0,0,0,0,0); |
---|
476 | |
---|
477 | switch ( $type ) |
---|
478 | { |
---|
479 | case 'us' : |
---|
480 | { |
---|
481 | list($year,$month,$day) = explode('-', $date); |
---|
482 | break; |
---|
483 | } |
---|
484 | case 'unix' : |
---|
485 | { |
---|
486 | list($year,$month,$day,$hour,$minute) = |
---|
487 | explode('.', date('Y.n.j.G.i', $date)); |
---|
488 | break; |
---|
489 | } |
---|
490 | case 'mysql_datetime' : |
---|
491 | { |
---|
492 | preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
493 | $date, $out); |
---|
494 | list($year,$month,$day,$hour,$minute,$second) = |
---|
495 | array($out[1],$out[2],$out[3],$out[4],$out[5],$out[6]); |
---|
496 | break; |
---|
497 | } |
---|
498 | } |
---|
499 | $formated_date = ''; |
---|
500 | // before 1970, Microsoft Windows can't mktime |
---|
501 | if ($year >= 1970) |
---|
502 | { |
---|
503 | // we ask midday because Windows think it's prior to midnight with a |
---|
504 | // zero and refuse to work |
---|
505 | $formated_date.= $lang['day'][date('w', mktime(12,0,0,$month,$day,$year))]; |
---|
506 | } |
---|
507 | $formated_date.= ' '.$day; |
---|
508 | $formated_date.= ' '.$lang['month'][(int)$month]; |
---|
509 | $formated_date.= ' '.$year; |
---|
510 | if ($show_time) |
---|
511 | { |
---|
512 | $formated_date.= ' '.$hour.':'.$minute; |
---|
513 | } |
---|
514 | |
---|
515 | return $formated_date; |
---|
516 | } |
---|
517 | |
---|
518 | function pwg_query($query) |
---|
519 | { |
---|
520 | global $conf,$page,$debug,$t2; |
---|
521 | |
---|
522 | $start = get_moment(); |
---|
523 | $result = mysql_query($query) or my_error($query."\n"); |
---|
524 | |
---|
525 | $time = get_moment() - $start; |
---|
526 | |
---|
527 | if (!isset($page['count_queries'])) |
---|
528 | { |
---|
529 | $page['count_queries'] = 0; |
---|
530 | $page['queries_time'] = 0; |
---|
531 | } |
---|
532 | |
---|
533 | $page['count_queries']++; |
---|
534 | $page['queries_time']+= $time; |
---|
535 | |
---|
536 | if ($conf['show_queries']) |
---|
537 | { |
---|
538 | $output = ''; |
---|
539 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
540 | $output.= "\n".$query; |
---|
541 | $output.= "\n".'(this query time : '; |
---|
542 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
543 | $output.= "\n".'(total SQL time : '; |
---|
544 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
545 | $output.= "\n".'(total time : '; |
---|
546 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
547 | $output.= "</pre>\n"; |
---|
548 | |
---|
549 | $debug .= $output; |
---|
550 | } |
---|
551 | |
---|
552 | return $result; |
---|
553 | } |
---|
554 | |
---|
555 | function pwg_debug( $string ) |
---|
556 | { |
---|
557 | global $debug,$t2,$page; |
---|
558 | |
---|
559 | $now = explode( ' ', microtime() ); |
---|
560 | $now2 = explode( '.', $now[0] ); |
---|
561 | $now2 = $now[1].'.'.$now2[1]; |
---|
562 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
563 | $debug .= '<p>'; |
---|
564 | $debug.= '['.$time.', '; |
---|
565 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
566 | $debug.= "</p>\n"; |
---|
567 | } |
---|
568 | |
---|
569 | /** |
---|
570 | * Redirects to the given URL (HTTP method) |
---|
571 | * |
---|
572 | * Note : once this function called, the execution doesn't go further |
---|
573 | * (presence of an exit() instruction. |
---|
574 | * |
---|
575 | * @param string $url |
---|
576 | * @return void |
---|
577 | */ |
---|
578 | function redirect_http( $url ) |
---|
579 | { |
---|
580 | if (ob_get_length () !== FALSE) |
---|
581 | { |
---|
582 | ob_clean(); |
---|
583 | } |
---|
584 | header('Request-URI: '.$url); |
---|
585 | header('Content-Location: '.$url); |
---|
586 | header('Location: '.$url); |
---|
587 | exit(); |
---|
588 | } |
---|
589 | |
---|
590 | /** |
---|
591 | * Redirects to the given URL (HTML method) |
---|
592 | * |
---|
593 | * Note : once this function called, the execution doesn't go further |
---|
594 | * (presence of an exit() instruction. |
---|
595 | * |
---|
596 | * @param string $url |
---|
597 | * @param string $title_msg |
---|
598 | * @param integer $refreh_time |
---|
599 | * @return void |
---|
600 | */ |
---|
601 | function redirect_html( $url , $msg = '', $refresh_time = 0) |
---|
602 | { |
---|
603 | global $user, $template, $lang_info, $conf, $lang, $t2, $page, $debug; |
---|
604 | |
---|
605 | if (!isset($lang_info)) |
---|
606 | { |
---|
607 | $user = build_user( $conf['guest_id'], true); |
---|
608 | include_once(get_language_filepath('common.lang.php')); |
---|
609 | trigger_action('loading_lang'); |
---|
610 | @include_once(get_language_filepath('local.lang.php')); |
---|
611 | list($tmpl, $thm) = explode('/', $conf['default_template']); |
---|
612 | $template = new Template(PHPWG_ROOT_PATH.'template/'.$tmpl, $thm); |
---|
613 | } |
---|
614 | else |
---|
615 | { |
---|
616 | $template = new Template(PHPWG_ROOT_PATH.'template/'.$user['template'], $user['theme']); |
---|
617 | } |
---|
618 | |
---|
619 | if (empty($msg)) |
---|
620 | { |
---|
621 | $redirect_msg = l10n('redirect_msg'); |
---|
622 | } |
---|
623 | else |
---|
624 | { |
---|
625 | $redirect_msg = $msg; |
---|
626 | } |
---|
627 | $redirect_msg = nl2br($redirect_msg); |
---|
628 | |
---|
629 | $refresh = $refresh_time; |
---|
630 | $url_link = $url; |
---|
631 | $title = 'redirection'; |
---|
632 | |
---|
633 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
634 | |
---|
635 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
636 | |
---|
637 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
638 | $template->parse('redirect'); |
---|
639 | |
---|
640 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
641 | |
---|
642 | exit(); |
---|
643 | } |
---|
644 | |
---|
645 | /** |
---|
646 | * Redirects to the given URL (Switch to HTTP method or HTML method) |
---|
647 | * |
---|
648 | * Note : once this function called, the execution doesn't go further |
---|
649 | * (presence of an exit() instruction. |
---|
650 | * |
---|
651 | * @param string $url |
---|
652 | * @param string $title_msg |
---|
653 | * @param integer $refreh_time |
---|
654 | * @return void |
---|
655 | */ |
---|
656 | function redirect( $url , $msg = '', $refresh_time = 0) |
---|
657 | { |
---|
658 | global $conf; |
---|
659 | |
---|
660 | // with RefeshTime <> 0, only html must be used |
---|
661 | if (($conf['default_redirect_method'] == 'http') and ($refresh_time == 0)) |
---|
662 | { |
---|
663 | redirect_http($url); |
---|
664 | } |
---|
665 | else |
---|
666 | { |
---|
667 | redirect_html($url, $msg, $refresh_time); |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | /** |
---|
672 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
673 | * |
---|
674 | * @param array $rejects |
---|
675 | * @returns string |
---|
676 | */ |
---|
677 | function get_query_string_diff($rejects = array()) |
---|
678 | { |
---|
679 | $query_string = ''; |
---|
680 | |
---|
681 | $str = $_SERVER['QUERY_STRING']; |
---|
682 | parse_str($str, $vars); |
---|
683 | |
---|
684 | $is_first = true; |
---|
685 | foreach ($vars as $key => $value) |
---|
686 | { |
---|
687 | if (!in_array($key, $rejects)) |
---|
688 | { |
---|
689 | $query_string.= $is_first ? '?' : '&'; |
---|
690 | $is_first = false; |
---|
691 | $query_string.= $key.'='.$value; |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | return $query_string; |
---|
696 | } |
---|
697 | |
---|
698 | function url_is_remote($url) |
---|
699 | { |
---|
700 | if (preg_match('/^https?:\/\/[~\/\.\w-]+$/', $url)) |
---|
701 | { |
---|
702 | return true; |
---|
703 | } |
---|
704 | return false; |
---|
705 | } |
---|
706 | |
---|
707 | /** |
---|
708 | * returns available template/theme |
---|
709 | */ |
---|
710 | function get_pwg_themes() |
---|
711 | { |
---|
712 | $themes = array(); |
---|
713 | |
---|
714 | $template_dir = PHPWG_ROOT_PATH.'template'; |
---|
715 | |
---|
716 | foreach (get_dirs($template_dir) as $template) |
---|
717 | { |
---|
718 | foreach (get_dirs($template_dir.'/'.$template.'/theme') as $theme) |
---|
719 | { |
---|
720 | array_push($themes, $template.'/'.$theme); |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | return $themes; |
---|
725 | } |
---|
726 | |
---|
727 | /* Returns the PATH to the thumbnail to be displayed. If the element does not |
---|
728 | * have a thumbnail, the default mime image path is returned. The PATH can be |
---|
729 | * used in the php script, but not sent to the browser. |
---|
730 | * @param array element_info assoc array containing element info from db |
---|
731 | * at least 'path', 'tn_ext' and 'id' should be present |
---|
732 | */ |
---|
733 | function get_thumbnail_path($element_info) |
---|
734 | { |
---|
735 | $path = get_thumbnail_location($element_info); |
---|
736 | if ( !url_is_remote($path) ) |
---|
737 | { |
---|
738 | $path = PHPWG_ROOT_PATH.$path; |
---|
739 | } |
---|
740 | return $path; |
---|
741 | } |
---|
742 | |
---|
743 | /* Returns the URL of the thumbnail to be displayed. If the element does not |
---|
744 | * have a thumbnail, the default mime image url is returned. The URL can be |
---|
745 | * sent to the browser, but not used in the php script. |
---|
746 | * @param array element_info assoc array containing element info from db |
---|
747 | * at least 'path', 'tn_ext' and 'id' should be present |
---|
748 | */ |
---|
749 | function get_thumbnail_url($element_info) |
---|
750 | { |
---|
751 | $path = get_thumbnail_location($element_info); |
---|
752 | if ( !url_is_remote($path) ) |
---|
753 | { |
---|
754 | $path = get_root_url().$path; |
---|
755 | } |
---|
756 | // plugins want another url ? |
---|
757 | $path = trigger_event('get_thumbnail_url', $path, $element_info); |
---|
758 | return $path; |
---|
759 | } |
---|
760 | |
---|
761 | /* returns the relative path of the thumnail with regards to to the root |
---|
762 | of phpwebgallery (not the current page!).This function is not intended to be |
---|
763 | called directly from code.*/ |
---|
764 | function get_thumbnail_location($element_info) |
---|
765 | { |
---|
766 | global $conf; |
---|
767 | if ( !empty( $element_info['tn_ext'] ) ) |
---|
768 | { |
---|
769 | $path = substr_replace( |
---|
770 | get_filename_wo_extension($element_info['path']), |
---|
771 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
772 | strrpos($element_info['path'],'/'), |
---|
773 | 1 |
---|
774 | ); |
---|
775 | $path.= '.'.$element_info['tn_ext']; |
---|
776 | } |
---|
777 | else |
---|
778 | { |
---|
779 | $path = get_themeconf('mime_icon_dir') |
---|
780 | .strtolower(get_extension($element_info['path'])).'.png'; |
---|
781 | } |
---|
782 | |
---|
783 | // plugins want another location ? |
---|
784 | $path = trigger_event( 'get_thumbnail_location', $path, $element_info); |
---|
785 | return $path; |
---|
786 | } |
---|
787 | |
---|
788 | |
---|
789 | // my_error returns (or send to standard output) the message concerning the |
---|
790 | // error occured for the last mysql query. |
---|
791 | function my_error($header) |
---|
792 | { |
---|
793 | global $conf; |
---|
794 | |
---|
795 | $error = '<pre>'; |
---|
796 | $error.= $header; |
---|
797 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
798 | $error.= mysql_error(); |
---|
799 | $error.= '</pre>'; |
---|
800 | |
---|
801 | if ($conf['die_on_sql_error']) |
---|
802 | { |
---|
803 | die($error); |
---|
804 | } |
---|
805 | else |
---|
806 | { |
---|
807 | echo $error; |
---|
808 | } |
---|
809 | } |
---|
810 | |
---|
811 | /** |
---|
812 | * creates an array based on a query, this function is a very common pattern |
---|
813 | * used here |
---|
814 | * |
---|
815 | * @param string $query |
---|
816 | * @param string $fieldname |
---|
817 | * @return array |
---|
818 | */ |
---|
819 | function array_from_query($query, $fieldname) |
---|
820 | { |
---|
821 | $array = array(); |
---|
822 | |
---|
823 | $result = pwg_query($query); |
---|
824 | while ($row = mysql_fetch_array($result)) |
---|
825 | { |
---|
826 | array_push($array, $row[$fieldname]); |
---|
827 | } |
---|
828 | |
---|
829 | return $array; |
---|
830 | } |
---|
831 | |
---|
832 | /** |
---|
833 | * instantiate number list for days in a template block |
---|
834 | * |
---|
835 | * @param string blockname |
---|
836 | * @param string selection |
---|
837 | */ |
---|
838 | function get_day_list($blockname, $selection) |
---|
839 | { |
---|
840 | global $template; |
---|
841 | |
---|
842 | $template->assign_block_vars( |
---|
843 | $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); |
---|
844 | |
---|
845 | for ($i = 1; $i <= 31; $i++) |
---|
846 | { |
---|
847 | $selected = ''; |
---|
848 | if ($i == (int)$selection) |
---|
849 | { |
---|
850 | $selected = 'selected="selected"'; |
---|
851 | } |
---|
852 | $template->assign_block_vars( |
---|
853 | $blockname, array('SELECTED' => $selected, |
---|
854 | 'VALUE' => $i, |
---|
855 | 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | /** |
---|
860 | * instantiate month list in a template block |
---|
861 | * |
---|
862 | * @param string blockname |
---|
863 | * @param string selection |
---|
864 | */ |
---|
865 | function get_month_list($blockname, $selection) |
---|
866 | { |
---|
867 | global $template, $lang; |
---|
868 | |
---|
869 | $template->assign_block_vars( |
---|
870 | $blockname, array('SELECTED' => '', |
---|
871 | 'VALUE' => 0, |
---|
872 | 'OPTION' => '------------')); |
---|
873 | |
---|
874 | for ($i = 1; $i <= 12; $i++) |
---|
875 | { |
---|
876 | $selected = ''; |
---|
877 | if ($i == (int)$selection) |
---|
878 | { |
---|
879 | $selected = 'selected="selected"'; |
---|
880 | } |
---|
881 | $template->assign_block_vars( |
---|
882 | $blockname, array('SELECTED' => $selected, |
---|
883 | 'VALUE' => $i, |
---|
884 | 'OPTION' => $lang['month'][$i])); |
---|
885 | } |
---|
886 | } |
---|
887 | |
---|
888 | /** |
---|
889 | * fill the current user caddie with given elements, if not already in |
---|
890 | * caddie |
---|
891 | * |
---|
892 | * @param array elements_id |
---|
893 | */ |
---|
894 | function fill_caddie($elements_id) |
---|
895 | { |
---|
896 | global $user; |
---|
897 | |
---|
898 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
899 | |
---|
900 | $query = ' |
---|
901 | SELECT element_id |
---|
902 | FROM '.CADDIE_TABLE.' |
---|
903 | WHERE user_id = '.$user['id'].' |
---|
904 | ;'; |
---|
905 | $in_caddie = array_from_query($query, 'element_id'); |
---|
906 | |
---|
907 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
908 | |
---|
909 | $datas = array(); |
---|
910 | |
---|
911 | foreach ($caddiables as $caddiable) |
---|
912 | { |
---|
913 | array_push($datas, array('element_id' => $caddiable, |
---|
914 | 'user_id' => $user['id'])); |
---|
915 | } |
---|
916 | |
---|
917 | if (count($caddiables) > 0) |
---|
918 | { |
---|
919 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
920 | } |
---|
921 | } |
---|
922 | |
---|
923 | /** |
---|
924 | * returns the element name from its filename |
---|
925 | * |
---|
926 | * @param string filename |
---|
927 | * @return string name |
---|
928 | */ |
---|
929 | function get_name_from_file($filename) |
---|
930 | { |
---|
931 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
932 | } |
---|
933 | |
---|
934 | /** |
---|
935 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
936 | * returned |
---|
937 | * |
---|
938 | * @param string key |
---|
939 | * @return string |
---|
940 | */ |
---|
941 | function l10n($key) |
---|
942 | { |
---|
943 | global $lang, $conf; |
---|
944 | |
---|
945 | if ($conf['debug_l10n'] and !isset($lang[$key])) |
---|
946 | { |
---|
947 | echo '[l10n] language key "'.$key.'" is not defined<br />'; |
---|
948 | } |
---|
949 | |
---|
950 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
951 | } |
---|
952 | |
---|
953 | /** |
---|
954 | * returns the prinft value for strings including %d |
---|
955 | * return is concorded with decimal value (singular, plural) |
---|
956 | * |
---|
957 | * @param singular string key |
---|
958 | * @param plural string key |
---|
959 | * @param decimal value |
---|
960 | * @return string |
---|
961 | */ |
---|
962 | function l10n_dec($singular_fmt_key, $plural_fmt_key, $decimal) |
---|
963 | { |
---|
964 | return sprintf(l10n(($decimal > 1 ? $plural_fmt_key : |
---|
965 | $singular_fmt_key)), $decimal); |
---|
966 | } |
---|
967 | |
---|
968 | /** |
---|
969 | * Translate string in string ascii7bits |
---|
970 | * It's possible to do that with iconv_substr |
---|
971 | * but this fonction is not avaible on all the providers. |
---|
972 | * |
---|
973 | * @param string str |
---|
974 | * @return string |
---|
975 | */ |
---|
976 | function str_translate_to_ascii7bits($str) |
---|
977 | { |
---|
978 | global $lang_table_translate_ascii7bits; |
---|
979 | |
---|
980 | $src_table = array_keys($lang_table_translate_ascii7bits); |
---|
981 | $dst_table = array_values($lang_table_translate_ascii7bits); |
---|
982 | |
---|
983 | return str_replace($src_table , $dst_table, $str); |
---|
984 | } |
---|
985 | |
---|
986 | /** |
---|
987 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
988 | * key is returned |
---|
989 | * |
---|
990 | * @param string key |
---|
991 | * @return string |
---|
992 | */ |
---|
993 | function get_themeconf($key) |
---|
994 | { |
---|
995 | global $template; |
---|
996 | |
---|
997 | return $template->get_themeconf($key); |
---|
998 | } |
---|
999 | |
---|
1000 | /** |
---|
1001 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
1002 | * |
---|
1003 | * @return string |
---|
1004 | */ |
---|
1005 | function get_webmaster_mail_address() |
---|
1006 | { |
---|
1007 | global $conf; |
---|
1008 | |
---|
1009 | $query = ' |
---|
1010 | SELECT '.$conf['user_fields']['email'].' |
---|
1011 | FROM '.USERS_TABLE.' |
---|
1012 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
1013 | ;'; |
---|
1014 | list($email) = mysql_fetch_array(pwg_query($query)); |
---|
1015 | |
---|
1016 | return $email; |
---|
1017 | } |
---|
1018 | |
---|
1019 | /** |
---|
1020 | * which upgrades are available ? |
---|
1021 | * |
---|
1022 | * @return array |
---|
1023 | */ |
---|
1024 | function get_available_upgrade_ids() |
---|
1025 | { |
---|
1026 | $upgrades_path = PHPWG_ROOT_PATH.'install/db'; |
---|
1027 | |
---|
1028 | $available_upgrade_ids = array(); |
---|
1029 | |
---|
1030 | if ($contents = opendir($upgrades_path)) |
---|
1031 | { |
---|
1032 | while (($node = readdir($contents)) !== false) |
---|
1033 | { |
---|
1034 | if (is_file($upgrades_path.'/'.$node) |
---|
1035 | and preg_match('/^(.*?)-database\.php$/', $node, $match)) |
---|
1036 | { |
---|
1037 | array_push($available_upgrade_ids, $match[1]); |
---|
1038 | } |
---|
1039 | } |
---|
1040 | } |
---|
1041 | natcasesort($available_upgrade_ids); |
---|
1042 | |
---|
1043 | return $available_upgrade_ids; |
---|
1044 | } |
---|
1045 | |
---|
1046 | /** |
---|
1047 | * Add configuration parameters from database to global $conf array |
---|
1048 | * |
---|
1049 | * @return void |
---|
1050 | */ |
---|
1051 | function load_conf_from_db() |
---|
1052 | { |
---|
1053 | global $conf; |
---|
1054 | |
---|
1055 | $query = ' |
---|
1056 | SELECT param,value |
---|
1057 | FROM '.CONFIG_TABLE.' |
---|
1058 | ;'; |
---|
1059 | $result = pwg_query($query); |
---|
1060 | |
---|
1061 | if (mysql_num_rows($result) == 0) |
---|
1062 | { |
---|
1063 | die('No configuration data'); |
---|
1064 | } |
---|
1065 | |
---|
1066 | while ($row = mysql_fetch_array($result)) |
---|
1067 | { |
---|
1068 | $conf[ $row['param'] ] = isset($row['value']) ? $row['value'] : ''; |
---|
1069 | |
---|
1070 | // If the field is true or false, the variable is transformed into a |
---|
1071 | // boolean value. |
---|
1072 | if ($conf[$row['param']] == 'true' or $conf[$row['param']] == 'false') |
---|
1073 | { |
---|
1074 | $conf[ $row['param'] ] = get_boolean($conf[ $row['param'] ]); |
---|
1075 | } |
---|
1076 | } |
---|
1077 | } |
---|
1078 | |
---|
1079 | /** |
---|
1080 | * Return basename of the current script |
---|
1081 | * Lower case convertion is applied on return value |
---|
1082 | * Return value is without file extention ".php" |
---|
1083 | * |
---|
1084 | * @param void |
---|
1085 | * |
---|
1086 | * @return script basename |
---|
1087 | */ |
---|
1088 | function script_basename() |
---|
1089 | { |
---|
1090 | if (!empty($_SERVER['SCRIPT_NAME'])) |
---|
1091 | { |
---|
1092 | $file_name = $_SERVER['SCRIPT_NAME']; |
---|
1093 | } |
---|
1094 | else if (!empty($_SERVER['SCRIPT_FILENAME'])) |
---|
1095 | { |
---|
1096 | $file_name = $_SERVER['SCRIPT_FILENAME']; |
---|
1097 | } |
---|
1098 | else |
---|
1099 | { |
---|
1100 | $file_name = ''; |
---|
1101 | } |
---|
1102 | |
---|
1103 | // $_SERVER return lower string following var and systems |
---|
1104 | return basename(strtolower($file_name), '.php'); |
---|
1105 | } |
---|
1106 | |
---|
1107 | /** |
---|
1108 | * Return value for the current page define on $conf['filter_pages'] |
---|
1109 | * Îf value is not defined, default value are returned |
---|
1110 | * |
---|
1111 | * @param value name |
---|
1112 | * |
---|
1113 | * @return filter page value |
---|
1114 | */ |
---|
1115 | function get_filter_page_value($value_name) |
---|
1116 | { |
---|
1117 | global $conf; |
---|
1118 | |
---|
1119 | $page_name = script_basename(); |
---|
1120 | |
---|
1121 | if (isset($conf['filter_pages'][$page_name][$value_name])) |
---|
1122 | { |
---|
1123 | return $conf['filter_pages'][$page_name][$value_name]; |
---|
1124 | } |
---|
1125 | else if (isset($conf['filter_pages']['default'][$value_name])) |
---|
1126 | { |
---|
1127 | return $conf['filter_pages']['default'][$value_name]; |
---|
1128 | } |
---|
1129 | else |
---|
1130 | { |
---|
1131 | return null; |
---|
1132 | } |
---|
1133 | } |
---|
1134 | |
---|
1135 | ?> |
---|