source: branches/release-1_3/include/functions.inc.php @ 264

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