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 1160 2006-04-13 22:25:40Z rub $ |
---|
9 | // | last update : $Date: 2006-04-13 22:25:40 +0000 (Thu, 13 Apr 2006) $ |
---|
10 | // | last modifier : $Author: rub $ |
---|
11 | // | revision : $Revision: 1160 $ |
---|
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 | |
---|
37 | //----------------------------------------------------------- generic functions |
---|
38 | |
---|
39 | /** |
---|
40 | * returns an array containing the possible values of an enum field |
---|
41 | * |
---|
42 | * @param string tablename |
---|
43 | * @param string fieldname |
---|
44 | */ |
---|
45 | function get_enums($table, $field) |
---|
46 | { |
---|
47 | // retrieving the properties of the table. Each line represents a field : |
---|
48 | // columns are 'Field', 'Type' |
---|
49 | $result = pwg_query('desc '.$table); |
---|
50 | while ($row = mysql_fetch_array($result)) |
---|
51 | { |
---|
52 | // we are only interested in the the field given in parameter for the |
---|
53 | // function |
---|
54 | if ($row['Field'] == $field) |
---|
55 | { |
---|
56 | // retrieving possible values of the enum field |
---|
57 | // enum('blue','green','black') |
---|
58 | $options = explode(',', substr($row['Type'], 5, -1)); |
---|
59 | foreach ($options as $i => $option) |
---|
60 | { |
---|
61 | $options[$i] = str_replace("'", '',$option); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | mysql_free_result($result); |
---|
66 | return $options; |
---|
67 | } |
---|
68 | |
---|
69 | // get_boolean transforms a string to a boolean value. If the string is |
---|
70 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
71 | // any other case, true is returned. |
---|
72 | function get_boolean( $string ) |
---|
73 | { |
---|
74 | $boolean = true; |
---|
75 | if ( preg_match( '/^false$/i', $string ) ) |
---|
76 | { |
---|
77 | $boolean = false; |
---|
78 | } |
---|
79 | return $boolean; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
84 | * |
---|
85 | * @param mixed $var |
---|
86 | * @return mixed |
---|
87 | */ |
---|
88 | function boolean_to_string($var) |
---|
89 | { |
---|
90 | if (is_bool($var)) |
---|
91 | { |
---|
92 | if ($var) |
---|
93 | { |
---|
94 | return 'true'; |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | return 'false'; |
---|
99 | } |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | return $var; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | // The function get_moment returns a float value coresponding to the number |
---|
108 | // of seconds since the unix epoch (1st January 1970) and the microseconds |
---|
109 | // are precised : e.g. 1052343429.89276600 |
---|
110 | function get_moment() |
---|
111 | { |
---|
112 | $t1 = explode( ' ', microtime() ); |
---|
113 | $t2 = explode( '.', $t1[0] ); |
---|
114 | $t2 = $t1[1].'.'.$t2[1]; |
---|
115 | return $t2; |
---|
116 | } |
---|
117 | |
---|
118 | // The function get_elapsed_time returns the number of seconds (with 3 |
---|
119 | // decimals precision) between the start time and the end time given. |
---|
120 | function get_elapsed_time( $start, $end ) |
---|
121 | { |
---|
122 | return number_format( $end - $start, 3, '.', ' ').' s'; |
---|
123 | } |
---|
124 | |
---|
125 | // - The replace_space function replaces space and '-' characters |
---|
126 | // by their HTML equivalent &nbsb; and − |
---|
127 | // - The function does not replace characters in HTML tags |
---|
128 | // - This function was created because IE5 does not respect the |
---|
129 | // CSS "white-space: nowrap;" property unless space and minus |
---|
130 | // characters are replaced like this function does. |
---|
131 | // - Example : |
---|
132 | // <div class="foo">My friend</div> |
---|
133 | // ( 01234567891111111111222222222233 ) |
---|
134 | // ( 0123456789012345678901 ) |
---|
135 | // becomes : |
---|
136 | // <div class="foo">My friend</div> |
---|
137 | function replace_space( $string ) |
---|
138 | { |
---|
139 | //return $string; |
---|
140 | $return_string = ''; |
---|
141 | // $remaining is the rest of the string where to replace spaces characters |
---|
142 | $remaining = $string; |
---|
143 | // $start represents the position of the next '<' character |
---|
144 | // $end represents the position of the next '>' character |
---|
145 | $start = 0; |
---|
146 | $end = 0; |
---|
147 | $start = strpos ( $remaining, '<' ); // -> 0 |
---|
148 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
149 | // as long as a '<' and his friend '>' are found, we loop |
---|
150 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
151 | { |
---|
152 | // $treatment is the part of the string to treat |
---|
153 | // In the first loop of our example, this variable is empty, but in the |
---|
154 | // second loop, it equals 'My friend' |
---|
155 | $treatment = substr ( $remaining, 0, $start ); |
---|
156 | // Replacement of ' ' by his equivalent ' ' |
---|
157 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
158 | $treatment = str_replace( '-', '−', $treatment ); |
---|
159 | // composing the string to return by adding the treated string and the |
---|
160 | // following HTML tag -> 'My friend</div>' |
---|
161 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
162 | // the remaining string is deplaced to the part after the '>' of this |
---|
163 | // loop |
---|
164 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
165 | $start = strpos ( $remaining, '<' ); |
---|
166 | $end = strpos ( $remaining, '>' ); |
---|
167 | } |
---|
168 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
169 | $treatment = str_replace( '-', '−', $treatment ); |
---|
170 | $return_string.= $treatment; |
---|
171 | |
---|
172 | return $return_string; |
---|
173 | } |
---|
174 | |
---|
175 | // get_extension returns the part of the string after the last "." |
---|
176 | function get_extension( $filename ) |
---|
177 | { |
---|
178 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
179 | } |
---|
180 | |
---|
181 | // get_filename_wo_extension returns the part of the string before the last |
---|
182 | // ".". |
---|
183 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
184 | function get_filename_wo_extension( $filename ) |
---|
185 | { |
---|
186 | return substr( $filename, 0, strrpos( $filename, '.' ) ); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * returns an array contening sub-directories, excluding "CVS" |
---|
191 | * |
---|
192 | * @param string $dir |
---|
193 | * @return array |
---|
194 | */ |
---|
195 | function get_dirs($directory) |
---|
196 | { |
---|
197 | $sub_dirs = array(); |
---|
198 | |
---|
199 | if ($opendir = opendir($directory)) |
---|
200 | { |
---|
201 | while ($file = readdir($opendir)) |
---|
202 | { |
---|
203 | if ($file != '.' |
---|
204 | and $file != '..' |
---|
205 | and is_dir($directory.'/'.$file) |
---|
206 | and $file != 'CVS' |
---|
207 | and $file != '.svn') |
---|
208 | { |
---|
209 | array_push($sub_dirs, $file); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | return $sub_dirs; |
---|
214 | } |
---|
215 | |
---|
216 | // The get_picture_size function return an array containing : |
---|
217 | // - $picture_size[0] : final width |
---|
218 | // - $picture_size[1] : final height |
---|
219 | // The final dimensions are calculated thanks to the original dimensions and |
---|
220 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
221 | // the width/height ratio |
---|
222 | function get_picture_size( $original_width, $original_height, |
---|
223 | $max_width, $max_height ) |
---|
224 | { |
---|
225 | $width = $original_width; |
---|
226 | $height = $original_height; |
---|
227 | $is_original_size = true; |
---|
228 | |
---|
229 | if ( $max_width != "" ) |
---|
230 | { |
---|
231 | if ( $original_width > $max_width ) |
---|
232 | { |
---|
233 | $width = $max_width; |
---|
234 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
235 | } |
---|
236 | } |
---|
237 | if ( $max_height != "" ) |
---|
238 | { |
---|
239 | if ( $original_height > $max_height ) |
---|
240 | { |
---|
241 | $height = $max_height; |
---|
242 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
243 | $is_original_size = false; |
---|
244 | } |
---|
245 | } |
---|
246 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
247 | and $max_width != 0 and $max_height != 0 ) |
---|
248 | { |
---|
249 | $ratioWidth = $original_width / $max_width; |
---|
250 | $ratioHeight = $original_height / $max_height; |
---|
251 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
252 | { |
---|
253 | if ( $ratioWidth < $ratioHeight ) |
---|
254 | { |
---|
255 | $width = floor( $original_width / $ratioHeight ); |
---|
256 | $height = $max_height; |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | $width = $max_width; |
---|
261 | $height = floor( $original_height / $ratioWidth ); |
---|
262 | } |
---|
263 | $is_original_size = false; |
---|
264 | } |
---|
265 | } |
---|
266 | $picture_size = array(); |
---|
267 | $picture_size[0] = $width; |
---|
268 | $picture_size[1] = $height; |
---|
269 | return $picture_size; |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * simplify a string to insert it into an URL |
---|
274 | * |
---|
275 | * based on str2url function from Dotclear |
---|
276 | * |
---|
277 | * @param string |
---|
278 | * @return string |
---|
279 | */ |
---|
280 | function str2url($str) |
---|
281 | { |
---|
282 | $str = strtr( |
---|
283 | $str, |
---|
284 | 'ÀÁÂÃÄÅàáâãäåÇçÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü¾ÝÿýÑñ', |
---|
285 | 'AAAAAAaaaaaaCcOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuYYyyNn' |
---|
286 | ); |
---|
287 | |
---|
288 | $str = str_replace('Æ', 'AE', $str); |
---|
289 | $str = str_replace('æ', 'ae', $str); |
---|
290 | $str = str_replace('¼', 'OE', $str); |
---|
291 | $str = str_replace('½', 'oe', $str); |
---|
292 | |
---|
293 | $str = preg_replace('/[^a-z0-9_\s\'\:\/\[\],-]/','',strtolower($str)); |
---|
294 | $str = preg_replace('/[\s\'\:\/\[\],-]+/',' ',trim($str)); |
---|
295 | $res = str_replace(' ','_',$str); |
---|
296 | |
---|
297 | return $res; |
---|
298 | } |
---|
299 | |
---|
300 | //-------------------------------------------- PhpWebGallery specific functions |
---|
301 | |
---|
302 | /** |
---|
303 | * returns an array with a list of {language_code => language_name} |
---|
304 | * |
---|
305 | * @returns array |
---|
306 | */ |
---|
307 | function get_languages() |
---|
308 | { |
---|
309 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
310 | $languages = array(); |
---|
311 | |
---|
312 | while ($file = readdir($dir)) |
---|
313 | { |
---|
314 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
315 | if (is_dir($path) and !is_link($path) and file_exists($path.'/iso.txt')) |
---|
316 | { |
---|
317 | list($language_name) = @file($path.'/iso.txt'); |
---|
318 | $languages[$file] = $language_name; |
---|
319 | } |
---|
320 | } |
---|
321 | closedir($dir); |
---|
322 | @asort($languages); |
---|
323 | @reset($languages); |
---|
324 | |
---|
325 | return $languages; |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * replaces the $search into <span style="$style">$search</span> in the |
---|
330 | * given $string. |
---|
331 | * |
---|
332 | * case insensitive replacements, does not replace characters in HTML tags |
---|
333 | * |
---|
334 | * @param string $string |
---|
335 | * @param string $search |
---|
336 | * @param string $style |
---|
337 | * @return string |
---|
338 | */ |
---|
339 | function add_style( $string, $search, $style ) |
---|
340 | { |
---|
341 | //return $string; |
---|
342 | $return_string = ''; |
---|
343 | $remaining = $string; |
---|
344 | |
---|
345 | $start = 0; |
---|
346 | $end = 0; |
---|
347 | $start = strpos ( $remaining, '<' ); |
---|
348 | $end = strpos ( $remaining, '>' ); |
---|
349 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
350 | { |
---|
351 | $treatment = substr ( $remaining, 0, $start ); |
---|
352 | $treatment = preg_replace( '/('.$search.')/i', |
---|
353 | '<span style="'.$style.'">\\0</span>', |
---|
354 | $treatment ); |
---|
355 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
356 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
357 | $start = strpos ( $remaining, '<' ); |
---|
358 | $end = strpos ( $remaining, '>' ); |
---|
359 | } |
---|
360 | $treatment = preg_replace( '/('.$search.')/i', |
---|
361 | '<span style="'.$style.'">\\0</span>', |
---|
362 | $remaining ); |
---|
363 | $return_string.= $treatment; |
---|
364 | |
---|
365 | return $return_string; |
---|
366 | } |
---|
367 | |
---|
368 | // replace_search replaces a searched words array string by the search in |
---|
369 | // another style for the given $string. |
---|
370 | function replace_search( $string, $search ) |
---|
371 | { |
---|
372 | // FIXME : with new advanced search, this function needs a rewrite |
---|
373 | return $string; |
---|
374 | |
---|
375 | $words = explode( ',', $search ); |
---|
376 | $style = 'background-color:white;color:red;'; |
---|
377 | foreach ( $words as $word ) { |
---|
378 | $string = add_style( $string, $word, $style ); |
---|
379 | } |
---|
380 | return $string; |
---|
381 | } |
---|
382 | |
---|
383 | function pwg_log( $file, $category, $picture = '' ) |
---|
384 | { |
---|
385 | global $conf, $user; |
---|
386 | |
---|
387 | if ($conf['log']) |
---|
388 | { |
---|
389 | if ( ($conf['history_admin'] ) or ( (! $conf['history_admin']) and (!is_admin()) ) ) |
---|
390 | { |
---|
391 | $login = ($user['id'] == $conf['guest_id']) |
---|
392 | ? 'guest' : addslashes($user['username']); |
---|
393 | |
---|
394 | $query = ' |
---|
395 | INSERT INTO '.HISTORY_TABLE.' |
---|
396 | (date,login,IP,file,category,picture) |
---|
397 | VALUES |
---|
398 | (NOW(), |
---|
399 | \''.$login.'\', |
---|
400 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
401 | \''.addslashes($file).'\', |
---|
402 | \''.addslashes(strip_tags($category)).'\', |
---|
403 | \''.addslashes($picture).'\') |
---|
404 | ;'; |
---|
405 | pwg_query($query); |
---|
406 | } |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | // format_date returns a formatted date for display. The date given in |
---|
411 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
412 | // american format (2003-09-15). By option, you can show the time. The |
---|
413 | // output is internationalized. |
---|
414 | // |
---|
415 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
416 | function format_date($date, $type = 'us', $show_time = false) |
---|
417 | { |
---|
418 | global $lang; |
---|
419 | |
---|
420 | list($year,$month,$day,$hour,$minute,$second) = array(0,0,0,0,0,0); |
---|
421 | |
---|
422 | switch ( $type ) |
---|
423 | { |
---|
424 | case 'us' : |
---|
425 | { |
---|
426 | list($year,$month,$day) = explode('-', $date); |
---|
427 | break; |
---|
428 | } |
---|
429 | case 'unix' : |
---|
430 | { |
---|
431 | list($year,$month,$day,$hour,$minute) = |
---|
432 | explode('.', date('Y.n.j.G.i', $date)); |
---|
433 | break; |
---|
434 | } |
---|
435 | case 'mysql_datetime' : |
---|
436 | { |
---|
437 | preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
438 | $date, $out); |
---|
439 | list($year,$month,$day,$hour,$minute,$second) = |
---|
440 | array($out[1],$out[2],$out[3],$out[4],$out[5],$out[6]); |
---|
441 | break; |
---|
442 | } |
---|
443 | } |
---|
444 | $formated_date = ''; |
---|
445 | // before 1970, Microsoft Windows can't mktime |
---|
446 | if ($year >= 1970) |
---|
447 | { |
---|
448 | // we ask midday because Windows think it's prior to midnight with a |
---|
449 | // zero and refuse to work |
---|
450 | $formated_date.= $lang['day'][date('w', mktime(12,0,0,$month,$day,$year))]; |
---|
451 | } |
---|
452 | $formated_date.= ' '.$day; |
---|
453 | $formated_date.= ' '.$lang['month'][(int)$month]; |
---|
454 | $formated_date.= ' '.$year; |
---|
455 | if ($show_time) |
---|
456 | { |
---|
457 | $formated_date.= ' '.$hour.':'.$minute; |
---|
458 | } |
---|
459 | |
---|
460 | return $formated_date; |
---|
461 | } |
---|
462 | |
---|
463 | function pwg_query($query) |
---|
464 | { |
---|
465 | global $conf,$page,$debug,$t2; |
---|
466 | |
---|
467 | $start = get_moment(); |
---|
468 | $result = mysql_query($query) or my_error($query."\n"); |
---|
469 | |
---|
470 | $time = get_moment() - $start; |
---|
471 | |
---|
472 | if (!isset($page['count_queries'])) |
---|
473 | { |
---|
474 | $page['count_queries'] = 0; |
---|
475 | $page['queries_time'] = 0; |
---|
476 | } |
---|
477 | |
---|
478 | $page['count_queries']++; |
---|
479 | $page['queries_time']+= $time; |
---|
480 | |
---|
481 | if ($conf['show_queries']) |
---|
482 | { |
---|
483 | $output = ''; |
---|
484 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
485 | $output.= "\n".$query; |
---|
486 | $output.= "\n".'(this query time : '; |
---|
487 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
488 | $output.= "\n".'(total SQL time : '; |
---|
489 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
490 | $output.= "\n".'(total time : '; |
---|
491 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
492 | $output.= "</pre>\n"; |
---|
493 | |
---|
494 | $debug .= $output; |
---|
495 | } |
---|
496 | |
---|
497 | return $result; |
---|
498 | } |
---|
499 | |
---|
500 | function pwg_debug( $string ) |
---|
501 | { |
---|
502 | global $debug,$t2,$page; |
---|
503 | |
---|
504 | $now = explode( ' ', microtime() ); |
---|
505 | $now2 = explode( '.', $now[0] ); |
---|
506 | $now2 = $now[1].'.'.$now2[1]; |
---|
507 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
508 | $debug .= '<p>'; |
---|
509 | $debug.= '['.$time.', '; |
---|
510 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
511 | $debug.= "</p>\n"; |
---|
512 | } |
---|
513 | |
---|
514 | /** |
---|
515 | * Redirects to the given URL |
---|
516 | * |
---|
517 | * Note : once this function called, the execution doesn't go further |
---|
518 | * (presence of an exit() instruction. |
---|
519 | * |
---|
520 | * @param string $url |
---|
521 | * @param string $title_msg |
---|
522 | * @param integer $refreh_time |
---|
523 | * @return void |
---|
524 | */ |
---|
525 | function redirect( $url , $msg = '', $refreh_time = 0) |
---|
526 | { |
---|
527 | global $user, $template, $lang_info, $conf, $lang, $t2, $page, $debug; |
---|
528 | |
---|
529 | // $redirect_msg, $refresh, $url_link and $title are required for creating an automated |
---|
530 | // refresh page in header.tpl |
---|
531 | if (!isset($msg) or ($msg == '')) |
---|
532 | { |
---|
533 | $redirect_msg = l10n('redirect_msg'); |
---|
534 | } |
---|
535 | else |
---|
536 | { |
---|
537 | $redirect_msg = $msg; |
---|
538 | } |
---|
539 | $redirect_msg = nl2br($redirect_msg); |
---|
540 | $refresh = $refreh_time; |
---|
541 | $url_link = $url; |
---|
542 | $title = 'redirection'; |
---|
543 | |
---|
544 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
545 | |
---|
546 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
547 | $template->parse('redirect'); |
---|
548 | |
---|
549 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
550 | |
---|
551 | exit(); |
---|
552 | } |
---|
553 | |
---|
554 | /** |
---|
555 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
556 | * |
---|
557 | * @param array $rejects |
---|
558 | * @returns string |
---|
559 | */ |
---|
560 | function get_query_string_diff($rejects = array()) |
---|
561 | { |
---|
562 | $query_string = ''; |
---|
563 | |
---|
564 | $str = $_SERVER['QUERY_STRING']; |
---|
565 | parse_str($str, $vars); |
---|
566 | |
---|
567 | $is_first = true; |
---|
568 | foreach ($vars as $key => $value) |
---|
569 | { |
---|
570 | if (!in_array($key, $rejects)) |
---|
571 | { |
---|
572 | $query_string.= $is_first ? '?' : '&'; |
---|
573 | $is_first = false; |
---|
574 | $query_string.= $key.'='.$value; |
---|
575 | } |
---|
576 | } |
---|
577 | |
---|
578 | return $query_string; |
---|
579 | } |
---|
580 | |
---|
581 | function url_is_remote($url) |
---|
582 | { |
---|
583 | if (preg_match('/^https?:\/\/[~\/\.\w-]+$/', $url)) |
---|
584 | { |
---|
585 | return true; |
---|
586 | } |
---|
587 | return false; |
---|
588 | } |
---|
589 | |
---|
590 | /** |
---|
591 | * returns available template/theme |
---|
592 | */ |
---|
593 | function get_pwg_themes() |
---|
594 | { |
---|
595 | $themes = array(); |
---|
596 | |
---|
597 | $template_dir = PHPWG_ROOT_PATH.'template'; |
---|
598 | |
---|
599 | foreach (get_dirs($template_dir) as $template) |
---|
600 | { |
---|
601 | foreach (get_dirs($template_dir.'/'.$template.'/theme') as $theme) |
---|
602 | { |
---|
603 | array_push($themes, $template.'/'.$theme); |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | return $themes; |
---|
608 | } |
---|
609 | |
---|
610 | /** |
---|
611 | * returns thumbnail filepath (or distant URL if thumbnail is remote) for a |
---|
612 | * given element |
---|
613 | * |
---|
614 | * the returned string can represente the filepath of the thumbnail or the |
---|
615 | * filepath to the corresponding icon for non picture elements |
---|
616 | * |
---|
617 | * @param string path |
---|
618 | * @param string tn_ext |
---|
619 | * @param bool with_rewrite if true returned path can't be used from the script |
---|
620 | * @return string |
---|
621 | */ |
---|
622 | function get_thumbnail_src($path, $tn_ext = '', $with_rewrite = true) |
---|
623 | { |
---|
624 | global $conf, $user; |
---|
625 | |
---|
626 | if ($tn_ext != '') |
---|
627 | { |
---|
628 | $src = substr_replace( |
---|
629 | get_filename_wo_extension($path), |
---|
630 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
631 | strrpos($path,'/'), |
---|
632 | 1 |
---|
633 | ); |
---|
634 | $src.= '.'.$tn_ext; |
---|
635 | if ($with_rewrite==true and !url_is_remote($src) ) |
---|
636 | { |
---|
637 | $src = get_root_url().$src; |
---|
638 | } |
---|
639 | } |
---|
640 | else |
---|
641 | { |
---|
642 | $src = ($with_rewrite==true) ? get_root_url() : ''; |
---|
643 | $src .= get_themeconf('mime_icon_dir'); |
---|
644 | $src.= strtolower(get_extension($path)).'.png'; |
---|
645 | } |
---|
646 | |
---|
647 | return $src; |
---|
648 | } |
---|
649 | |
---|
650 | // my_error returns (or send to standard output) the message concerning the |
---|
651 | // error occured for the last mysql query. |
---|
652 | function my_error($header) |
---|
653 | { |
---|
654 | $error = '<pre>'; |
---|
655 | $error.= $header; |
---|
656 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
657 | $error.= mysql_error(); |
---|
658 | $error.= '</pre>'; |
---|
659 | die ($error); |
---|
660 | } |
---|
661 | |
---|
662 | /** |
---|
663 | * creates an array based on a query, this function is a very common pattern |
---|
664 | * used here |
---|
665 | * |
---|
666 | * @param string $query |
---|
667 | * @param string $fieldname |
---|
668 | * @return array |
---|
669 | */ |
---|
670 | function array_from_query($query, $fieldname) |
---|
671 | { |
---|
672 | $array = array(); |
---|
673 | |
---|
674 | $result = pwg_query($query); |
---|
675 | while ($row = mysql_fetch_array($result)) |
---|
676 | { |
---|
677 | array_push($array, $row[$fieldname]); |
---|
678 | } |
---|
679 | |
---|
680 | return $array; |
---|
681 | } |
---|
682 | |
---|
683 | /** |
---|
684 | * instantiate number list for days in a template block |
---|
685 | * |
---|
686 | * @param string blockname |
---|
687 | * @param string selection |
---|
688 | */ |
---|
689 | function get_day_list($blockname, $selection) |
---|
690 | { |
---|
691 | global $template; |
---|
692 | |
---|
693 | $template->assign_block_vars( |
---|
694 | $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); |
---|
695 | |
---|
696 | for ($i = 1; $i <= 31; $i++) |
---|
697 | { |
---|
698 | $selected = ''; |
---|
699 | if ($i == (int)$selection) |
---|
700 | { |
---|
701 | $selected = 'selected="selected"'; |
---|
702 | } |
---|
703 | $template->assign_block_vars( |
---|
704 | $blockname, array('SELECTED' => $selected, |
---|
705 | 'VALUE' => $i, |
---|
706 | 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | /** |
---|
711 | * instantiate month list in a template block |
---|
712 | * |
---|
713 | * @param string blockname |
---|
714 | * @param string selection |
---|
715 | */ |
---|
716 | function get_month_list($blockname, $selection) |
---|
717 | { |
---|
718 | global $template, $lang; |
---|
719 | |
---|
720 | $template->assign_block_vars( |
---|
721 | $blockname, array('SELECTED' => '', |
---|
722 | 'VALUE' => 0, |
---|
723 | 'OPTION' => '------------')); |
---|
724 | |
---|
725 | for ($i = 1; $i <= 12; $i++) |
---|
726 | { |
---|
727 | $selected = ''; |
---|
728 | if ($i == (int)$selection) |
---|
729 | { |
---|
730 | $selected = 'selected="selected"'; |
---|
731 | } |
---|
732 | $template->assign_block_vars( |
---|
733 | $blockname, array('SELECTED' => $selected, |
---|
734 | 'VALUE' => $i, |
---|
735 | 'OPTION' => $lang['month'][$i])); |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | /** |
---|
740 | * fill the current user caddie with given elements, if not already in |
---|
741 | * caddie |
---|
742 | * |
---|
743 | * @param array elements_id |
---|
744 | */ |
---|
745 | function fill_caddie($elements_id) |
---|
746 | { |
---|
747 | global $user; |
---|
748 | |
---|
749 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
750 | |
---|
751 | $query = ' |
---|
752 | SELECT element_id |
---|
753 | FROM '.CADDIE_TABLE.' |
---|
754 | WHERE user_id = '.$user['id'].' |
---|
755 | ;'; |
---|
756 | $in_caddie = array_from_query($query, 'element_id'); |
---|
757 | |
---|
758 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
759 | |
---|
760 | $datas = array(); |
---|
761 | |
---|
762 | foreach ($caddiables as $caddiable) |
---|
763 | { |
---|
764 | array_push($datas, array('element_id' => $caddiable, |
---|
765 | 'user_id' => $user['id'])); |
---|
766 | } |
---|
767 | |
---|
768 | if (count($caddiables) > 0) |
---|
769 | { |
---|
770 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | /** |
---|
775 | * returns the element name from its filename |
---|
776 | * |
---|
777 | * @param string filename |
---|
778 | * @return string name |
---|
779 | */ |
---|
780 | function get_name_from_file($filename) |
---|
781 | { |
---|
782 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
783 | } |
---|
784 | |
---|
785 | /** |
---|
786 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
787 | * returned |
---|
788 | * |
---|
789 | * @param string key |
---|
790 | * @return string |
---|
791 | */ |
---|
792 | function raw_l10n($key) |
---|
793 | { |
---|
794 | global $lang, $conf; |
---|
795 | |
---|
796 | if ($conf['debug_l10n'] and !isset($lang[$key])) |
---|
797 | { |
---|
798 | echo '[l10n] language key "'.$key.'" is not defined<br />'; |
---|
799 | } |
---|
800 | |
---|
801 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
802 | } |
---|
803 | /** |
---|
804 | * Like l10n but converts html entities |
---|
805 | * |
---|
806 | * @param string key |
---|
807 | * @return string |
---|
808 | */ |
---|
809 | function l10n($key) |
---|
810 | { |
---|
811 | return htmlentities(raw_l10n($key),ENT_QUOTES); |
---|
812 | } |
---|
813 | |
---|
814 | /** |
---|
815 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
816 | * key is returned |
---|
817 | * |
---|
818 | * @param string key |
---|
819 | * @return string |
---|
820 | */ |
---|
821 | function get_themeconf($key) |
---|
822 | { |
---|
823 | global $themeconf; |
---|
824 | |
---|
825 | return $themeconf[$key]; |
---|
826 | } |
---|
827 | |
---|
828 | /** |
---|
829 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
830 | * |
---|
831 | * @return string |
---|
832 | */ |
---|
833 | function get_webmaster_mail_address() |
---|
834 | { |
---|
835 | global $conf; |
---|
836 | |
---|
837 | $query = ' |
---|
838 | SELECT '.$conf['user_fields']['email'].' |
---|
839 | FROM '.USERS_TABLE.' |
---|
840 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
841 | ;'; |
---|
842 | list($email) = mysql_fetch_array(pwg_query($query)); |
---|
843 | |
---|
844 | return $email; |
---|
845 | } |
---|
846 | |
---|
847 | /** |
---|
848 | * which upgrades are available ? |
---|
849 | * |
---|
850 | * @return array |
---|
851 | */ |
---|
852 | function get_available_upgrade_ids() |
---|
853 | { |
---|
854 | $upgrades_path = PHPWG_ROOT_PATH.'install/db'; |
---|
855 | |
---|
856 | $available_upgrade_ids = array(); |
---|
857 | |
---|
858 | if ($contents = opendir($upgrades_path)) |
---|
859 | { |
---|
860 | while (($node = readdir($contents)) !== false) |
---|
861 | { |
---|
862 | if (is_file($upgrades_path.'/'.$node) |
---|
863 | and preg_match('/^(.*?)-database\.php$/', $node, $match)) |
---|
864 | { |
---|
865 | array_push($available_upgrade_ids, $match[1]); |
---|
866 | } |
---|
867 | } |
---|
868 | } |
---|
869 | natcasesort($available_upgrade_ids); |
---|
870 | |
---|
871 | return $available_upgrade_ids; |
---|
872 | } |
---|
873 | |
---|
874 | /** |
---|
875 | * Adaptation of _HTTPRequestToString (http://fr.php.net/urlencode) |
---|
876 | * |
---|
877 | * |
---|
878 | * @return array request to string |
---|
879 | */ |
---|
880 | function http_request_to_string($arr_request, $var_name, $separator='&') { |
---|
881 | $ret = ""; |
---|
882 | if (is_array($arr_request)) { |
---|
883 | foreach ($arr_request as $key => $value) { |
---|
884 | if (is_array($value)) { |
---|
885 | if ($var_name) { |
---|
886 | $ret .= http_request_to_string($value, "{$var_name}[{$key}]", $separator); |
---|
887 | } else { |
---|
888 | $ret .= http_request_to_string($value, "{$key}", $separator); |
---|
889 | } |
---|
890 | } else { |
---|
891 | if ($var_name) { |
---|
892 | $ret .= "{$var_name}[{$key}]=".urlencode($value)."&"; |
---|
893 | } else { |
---|
894 | $ret .= "{$key}=".urlencode($value)."&"; |
---|
895 | } |
---|
896 | } |
---|
897 | } |
---|
898 | } |
---|
899 | if (!$var_name) { |
---|
900 | $ret = substr($ret,0,-1); |
---|
901 | } |
---|
902 | return $ret; |
---|
903 | } |
---|
904 | |
---|
905 | /** |
---|
906 | * Post request HTTP on backgroung and redirec to selected url |
---|
907 | * |
---|
908 | * Note : once this function called, the execution doesn't go further |
---|
909 | * (presence of an exit() instruction. |
---|
910 | * |
---|
911 | * @param string $url_redirect |
---|
912 | * @param string $redirect_message |
---|
913 | * @param integer $redirect_refreh_time |
---|
914 | * @return void |
---|
915 | */ |
---|
916 | function re_post_http($url_redirect, $redirect_message, $redirect_refreh_time) |
---|
917 | { |
---|
918 | global $conf; |
---|
919 | |
---|
920 | $data_post = http_request_to_string($_POST, ''); |
---|
921 | |
---|
922 | $message_post = "POST ".$_SERVER['PHP_SELF'].html_entity_decode(get_query_string_diff(array()))." HTTP/1.1\r\n"; |
---|
923 | |
---|
924 | /* foreach (array_flip(array_diff(array_flip(apache_request_headers()), array('Content-Type', 'Content-Length'))) as $header_name => $header_value) |
---|
925 | { |
---|
926 | $message_post .= $header_name.": ".$header_value."\r\n"; |
---|
927 | }*/ |
---|
928 | $message_post .= "Host: ".$_SERVER['HTTP_HOST']."\r\n"; |
---|
929 | $message_post .= "Cookie: ".$conf['session_name']."=".$_COOKIE[$conf['session_name']]."\r\n"; |
---|
930 | |
---|
931 | |
---|
932 | $message_post .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
---|
933 | $message_post .= "Content-Length: ".strlen($data_post)."\r\n"; |
---|
934 | $message_post .= "\r\n"; |
---|
935 | $message_post .= $data_post."\r\n"; |
---|
936 | |
---|
937 | $fd = fsockopen($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT']); |
---|
938 | fputs($fd, $message_post); |
---|
939 | fclose($fd); |
---|
940 | |
---|
941 | redirect($url_redirect, $redirect_message, $redirect_refreh_time); |
---|
942 | //exit(); done by redirect |
---|
943 | } |
---|
944 | |
---|
945 | ?> |
---|