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

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

Using the same name for variables in ./include/mysql.inc.php

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 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 141 2003-09-20 15:04:09Z 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// get_dirs retourne un tableau contenant tous les sous-répertoires d'un
161// répertoire
162function get_dirs( $rep )
163{
164  $sub_rep = array();
165
166  if ( $opendir = opendir ( $rep ) )
167  {
168    while ( $file = readdir ( $opendir ) )
169    {
170      if ( $file != '.' and $file != '..' and is_dir ( $rep.$file ) )
171      {
172        array_push( $sub_rep, $file );
173      }
174    }
175  }
176  return $sub_rep;
177}
178
179// The get_picture_size function return an array containing :
180//      - $picture_size[0] : final width
181//      - $picture_size[1] : final height
182// The final dimensions are calculated thanks to the original dimensions and
183// the maximum dimensions given in parameters.  get_picture_size respects
184// the width/height ratio
185function get_picture_size( $original_width, $original_height,
186                           $max_width, $max_height )
187{
188  $width = $original_width;
189  $height = $original_height;
190  $is_original_size = true;
191               
192  if ( $max_width != "" )
193  {
194    if ( $original_width > $max_width )
195    {
196      $width = $max_width;
197      $height = floor( ( $width * $original_height ) / $original_width );
198    }
199  }
200  if ( $max_height != "" )
201  {
202    if ( $original_height > $max_height )
203    {
204      $height = $max_height;
205      $width = floor( ( $height * $original_width ) / $original_height );
206      $is_original_size = false;
207    }
208  }
209  if ( is_numeric( $max_width ) and is_numeric( $max_height )
210       and $max_width != 0 and $max_height != 0 )
211  {
212    $ratioWidth = $original_width / $max_width;
213    $ratioHeight = $original_height / $max_height;
214    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
215    {
216      if ( $ratioWidth < $ratioHeight )
217      { 
218        $width = floor( $original_width / $ratioHeight );
219        $height = $max_height;
220      }
221      else
222      { 
223        $width = $max_width; 
224        $height = floor( $original_height / $ratioWidth );
225      }
226      $is_original_size = false;
227    }
228  }
229  $picture_size = array();
230  $picture_size[0] = $width;
231  $picture_size[1] = $height;
232  return $picture_size;
233}
234//-------------------------------------------- PhpWebGallery specific functions
235
236// get_languages retourne un tableau contenant tous les languages
237// disponibles pour PhpWebGallery
238function get_languages( $rep_language )
239{
240  $languages = array();
241  $i = 0;
242  if ( $opendir = opendir ( $rep_language ) )
243  {
244    while ( $file = readdir ( $opendir ) )
245    {
246      if ( is_file ( $rep_language.$file )
247           and $file != "index.php"
248           and strrchr ( $file, "." ) == ".php" )
249      {
250        $languages[$i++] =
251          substr ( $file, 0, strlen ( $file )
252                   - strlen ( strrchr ( $file, "." ) ) );
253      }
254    }
255  }
256  return $languages;
257}
258
259// get_themes retourne un tableau contenant tous les "template - couleur"
260function get_themes( $theme_dir )
261{
262  $themes = array();
263  $main_themes = get_dirs( $theme_dir );
264  for ( $i = 0; $i < sizeof( $main_themes ); $i++ )
265  {
266    $colors = get_dirs( $theme_dir.$main_themes[$i].'/' );
267    for ( $j = 0; $j < sizeof( $colors ); $j++ )
268    {
269      array_push( $themes, $main_themes[$i].' - '.$colors[$j] );
270    }
271  }
272  return $themes;
273}
274
275// - add_style replaces the
276//         $search  into <span style="$style">$search</span>
277// in the given $string.
278// - The function does not replace characters in HTML tags
279function add_style( $string, $search, $style )
280{
281  //return $string;
282  $return_string = '';
283  $remaining = $string;
284
285  $start = 0;
286  $end = 0;
287  $start = strpos ( $remaining, '<' );
288  $end   = strpos ( $remaining, '>' );
289  while ( is_numeric( $start ) and is_numeric( $end ) )
290  {
291    $treatment = substr ( $remaining, 0, $start );
292    $treatment = str_replace( $search, '<span style="'.$style.'">'.
293                              $search.'</span>', $treatment );
294    $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 );
295    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
296    $start = strpos ( $remaining, '<' );
297    $end   = strpos ( $remaining, '>' );
298  }
299  $treatment = str_replace( $search, '<span style="'.$style.'">'.
300                            $search.'</span>', $remaining );
301  $return_string.= $treatment;
302               
303  return $return_string;
304}
305
306// replace_search replaces a searched words array string by the search in
307// another style for the given $string.
308function replace_search( $string, $search )
309{
310  $words = explode( ',', $search );
311  $style = 'background-color:white;color:red;';
312  foreach ( $words as $word ) {
313    $string = add_style( $string, $word, $style );
314  }
315  return $string;
316}
317
318function database_connection()
319{
320  include( PREFIX_INCLUDE.'./include/mysql.inc.php' );
321  define( PREFIX_TABLE, $prefixeTable );
322
323  @mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
324    or die ( "Could not connect to server" );
325  @mysql_select_db( $cfgBase )
326    or die ( "Could not connect to database" );
327}
328
329function pwg_log( $file, $category, $picture = '' )
330{
331  global $conf, $user;
332
333  if ( $conf['log'] )
334  {
335    $query = 'insert into '.PREFIX_TABLE.'history';
336    $query.= ' (date,login,IP,file,category,picture) values';
337    $query.= " (".time().", '".$user['username']."'";
338    $query.= ",'".$_SERVER['REMOTE_ADDR']."'";
339    $query.= ",'".$file."','".$category."','".$picture."');";
340    mysql_query( $query );
341  }
342}
343
344function templatize_array( $array, $global_array_name, $handle )
345{
346  global $vtp, $lang, $page, $user, $conf;
347
348  foreach ( $array as $value ) {
349    $vtp->setGlobalVar( $handle, $value, ${$global_array_name}[$value] );
350  }
351}
352
353// format_date returns a formatted date for display. The date given in
354// argument can be a unixdate (number of seconds since the 01.01.1970) or an
355// american format (2003-09-15). By option, you can show the time. The
356// output is internationalized.
357//
358// format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52"
359function format_date( $date, $type = 'us', $show_time = false )
360{
361  global $lang;
362
363  switch ( $type )
364  {
365  case 'us' :
366    list( $year,$month,$day ) = explode( '-', $date );
367    $unixdate = mktime(0,0,0,$month,$day,$year);
368    break;
369  case 'unix' :
370    $unixdate = $date;
371    break;
372  }
373  $formated_date = $lang['day'][date( "w", $unixdate )];
374  $formated_date.= date( " j ", $unixdate );
375  $formated_date.= $lang['month'][date( "n", $unixdate )];
376  $formated_date.= date( ' Y', $unixdate );
377  if ( $show_time )
378  {
379    $formated_date.= date( ' G:i', $unixdate );
380  }
381
382  return $formated_date;
383}
384
385// notify sends a email to every admin of the gallery
386function notify( $type, $infos = '' )
387{
388  global $conf;
389
390  $headers = 'From: '.$conf['webmaster'].' <'.$conf['mail_webmaster'].'>'."\n";
391  $headers.= 'Reply-To: '.$conf['mail_webmaster']."\n";
392  $headers.= 'X-Mailer: PhpWebGallery, PHP '.phpversion();
393
394  $options = '-f '.$conf['mail_webmaster'];
395  // retrieving all administrators
396  $query = 'SELECT username,mail_address,language';
397  $query.= ' FROM '.PREFIX_TABLE.'users';
398  $query.= " WHERE status = 'admin'";
399  $query.= ' AND mail_address IS NOT NULL';
400  $query.= ';';
401  $result = mysql_query( $query );
402  while ( $row = mysql_fetch_array( $result ) )
403  {
404    $to = $row['mail_address'];
405    include( PREFIX_INCLUDE.'./language/'.$row['language'].'.php' );
406    $content = $lang['mail_hello']."\n\n";
407    switch ( $type )
408    {
409    case 'upload' :
410      $subject = $lang['mail_new_upload_subject'];
411      $content.= $lang['mail_new_upload_content'];
412      break;
413    case 'comment' :
414      $subject = $lang['mail_new_comment_subject'];
415      $content.= $lang['mail_new_comment_content'];
416      break;
417    }
418    $infos = str_replace( '&nbsp;',  ' ', $infos );
419    $infos = str_replace( '&minus;', '-', $infos );
420    $content.= "\n\n".$infos;
421    $content.= "\n\n-- \nPhpWebGallery ".$conf['version'];
422    $content = wordwrap( $content, 72 );
423    @mail( $to, $subject, $content, $headers, $options );
424  }
425}
426?>
Note: See TracBrowser for help on using the repository browser.