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-12 16:05:49 +0000 (Sun, 12 Feb 2006) $ |
---|
10 | // | last modifier : $Author: nikrou $ |
---|
11 | // | revision : $Revision: 1035 $ |
---|
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 templates/themes |
---|
550 | */ |
---|
551 | function get_templates() |
---|
552 | { |
---|
553 | return get_dirs(PHPWG_ROOT_PATH.'theme'); |
---|
554 | } |
---|
555 | function get_themes() |
---|
556 | { |
---|
557 | $themes = array(); |
---|
558 | |
---|
559 | foreach (get_dirs(PHPWG_ROOT_PATH.'template') as $template) |
---|
560 | { |
---|
561 | foreach (get_dirs(PHPWG_ROOT_PATH.'template/'.$template.'/theme') as $theme) |
---|
562 | { |
---|
563 | array_push($themes, $template.'/'.$theme); |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | return $themes; |
---|
568 | } |
---|
569 | |
---|
570 | /** |
---|
571 | * returns thumbnail filepath (or distant URL if thumbnail is remote) for a |
---|
572 | * given element |
---|
573 | * |
---|
574 | * the returned string can represente the filepath of the thumbnail or the |
---|
575 | * filepath to the corresponding icon for non picture elements |
---|
576 | * |
---|
577 | * @param string path |
---|
578 | * @param string tn_ext |
---|
579 | * @return string |
---|
580 | */ |
---|
581 | function get_thumbnail_src($path, $tn_ext = '') |
---|
582 | { |
---|
583 | global $conf, $user; |
---|
584 | |
---|
585 | if ($tn_ext != '') |
---|
586 | { |
---|
587 | $src = substr_replace(get_filename_wo_extension($path), |
---|
588 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
589 | strrpos($path,'/'), |
---|
590 | 1); |
---|
591 | $src.= '.'.$tn_ext; |
---|
592 | } |
---|
593 | else |
---|
594 | { |
---|
595 | $src = get_themeconf('mime_icon_dir'); |
---|
596 | $src.= strtolower(get_extension($path)).'.png'; |
---|
597 | } |
---|
598 | |
---|
599 | return $src; |
---|
600 | } |
---|
601 | |
---|
602 | // my_error returns (or send to standard output) the message concerning the |
---|
603 | // error occured for the last mysql query. |
---|
604 | function my_error($header) |
---|
605 | { |
---|
606 | $error = '<pre>'; |
---|
607 | $error.= $header; |
---|
608 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
609 | $error.= mysql_error(); |
---|
610 | $error.= '</pre>'; |
---|
611 | die ($error); |
---|
612 | } |
---|
613 | |
---|
614 | /** |
---|
615 | * creates an array based on a query, this function is a very common pattern |
---|
616 | * used here |
---|
617 | * |
---|
618 | * @param string $query |
---|
619 | * @param string $fieldname |
---|
620 | * @return array |
---|
621 | */ |
---|
622 | function array_from_query($query, $fieldname) |
---|
623 | { |
---|
624 | $array = array(); |
---|
625 | |
---|
626 | $result = pwg_query($query); |
---|
627 | while ($row = mysql_fetch_array($result)) |
---|
628 | { |
---|
629 | array_push($array, $row[$fieldname]); |
---|
630 | } |
---|
631 | |
---|
632 | return $array; |
---|
633 | } |
---|
634 | |
---|
635 | /** |
---|
636 | * instantiate number list for days in a template block |
---|
637 | * |
---|
638 | * @param string blockname |
---|
639 | * @param string selection |
---|
640 | */ |
---|
641 | function get_day_list($blockname, $selection) |
---|
642 | { |
---|
643 | global $template; |
---|
644 | |
---|
645 | $template->assign_block_vars( |
---|
646 | $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); |
---|
647 | |
---|
648 | for ($i = 1; $i <= 31; $i++) |
---|
649 | { |
---|
650 | $selected = ''; |
---|
651 | if ($i == (int)$selection) |
---|
652 | { |
---|
653 | $selected = 'selected="selected"'; |
---|
654 | } |
---|
655 | $template->assign_block_vars( |
---|
656 | $blockname, array('SELECTED' => $selected, |
---|
657 | 'VALUE' => $i, |
---|
658 | 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | /** |
---|
663 | * instantiate month list in a template block |
---|
664 | * |
---|
665 | * @param string blockname |
---|
666 | * @param string selection |
---|
667 | */ |
---|
668 | function get_month_list($blockname, $selection) |
---|
669 | { |
---|
670 | global $template, $lang; |
---|
671 | |
---|
672 | $template->assign_block_vars( |
---|
673 | $blockname, array('SELECTED' => '', |
---|
674 | 'VALUE' => 0, |
---|
675 | 'OPTION' => '------------')); |
---|
676 | |
---|
677 | for ($i = 1; $i <= 12; $i++) |
---|
678 | { |
---|
679 | $selected = ''; |
---|
680 | if ($i == (int)$selection) |
---|
681 | { |
---|
682 | $selected = 'selected="selected"'; |
---|
683 | } |
---|
684 | $template->assign_block_vars( |
---|
685 | $blockname, array('SELECTED' => $selected, |
---|
686 | 'VALUE' => $i, |
---|
687 | 'OPTION' => $lang['month'][$i])); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | /** |
---|
692 | * fill the current user caddie with given elements, if not already in |
---|
693 | * caddie |
---|
694 | * |
---|
695 | * @param array elements_id |
---|
696 | */ |
---|
697 | function fill_caddie($elements_id) |
---|
698 | { |
---|
699 | global $user; |
---|
700 | |
---|
701 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
702 | |
---|
703 | $query = ' |
---|
704 | SELECT element_id |
---|
705 | FROM '.CADDIE_TABLE.' |
---|
706 | WHERE user_id = '.$user['id'].' |
---|
707 | ;'; |
---|
708 | $in_caddie = array_from_query($query, 'element_id'); |
---|
709 | |
---|
710 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
711 | |
---|
712 | $datas = array(); |
---|
713 | |
---|
714 | foreach ($caddiables as $caddiable) |
---|
715 | { |
---|
716 | array_push($datas, array('element_id' => $caddiable, |
---|
717 | 'user_id' => $user['id'])); |
---|
718 | } |
---|
719 | |
---|
720 | if (count($caddiables) > 0) |
---|
721 | { |
---|
722 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | /** |
---|
727 | * returns the element name from its filename |
---|
728 | * |
---|
729 | * @param string filename |
---|
730 | * @return string name |
---|
731 | */ |
---|
732 | function get_name_from_file($filename) |
---|
733 | { |
---|
734 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
735 | } |
---|
736 | |
---|
737 | /** |
---|
738 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
739 | * returned |
---|
740 | * |
---|
741 | * @param string key |
---|
742 | * @return string |
---|
743 | */ |
---|
744 | function l10n($key) |
---|
745 | { |
---|
746 | global $lang, $conf; |
---|
747 | |
---|
748 | if ($conf['debug_l10n'] and !isset($lang[$key])) |
---|
749 | { |
---|
750 | echo '[l10n] language key "'.$key.'" is not defined<br />'; |
---|
751 | } |
---|
752 | |
---|
753 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
754 | } |
---|
755 | |
---|
756 | /** |
---|
757 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
758 | * key is returned |
---|
759 | * |
---|
760 | * @param string key |
---|
761 | * @return string |
---|
762 | */ |
---|
763 | function get_themeconf($key) |
---|
764 | { |
---|
765 | global $themeconf; |
---|
766 | |
---|
767 | return $themeconf[$key]; |
---|
768 | } |
---|
769 | |
---|
770 | /** |
---|
771 | * Prepends and appends a string at each value of the given array. |
---|
772 | * |
---|
773 | * @param array |
---|
774 | * @param string prefix to each array values |
---|
775 | * @param string suffix to each array values |
---|
776 | */ |
---|
777 | function prepend_append_array_items($array, $prepend_str, $append_str) |
---|
778 | { |
---|
779 | array_walk( |
---|
780 | $array, |
---|
781 | create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";') |
---|
782 | ); |
---|
783 | |
---|
784 | return $array; |
---|
785 | } |
---|
786 | |
---|
787 | /** |
---|
788 | * returns search rules stored into a serialized array in "search" |
---|
789 | * table. Each search rules set is numericaly identified. |
---|
790 | * |
---|
791 | * @param int search_id |
---|
792 | * @return array |
---|
793 | */ |
---|
794 | function get_search_array($search_id) |
---|
795 | { |
---|
796 | if (!is_numeric($search_id)) |
---|
797 | { |
---|
798 | die('Search id must be an integer'); |
---|
799 | } |
---|
800 | |
---|
801 | $query = ' |
---|
802 | SELECT rules |
---|
803 | FROM '.SEARCH_TABLE.' |
---|
804 | WHERE id = '.$search_id.' |
---|
805 | ;'; |
---|
806 | list($serialized_rules) = mysql_fetch_row(pwg_query($query)); |
---|
807 | |
---|
808 | return unserialize($serialized_rules); |
---|
809 | } |
---|
810 | |
---|
811 | /** |
---|
812 | * returns the SQL clause from a search identifier |
---|
813 | * |
---|
814 | * Search rules are stored in search table as a serialized array. This array |
---|
815 | * need to be transformed into an SQL clause to be used in queries. |
---|
816 | * |
---|
817 | * @param int search_id |
---|
818 | * @return string |
---|
819 | */ |
---|
820 | function get_sql_search_clause($search_id) |
---|
821 | { |
---|
822 | $search = get_search_array($search_id); |
---|
823 | |
---|
824 | // SQL where clauses are stored in $clauses array during query |
---|
825 | // construction |
---|
826 | $clauses = array(); |
---|
827 | |
---|
828 | foreach (array('file','name','comment','keywords','author') as $textfield) |
---|
829 | { |
---|
830 | if (isset($search['fields'][$textfield])) |
---|
831 | { |
---|
832 | $local_clauses = array(); |
---|
833 | foreach ($search['fields'][$textfield]['words'] as $word) |
---|
834 | { |
---|
835 | array_push($local_clauses, $textfield." LIKE '%".$word."%'"); |
---|
836 | } |
---|
837 | |
---|
838 | // adds brackets around where clauses |
---|
839 | $local_clauses = prepend_append_array_items($local_clauses, '(', ')'); |
---|
840 | |
---|
841 | array_push( |
---|
842 | $clauses, |
---|
843 | implode( |
---|
844 | ' '.$search['fields'][$textfield]['mode'].' ', |
---|
845 | $local_clauses |
---|
846 | ) |
---|
847 | ); |
---|
848 | } |
---|
849 | } |
---|
850 | |
---|
851 | if (isset($search['fields']['allwords'])) |
---|
852 | { |
---|
853 | $fields = array('file', 'name', 'comment', 'keywords', 'author'); |
---|
854 | // in the OR mode, request bust be : |
---|
855 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
856 | // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
857 | // |
---|
858 | // in the AND mode : |
---|
859 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
860 | // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
861 | $word_clauses = array(); |
---|
862 | foreach ($search['fields']['allwords']['words'] as $word) |
---|
863 | { |
---|
864 | $field_clauses = array(); |
---|
865 | foreach ($fields as $field) |
---|
866 | { |
---|
867 | array_push($field_clauses, $field." LIKE '%".$word."%'"); |
---|
868 | } |
---|
869 | // adds brackets around where clauses |
---|
870 | array_push( |
---|
871 | $word_clauses, |
---|
872 | implode( |
---|
873 | "\n OR ", |
---|
874 | $field_clauses |
---|
875 | ) |
---|
876 | ); |
---|
877 | } |
---|
878 | |
---|
879 | array_walk( |
---|
880 | $word_clauses, |
---|
881 | create_function('&$s','$s="(".$s.")";') |
---|
882 | ); |
---|
883 | |
---|
884 | array_push( |
---|
885 | $clauses, |
---|
886 | "\n ". |
---|
887 | implode( |
---|
888 | "\n ". |
---|
889 | $search['fields']['allwords']['mode']. |
---|
890 | "\n ", |
---|
891 | $word_clauses |
---|
892 | ) |
---|
893 | ); |
---|
894 | } |
---|
895 | |
---|
896 | foreach (array('date_available', 'date_creation') as $datefield) |
---|
897 | { |
---|
898 | if (isset($search['fields'][$datefield])) |
---|
899 | { |
---|
900 | array_push( |
---|
901 | $clauses, |
---|
902 | $datefield." = '".$search['fields'][$datefield]['date']."'" |
---|
903 | ); |
---|
904 | } |
---|
905 | |
---|
906 | foreach (array('after','before') as $suffix) |
---|
907 | { |
---|
908 | $key = $datefield.'-'.$suffix; |
---|
909 | |
---|
910 | if (isset($search['fields'][$key])) |
---|
911 | { |
---|
912 | array_push( |
---|
913 | $clauses, |
---|
914 | |
---|
915 | $datefield. |
---|
916 | ($suffix == 'after' ? ' >' : ' <'). |
---|
917 | ($search['fields'][$key]['inc'] ? '=' : ''). |
---|
918 | " '".$search['fields'][$key]['date']."'" |
---|
919 | |
---|
920 | ); |
---|
921 | } |
---|
922 | } |
---|
923 | } |
---|
924 | |
---|
925 | if (isset($search['fields']['cat'])) |
---|
926 | { |
---|
927 | if ($search['fields']['cat']['sub_inc']) |
---|
928 | { |
---|
929 | // searching all the categories id of sub-categories |
---|
930 | $cat_ids = get_subcat_ids($search['fields']['cat']['words']); |
---|
931 | } |
---|
932 | else |
---|
933 | { |
---|
934 | $cat_ids = $search['fields']['cat']['words']; |
---|
935 | } |
---|
936 | |
---|
937 | $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; |
---|
938 | array_push($clauses, $local_clause); |
---|
939 | } |
---|
940 | |
---|
941 | // adds brackets around where clauses |
---|
942 | $clauses = prepend_append_array_items($clauses, '(', ')'); |
---|
943 | |
---|
944 | $where_separator = |
---|
945 | implode( |
---|
946 | "\n ".$search['mode'].' ', |
---|
947 | $clauses |
---|
948 | ); |
---|
949 | |
---|
950 | $search_clause = $where_separator; |
---|
951 | |
---|
952 | if (isset($forbidden)) |
---|
953 | { |
---|
954 | $search_clause.= "\n AND ".$forbidden; |
---|
955 | } |
---|
956 | |
---|
957 | return $search_clause; |
---|
958 | } |
---|
959 | |
---|
960 | /** |
---|
961 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
962 | * |
---|
963 | * @return string |
---|
964 | */ |
---|
965 | function get_webmaster_mail_address() |
---|
966 | { |
---|
967 | global $conf; |
---|
968 | |
---|
969 | $query = ' |
---|
970 | SELECT '.$conf['user_fields']['email'].' |
---|
971 | FROM '.USERS_TABLE.' |
---|
972 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
973 | ;'; |
---|
974 | list($email) = mysql_fetch_array(pwg_query($query)); |
---|
975 | |
---|
976 | return $email; |
---|
977 | } |
---|
978 | |
---|
979 | /** |
---|
980 | * which upgrades are available ? |
---|
981 | * |
---|
982 | * @return array |
---|
983 | */ |
---|
984 | function get_available_upgrade_ids() |
---|
985 | { |
---|
986 | $upgrades_path = PHPWG_ROOT_PATH.'install/db'; |
---|
987 | |
---|
988 | $available_upgrade_ids = array(); |
---|
989 | |
---|
990 | if ($contents = opendir($upgrades_path)) |
---|
991 | { |
---|
992 | while (($node = readdir($contents)) !== false) |
---|
993 | { |
---|
994 | if (is_file($upgrades_path.'/'.$node) |
---|
995 | and preg_match('/^(.*?)-database\.php$/', $node, $match)) |
---|
996 | { |
---|
997 | array_push($available_upgrade_ids, $match[1]); |
---|
998 | } |
---|
999 | } |
---|
1000 | } |
---|
1001 | natcasesort($available_upgrade_ids); |
---|
1002 | |
---|
1003 | return $available_upgrade_ids; |
---|
1004 | } |
---|
1005 | ?> |
---|