source: trunk/include/functions.inc.php @ 360

Last change on this file since 360 was 360, checked in by z0rglub, 21 years ago

adding phpdocumentor variables to function get_enums

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