source: branches/1.3/include/functions.inc.php @ 11558

Last change on this file since 11558 was 559, checked in by z0rglub, 20 years ago

bug 57 : undefined variable isadmin when notifying upload

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 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 559 2004-10-07 21:43:12Z 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/**
264 * replaces the $search into <span style="$style">$search</span> in the
265 * given $string.
266 *
267 * case insensitive replacements, does not replace characters in HTML tags
268 *
269 * @param string $string
270 * @param string $search
271 * @param string $style
272 * @return string
273 */
274function add_style( $string, $search, $style )
275{
276  //return $string;
277  $return_string = '';
278  $remaining = $string;
279
280  $start = 0;
281  $end = 0;
282  $start = strpos ( $remaining, '<' );
283  $end   = strpos ( $remaining, '>' );
284  while ( is_numeric( $start ) and is_numeric( $end ) )
285  {
286    $treatment = substr ( $remaining, 0, $start );
287    $treatment = preg_replace( '/('.$search.')/i',
288                               '<span style="'.$style.'">\\0</span>',
289                               $treatment );
290    $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 );
291    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
292    $start = strpos ( $remaining, '<' );
293    $end   = strpos ( $remaining, '>' );
294  }
295  $treatment = preg_replace( '/('.$search.')/i',
296                             '<span style="'.$style.'">\\0</span>',
297                             $remaining );
298  $return_string.= $treatment;
299               
300  return $return_string;
301}
302
303// replace_search replaces a searched words array string by the search in
304// another style for the given $string.
305function replace_search( $string, $search )
306{
307  $words = explode( ',', $search );
308  $style = 'background-color:white;color:red;';
309  foreach ( $words as $word ) {
310    $string = add_style( $string, $word, $style );
311  }
312  return $string;
313}
314
315function database_connection()
316{
317  include( PREFIX_INCLUDE.'./include/mysql.inc.php' );
318  define( 'PREFIX_TABLE', $prefixeTable );
319
320  @mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
321    or die ( "Could not connect to server" );
322  @mysql_select_db( $cfgBase )
323    or die ( "Could not connect to database" );
324}
325
326function pwg_log( $file, $category, $picture = '' )
327{
328  global $conf, $user;
329
330  if ( $conf['log'] )
331  {
332    $query = 'insert into '.PREFIX_TABLE.'history';
333    $query.= ' (date,login,IP,file,category,picture) values';
334    $query.= " (".time().", '".$user['username']."'";
335    $query.= ",'".$_SERVER['REMOTE_ADDR']."'";
336    $query.= ",'".$file."','".$category."','".$picture."');";
337    mysql_query( $query );
338  }
339}
340
341function templatize_array( $array, $global_array_name, $handle )
342{
343  global $vtp, $lang, $page, $user, $conf;
344
345  foreach ( $array as $value ) {
346  if (isset(${$global_array_name}[$value]))
347    $vtp->setGlobalVar( $handle, $value, ${$global_array_name}[$value] );
348  }
349}
350
351// format_date returns a formatted date for display. The date given in
352// argument can be a unixdate (number of seconds since the 01.01.1970) or an
353// american format (2003-09-15). By option, you can show the time. The
354// output is internationalized.
355//
356// format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52"
357function format_date( $date, $type = 'us', $show_time = false )
358{
359  global $lang;
360
361  switch ( $type )
362  {
363  case 'us' :
364    list( $year,$month,$day ) = explode( '-', $date );
365    $unixdate = mktime(0,0,0,$month,$day,$year);
366    break;
367  case 'unix' :
368    $unixdate = $date;
369    break;
370  }
371  $formated_date = $lang['day'][date( "w", $unixdate )];
372  $formated_date.= date( " j ", $unixdate );
373  $formated_date.= $lang['month'][date( "n", $unixdate )];
374  $formated_date.= date( ' Y', $unixdate );
375  if ( $show_time )
376  {
377    $formated_date.= date( ' G:i', $unixdate );
378  }
379
380  return $formated_date;
381}
382
383// notify sends a email to every admin of the gallery
384function notify( $type, $infos = '' )
385{
386  global $conf;
387
388  $headers = 'From: '.$conf['webmaster'].' <'.$conf['mail_webmaster'].'>'."\n";
389  $headers.= 'Reply-To: '.$conf['mail_webmaster']."\n";
390  $headers.= 'X-Mailer: PhpWebGallery, PHP '.phpversion();
391
392  $options = '-f '.$conf['mail_webmaster'];
393  // retrieving all administrators
394  $query = 'SELECT username,mail_address,language';
395  $query.= ' FROM '.PREFIX_TABLE.'users';
396  $query.= " WHERE status = 'admin'";
397  $query.= ' AND mail_address IS NOT NULL';
398  $query.= ';';
399  $result = mysql_query( $query );
400  while ( $row = mysql_fetch_array( $result ) )
401  {
402    $to = $row['mail_address'];
403    $isadmin = false;
404    include( PREFIX_INCLUDE.'./language/'.$row['language'].'.php' );
405    $content = $lang['mail_hello']."\n\n";
406    switch ( $type )
407    {
408    case 'upload' :
409      $subject = $lang['mail_new_upload_subject'];
410      $content.= $lang['mail_new_upload_content'];
411      break;
412    case 'comment' :
413      $subject = $lang['mail_new_comment_subject'];
414      $content.= $lang['mail_new_comment_content'];
415      break;
416    }
417    $infos = str_replace( '&nbsp;',  ' ', $infos );
418    $infos = str_replace( '&minus;', '-', $infos );
419    $content.= "\n\n".$infos;
420    $content.= "\n\n-- \nPhpWebGallery ".$conf['version'];
421    $content = wordwrap( $content, 72 );
422    @mail( $to, $subject, $content, $headers, $options );
423  }
424}
425
426function pwg_write_debug()
427{
428  global $debug;
429 
430  $fp = @fopen( './log/debug.log', 'a+' );
431  fwrite( $fp, "\n\n" );
432  fwrite( $fp, $debug );
433  fclose( $fp );
434}
435
436function pwg_query( $query )
437{
438  global $count_queries,$queries_time;
439
440  $start = get_moment();
441  $output = '';
442 
443  $count_queries++;
444  $output.= '<br /><br />['.$count_queries.'] '.$query;
445  $result = mysql_query( $query );
446  $time = get_moment() - $start;
447  $queries_time+= $time;
448  $output.= '<b>('.number_format( $time, 3, '.', ' ').' s)</b>';
449  $output.= '('.number_format( $queries_time, 3, '.', ' ').' s)';
450
451  // echo $output;
452 
453  return $result;
454}
455
456function pwg_debug( $string )
457{
458  global $debug,$t2,$count_queries;
459
460  $now = explode( ' ', microtime() );
461  $now2 = explode( '.', $now[0] );
462  $now2 = $now[1].'.'.$now2[1];
463  $time = number_format( $now2 - $t2, 3, '.', ' ').' s';
464  $debug.= '['.$time.', ';
465  $debug.= $count_queries.' queries] : '.$string;
466  $debug.= "\n";
467}
468?>
Note: See TracBrowser for help on using the repository browser.