source: branches/z0rglub/include/functions.inc.php @ 3152

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

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 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' );
20
21//----------------------------------------------------------- generic functions
22
23// The function get_boolean transforms a string to a boolean value. If the
24// string is "false" (case insensitive), then the boolean value false is
25// returned. In any other case, true is returned.
26function get_boolean( $string )
27{
28  $boolean = true;
29  if ( preg_match( '/^false$/i', $string ) )
30  {
31    $boolean = false;
32  }
33  return $boolean;
34}
35
36// The function array_remove removes a value from the given array if the value
37// existed in this array.
38function array_remove( $array, $value )
39{
40  $i = 0;
41  $output = array();
42  foreach ( $array as $v )
43    {
44      if ( $v != $value )
45      {
46        $output[$i++] = $v;
47      }
48    }
49  return implode( ',', $output );
50}
51
52// The function get_moment returns a float value coresponding to the number
53// of seconds since the unix epoch (1st January 1970) and the microseconds
54// are precised : e.g. 1052343429.89276600
55function get_moment()
56{
57  $t1 = explode( " ", microtime() );
58  $t2 = explode( ".", $t1[0] );
59  $t2 = $t1[1].".".$t2[1];
60  return $t2;
61}
62
63// The function get_elapsed_time returns the number of seconds (with 3
64// decimals precision) between the start time and the end time given.
65function get_elapsed_time( $start, $end )
66{
67  return number_format( $end - $start, 3, '.', ' ').' s';
68}
69
70// - The replace_space function replaces space and '-' characters
71//   by their HTML equivalent  &nbsb; and &minus;
72// - The function does not replace characters in HTML tags
73// - This function was created because IE5 does not respect the
74//   CSS "white-space: nowrap;" property unless space and minus
75//   characters are replaced like this function does.
76function replace_space( $string )
77{
78  //return $string;             
79  $return_string = "";
80  $remaining = $string;
81               
82  $start = 0;
83  $end = 0;
84  $start = strpos ( $remaining, "<" );
85  $end = strpos ( $remaining, ">" );
86  while ( is_numeric( $start ) and is_numeric( $end ) )
87  {
88    $treatment = substr ( $remaining, 0, $start );
89    $treatment = str_replace( " ", "&nbsp;", $treatment );
90    $treatment = str_replace( "-", "&minus;", $treatment );
91    $return_string.= $treatment.substr ( $remaining, $start,
92                                         $end - $start + 1 );
93    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
94    $start = strpos ( $remaining, "<" );
95    $end = strpos ( $remaining, ">" );
96  }
97  $treatment = str_replace( " ", "&nbsp;", $remaining );
98  $treatment = str_replace( "-", "&minus;", $treatment );
99  $return_string.= $treatment;
100               
101  return $return_string;
102}
103
104// get_dirs retourne un tableau contenant tous les sous-répertoires d'un
105// répertoire
106function get_dirs( $rep )
107{
108  $sub_rep = array();
109
110  if ( $opendir = opendir ( $rep ) )
111  {
112    while ( $file = readdir ( $opendir ) )
113    {
114      if ( $file != "." and $file != ".." and is_dir ( $rep.$file ) )
115      {
116        array_push( $sub_rep, $file );
117      }
118    }
119  }
120  return $sub_rep;
121}
122
123// The get_picture_size function return an array containing :
124//      - $picture_size[0] : final width
125//      - $picture_size[1] : final height
126// The final dimensions are calculated thanks to the original dimensions and
127// the maximum dimensions given in parameters.  get_picture_size respects
128// the width/height ratio
129function get_picture_size( $original_width, $original_height,
130                           $max_width, $max_height )
131{
132  $width = $original_width;
133  $height = $original_height;
134  $is_original_size = true;
135               
136  if ( $max_width != "" )
137  {
138    if ( $original_width > $max_width )
139    {
140      $width = $max_width;
141      $height = floor( ( $width * $original_height ) / $original_width );
142    }
143  }
144  if ( $max_height != "" )
145  {
146    if ( $original_height > $max_height )
147    {
148      $height = $max_height;
149      $width = floor( ( $height * $original_width ) / $original_height );
150      $is_original_size = false;
151    }
152  }
153  if ( is_numeric( $max_width ) and is_numeric( $max_height )
154       and $max_width != 0 and $max_height != 0 )
155  {
156    $ratioWidth = $original_width / $max_width;
157    $ratioHeight = $original_height / $max_height;
158    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
159    {
160      if ( $ratioWidth < $ratioHeight )
161      { 
162        $width = floor( $original_width / $ratioHeight );
163        $height = $max_height;
164      }
165      else
166      { 
167        $width = $max_width; 
168        $height = floor( $original_height / $ratioWidth );
169      }
170      $is_original_size = false;
171    }
172  }
173  $picture_size = array();
174  $picture_size[0] = $width;
175  $picture_size[1] = $height;
176  return $picture_size;
177}
178
179//-------------------------------------------- PhpWebGallery specific functions
180
181// get_languages retourne un tableau contenant tous les languages
182// disponibles pour PhpWebGallery
183function get_languages( $rep_language )
184{
185  $languages = array();
186  $i = 0;
187  if ( $opendir = opendir ( $rep_language ) )
188  {
189    while ( $file = readdir ( $opendir ) )
190    {
191      if ( is_file ( $rep_language.$file )
192           and $file != "index.php"
193           and strrchr ( $file, "." ) == ".php" )
194      {
195        $languages[$i++] =
196          substr ( $file, 0, strlen ( $file )
197                   - strlen ( strrchr ( $file, "." ) ) );
198      }
199    }
200  }
201  return $languages;
202}
203
204// get_themes retourne un tableau contenant tous les "template - couleur"
205function get_themes( $theme_dir )
206{
207  $themes = array();
208  $main_themes = get_dirs( $theme_dir );
209  for ( $i = 0; $i < sizeof( $main_themes ); $i++ )
210  {
211    $colors = get_dirs( $theme_dir.$main_themes[$i].'/' );
212    for ( $j = 0; $j < sizeof( $colors ); $j++ )
213    {
214      array_push( $themes, $main_themes[$i].' - '.$colors[$j] );
215    }
216  }
217  return $themes;
218}
219
220// - The replace_search function replaces a $search string by the search in
221// another color
222// - The function does not replace characters in HTML tags
223function replace_search( $string, $search )
224{
225  //return $string;
226  $style_search = "background-color:white;color:red;";
227  $return_string = "";
228  $remaining = $string;
229
230  $start = 0;
231  $end = 0;
232  $start = strpos ( $remaining, "<" );
233  $end = strpos ( $remaining, ">" );
234  while ( is_numeric( $start ) and is_numeric( $end ) )
235  {
236    $treatment = substr ( $remaining, 0, $start );
237    $treatment = eregi_replace( $search, "<span style=\"".$style_search."\">".
238                                $search."</span>", $treatment );
239    $return_string.= $treatment.substr ( $remaining, $start,
240                                         $end - $start + 1 );
241    $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) );
242    $start = strpos ( $remaining, "<" );
243    $end = strpos ( $remaining, ">" );
244  }
245  $treatment = eregi_replace( $search, "<span style=\"".$style_search."\">".
246                              $search."</span>", $remaining );
247  $return_string.= $treatment;
248               
249  return $return_string;
250}
251
252function database_connection()
253{
254  global $cfgHote,$cfgUser,$cfgPassword,$cfgBase;
255  @mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
256    or die ( "Could not connect to server" );
257  @mysql_select_db( $cfgBase )
258    or die ( "Could not connect to database" );
259}
260
261function pwg_log( $file, $category, $picture = '' )
262{
263  global $conf, $user, $prefixeTable;
264
265  if ( $conf['log'] )
266  {
267    $query = 'insert into '.$prefixeTable.'history';
268    $query.= ' (date,login,IP,file,category,picture) values';
269    $query.= " (".time().", '".$user['pseudo']."'";
270    $query.= ",'".$_SERVER['REMOTE_ADDR']."'";
271    $query.= ",'".$file."','".$category."','".$picture."');";
272    mysql_query( $query );
273  }
274}
275
276function templatize_array( $array, $global_array_name )
277{
278  global $vtp, $handle, $lang, $page, $user, $conf;
279
280  for( $i = 0; $i < sizeof( $array ); $i++ )
281  {
282    $vtp->setGlobalVar( $handle, $array[$i],
283                        ${$global_array_name}[$array[$i]] );
284  }
285}
286?>
Note: See TracBrowser for help on using the repository browser.