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

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

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1<?php
2/***************************************************************************
3 *                             functions.inc.php                           *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17include( 'functions_user.inc.php' );
18include( 'functions_session.inc.php' );
19include( 'functions_category.inc.php' );
20include( 'functions_xml.inc.php' );
21include( 'functions_group.inc.php' );
22
23//----------------------------------------------------------- generic functions
24
25// get_enums returns an array containing the possible values of a enum field
26// in a table of the database.
27function get_enums( $table, $field )
28{
29  // retrieving the properties of the table. Each line represents a field :
30  // columns are 'Field', 'Type'
31  $result=mysql_query("desc $table");
32  while ( $row = mysql_fetch_array( $result ) ) 
33  {
34    // we are only interested in the the field given in parameter for the
35    // function
36    if ( $row['Field']==$field )
37    {
38      // retrieving possible values of the enum field
39      // enum('blue','green','black')
40      $option = explode( ',', substr($row['Type'], 5, -1 ) );
41      for ( $i = 0; $i < sizeof( $option ); $i++ )
42      {
43        // deletion of quotation marks
44        $option[$i] = str_replace( "'", '',$option[$i] );
45      }                 
46    }
47  }
48  mysql_free_result( $result );
49  return $option;
50}
51
52// get_boolean transforms a string to a boolean value. If the string is
53// "false" (case insensitive), then the boolean value false is returned. In
54// any other case, true is returned.
55function get_boolean( $string )
56{
57  $boolean = true;
58  if ( preg_match( '/^false$/i', $string ) )
59  {
60    $boolean = false;
61  }
62  return $boolean;
63}
64
65// array_remove removes a value from the given array if the value existed in
66// this array.
67function array_remove( $array, $value )
68{
69  $output = array();
70  foreach ( $array as $v ) {
71    if ( $v != $value )
72    {
73      array_push( $output, $v );
74    }
75  }
76  return $output;
77}
78
79// The function get_moment returns a float value coresponding to the number
80// of seconds since the unix epoch (1st January 1970) and the microseconds
81// are precised : e.g. 1052343429.89276600
82function get_moment()
83{
84  $t1 = explode( ' ', microtime() );
85  $t2 = explode( '.', $t1[0] );
86  $t2 = $t1[1].'.'.$t2[1];
87  return $t2;
88}
89
90// The function get_elapsed_time returns the number of seconds (with 3
91// decimals precision) between the start time and the end time given.
92function get_elapsed_time( $start, $end )
93{
94  return number_format( $end - $start, 3, '.', ' ').' s';
95}
96
97// - The replace_space function replaces space and '-' characters
98//   by their HTML equivalent  &nbsb; and &minus;
99// - The function does not replace characters in HTML tags
100// - This function was created because IE5 does not respect the
101//   CSS "white-space: nowrap;" property unless space and minus
102//   characters are replaced like this function does.
103// - Example :
104//                 <div class="foo">My friend</div>
105//               ( 01234567891111111111222222222233 )
106//               (           0123456789012345678901 )
107// becomes :
108//             <div class="foo">My&nbsp;friend</div>
109function replace_space( $string )
110{
111  //return $string;
112  $return_string = '';
113  // $remaining is the rest of the string where to replace spaces characters
114  $remaining = $string;
115  // $start represents the position of the next '<' character
116  // $end   represents the position of the next '>' character
117  $start = 0;
118  $end = 0;
119  $start = strpos ( $remaining, '<' ); // -> 0
120  $end   = strpos ( $remaining, '>' ); // -> 16
121  // as long as a '<' and his friend '>' are found, we loop
122  while ( is_numeric( $start ) and is_numeric( $end ) )
123  {
124    // $treatment is the part of the string to treat
125    // In the first loop of our example, this variable is empty, but in the
126    // second loop, it equals 'My friend'
127    $treatment = substr ( $remaining, 0, $start );
128    // Replacement of ' ' by his equivalent '&nbsp;'
129    $treatment = str_replace( ' ', '&nbsp;', $treatment );
130    $treatment = str_replace( '-', '&minus;', $treatment );
131    // composing the string to return by adding the treated string and the
132    // following HTML tag -> 'My&nbsp;friend</div>'
133    $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 );
134    // the remaining string is deplaced to the part after the '>' of this
135    // loop
136    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
137    $start = strpos ( $remaining, '<' );
138    $end   = strpos ( $remaining, '>' );
139  }
140  $treatment = str_replace( ' ', '&nbsp;', $remaining );
141  $treatment = str_replace( '-', '&minus;', $treatment );
142  $return_string.= $treatment;
143
144  return $return_string;
145}
146
147// get_extension returns the part of the string after the last "."
148function get_extension( $filename )
149{
150  return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
151}
152
153// get_filename_wo_extension returns the part of the string before the last
154// ".".
155// get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar'
156function get_filename_wo_extension( $filename )
157{
158  return substr( $filename, 0, strrpos( $filename, '.' ) );
159}
160
161// get_dirs retourne un tableau contenant tous les sous-répertoires d'un
162// répertoire
163function get_dirs( $rep )
164{
165  $sub_rep = array();
166
167  if ( $opendir = opendir ( $rep ) )
168  {
169    while ( $file = readdir ( $opendir ) )
170    {
171      if ( $file != '.' and $file != '..' and is_dir ( $rep.$file ) )
172      {
173        array_push( $sub_rep, $file );
174      }
175    }
176  }
177  return $sub_rep;
178}
179
180// The get_picture_size function return an array containing :
181//      - $picture_size[0] : final width
182//      - $picture_size[1] : final height
183// The final dimensions are calculated thanks to the original dimensions and
184// the maximum dimensions given in parameters.  get_picture_size respects
185// the width/height ratio
186function get_picture_size( $original_width, $original_height,
187                           $max_width, $max_height )
188{
189  $width = $original_width;
190  $height = $original_height;
191  $is_original_size = true;
192               
193  if ( $max_width != "" )
194  {
195    if ( $original_width > $max_width )
196    {
197      $width = $max_width;
198      $height = floor( ( $width * $original_height ) / $original_width );
199    }
200  }
201  if ( $max_height != "" )
202  {
203    if ( $original_height > $max_height )
204    {
205      $height = $max_height;
206      $width = floor( ( $height * $original_width ) / $original_height );
207      $is_original_size = false;
208    }
209  }
210  if ( is_numeric( $max_width ) and is_numeric( $max_height )
211       and $max_width != 0 and $max_height != 0 )
212  {
213    $ratioWidth = $original_width / $max_width;
214    $ratioHeight = $original_height / $max_height;
215    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
216    {
217      if ( $ratioWidth < $ratioHeight )
218      { 
219        $width = floor( $original_width / $ratioHeight );
220        $height = $max_height;
221      }
222      else
223      { 
224        $width = $max_width; 
225        $height = floor( $original_height / $ratioWidth );
226      }
227      $is_original_size = false;
228    }
229  }
230  $picture_size = array();
231  $picture_size[0] = $width;
232  $picture_size[1] = $height;
233  return $picture_size;
234}
235//-------------------------------------------- PhpWebGallery specific functions
236
237// get_languages retourne un tableau contenant tous les languages
238// disponibles pour PhpWebGallery
239function get_languages( $rep_language )
240{
241  $languages = array();
242  $i = 0;
243  if ( $opendir = opendir ( $rep_language ) )
244  {
245    while ( $file = readdir ( $opendir ) )
246    {
247      if ( is_file ( $rep_language.$file )
248           and $file != "index.php"
249           and strrchr ( $file, "." ) == ".php" )
250      {
251        $languages[$i++] =
252          substr ( $file, 0, strlen ( $file )
253                   - strlen ( strrchr ( $file, "." ) ) );
254      }
255    }
256  }
257  return $languages;
258}
259
260// get_themes retourne un tableau contenant tous les "template - couleur"
261function get_themes( $theme_dir )
262{
263  $themes = array();
264  $main_themes = get_dirs( $theme_dir );
265  for ( $i = 0; $i < sizeof( $main_themes ); $i++ )
266  {
267    $colors = get_dirs( $theme_dir.$main_themes[$i].'/' );
268    for ( $j = 0; $j < sizeof( $colors ); $j++ )
269    {
270      array_push( $themes, $main_themes[$i].' - '.$colors[$j] );
271    }
272  }
273  return $themes;
274}
275
276// - add_style replaces the
277//         $search  into <span style="$style">$search</span>
278// in the given $string.
279// - The function does not replace characters in HTML tags
280function add_style( $string, $search, $style )
281{
282  //return $string;
283  $return_string = '';
284  $remaining = $string;
285
286  $start = 0;
287  $end = 0;
288  $start = strpos ( $remaining, '<' );
289  $end   = strpos ( $remaining, '>' );
290  while ( is_numeric( $start ) and is_numeric( $end ) )
291  {
292    $treatment = substr ( $remaining, 0, $start );
293    $treatment = str_replace( $search, '<span style="'.$style.'">'.
294                              $search.'</span>', $treatment );
295    $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 );
296    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
297    $start = strpos ( $remaining, '<' );
298    $end   = strpos ( $remaining, '>' );
299  }
300  $treatment = str_replace( $search, '<span style="'.$style.'">'.
301                            $search.'</span>', $remaining );
302  $return_string.= $treatment;
303               
304  return $return_string;
305}
306
307// replace_search replaces a searched words array string by the search in
308// another style for the given $string.
309function replace_search( $string, $search )
310{
311  $words = explode( ',', $search );
312  $style = 'background-color:white;color:red;';
313  foreach ( $words as $word ) {
314    $string = add_style( $string, $word, $style );
315  }
316  return $string;
317}
318
319function database_connection()
320{
321  include( PREFIX_INCLUDE.'./include/mysql.inc.php' );
322  define( PREFIX_TABLE, $prefix_table );
323
324  @mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
325    or die ( "Could not connect to server" );
326  @mysql_select_db( $cfgBase )
327    or die ( "Could not connect to database" );
328}
329
330function pwg_log( $file, $category, $picture = '' )
331{
332  global $conf, $user;
333
334  if ( $conf['log'] )
335  {
336    $query = 'insert into '.PREFIX_TABLE.'history';
337    $query.= ' (date,login,IP,file,category,picture) values';
338    $query.= " (".time().", '".$user['username']."'";
339    $query.= ",'".$_SERVER['REMOTE_ADDR']."'";
340    $query.= ",'".$file."','".$category."','".$picture."');";
341    mysql_query( $query );
342  }
343}
344
345function templatize_array( $array, $global_array_name, $handle )
346{
347  global $vtp, $lang, $page, $user, $conf;
348
349  foreach ( $array as $value ) {
350    $vtp->setGlobalVar( $handle, $value, ${$global_array_name}[$value] );
351  }
352}
353?>
Note: See TracBrowser for help on using the repository browser.