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 | ***************************************************************************/ |
---|
17 | include( 'functions_user.inc.php' ); |
---|
18 | include( 'functions_session.inc.php' ); |
---|
19 | include( 'functions_category.inc.php' ); |
---|
20 | |
---|
21 | //----------------------------------------------------------- generic functions |
---|
22 | |
---|
23 | // get_enums returns an array containing the possible values of a enum field |
---|
24 | // in a table of the database. |
---|
25 | function get_enums( $table, $field ) |
---|
26 | { |
---|
27 | // retrieving the properties of the table. Each line represents a field : |
---|
28 | // columns are 'Field', 'Type' |
---|
29 | $result=mysql_query("desc $table"); |
---|
30 | while ( $row = mysql_fetch_array( $result ) ) |
---|
31 | { |
---|
32 | // we are only interested in the the field given in parameter for the |
---|
33 | // function |
---|
34 | if ( $row['Field']==$field ) |
---|
35 | { |
---|
36 | // retrieving possible values of the enum field |
---|
37 | // enum('blue','green','black') |
---|
38 | $option = explode( ',', substr($row['Type'], 5, -1 ) ); |
---|
39 | for ( $i = 0; $i < sizeof( $option ); $i++ ) |
---|
40 | { |
---|
41 | // deletion of quotation marks |
---|
42 | $option[$i] = str_replace( "'", '',$option[$i] ); |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | mysql_free_result( $result ); |
---|
47 | return $option; |
---|
48 | } |
---|
49 | |
---|
50 | // get_boolean transforms a string to a boolean value. If the string is |
---|
51 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
52 | // any other case, true is returned. |
---|
53 | function get_boolean( $string ) |
---|
54 | { |
---|
55 | $boolean = true; |
---|
56 | if ( preg_match( '/^false$/i', $string ) ) |
---|
57 | { |
---|
58 | $boolean = false; |
---|
59 | } |
---|
60 | return $boolean; |
---|
61 | } |
---|
62 | |
---|
63 | // array_remove removes a value from the given array if the value existed in |
---|
64 | // this array. |
---|
65 | function array_remove( $array, $value ) |
---|
66 | { |
---|
67 | $i = 0; |
---|
68 | $output = array(); |
---|
69 | foreach ( $array as $v ) |
---|
70 | { |
---|
71 | if ( $v != $value ) |
---|
72 | { |
---|
73 | $output[$i++] = $v; |
---|
74 | } |
---|
75 | } |
---|
76 | return implode( ',', $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 |
---|
82 | function 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. |
---|
92 | function 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 − |
---|
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 | function replace_space( $string ) |
---|
104 | { |
---|
105 | //return $string; |
---|
106 | $return_string = ""; |
---|
107 | $remaining = $string; |
---|
108 | |
---|
109 | $start = 0; |
---|
110 | $end = 0; |
---|
111 | $start = strpos ( $remaining, '<' ); |
---|
112 | $end = strpos ( $remaining, '>' ); |
---|
113 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
114 | { |
---|
115 | $treatment = substr ( $remaining, 0, $start ); |
---|
116 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
117 | $treatment = str_replace( '-', '−', $treatment ); |
---|
118 | $return_string.= $treatment.substr ( $remaining, $start, |
---|
119 | $end - $start + 1 ); |
---|
120 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
121 | $start = strpos ( $remaining, '<' ); |
---|
122 | $end = strpos ( $remaining, '>' ); |
---|
123 | } |
---|
124 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
125 | $treatment = str_replace( '-', '−', $treatment ); |
---|
126 | $return_string.= $treatment; |
---|
127 | |
---|
128 | return $return_string; |
---|
129 | } |
---|
130 | |
---|
131 | // get_dirs retourne un tableau contenant tous les sous-répertoires d'un |
---|
132 | // répertoire |
---|
133 | function get_dirs( $rep ) |
---|
134 | { |
---|
135 | $sub_rep = array(); |
---|
136 | |
---|
137 | if ( $opendir = opendir ( $rep ) ) |
---|
138 | { |
---|
139 | while ( $file = readdir ( $opendir ) ) |
---|
140 | { |
---|
141 | if ( $file != "." and $file != ".." and is_dir ( $rep.$file ) ) |
---|
142 | { |
---|
143 | array_push( $sub_rep, $file ); |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | return $sub_rep; |
---|
148 | } |
---|
149 | |
---|
150 | // The get_picture_size function return an array containing : |
---|
151 | // - $picture_size[0] : final width |
---|
152 | // - $picture_size[1] : final height |
---|
153 | // The final dimensions are calculated thanks to the original dimensions and |
---|
154 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
155 | // the width/height ratio |
---|
156 | function get_picture_size( $original_width, $original_height, |
---|
157 | $max_width, $max_height ) |
---|
158 | { |
---|
159 | $width = $original_width; |
---|
160 | $height = $original_height; |
---|
161 | $is_original_size = true; |
---|
162 | |
---|
163 | if ( $max_width != "" ) |
---|
164 | { |
---|
165 | if ( $original_width > $max_width ) |
---|
166 | { |
---|
167 | $width = $max_width; |
---|
168 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
169 | } |
---|
170 | } |
---|
171 | if ( $max_height != "" ) |
---|
172 | { |
---|
173 | if ( $original_height > $max_height ) |
---|
174 | { |
---|
175 | $height = $max_height; |
---|
176 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
177 | $is_original_size = false; |
---|
178 | } |
---|
179 | } |
---|
180 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
181 | and $max_width != 0 and $max_height != 0 ) |
---|
182 | { |
---|
183 | $ratioWidth = $original_width / $max_width; |
---|
184 | $ratioHeight = $original_height / $max_height; |
---|
185 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
186 | { |
---|
187 | if ( $ratioWidth < $ratioHeight ) |
---|
188 | { |
---|
189 | $width = floor( $original_width / $ratioHeight ); |
---|
190 | $height = $max_height; |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | $width = $max_width; |
---|
195 | $height = floor( $original_height / $ratioWidth ); |
---|
196 | } |
---|
197 | $is_original_size = false; |
---|
198 | } |
---|
199 | } |
---|
200 | $picture_size = array(); |
---|
201 | $picture_size[0] = $width; |
---|
202 | $picture_size[1] = $height; |
---|
203 | return $picture_size; |
---|
204 | } |
---|
205 | |
---|
206 | //-------------------------------------------- PhpWebGallery specific functions |
---|
207 | |
---|
208 | // get_languages retourne un tableau contenant tous les languages |
---|
209 | // disponibles pour PhpWebGallery |
---|
210 | function get_languages( $rep_language ) |
---|
211 | { |
---|
212 | $languages = array(); |
---|
213 | $i = 0; |
---|
214 | if ( $opendir = opendir ( $rep_language ) ) |
---|
215 | { |
---|
216 | while ( $file = readdir ( $opendir ) ) |
---|
217 | { |
---|
218 | if ( is_file ( $rep_language.$file ) |
---|
219 | and $file != "index.php" |
---|
220 | and strrchr ( $file, "." ) == ".php" ) |
---|
221 | { |
---|
222 | $languages[$i++] = |
---|
223 | substr ( $file, 0, strlen ( $file ) |
---|
224 | - strlen ( strrchr ( $file, "." ) ) ); |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | return $languages; |
---|
229 | } |
---|
230 | |
---|
231 | // get_themes retourne un tableau contenant tous les "template - couleur" |
---|
232 | function get_themes( $theme_dir ) |
---|
233 | { |
---|
234 | $themes = array(); |
---|
235 | $main_themes = get_dirs( $theme_dir ); |
---|
236 | for ( $i = 0; $i < sizeof( $main_themes ); $i++ ) |
---|
237 | { |
---|
238 | $colors = get_dirs( $theme_dir.$main_themes[$i].'/' ); |
---|
239 | for ( $j = 0; $j < sizeof( $colors ); $j++ ) |
---|
240 | { |
---|
241 | array_push( $themes, $main_themes[$i].' - '.$colors[$j] ); |
---|
242 | } |
---|
243 | } |
---|
244 | return $themes; |
---|
245 | } |
---|
246 | |
---|
247 | // - The replace_search function replaces a $search string by the search in |
---|
248 | // another color |
---|
249 | // - The function does not replace characters in HTML tags |
---|
250 | function replace_search( $string, $search ) |
---|
251 | { |
---|
252 | //return $string; |
---|
253 | $style_search = "background-color:white;color:red;"; |
---|
254 | $return_string = ""; |
---|
255 | $remaining = $string; |
---|
256 | |
---|
257 | $start = 0; |
---|
258 | $end = 0; |
---|
259 | $start = strpos ( $remaining, '<' ); |
---|
260 | $end = strpos ( $remaining, '>' ); |
---|
261 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
262 | { |
---|
263 | $treatment = substr ( $remaining, 0, $start ); |
---|
264 | $treatment = str_replace( $search, '<span style="'.$style_search.'">'. |
---|
265 | $search.'</span>', $treatment ); |
---|
266 | $return_string.= $treatment.substr ( $remaining, $start, |
---|
267 | $end - $start + 1 ); |
---|
268 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
269 | $start = strpos ( $remaining, '<' ); |
---|
270 | $end = strpos ( $remaining, '>' ); |
---|
271 | } |
---|
272 | $treatment = str_replace( $search, '<span style="'.$style_search.'">'. |
---|
273 | $search.'</span>', $remaining ); |
---|
274 | $return_string.= $treatment; |
---|
275 | |
---|
276 | return $return_string; |
---|
277 | } |
---|
278 | |
---|
279 | function database_connection() |
---|
280 | { |
---|
281 | global $cfgHote,$cfgUser,$cfgPassword,$cfgBase; |
---|
282 | @mysql_connect( $cfgHote, $cfgUser, $cfgPassword ) |
---|
283 | or die ( "Could not connect to server" ); |
---|
284 | @mysql_select_db( $cfgBase ) |
---|
285 | or die ( "Could not connect to database" ); |
---|
286 | } |
---|
287 | |
---|
288 | function pwg_log( $file, $category, $picture = '' ) |
---|
289 | { |
---|
290 | global $conf, $user, $prefixeTable; |
---|
291 | |
---|
292 | if ( $conf['log'] ) |
---|
293 | { |
---|
294 | $query = 'insert into '.$prefixeTable.'history'; |
---|
295 | $query.= ' (date,login,IP,file,category,picture) values'; |
---|
296 | $query.= " (".time().", '".$user['pseudo']."'"; |
---|
297 | $query.= ",'".$_SERVER['REMOTE_ADDR']."'"; |
---|
298 | $query.= ",'".$file."','".$category."','".$picture."');"; |
---|
299 | mysql_query( $query ); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | function templatize_array( $array, $global_array_name, $handle ) |
---|
304 | { |
---|
305 | global $vtp, $lang, $page, $user, $conf; |
---|
306 | |
---|
307 | for( $i = 0; $i < sizeof( $array ); $i++ ) |
---|
308 | { |
---|
309 | $vtp->setGlobalVar( $handle, $array[$i], |
---|
310 | ${$global_array_name}[$array[$i]] ); |
---|
311 | } |
---|
312 | } |
---|
313 | ?> |
---|