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

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