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

Last change on this file since 21 was 21, 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.8 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  $i = 0;
70  $output = array();
71  foreach ( $array as $v )
72    {
73      if ( $v != $value )
74      {
75        $output[$i++] = $v;
76      }
77    }
78  return implode( ',', $output );
79}
80
81// The function get_moment returns a float value coresponding to the number
82// of seconds since the unix epoch (1st January 1970) and the microseconds
83// are precised : e.g. 1052343429.89276600
84function get_moment()
85{
86  $t1 = explode( ' ', microtime() );
87  $t2 = explode( '.', $t1[0] );
88  $t2 = $t1[1].'.'.$t2[1];
89  return $t2;
90}
91
92// The function get_elapsed_time returns the number of seconds (with 3
93// decimals precision) between the start time and the end time given.
94function get_elapsed_time( $start, $end )
95{
96  return number_format( $end - $start, 3, '.', ' ').' s';
97}
98
99// - The replace_space function replaces space and '-' characters
100//   by their HTML equivalent  &nbsb; and &minus;
101// - The function does not replace characters in HTML tags
102// - This function was created because IE5 does not respect the
103//   CSS "white-space: nowrap;" property unless space and minus
104//   characters are replaced like this function does.
105// - Example :
106//                 <div class="foo">My friend</div>
107//               ( 01234567891111111111222222222233 )
108//               (           0123456789012345678901 )
109// becomes :
110//             <div class="foo">My&nbsp;friend</div>
111function replace_space( $string )
112{
113  //return $string;
114  $return_string = '';
115  // $remaining is the rest of the string where to replace spaces characters
116  $remaining = $string;
117  // $start represents the position of the next '<' character
118  // $end   represents the position of the next '>' character
119  $start = 0;
120  $end = 0;
121  $start = strpos ( $remaining, '<' ); // -> 0
122  $end   = strpos ( $remaining, '>' ); // -> 16
123  // as long as a '<' and his friend '>' are found, we loop
124  while ( is_numeric( $start ) and is_numeric( $end ) )
125  {
126    // $treatment is the part of the string to treat
127    // In the first loop of our example, this variable is empty, but in the
128    // second loop, it equals 'My friend'
129    $treatment = substr ( $remaining, 0, $start );
130    // Replacement of ' ' by his equivalent '&nbsp;'
131    $treatment = str_replace( ' ', '&nbsp;', $treatment );
132    $treatment = str_replace( '-', '&minus;', $treatment );
133    // composing the string to return by adding the treated string and the
134    // following HTML tag -> 'My&nbsp;friend</div>'
135    $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 );
136    // the remaining string is deplaced to the part after the '>' of this
137    // loop
138    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
139    $start = strpos ( $remaining, '<' );
140    $end   = strpos ( $remaining, '>' );
141  }
142  $treatment = str_replace( ' ', '&nbsp;', $remaining );
143  $treatment = str_replace( '-', '&minus;', $treatment );
144  $return_string.= $treatment;
145
146  return $return_string;
147}
148
149// get_extension returns the part of the string after the last "."
150function get_extension( $filename )
151{
152  return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
153}
154
155// get_filename_wo_extension returns the part of the string before the last
156// ".".
157// get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar'
158function get_filename_wo_extension( $filename )
159{
160  return substr( $filename, 0, strrpos( $filename, '.' ) );
161}
162
163// get_dirs retourne un tableau contenant tous les sous-répertoires d'un
164// répertoire
165function get_dirs( $rep )
166{
167  $sub_rep = array();
168
169  if ( $opendir = opendir ( $rep ) )
170  {
171    while ( $file = readdir ( $opendir ) )
172    {
173      if ( $file != '.' and $file != '..' and is_dir ( $rep.$file ) )
174      {
175        array_push( $sub_rep, $file );
176      }
177    }
178  }
179  return $sub_rep;
180}
181
182// The get_picture_size function return an array containing :
183//      - $picture_size[0] : final width
184//      - $picture_size[1] : final height
185// The final dimensions are calculated thanks to the original dimensions and
186// the maximum dimensions given in parameters.  get_picture_size respects
187// the width/height ratio
188function get_picture_size( $original_width, $original_height,
189                           $max_width, $max_height )
190{
191  $width = $original_width;
192  $height = $original_height;
193  $is_original_size = true;
194               
195  if ( $max_width != "" )
196  {
197    if ( $original_width > $max_width )
198    {
199      $width = $max_width;
200      $height = floor( ( $width * $original_height ) / $original_width );
201    }
202  }
203  if ( $max_height != "" )
204  {
205    if ( $original_height > $max_height )
206    {
207      $height = $max_height;
208      $width = floor( ( $height * $original_width ) / $original_height );
209      $is_original_size = false;
210    }
211  }
212  if ( is_numeric( $max_width ) and is_numeric( $max_height )
213       and $max_width != 0 and $max_height != 0 )
214  {
215    $ratioWidth = $original_width / $max_width;
216    $ratioHeight = $original_height / $max_height;
217    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
218    {
219      if ( $ratioWidth < $ratioHeight )
220      { 
221        $width = floor( $original_width / $ratioHeight );
222        $height = $max_height;
223      }
224      else
225      { 
226        $width = $max_width; 
227        $height = floor( $original_height / $ratioWidth );
228      }
229      $is_original_size = false;
230    }
231  }
232  $picture_size = array();
233  $picture_size[0] = $width;
234  $picture_size[1] = $height;
235  return $picture_size;
236}
237//-------------------------------------------- PhpWebGallery specific functions
238
239// get_languages retourne un tableau contenant tous les languages
240// disponibles pour PhpWebGallery
241function get_languages( $rep_language )
242{
243  $languages = array();
244  $i = 0;
245  if ( $opendir = opendir ( $rep_language ) )
246  {
247    while ( $file = readdir ( $opendir ) )
248    {
249      if ( is_file ( $rep_language.$file )
250           and $file != "index.php"
251           and strrchr ( $file, "." ) == ".php" )
252      {
253        $languages[$i++] =
254          substr ( $file, 0, strlen ( $file )
255                   - strlen ( strrchr ( $file, "." ) ) );
256      }
257    }
258  }
259  return $languages;
260}
261
262// get_themes retourne un tableau contenant tous les "template - couleur"
263function get_themes( $theme_dir )
264{
265  $themes = array();
266  $main_themes = get_dirs( $theme_dir );
267  for ( $i = 0; $i < sizeof( $main_themes ); $i++ )
268  {
269    $colors = get_dirs( $theme_dir.$main_themes[$i].'/' );
270    for ( $j = 0; $j < sizeof( $colors ); $j++ )
271    {
272      array_push( $themes, $main_themes[$i].' - '.$colors[$j] );
273    }
274  }
275  return $themes;
276}
277
278// - add_style replaces the
279//         $search  into <span style="$style">$search</span>
280// in the given $string.
281// - The function does not replace characters in HTML tags
282function add_style( $string, $search, $style )
283{
284  //return $string;
285  $return_string = '';
286  $remaining = $string;
287
288  $start = 0;
289  $end = 0;
290  $start = strpos ( $remaining, '<' );
291  $end   = strpos ( $remaining, '>' );
292  while ( is_numeric( $start ) and is_numeric( $end ) )
293  {
294    $treatment = substr ( $remaining, 0, $start );
295    $treatment = str_replace( $search, '<span style="'.$style.'">'.
296                              $search.'</span>', $treatment );
297    $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 );
298    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
299    $start = strpos ( $remaining, '<' );
300    $end   = strpos ( $remaining, '>' );
301  }
302  $treatment = str_replace( $search, '<span style="'.$style.'">'.
303                            $search.'</span>', $remaining );
304  $return_string.= $treatment;
305               
306  return $return_string;
307}
308
309// replace_search replaces a searched words array string by the search in
310// another style for the given $string.
311function replace_search( $string, $search )
312{
313  $words = explode( ',', $search );
314  $style = 'background-color:white;color:red;';
315  foreach ( $words as $word ) {
316    $string = add_style( $string, $word, $style );
317  }
318  return $string;
319}
320
321function database_connection()
322{
323//   $xml_content = getXmlCode( PREFIXE_INCLUDE.'./include/database_config.xml' );
324//   $mysql_conf = getChild( $xml_content, 'mysql' );
325
326//   $cfgHote     = getAttribute( $mysql_conf, 'host' );
327//   $cfgUser     = getAttribute( $mysql_conf, 'user' );
328//   $cfgPassword = getAttribute( $mysql_conf, 'password' );
329//   $cfgBase     = getAttribute( $mysql_conf, 'base' );
330//   define( PREFIX_TABLE, getAttribute( $mysql_conf, 'tablePrefix' ) );
331
332  include( PREFIX_INCLUDE.'./include/mysql.inc.php' );
333  define( PREFIX_TABLE, $prefix_table );
334
335  @mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
336    or die ( "Could not connect to server" );
337  @mysql_select_db( $cfgBase )
338    or die ( "Could not connect to database" );
339}
340
341function pwg_log( $file, $category, $picture = '' )
342{
343  global $conf, $user;
344
345  if ( $conf['log'] )
346  {
347    $query = 'insert into '.PREFIX_TABLE.'history';
348    $query.= ' (date,login,IP,file,category,picture) values';
349    $query.= " (".time().", '".$user['pseudo']."'";
350    $query.= ",'".$_SERVER['REMOTE_ADDR']."'";
351    $query.= ",'".$file."','".$category."','".$picture."');";
352    mysql_query( $query );
353  }
354}
355
356function templatize_array( $array, $global_array_name, $handle )
357{
358  global $vtp, $lang, $page, $user, $conf;
359
360  for( $i = 0; $i < sizeof( $array ); $i++ )
361  {
362    $vtp->setGlobalVar( $handle, $array[$i],
363                        ${$global_array_name}[$array[$i]] );
364  }
365}
366?>
Note: See TracBrowser for help on using the repository browser.