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-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2006-02-20 22:14:24 +0000 (Mon, 20 Feb 2006) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 1048 $ |
---|
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 | |
---|
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 | return substr( $filename, 0, strrpos( $filename, '.' ) ); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * returns an array contening sub-directories, excluding "CVS" |
---|
189 | * |
---|
190 | * @param string $dir |
---|
191 | * @return array |
---|
192 | */ |
---|
193 | function get_dirs($directory) |
---|
194 | { |
---|
195 | $sub_dirs = array(); |
---|
196 | |
---|
197 | if ($opendir = opendir($directory)) |
---|
198 | { |
---|
199 | while ($file = readdir($opendir)) |
---|
200 | { |
---|
201 | if ($file != '.' |
---|
202 | and $file != '..' |
---|
203 | and is_dir($directory.'/'.$file) |
---|
204 | and $file != 'CVS' |
---|
205 | and $file != '.svn') |
---|
206 | { |
---|
207 | array_push($sub_dirs, $file); |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | return $sub_dirs; |
---|
212 | } |
---|
213 | |
---|
214 | // The get_picture_size function return an array containing : |
---|
215 | // - $picture_size[0] : final width |
---|
216 | // - $picture_size[1] : final height |
---|
217 | // The final dimensions are calculated thanks to the original dimensions and |
---|
218 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
219 | // the width/height ratio |
---|
220 | function get_picture_size( $original_width, $original_height, |
---|
221 | $max_width, $max_height ) |
---|
222 | { |
---|
223 | $width = $original_width; |
---|
224 | $height = $original_height; |
---|
225 | $is_original_size = true; |
---|
226 | |
---|
227 | if ( $max_width != "" ) |
---|
228 | { |
---|
229 | if ( $original_width > $max_width ) |
---|
230 | { |
---|
231 | $width = $max_width; |
---|
232 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
233 | } |
---|
234 | } |
---|
235 | if ( $max_height != "" ) |
---|
236 | { |
---|
237 | if ( $original_height > $max_height ) |
---|
238 | { |
---|
239 | $height = $max_height; |
---|
240 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
241 | $is_original_size = false; |
---|
242 | } |
---|
243 | } |
---|
244 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
245 | and $max_width != 0 and $max_height != 0 ) |
---|
246 | { |
---|
247 | $ratioWidth = $original_width / $max_width; |
---|
248 | $ratioHeight = $original_height / $max_height; |
---|
249 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
250 | { |
---|
251 | if ( $ratioWidth < $ratioHeight ) |
---|
252 | { |
---|
253 | $width = floor( $original_width / $ratioHeight ); |
---|
254 | $height = $max_height; |
---|
255 | } |
---|
256 | else |
---|
257 | { |
---|
258 | $width = $max_width; |
---|
259 | $height = floor( $original_height / $ratioWidth ); |
---|
260 | } |
---|
261 | $is_original_size = false; |
---|
262 | } |
---|
263 | } |
---|
264 | $picture_size = array(); |
---|
265 | $picture_size[0] = $width; |
---|
266 | $picture_size[1] = $height; |
---|
267 | return $picture_size; |
---|
268 | } |
---|
269 | //-------------------------------------------- PhpWebGallery specific functions |
---|
270 | |
---|
271 | /** |
---|
272 | * returns an array with a list of {language_code => language_name} |
---|
273 | * |
---|
274 | * @returns array |
---|
275 | */ |
---|
276 | function get_languages() |
---|
277 | { |
---|
278 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
279 | $languages = array(); |
---|
280 | |
---|
281 | while ($file = readdir($dir)) |
---|
282 | { |
---|
283 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
284 | if (is_dir($path) and !is_link($path) and file_exists($path.'/iso.txt')) |
---|
285 | { |
---|
286 | list($language_name) = @file($path.'/iso.txt'); |
---|
287 | $languages[$file] = $language_name; |
---|
288 | } |
---|
289 | } |
---|
290 | closedir($dir); |
---|
291 | @asort($languages); |
---|
292 | @reset($languages); |
---|
293 | |
---|
294 | return $languages; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * replaces the $search into <span style="$style">$search</span> in the |
---|
299 | * given $string. |
---|
300 | * |
---|
301 | * case insensitive replacements, does not replace characters in HTML tags |
---|
302 | * |
---|
303 | * @param string $string |
---|
304 | * @param string $search |
---|
305 | * @param string $style |
---|
306 | * @return string |
---|
307 | */ |
---|
308 | function add_style( $string, $search, $style ) |
---|
309 | { |
---|
310 | //return $string; |
---|
311 | $return_string = ''; |
---|
312 | $remaining = $string; |
---|
313 | |
---|
314 | $start = 0; |
---|
315 | $end = 0; |
---|
316 | $start = strpos ( $remaining, '<' ); |
---|
317 | $end = strpos ( $remaining, '>' ); |
---|
318 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
319 | { |
---|
320 | $treatment = substr ( $remaining, 0, $start ); |
---|
321 | $treatment = preg_replace( '/('.$search.')/i', |
---|
322 | '<span style="'.$style.'">\\0</span>', |
---|
323 | $treatment ); |
---|
324 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
325 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
326 | $start = strpos ( $remaining, '<' ); |
---|
327 | $end = strpos ( $remaining, '>' ); |
---|
328 | } |
---|
329 | $treatment = preg_replace( '/('.$search.')/i', |
---|
330 | '<span style="'.$style.'">\\0</span>', |
---|
331 | $remaining ); |
---|
332 | $return_string.= $treatment; |
---|
333 | |
---|
334 | return $return_string; |
---|
335 | } |
---|
336 | |
---|
337 | // replace_search replaces a searched words array string by the search in |
---|
338 | // another style for the given $string. |
---|
339 | function replace_search( $string, $search ) |
---|
340 | { |
---|
341 | // FIXME : with new advanced search, this function needs a rewrite |
---|
342 | return $string; |
---|
343 | |
---|
344 | $words = explode( ',', $search ); |
---|
345 | $style = 'background-color:white;color:red;'; |
---|
346 | foreach ( $words as $word ) { |
---|
347 | $string = add_style( $string, $word, $style ); |
---|
348 | } |
---|
349 | return $string; |
---|
350 | } |
---|
351 | |
---|
352 | function pwg_log( $file, $category, $picture = '' ) |
---|
353 | { |
---|
354 | global $conf, $user; |
---|
355 | |
---|
356 | if ($conf['log']) |
---|
357 | { |
---|
358 | if ( ($conf['history_admin'] ) or ( (! $conf['history_admin']) and ($user['status'] != 'admin') ) ) |
---|
359 | { |
---|
360 | $login = ($user['id'] == $conf['guest_id']) |
---|
361 | ? 'guest' : addslashes($user['username']); |
---|
362 | |
---|
363 | $query = ' |
---|
364 | INSERT INTO '.HISTORY_TABLE.' |
---|
365 | (date,login,IP,file,category,picture) |
---|
366 | VALUES |
---|
367 | (NOW(), |
---|
368 | \''.$login.'\', |
---|
369 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
370 | \''.addslashes($file).'\', |
---|
371 | \''.addslashes(strip_tags($category)).'\', |
---|
372 | \''.addslashes($picture).'\') |
---|
373 | ;'; |
---|
374 | pwg_query($query); |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | // format_date returns a formatted date for display. The date given in |
---|
380 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
381 | // american format (2003-09-15). By option, you can show the time. The |
---|
382 | // output is internationalized. |
---|
383 | // |
---|
384 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
385 | function format_date($date, $type = 'us', $show_time = false) |
---|
386 | { |
---|
387 | global $lang; |
---|
388 | |
---|
389 | list($year,$month,$day,$hour,$minute,$second) = array(0,0,0,0,0,0); |
---|
390 | |
---|
391 | switch ( $type ) |
---|
392 | { |
---|
393 | case 'us' : |
---|
394 | { |
---|
395 | list($year,$month,$day) = explode('-', $date); |
---|
396 | break; |
---|
397 | } |
---|
398 | case 'unix' : |
---|
399 | { |
---|
400 | list($year,$month,$day,$hour,$minute) = |
---|
401 | explode('.', date('Y.n.j.G.i', $date)); |
---|
402 | break; |
---|
403 | } |
---|
404 | case 'mysql_datetime' : |
---|
405 | { |
---|
406 | preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
407 | $date, $out); |
---|
408 | list($year,$month,$day,$hour,$minute,$second) = |
---|
409 | array($out[1],$out[2],$out[3],$out[4],$out[5],$out[6]); |
---|
410 | break; |
---|
411 | } |
---|
412 | } |
---|
413 | $formated_date = ''; |
---|
414 | // before 1970, Microsoft Windows can't mktime |
---|
415 | if ($year >= 1970) |
---|
416 | { |
---|
417 | // we ask midday because Windows think it's prior to midnight with a |
---|
418 | // zero and refuse to work |
---|
419 | $formated_date.= $lang['day'][date('w', mktime(12,0,0,$month,$day,$year))]; |
---|
420 | } |
---|
421 | $formated_date.= ' '.$day; |
---|
422 | $formated_date.= ' '.$lang['month'][(int)$month]; |
---|
423 | $formated_date.= ' '.$year; |
---|
424 | if ($show_time) |
---|
425 | { |
---|
426 | $formated_date.= ' '.$hour.':'.$minute; |
---|
427 | } |
---|
428 | |
---|
429 | return $formated_date; |
---|
430 | } |
---|
431 | |
---|
432 | function pwg_query($query) |
---|
433 | { |
---|
434 | global $conf,$page,$debug,$t2; |
---|
435 | |
---|
436 | $start = get_moment(); |
---|
437 | $result = mysql_query($query) or my_error($query."\n"); |
---|
438 | |
---|
439 | $time = get_moment() - $start; |
---|
440 | |
---|
441 | if (!isset($page['count_queries'])) |
---|
442 | { |
---|
443 | $page['count_queries'] = 0; |
---|
444 | $page['queries_time'] = 0; |
---|
445 | } |
---|
446 | |
---|
447 | $page['count_queries']++; |
---|
448 | $page['queries_time']+= $time; |
---|
449 | |
---|
450 | if ($conf['show_queries']) |
---|
451 | { |
---|
452 | $output = ''; |
---|
453 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
454 | $output.= "\n".$query; |
---|
455 | $output.= "\n".'(this query time : '; |
---|
456 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
457 | $output.= "\n".'(total SQL time : '; |
---|
458 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
459 | $output.= "\n".'(total time : '; |
---|
460 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
461 | $output.= "</pre>\n"; |
---|
462 | |
---|
463 | $debug .= $output; |
---|
464 | } |
---|
465 | |
---|
466 | return $result; |
---|
467 | } |
---|
468 | |
---|
469 | function pwg_debug( $string ) |
---|
470 | { |
---|
471 | global $debug,$t2,$page; |
---|
472 | |
---|
473 | $now = explode( ' ', microtime() ); |
---|
474 | $now2 = explode( '.', $now[0] ); |
---|
475 | $now2 = $now[1].'.'.$now2[1]; |
---|
476 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
477 | $debug .= '<p>'; |
---|
478 | $debug.= '['.$time.', '; |
---|
479 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
480 | $debug.= "</p>\n"; |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | * Redirects to the given URL |
---|
485 | * |
---|
486 | * Note : once this function called, the execution doesn't go further |
---|
487 | * (presence of an exit() instruction. |
---|
488 | * |
---|
489 | * @param string $url |
---|
490 | * @return void |
---|
491 | */ |
---|
492 | function redirect( $url ) |
---|
493 | { |
---|
494 | global $user, $template, $lang_info, $conf, $lang, $t2, $page, $debug; |
---|
495 | |
---|
496 | // $refresh, $url_link and $title are required for creating an automated |
---|
497 | // refresh page in header.tpl |
---|
498 | $refresh = 0; |
---|
499 | $url_link = $url; |
---|
500 | $title = 'redirection'; |
---|
501 | |
---|
502 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
503 | |
---|
504 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
505 | $template->parse('redirect'); |
---|
506 | |
---|
507 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
508 | |
---|
509 | exit(); |
---|
510 | } |
---|
511 | |
---|
512 | /** |
---|
513 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
514 | * |
---|
515 | * @param array $rejects |
---|
516 | * @returns string |
---|
517 | */ |
---|
518 | function get_query_string_diff($rejects = array()) |
---|
519 | { |
---|
520 | $query_string = ''; |
---|
521 | |
---|
522 | $str = $_SERVER['QUERY_STRING']; |
---|
523 | parse_str($str, $vars); |
---|
524 | |
---|
525 | $is_first = true; |
---|
526 | foreach ($vars as $key => $value) |
---|
527 | { |
---|
528 | if (!in_array($key, $rejects)) |
---|
529 | { |
---|
530 | $query_string.= $is_first ? '?' : '&'; |
---|
531 | $is_first = false; |
---|
532 | $query_string.= $key.'='.$value; |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | return $query_string; |
---|
537 | } |
---|
538 | |
---|
539 | function url_is_remote($url) |
---|
540 | { |
---|
541 | if (preg_match('/^https?:\/\/[~\/\.\w-]+$/', $url)) |
---|
542 | { |
---|
543 | return true; |
---|
544 | } |
---|
545 | return false; |
---|
546 | } |
---|
547 | |
---|
548 | /** |
---|
549 | * returns available template/theme |
---|
550 | */ |
---|
551 | function get_pwg_themes() |
---|
552 | { |
---|
553 | $themes = array(); |
---|
554 | |
---|
555 | $template_dir = PHPWG_ROOT_PATH.'template'; |
---|
556 | |
---|
557 | foreach (get_dirs($template_dir) as $template) |
---|
558 | { |
---|
559 | foreach (get_dirs($template_dir.'/'.$template.'/theme') as $theme) |
---|
560 | { |
---|
561 | array_push($themes, $template.'/'.$theme); |
---|
562 | } |
---|
563 | } |
---|
564 | |
---|
565 | return $themes; |
---|
566 | } |
---|
567 | |
---|
568 | /** |
---|
569 | * returns thumbnail filepath (or distant URL if thumbnail is remote) for a |
---|
570 | * given element |
---|
571 | * |
---|
572 | * the returned string can represente the filepath of the thumbnail or the |
---|
573 | * filepath to the corresponding icon for non picture elements |
---|
574 | * |
---|
575 | * @param string path |
---|
576 | * @param string tn_ext |
---|
577 | * @return string |
---|
578 | */ |
---|
579 | function get_thumbnail_src($path, $tn_ext = '') |
---|
580 | { |
---|
581 | global $conf, $user; |
---|
582 | |
---|
583 | if ($tn_ext != '') |
---|
584 | { |
---|
585 | $src = substr_replace(get_filename_wo_extension($path), |
---|
586 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
587 | strrpos($path,'/'), |
---|
588 | 1); |
---|
589 | $src.= '.'.$tn_ext; |
---|
590 | } |
---|
591 | else |
---|
592 | { |
---|
593 | $src = get_themeconf('mime_icon_dir'); |
---|
594 | $src.= strtolower(get_extension($path)).'.png'; |
---|
595 | } |
---|
596 | |
---|
597 | return $src; |
---|
598 | } |
---|
599 | |
---|
600 | // my_error returns (or send to standard output) the message concerning the |
---|
601 | // error occured for the last mysql query. |
---|
602 | function my_error($header) |
---|
603 | { |
---|
604 | $error = '<pre>'; |
---|
605 | $error.= $header; |
---|
606 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
607 | $error.= mysql_error(); |
---|
608 | $error.= '</pre>'; |
---|
609 | die ($error); |
---|
610 | } |
---|
611 | |
---|
612 | /** |
---|
613 | * creates an array based on a query, this function is a very common pattern |
---|
614 | * used here |
---|
615 | * |
---|
616 | * @param string $query |
---|
617 | * @param string $fieldname |
---|
618 | * @return array |
---|
619 | */ |
---|
620 | function array_from_query($query, $fieldname) |
---|
621 | { |
---|
622 | $array = array(); |
---|
623 | |
---|
624 | $result = pwg_query($query); |
---|
625 | while ($row = mysql_fetch_array($result)) |
---|
626 | { |
---|
627 | array_push($array, $row[$fieldname]); |
---|
628 | } |
---|
629 | |
---|
630 | return $array; |
---|
631 | } |
---|
632 | |
---|
633 | /** |
---|
634 | * instantiate number list for days in a template block |
---|
635 | * |
---|
636 | * @param string blockname |
---|
637 | * @param string selection |
---|
638 | */ |
---|
639 | function get_day_list($blockname, $selection) |
---|
640 | { |
---|
641 | global $template; |
---|
642 | |
---|
643 | $template->assign_block_vars( |
---|
644 | $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); |
---|
645 | |
---|
646 | for ($i = 1; $i <= 31; $i++) |
---|
647 | { |
---|
648 | $selected = ''; |
---|
649 | if ($i == (int)$selection) |
---|
650 | { |
---|
651 | $selected = 'selected="selected"'; |
---|
652 | } |
---|
653 | $template->assign_block_vars( |
---|
654 | $blockname, array('SELECTED' => $selected, |
---|
655 | 'VALUE' => $i, |
---|
656 | 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); |
---|
657 | } |
---|
658 | } |
---|
659 | |
---|
660 | /** |
---|
661 | * instantiate month list in a template block |
---|
662 | * |
---|
663 | * @param string blockname |
---|
664 | * @param string selection |
---|
665 | */ |
---|
666 | function get_month_list($blockname, $selection) |
---|
667 | { |
---|
668 | global $template, $lang; |
---|
669 | |
---|
670 | $template->assign_block_vars( |
---|
671 | $blockname, array('SELECTED' => '', |
---|
672 | 'VALUE' => 0, |
---|
673 | 'OPTION' => '------------')); |
---|
674 | |
---|
675 | for ($i = 1; $i <= 12; $i++) |
---|
676 | { |
---|
677 | $selected = ''; |
---|
678 | if ($i == (int)$selection) |
---|
679 | { |
---|
680 | $selected = 'selected="selected"'; |
---|
681 | } |
---|
682 | $template->assign_block_vars( |
---|
683 | $blockname, array('SELECTED' => $selected, |
---|
684 | 'VALUE' => $i, |
---|
685 | 'OPTION' => $lang['month'][$i])); |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * fill the current user caddie with given elements, if not already in |
---|
691 | * caddie |
---|
692 | * |
---|
693 | * @param array elements_id |
---|
694 | */ |
---|
695 | function fill_caddie($elements_id) |
---|
696 | { |
---|
697 | global $user; |
---|
698 | |
---|
699 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
700 | |
---|
701 | $query = ' |
---|
702 | SELECT element_id |
---|
703 | FROM '.CADDIE_TABLE.' |
---|
704 | WHERE user_id = '.$user['id'].' |
---|
705 | ;'; |
---|
706 | $in_caddie = array_from_query($query, 'element_id'); |
---|
707 | |
---|
708 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
709 | |
---|
710 | $datas = array(); |
---|
711 | |
---|
712 | foreach ($caddiables as $caddiable) |
---|
713 | { |
---|
714 | array_push($datas, array('element_id' => $caddiable, |
---|
715 | 'user_id' => $user['id'])); |
---|
716 | } |
---|
717 | |
---|
718 | if (count($caddiables) > 0) |
---|
719 | { |
---|
720 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | /** |
---|
725 | * returns the element name from its filename |
---|
726 | * |
---|
727 | * @param string filename |
---|
728 | * @return string name |
---|
729 | */ |
---|
730 | function get_name_from_file($filename) |
---|
731 | { |
---|
732 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
733 | } |
---|
734 | |
---|
735 | /** |
---|
736 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
737 | * returned |
---|
738 | * |
---|
739 | * @param string key |
---|
740 | * @return string |
---|
741 | */ |
---|
742 | function l10n($key) |
---|
743 | { |
---|
744 | global $lang, $conf; |
---|
745 | |
---|
746 | if ($conf['debug_l10n'] and !isset($lang[$key])) |
---|
747 | { |
---|
748 | echo '[l10n] language key "'.$key.'" is not defined<br />'; |
---|
749 | } |
---|
750 | |
---|
751 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
752 | } |
---|
753 | |
---|
754 | /** |
---|
755 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
756 | * key is returned |
---|
757 | * |
---|
758 | * @param string key |
---|
759 | * @return string |
---|
760 | */ |
---|
761 | function get_themeconf($key) |
---|
762 | { |
---|
763 | global $themeconf; |
---|
764 | |
---|
765 | return $themeconf[$key]; |
---|
766 | } |
---|
767 | |
---|
768 | /** |
---|
769 | * Prepends and appends a string at each value of the given array. |
---|
770 | * |
---|
771 | * @param array |
---|
772 | * @param string prefix to each array values |
---|
773 | * @param string suffix to each array values |
---|
774 | */ |
---|
775 | function prepend_append_array_items($array, $prepend_str, $append_str) |
---|
776 | { |
---|
777 | array_walk( |
---|
778 | $array, |
---|
779 | create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";') |
---|
780 | ); |
---|
781 | |
---|
782 | return $array; |
---|
783 | } |
---|
784 | |
---|
785 | /** |
---|
786 | * returns search rules stored into a serialized array in "search" |
---|
787 | * table. Each search rules set is numericaly identified. |
---|
788 | * |
---|
789 | * @param int search_id |
---|
790 | * @return array |
---|
791 | */ |
---|
792 | function get_search_array($search_id) |
---|
793 | { |
---|
794 | if (!is_numeric($search_id)) |
---|
795 | { |
---|
796 | die('Search id must be an integer'); |
---|
797 | } |
---|
798 | |
---|
799 | $query = ' |
---|
800 | SELECT rules |
---|
801 | FROM '.SEARCH_TABLE.' |
---|
802 | WHERE id = '.$search_id.' |
---|
803 | ;'; |
---|
804 | list($serialized_rules) = mysql_fetch_row(pwg_query($query)); |
---|
805 | |
---|
806 | return unserialize($serialized_rules); |
---|
807 | } |
---|
808 | |
---|
809 | /** |
---|
810 | * returns the SQL clause from a search identifier |
---|
811 | * |
---|
812 | * Search rules are stored in search table as a serialized array. This array |
---|
813 | * need to be transformed into an SQL clause to be used in queries. |
---|
814 | * |
---|
815 | * @param int search_id |
---|
816 | * @return string |
---|
817 | */ |
---|
818 | function get_sql_search_clause($search_id) |
---|
819 | { |
---|
820 | $search = get_search_array($search_id); |
---|
821 | |
---|
822 | // SQL where clauses are stored in $clauses array during query |
---|
823 | // construction |
---|
824 | $clauses = array(); |
---|
825 | |
---|
826 | foreach (array('file','name','comment','keywords','author') as $textfield) |
---|
827 | { |
---|
828 | if (isset($search['fields'][$textfield])) |
---|
829 | { |
---|
830 | $local_clauses = array(); |
---|
831 | foreach ($search['fields'][$textfield]['words'] as $word) |
---|
832 | { |
---|
833 | array_push($local_clauses, $textfield." LIKE '%".$word."%'"); |
---|
834 | } |
---|
835 | |
---|
836 | // adds brackets around where clauses |
---|
837 | $local_clauses = prepend_append_array_items($local_clauses, '(', ')'); |
---|
838 | |
---|
839 | array_push( |
---|
840 | $clauses, |
---|
841 | implode( |
---|
842 | ' '.$search['fields'][$textfield]['mode'].' ', |
---|
843 | $local_clauses |
---|
844 | ) |
---|
845 | ); |
---|
846 | } |
---|
847 | } |
---|
848 | |
---|
849 | if (isset($search['fields']['allwords'])) |
---|
850 | { |
---|
851 | $fields = array('file', 'name', 'comment', 'keywords', 'author'); |
---|
852 | // in the OR mode, request bust be : |
---|
853 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
854 | // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
855 | // |
---|
856 | // in the AND mode : |
---|
857 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
858 | // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
859 | $word_clauses = array(); |
---|
860 | foreach ($search['fields']['allwords']['words'] as $word) |
---|
861 | { |
---|
862 | $field_clauses = array(); |
---|
863 | foreach ($fields as $field) |
---|
864 | { |
---|
865 | array_push($field_clauses, $field." LIKE '%".$word."%'"); |
---|
866 | } |
---|
867 | // adds brackets around where clauses |
---|
868 | array_push( |
---|
869 | $word_clauses, |
---|
870 | implode( |
---|
871 | "\n OR ", |
---|
872 | $field_clauses |
---|
873 | ) |
---|
874 | ); |
---|
875 | } |
---|
876 | |
---|
877 | array_walk( |
---|
878 | $word_clauses, |
---|
879 | create_function('&$s','$s="(".$s.")";') |
---|
880 | ); |
---|
881 | |
---|
882 | array_push( |
---|
883 | $clauses, |
---|
884 | "\n ". |
---|
885 | implode( |
---|
886 | "\n ". |
---|
887 | $search['fields']['allwords']['mode']. |
---|
888 | "\n ", |
---|
889 | $word_clauses |
---|
890 | ) |
---|
891 | ); |
---|
892 | } |
---|
893 | |
---|
894 | foreach (array('date_available', 'date_creation') as $datefield) |
---|
895 | { |
---|
896 | if (isset($search['fields'][$datefield])) |
---|
897 | { |
---|
898 | array_push( |
---|
899 | $clauses, |
---|
900 | $datefield." = '".$search['fields'][$datefield]['date']."'" |
---|
901 | ); |
---|
902 | } |
---|
903 | |
---|
904 | foreach (array('after','before') as $suffix) |
---|
905 | { |
---|
906 | $key = $datefield.'-'.$suffix; |
---|
907 | |
---|
908 | if (isset($search['fields'][$key])) |
---|
909 | { |
---|
910 | array_push( |
---|
911 | $clauses, |
---|
912 | |
---|
913 | $datefield. |
---|
914 | ($suffix == 'after' ? ' >' : ' <'). |
---|
915 | ($search['fields'][$key]['inc'] ? '=' : ''). |
---|
916 | " '".$search['fields'][$key]['date']."'" |
---|
917 | |
---|
918 | ); |
---|
919 | } |
---|
920 | } |
---|
921 | } |
---|
922 | |
---|
923 | if (isset($search['fields']['cat'])) |
---|
924 | { |
---|
925 | if ($search['fields']['cat']['sub_inc']) |
---|
926 | { |
---|
927 | // searching all the categories id of sub-categories |
---|
928 | $cat_ids = get_subcat_ids($search['fields']['cat']['words']); |
---|
929 | } |
---|
930 | else |
---|
931 | { |
---|
932 | $cat_ids = $search['fields']['cat']['words']; |
---|
933 | } |
---|
934 | |
---|
935 | $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; |
---|
936 | array_push($clauses, $local_clause); |
---|
937 | } |
---|
938 | |
---|
939 | // adds brackets around where clauses |
---|
940 | $clauses = prepend_append_array_items($clauses, '(', ')'); |
---|
941 | |
---|
942 | $where_separator = |
---|
943 | implode( |
---|
944 | "\n ".$search['mode'].' ', |
---|
945 | $clauses |
---|
946 | ); |
---|
947 | |
---|
948 | $search_clause = $where_separator; |
---|
949 | |
---|
950 | if (isset($forbidden)) |
---|
951 | { |
---|
952 | $search_clause.= "\n AND ".$forbidden; |
---|
953 | } |
---|
954 | |
---|
955 | return $search_clause; |
---|
956 | } |
---|
957 | |
---|
958 | /** |
---|
959 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
960 | * |
---|
961 | * @return string |
---|
962 | */ |
---|
963 | function get_webmaster_mail_address() |
---|
964 | { |
---|
965 | global $conf; |
---|
966 | |
---|
967 | $query = ' |
---|
968 | SELECT '.$conf['user_fields']['email'].' |
---|
969 | FROM '.USERS_TABLE.' |
---|
970 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
971 | ;'; |
---|
972 | list($email) = mysql_fetch_array(pwg_query($query)); |
---|
973 | |
---|
974 | return $email; |
---|
975 | } |
---|
976 | |
---|
977 | /** |
---|
978 | * which upgrades are available ? |
---|
979 | * |
---|
980 | * @return array |
---|
981 | */ |
---|
982 | function get_available_upgrade_ids() |
---|
983 | { |
---|
984 | $upgrades_path = PHPWG_ROOT_PATH.'install/db'; |
---|
985 | |
---|
986 | $available_upgrade_ids = array(); |
---|
987 | |
---|
988 | if ($contents = opendir($upgrades_path)) |
---|
989 | { |
---|
990 | while (($node = readdir($contents)) !== false) |
---|
991 | { |
---|
992 | if (is_file($upgrades_path.'/'.$node) |
---|
993 | and preg_match('/^(.*?)-database\.php$/', $node, $match)) |
---|
994 | { |
---|
995 | array_push($available_upgrade_ids, $match[1]); |
---|
996 | } |
---|
997 | } |
---|
998 | } |
---|
999 | natcasesort($available_upgrade_ids); |
---|
1000 | |
---|
1001 | return $available_upgrade_ids; |
---|
1002 | } |
---|
1003 | ?> |
---|