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