1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | functions.inc.php | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | application : PhpWebGallery <http://phpwebgallery.net> | |
---|
6 | // | branch : BSF (Best So Far) | |
---|
7 | // +-----------------------------------------------------------------------+ |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2004-05-20 21:22:44 +0000 (Thu, 20 May 2004) $ |
---|
10 | // | last modifier : $Author: z0rglub $ |
---|
11 | // | revision : $Revision: 420 $ |
---|
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/htmlfunctions.inc.php' ); |
---|
34 | |
---|
35 | //----------------------------------------------------------- generic functions |
---|
36 | |
---|
37 | /** |
---|
38 | * possible values of an "enum" field |
---|
39 | * |
---|
40 | * get_enums returns an array containing the possible values of a enum field |
---|
41 | * in a table of the database. |
---|
42 | * |
---|
43 | * @param string table in the database |
---|
44 | * @param string field name in this table |
---|
45 | * @uses str_replace |
---|
46 | * @uses explode |
---|
47 | * @uses sizeof |
---|
48 | * @uses substr |
---|
49 | */ |
---|
50 | function get_enums( $table, $field ) |
---|
51 | { |
---|
52 | // retrieving the properties of the table. Each line represents a field : |
---|
53 | // columns are 'Field', 'Type' |
---|
54 | $result=mysql_query("desc $table"); |
---|
55 | while ( $row = mysql_fetch_array( $result ) ) |
---|
56 | { |
---|
57 | // we are only interested in the the field given in parameter for the |
---|
58 | // function |
---|
59 | if ( $row['Field']==$field ) |
---|
60 | { |
---|
61 | // retrieving possible values of the enum field |
---|
62 | // enum('blue','green','black') |
---|
63 | $option = explode( ',', substr($row['Type'], 5, -1 ) ); |
---|
64 | for ( $i = 0; $i < sizeof( $option ); $i++ ) |
---|
65 | { |
---|
66 | // deletion of quotation marks |
---|
67 | $option[$i] = str_replace( "'", '',$option[$i] ); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | mysql_free_result( $result ); |
---|
72 | return $option; |
---|
73 | } |
---|
74 | |
---|
75 | // get_boolean transforms a string to a boolean value. If the string is |
---|
76 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
77 | // any other case, true is returned. |
---|
78 | function get_boolean( $string ) |
---|
79 | { |
---|
80 | $boolean = true; |
---|
81 | if ( preg_match( '/^false$/i', $string ) ) |
---|
82 | { |
---|
83 | $boolean = false; |
---|
84 | } |
---|
85 | return $boolean; |
---|
86 | } |
---|
87 | |
---|
88 | // array_remove removes a value from the given array if the value existed in |
---|
89 | // this array. |
---|
90 | function array_remove( $array, $value ) |
---|
91 | { |
---|
92 | $output = array(); |
---|
93 | foreach ( $array as $v ) { |
---|
94 | if ( $v != $value ) array_push( $output, $v ); |
---|
95 | } |
---|
96 | return $output; |
---|
97 | } |
---|
98 | |
---|
99 | // The function get_moment returns a float value coresponding to the number |
---|
100 | // of seconds since the unix epoch (1st January 1970) and the microseconds |
---|
101 | // are precised : e.g. 1052343429.89276600 |
---|
102 | function get_moment() |
---|
103 | { |
---|
104 | $t1 = explode( ' ', microtime() ); |
---|
105 | $t2 = explode( '.', $t1[0] ); |
---|
106 | $t2 = $t1[1].'.'.$t2[1]; |
---|
107 | return $t2; |
---|
108 | } |
---|
109 | |
---|
110 | // The function get_elapsed_time returns the number of seconds (with 3 |
---|
111 | // decimals precision) between the start time and the end time given. |
---|
112 | function get_elapsed_time( $start, $end ) |
---|
113 | { |
---|
114 | return number_format( $end - $start, 3, '.', ' ').' s'; |
---|
115 | } |
---|
116 | |
---|
117 | // - The replace_space function replaces space and '-' characters |
---|
118 | // by their HTML equivalent &nbsb; and − |
---|
119 | // - The function does not replace characters in HTML tags |
---|
120 | // - This function was created because IE5 does not respect the |
---|
121 | // CSS "white-space: nowrap;" property unless space and minus |
---|
122 | // characters are replaced like this function does. |
---|
123 | // - Example : |
---|
124 | // <div class="foo">My friend</div> |
---|
125 | // ( 01234567891111111111222222222233 ) |
---|
126 | // ( 0123456789012345678901 ) |
---|
127 | // becomes : |
---|
128 | // <div class="foo">My friend</div> |
---|
129 | function replace_space( $string ) |
---|
130 | { |
---|
131 | //return $string; |
---|
132 | $return_string = ''; |
---|
133 | // $remaining is the rest of the string where to replace spaces characters |
---|
134 | $remaining = $string; |
---|
135 | // $start represents the position of the next '<' character |
---|
136 | // $end represents the position of the next '>' character |
---|
137 | $start = 0; |
---|
138 | $end = 0; |
---|
139 | $start = strpos ( $remaining, '<' ); // -> 0 |
---|
140 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
141 | // as long as a '<' and his friend '>' are found, we loop |
---|
142 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
143 | { |
---|
144 | // $treatment is the part of the string to treat |
---|
145 | // In the first loop of our example, this variable is empty, but in the |
---|
146 | // second loop, it equals 'My friend' |
---|
147 | $treatment = substr ( $remaining, 0, $start ); |
---|
148 | // Replacement of ' ' by his equivalent ' ' |
---|
149 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
150 | $treatment = str_replace( '-', '−', $treatment ); |
---|
151 | // composing the string to return by adding the treated string and the |
---|
152 | // following HTML tag -> 'My friend</div>' |
---|
153 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
154 | // the remaining string is deplaced to the part after the '>' of this |
---|
155 | // loop |
---|
156 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
157 | $start = strpos ( $remaining, '<' ); |
---|
158 | $end = strpos ( $remaining, '>' ); |
---|
159 | } |
---|
160 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
161 | $treatment = str_replace( '-', '−', $treatment ); |
---|
162 | $return_string.= $treatment; |
---|
163 | |
---|
164 | return $return_string; |
---|
165 | } |
---|
166 | |
---|
167 | // get_extension returns the part of the string after the last "." |
---|
168 | function get_extension( $filename ) |
---|
169 | { |
---|
170 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
171 | } |
---|
172 | |
---|
173 | // get_filename_wo_extension returns the part of the string before the last |
---|
174 | // ".". |
---|
175 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
176 | function get_filename_wo_extension( $filename ) |
---|
177 | { |
---|
178 | return substr( $filename, 0, strrpos( $filename, '.' ) ); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * returns an array contening sub-directories |
---|
183 | * |
---|
184 | * @param string $dir |
---|
185 | * @return array |
---|
186 | */ |
---|
187 | function get_dirs( $directory ) |
---|
188 | { |
---|
189 | $sub_dirs = array(); |
---|
190 | |
---|
191 | if ( $opendir = opendir( $directory ) ) |
---|
192 | { |
---|
193 | while ( $file = readdir ( $opendir ) ) |
---|
194 | { |
---|
195 | if ( $file != '.' and $file != '..' and is_dir ( $directory.'/'.$file ) ) |
---|
196 | { |
---|
197 | array_push( $sub_dirs, $file ); |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | return $sub_dirs; |
---|
202 | } |
---|
203 | |
---|
204 | // The get_picture_size function return an array containing : |
---|
205 | // - $picture_size[0] : final width |
---|
206 | // - $picture_size[1] : final height |
---|
207 | // The final dimensions are calculated thanks to the original dimensions and |
---|
208 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
209 | // the width/height ratio |
---|
210 | function get_picture_size( $original_width, $original_height, |
---|
211 | $max_width, $max_height ) |
---|
212 | { |
---|
213 | $width = $original_width; |
---|
214 | $height = $original_height; |
---|
215 | $is_original_size = true; |
---|
216 | |
---|
217 | if ( $max_width != "" ) |
---|
218 | { |
---|
219 | if ( $original_width > $max_width ) |
---|
220 | { |
---|
221 | $width = $max_width; |
---|
222 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
223 | } |
---|
224 | } |
---|
225 | if ( $max_height != "" ) |
---|
226 | { |
---|
227 | if ( $original_height > $max_height ) |
---|
228 | { |
---|
229 | $height = $max_height; |
---|
230 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
231 | $is_original_size = false; |
---|
232 | } |
---|
233 | } |
---|
234 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
235 | and $max_width != 0 and $max_height != 0 ) |
---|
236 | { |
---|
237 | $ratioWidth = $original_width / $max_width; |
---|
238 | $ratioHeight = $original_height / $max_height; |
---|
239 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
240 | { |
---|
241 | if ( $ratioWidth < $ratioHeight ) |
---|
242 | { |
---|
243 | $width = floor( $original_width / $ratioHeight ); |
---|
244 | $height = $max_height; |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | $width = $max_width; |
---|
249 | $height = floor( $original_height / $ratioWidth ); |
---|
250 | } |
---|
251 | $is_original_size = false; |
---|
252 | } |
---|
253 | } |
---|
254 | $picture_size = array(); |
---|
255 | $picture_size[0] = $width; |
---|
256 | $picture_size[1] = $height; |
---|
257 | return $picture_size; |
---|
258 | } |
---|
259 | //-------------------------------------------- PhpWebGallery specific functions |
---|
260 | |
---|
261 | // get_languages retourne un tableau contenant tous les languages |
---|
262 | // disponibles pour PhpWebGallery |
---|
263 | function get_languages( $rep_language ) |
---|
264 | { |
---|
265 | global $lang; |
---|
266 | $languages = array(); |
---|
267 | $i = 0; |
---|
268 | if ( $opendir = opendir ( $rep_language ) ) |
---|
269 | { |
---|
270 | while ( $file = readdir ( $opendir ) ) |
---|
271 | { |
---|
272 | if ( is_dir ( $rep_language.$file )&& !substr_count($file,'.') && isset($lang['lang'][$file])) |
---|
273 | { |
---|
274 | $languages[$i++] =$lang['lang'][$file]; |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | return $languages; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * replaces the $search into <span style="$style">$search</span> in the |
---|
283 | * given $string. |
---|
284 | * |
---|
285 | * case insensitive replacements, does not replace characters in HTML tags |
---|
286 | * |
---|
287 | * @param string $string |
---|
288 | * @param string $search |
---|
289 | * @param string $style |
---|
290 | * @return string |
---|
291 | */ |
---|
292 | function add_style( $string, $search, $style ) |
---|
293 | { |
---|
294 | //return $string; |
---|
295 | $return_string = ''; |
---|
296 | $remaining = $string; |
---|
297 | |
---|
298 | $start = 0; |
---|
299 | $end = 0; |
---|
300 | $start = strpos ( $remaining, '<' ); |
---|
301 | $end = strpos ( $remaining, '>' ); |
---|
302 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
303 | { |
---|
304 | $treatment = substr ( $remaining, 0, $start ); |
---|
305 | $treatment = preg_replace( '/('.$search.')/i', |
---|
306 | '<span style="'.$style.'">\\0</span>', |
---|
307 | $treatment ); |
---|
308 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
309 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
310 | $start = strpos ( $remaining, '<' ); |
---|
311 | $end = strpos ( $remaining, '>' ); |
---|
312 | } |
---|
313 | $treatment = preg_replace( '/('.$search.')/i', |
---|
314 | '<span style="'.$style.'">\\0</span>', |
---|
315 | $remaining ); |
---|
316 | $return_string.= $treatment; |
---|
317 | |
---|
318 | return $return_string; |
---|
319 | } |
---|
320 | |
---|
321 | // replace_search replaces a searched words array string by the search in |
---|
322 | // another style for the given $string. |
---|
323 | function replace_search( $string, $search ) |
---|
324 | { |
---|
325 | $words = explode( ',', $search ); |
---|
326 | $style = 'background-color:white;color:red;'; |
---|
327 | foreach ( $words as $word ) { |
---|
328 | $string = add_style( $string, $word, $style ); |
---|
329 | } |
---|
330 | return $string; |
---|
331 | } |
---|
332 | |
---|
333 | function pwg_log( $file, $category, $picture = '' ) |
---|
334 | { |
---|
335 | global $conf, $user; |
---|
336 | |
---|
337 | if ( $conf['log'] ) |
---|
338 | { |
---|
339 | $query = 'insert into '.PREFIX_TABLE.'history'; |
---|
340 | $query.= ' (date,login,IP,file,category,picture) values'; |
---|
341 | $query.= " (".time().", '".$user['username']."'"; |
---|
342 | $query.= ",'".$_SERVER['REMOTE_ADDR']."'"; |
---|
343 | $query.= ",'".$file."','".$category."','".$picture."');"; |
---|
344 | mysql_query( $query ); |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | function templatize_array( $array, $global_array_name, $handle ) |
---|
349 | { |
---|
350 | global $vtp, $lang, $page, $user, $conf; |
---|
351 | |
---|
352 | foreach ( $array as $value ) { |
---|
353 | if (isset(${$global_array_name}[$value])) |
---|
354 | $vtp->setGlobalVar( $handle, $value, ${$global_array_name}[$value] ); |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | // format_date returns a formatted date for display. The date given in |
---|
359 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
360 | // american format (2003-09-15). By option, you can show the time. The |
---|
361 | // output is internationalized. |
---|
362 | // |
---|
363 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
364 | function format_date( $date, $type = 'us', $show_time = false ) |
---|
365 | { |
---|
366 | global $lang; |
---|
367 | |
---|
368 | switch ( $type ) |
---|
369 | { |
---|
370 | case 'us' : |
---|
371 | list( $year,$month,$day ) = explode( '-', $date ); |
---|
372 | $unixdate = mktime(0,0,0,$month,$day,$year); |
---|
373 | break; |
---|
374 | case 'unix' : |
---|
375 | $unixdate = $date; |
---|
376 | break; |
---|
377 | case 'mysql_datetime' : |
---|
378 | preg_match( '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
379 | $date, $matches ); |
---|
380 | $unixdate = mktime($matches[4],$matches[5],$matches[6], |
---|
381 | $matches[2],$matches[3],$matches[1]); |
---|
382 | break; |
---|
383 | } |
---|
384 | $formated_date = $lang['day'][date( "w", $unixdate )]; |
---|
385 | $formated_date.= date( " j ", $unixdate ); |
---|
386 | $formated_date.= $lang['month'][date( "n", $unixdate )]; |
---|
387 | $formated_date.= date( ' Y', $unixdate ); |
---|
388 | if ( $show_time ) |
---|
389 | { |
---|
390 | $formated_date.= date( ' G:i', $unixdate ); |
---|
391 | } |
---|
392 | |
---|
393 | return $formated_date; |
---|
394 | } |
---|
395 | |
---|
396 | // notify sends a email to every admin of the gallery |
---|
397 | function notify( $type, $infos = '' ) |
---|
398 | { |
---|
399 | global $conf; |
---|
400 | |
---|
401 | $headers = 'From: '.$conf['webmaster'].' <'.$conf['mail_webmaster'].'>'."\n"; |
---|
402 | $headers.= 'Reply-To: '.$conf['mail_webmaster']."\n"; |
---|
403 | $headers.= 'X-Mailer: PhpWebGallery, PHP '.phpversion(); |
---|
404 | |
---|
405 | $options = '-f '.$conf['mail_webmaster']; |
---|
406 | // retrieving all administrators |
---|
407 | $query = 'SELECT username,mail_address,language'; |
---|
408 | $query.= ' FROM '.USERS_TABLE; |
---|
409 | $query.= " WHERE status = 'admin'"; |
---|
410 | $query.= ' AND mail_address IS NOT NULL'; |
---|
411 | $query.= ';'; |
---|
412 | $result = mysql_query( $query ); |
---|
413 | while ( $row = mysql_fetch_array( $result ) ) |
---|
414 | { |
---|
415 | $to = $row['mail_address']; |
---|
416 | include( PHPWG_ROOT_PATH.'language/'.$row['language'].'.php' ); |
---|
417 | $content = $lang['mail_hello']."\n\n"; |
---|
418 | switch ( $type ) |
---|
419 | { |
---|
420 | case 'upload' : |
---|
421 | $subject = $lang['mail_new_upload_subject']; |
---|
422 | $content.= $lang['mail_new_upload_content']; |
---|
423 | break; |
---|
424 | case 'comment' : |
---|
425 | $subject = $lang['mail_new_comment_subject']; |
---|
426 | $content.= $lang['mail_new_comment_content']; |
---|
427 | break; |
---|
428 | } |
---|
429 | $infos = str_replace( ' ', ' ', $infos ); |
---|
430 | $infos = str_replace( '−', '-', $infos ); |
---|
431 | $content.= "\n\n".$infos; |
---|
432 | $content.= "\n\n-- \nPhpWebGallery ".$conf['version']; |
---|
433 | $content = wordwrap( $content, 72 ); |
---|
434 | @mail( $to, $subject, $content, $headers, $options ); |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | function pwg_write_debug() |
---|
439 | { |
---|
440 | global $debug; |
---|
441 | |
---|
442 | $fp = @fopen( './log/debug.log', 'a+' ); |
---|
443 | fwrite( $fp, "\n\n" ); |
---|
444 | fwrite( $fp, $debug ); |
---|
445 | fclose( $fp ); |
---|
446 | } |
---|
447 | |
---|
448 | function pwg_query( $query ) |
---|
449 | { |
---|
450 | global $count_queries,$queries_time; |
---|
451 | |
---|
452 | $start = get_moment(); |
---|
453 | $output = ''; |
---|
454 | |
---|
455 | $count_queries++; |
---|
456 | $output.= '<br /><br />['.$count_queries.'] '.$query; |
---|
457 | $result = mysql_query( $query ); |
---|
458 | $time = get_moment() - $start; |
---|
459 | $queries_time+= $time; |
---|
460 | $output.= '<b>('.number_format( $time, 3, '.', ' ').' s)</b>'; |
---|
461 | $output.= '('.number_format( $queries_time, 3, '.', ' ').' s)'; |
---|
462 | |
---|
463 | // echo $output; |
---|
464 | |
---|
465 | return $result; |
---|
466 | } |
---|
467 | |
---|
468 | function pwg_debug( $string ) |
---|
469 | { |
---|
470 | global $debug,$t2,$count_queries; |
---|
471 | |
---|
472 | $now = explode( ' ', microtime() ); |
---|
473 | $now2 = explode( '.', $now[0] ); |
---|
474 | $now2 = $now[1].'.'.$now2[1]; |
---|
475 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
476 | $debug.= '['.$time.', '; |
---|
477 | $debug.= $count_queries.' queries] : '.$string; |
---|
478 | $debug.= "\n"; |
---|
479 | } |
---|
480 | |
---|
481 | /** |
---|
482 | * Redirects to the given URL |
---|
483 | * |
---|
484 | * Note : once this function called, the execution doesn't go further |
---|
485 | * (presence of an exit() instruction. |
---|
486 | * |
---|
487 | * @param string $url |
---|
488 | * @return void |
---|
489 | */ |
---|
490 | function redirect( $url ) |
---|
491 | { |
---|
492 | global $user, $template, $lang_info, $conf, $lang, $t2; |
---|
493 | |
---|
494 | // $refresh, $url_link and $title are required for creating an automated |
---|
495 | // refresh page in header.tpl |
---|
496 | $refresh = 5; |
---|
497 | $url_link = $url; |
---|
498 | $title = 'redirection'; |
---|
499 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
500 | |
---|
501 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
502 | $template->pparse('redirect'); |
---|
503 | |
---|
504 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
505 | |
---|
506 | exit(); |
---|
507 | } |
---|
508 | ?> |
---|