[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1109] | 5 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[1113] | 8 | // | file : $Id: functions.inc.php 1510 2006-07-28 08:36:38Z nikrou $ |
---|
[362] | 9 | // | last update : $Date: 2006-07-28 08:36:38 +0000 (Fri, 28 Jul 2006) $ |
---|
| 10 | // | last modifier : $Author: nikrou $ |
---|
| 11 | // | revision : $Revision: 1510 $ |
---|
| 12 | // +-----------------------------------------------------------------------+ |
---|
| 13 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 14 | // | it under the terms of the GNU General Public License as published by | |
---|
| 15 | // | the Free Software Foundation | |
---|
| 16 | // | | |
---|
| 17 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 20 | // | General Public License for more details. | |
---|
| 21 | // | | |
---|
| 22 | // | You should have received a copy of the GNU General Public License | |
---|
| 23 | // | along with this program; if not, write to the Free Software | |
---|
| 24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 25 | // | USA. | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
[405] | 27 | |
---|
[394] | 28 | include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' ); |
---|
| 29 | include_once( PHPWG_ROOT_PATH .'include/functions_session.inc.php' ); |
---|
| 30 | include_once( PHPWG_ROOT_PATH .'include/functions_category.inc.php' ); |
---|
| 31 | include_once( PHPWG_ROOT_PATH .'include/functions_xml.inc.php' ); |
---|
| 32 | include_once( PHPWG_ROOT_PATH .'include/functions_group.inc.php' ); |
---|
[477] | 33 | include_once( PHPWG_ROOT_PATH .'include/functions_html.inc.php' ); |
---|
[1119] | 34 | include_once( PHPWG_ROOT_PATH .'include/functions_tag.inc.php' ); |
---|
[1109] | 35 | include_once( PHPWG_ROOT_PATH .'include/functions_url.inc.php' ); |
---|
[2] | 36 | |
---|
| 37 | //----------------------------------------------------------- generic functions |
---|
| 38 | |
---|
[777] | 39 | /** |
---|
| 40 | * returns an array containing the possible values of an enum field |
---|
| 41 | * |
---|
| 42 | * @param string tablename |
---|
| 43 | * @param string fieldname |
---|
| 44 | */ |
---|
| 45 | function get_enums($table, $field) |
---|
| 46 | { |
---|
| 47 | // retrieving the properties of the table. Each line represents a field : |
---|
| 48 | // columns are 'Field', 'Type' |
---|
| 49 | $result = pwg_query('desc '.$table); |
---|
| 50 | while ($row = mysql_fetch_array($result)) |
---|
| 51 | { |
---|
| 52 | // we are only interested in the the field given in parameter for the |
---|
| 53 | // function |
---|
| 54 | if ($row['Field'] == $field) |
---|
| 55 | { |
---|
| 56 | // retrieving possible values of the enum field |
---|
| 57 | // enum('blue','green','black') |
---|
| 58 | $options = explode(',', substr($row['Type'], 5, -1)); |
---|
| 59 | foreach ($options as $i => $option) |
---|
| 60 | { |
---|
| 61 | $options[$i] = str_replace("'", '',$option); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | mysql_free_result($result); |
---|
| 66 | return $options; |
---|
| 67 | } |
---|
| 68 | |
---|
[6] | 69 | // get_boolean transforms a string to a boolean value. If the string is |
---|
| 70 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
| 71 | // any other case, true is returned. |
---|
[2] | 72 | function get_boolean( $string ) |
---|
| 73 | { |
---|
| 74 | $boolean = true; |
---|
| 75 | if ( preg_match( '/^false$/i', $string ) ) |
---|
| 76 | { |
---|
| 77 | $boolean = false; |
---|
| 78 | } |
---|
| 79 | return $boolean; |
---|
| 80 | } |
---|
| 81 | |
---|
[661] | 82 | /** |
---|
| 83 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
| 84 | * |
---|
| 85 | * @param mixed $var |
---|
| 86 | * @return mixed |
---|
| 87 | */ |
---|
| 88 | function boolean_to_string($var) |
---|
| 89 | { |
---|
| 90 | if (is_bool($var)) |
---|
| 91 | { |
---|
| 92 | if ($var) |
---|
| 93 | { |
---|
| 94 | return 'true'; |
---|
| 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | return 'false'; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | return $var; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
[2] | 107 | // The function get_moment returns a float value coresponding to the number |
---|
| 108 | // of seconds since the unix epoch (1st January 1970) and the microseconds |
---|
| 109 | // are precised : e.g. 1052343429.89276600 |
---|
| 110 | function get_moment() |
---|
| 111 | { |
---|
[9] | 112 | $t1 = explode( ' ', microtime() ); |
---|
| 113 | $t2 = explode( '.', $t1[0] ); |
---|
| 114 | $t2 = $t1[1].'.'.$t2[1]; |
---|
[2] | 115 | return $t2; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // The function get_elapsed_time returns the number of seconds (with 3 |
---|
| 119 | // decimals precision) between the start time and the end time given. |
---|
| 120 | function get_elapsed_time( $start, $end ) |
---|
| 121 | { |
---|
| 122 | return number_format( $end - $start, 3, '.', ' ').' s'; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // - The replace_space function replaces space and '-' characters |
---|
| 126 | // by their HTML equivalent &nbsb; and − |
---|
| 127 | // - The function does not replace characters in HTML tags |
---|
| 128 | // - This function was created because IE5 does not respect the |
---|
| 129 | // CSS "white-space: nowrap;" property unless space and minus |
---|
| 130 | // characters are replaced like this function does. |
---|
[15] | 131 | // - Example : |
---|
| 132 | // <div class="foo">My friend</div> |
---|
| 133 | // ( 01234567891111111111222222222233 ) |
---|
| 134 | // ( 0123456789012345678901 ) |
---|
| 135 | // becomes : |
---|
| 136 | // <div class="foo">My friend</div> |
---|
[2] | 137 | function replace_space( $string ) |
---|
| 138 | { |
---|
[15] | 139 | //return $string; |
---|
| 140 | $return_string = ''; |
---|
| 141 | // $remaining is the rest of the string where to replace spaces characters |
---|
[2] | 142 | $remaining = $string; |
---|
[15] | 143 | // $start represents the position of the next '<' character |
---|
| 144 | // $end represents the position of the next '>' character |
---|
[2] | 145 | $start = 0; |
---|
| 146 | $end = 0; |
---|
[15] | 147 | $start = strpos ( $remaining, '<' ); // -> 0 |
---|
| 148 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
| 149 | // as long as a '<' and his friend '>' are found, we loop |
---|
[2] | 150 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
| 151 | { |
---|
[15] | 152 | // $treatment is the part of the string to treat |
---|
| 153 | // In the first loop of our example, this variable is empty, but in the |
---|
| 154 | // second loop, it equals 'My friend' |
---|
[2] | 155 | $treatment = substr ( $remaining, 0, $start ); |
---|
[15] | 156 | // Replacement of ' ' by his equivalent ' ' |
---|
[6] | 157 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
| 158 | $treatment = str_replace( '-', '−', $treatment ); |
---|
[15] | 159 | // composing the string to return by adding the treated string and the |
---|
| 160 | // following HTML tag -> 'My friend</div>' |
---|
| 161 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
| 162 | // the remaining string is deplaced to the part after the '>' of this |
---|
| 163 | // loop |
---|
[2] | 164 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
[6] | 165 | $start = strpos ( $remaining, '<' ); |
---|
| 166 | $end = strpos ( $remaining, '>' ); |
---|
[2] | 167 | } |
---|
[6] | 168 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
| 169 | $treatment = str_replace( '-', '−', $treatment ); |
---|
[2] | 170 | $return_string.= $treatment; |
---|
[15] | 171 | |
---|
[2] | 172 | return $return_string; |
---|
| 173 | } |
---|
| 174 | |
---|
[13] | 175 | // get_extension returns the part of the string after the last "." |
---|
| 176 | function get_extension( $filename ) |
---|
| 177 | { |
---|
| 178 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | // get_filename_wo_extension returns the part of the string before the last |
---|
| 182 | // ".". |
---|
| 183 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
| 184 | function get_filename_wo_extension( $filename ) |
---|
| 185 | { |
---|
| 186 | return substr( $filename, 0, strrpos( $filename, '.' ) ); |
---|
| 187 | } |
---|
| 188 | |
---|
[345] | 189 | /** |
---|
[512] | 190 | * returns an array contening sub-directories, excluding "CVS" |
---|
[345] | 191 | * |
---|
| 192 | * @param string $dir |
---|
| 193 | * @return array |
---|
| 194 | */ |
---|
[512] | 195 | function get_dirs($directory) |
---|
[2] | 196 | { |
---|
[345] | 197 | $sub_dirs = array(); |
---|
[2] | 198 | |
---|
[512] | 199 | if ($opendir = opendir($directory)) |
---|
[2] | 200 | { |
---|
[512] | 201 | while ($file = readdir($opendir)) |
---|
[2] | 202 | { |
---|
[512] | 203 | if ($file != '.' |
---|
| 204 | and $file != '..' |
---|
| 205 | and is_dir($directory.'/'.$file) |
---|
[1035] | 206 | and $file != 'CVS' |
---|
[1092] | 207 | and $file != '.svn') |
---|
[2] | 208 | { |
---|
[512] | 209 | array_push($sub_dirs, $file); |
---|
[2] | 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
[345] | 213 | return $sub_dirs; |
---|
[2] | 214 | } |
---|
| 215 | |
---|
| 216 | // The get_picture_size function return an array containing : |
---|
| 217 | // - $picture_size[0] : final width |
---|
| 218 | // - $picture_size[1] : final height |
---|
| 219 | // The final dimensions are calculated thanks to the original dimensions and |
---|
| 220 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
| 221 | // the width/height ratio |
---|
| 222 | function get_picture_size( $original_width, $original_height, |
---|
| 223 | $max_width, $max_height ) |
---|
| 224 | { |
---|
| 225 | $width = $original_width; |
---|
| 226 | $height = $original_height; |
---|
| 227 | $is_original_size = true; |
---|
[1086] | 228 | |
---|
[2] | 229 | if ( $max_width != "" ) |
---|
| 230 | { |
---|
| 231 | if ( $original_width > $max_width ) |
---|
| 232 | { |
---|
| 233 | $width = $max_width; |
---|
| 234 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | if ( $max_height != "" ) |
---|
| 238 | { |
---|
| 239 | if ( $original_height > $max_height ) |
---|
| 240 | { |
---|
| 241 | $height = $max_height; |
---|
| 242 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
| 243 | $is_original_size = false; |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
| 247 | and $max_width != 0 and $max_height != 0 ) |
---|
| 248 | { |
---|
| 249 | $ratioWidth = $original_width / $max_width; |
---|
| 250 | $ratioHeight = $original_height / $max_height; |
---|
| 251 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
| 252 | { |
---|
| 253 | if ( $ratioWidth < $ratioHeight ) |
---|
[1086] | 254 | { |
---|
[2] | 255 | $width = floor( $original_width / $ratioHeight ); |
---|
| 256 | $height = $max_height; |
---|
| 257 | } |
---|
| 258 | else |
---|
[1086] | 259 | { |
---|
| 260 | $width = $max_width; |
---|
[2] | 261 | $height = floor( $original_height / $ratioWidth ); |
---|
| 262 | } |
---|
| 263 | $is_original_size = false; |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | $picture_size = array(); |
---|
| 267 | $picture_size[0] = $width; |
---|
| 268 | $picture_size[1] = $height; |
---|
| 269 | return $picture_size; |
---|
| 270 | } |
---|
[1119] | 271 | |
---|
| 272 | /** |
---|
| 273 | * simplify a string to insert it into an URL |
---|
| 274 | * |
---|
| 275 | * based on str2url function from Dotclear |
---|
| 276 | * |
---|
| 277 | * @param string |
---|
| 278 | * @return string |
---|
| 279 | */ |
---|
| 280 | function str2url($str) |
---|
| 281 | { |
---|
| 282 | $str = strtr( |
---|
| 283 | $str, |
---|
| 284 | 'ÀÁÂÃÄÅàáâãäåÇçÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü¾ÝÿýÑñ', |
---|
| 285 | 'AAAAAAaaaaaaCcOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuYYyyNn' |
---|
| 286 | ); |
---|
| 287 | |
---|
| 288 | $str = str_replace('Æ', 'AE', $str); |
---|
| 289 | $str = str_replace('æ', 'ae', $str); |
---|
| 290 | $str = str_replace('¼', 'OE', $str); |
---|
| 291 | $str = str_replace('½', 'oe', $str); |
---|
| 292 | |
---|
[1131] | 293 | $str = preg_replace('/[^a-z0-9_\s\'\:\/\[\],-]/','',strtolower($str)); |
---|
| 294 | $str = preg_replace('/[\s\'\:\/\[\],-]+/',' ',trim($str)); |
---|
[1119] | 295 | $res = str_replace(' ','_',$str); |
---|
[1131] | 296 | |
---|
[1119] | 297 | return $res; |
---|
| 298 | } |
---|
| 299 | |
---|
[2] | 300 | //-------------------------------------------- PhpWebGallery specific functions |
---|
| 301 | |
---|
[512] | 302 | /** |
---|
| 303 | * returns an array with a list of {language_code => language_name} |
---|
| 304 | * |
---|
| 305 | * @returns array |
---|
| 306 | */ |
---|
| 307 | function get_languages() |
---|
[2] | 308 | { |
---|
[512] | 309 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
[2] | 310 | $languages = array(); |
---|
[512] | 311 | |
---|
| 312 | while ($file = readdir($dir)) |
---|
[2] | 313 | { |
---|
[749] | 314 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
[512] | 315 | if (is_dir($path) and !is_link($path) and file_exists($path.'/iso.txt')) |
---|
[2] | 316 | { |
---|
[512] | 317 | list($language_name) = @file($path.'/iso.txt'); |
---|
[528] | 318 | $languages[$file] = $language_name; |
---|
[2] | 319 | } |
---|
| 320 | } |
---|
[512] | 321 | closedir($dir); |
---|
| 322 | @asort($languages); |
---|
| 323 | @reset($languages); |
---|
| 324 | |
---|
[2] | 325 | return $languages; |
---|
| 326 | } |
---|
| 327 | |
---|
[409] | 328 | /** |
---|
| 329 | * replaces the $search into <span style="$style">$search</span> in the |
---|
| 330 | * given $string. |
---|
| 331 | * |
---|
| 332 | * case insensitive replacements, does not replace characters in HTML tags |
---|
| 333 | * |
---|
| 334 | * @param string $string |
---|
| 335 | * @param string $search |
---|
| 336 | * @param string $style |
---|
| 337 | * @return string |
---|
| 338 | */ |
---|
[17] | 339 | function add_style( $string, $search, $style ) |
---|
[2] | 340 | { |
---|
| 341 | //return $string; |
---|
[17] | 342 | $return_string = ''; |
---|
[2] | 343 | $remaining = $string; |
---|
| 344 | |
---|
| 345 | $start = 0; |
---|
| 346 | $end = 0; |
---|
[6] | 347 | $start = strpos ( $remaining, '<' ); |
---|
| 348 | $end = strpos ( $remaining, '>' ); |
---|
[2] | 349 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
| 350 | { |
---|
| 351 | $treatment = substr ( $remaining, 0, $start ); |
---|
[409] | 352 | $treatment = preg_replace( '/('.$search.')/i', |
---|
| 353 | '<span style="'.$style.'">\\0</span>', |
---|
| 354 | $treatment ); |
---|
[17] | 355 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
[2] | 356 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
[6] | 357 | $start = strpos ( $remaining, '<' ); |
---|
| 358 | $end = strpos ( $remaining, '>' ); |
---|
[2] | 359 | } |
---|
[409] | 360 | $treatment = preg_replace( '/('.$search.')/i', |
---|
| 361 | '<span style="'.$style.'">\\0</span>', |
---|
| 362 | $remaining ); |
---|
[2] | 363 | $return_string.= $treatment; |
---|
[1086] | 364 | |
---|
[2] | 365 | return $return_string; |
---|
| 366 | } |
---|
| 367 | |
---|
[17] | 368 | // replace_search replaces a searched words array string by the search in |
---|
| 369 | // another style for the given $string. |
---|
| 370 | function replace_search( $string, $search ) |
---|
| 371 | { |
---|
[717] | 372 | // FIXME : with new advanced search, this function needs a rewrite |
---|
| 373 | return $string; |
---|
[1086] | 374 | |
---|
[17] | 375 | $words = explode( ',', $search ); |
---|
| 376 | $style = 'background-color:white;color:red;'; |
---|
| 377 | foreach ( $words as $word ) { |
---|
| 378 | $string = add_style( $string, $word, $style ); |
---|
| 379 | } |
---|
| 380 | return $string; |
---|
| 381 | } |
---|
| 382 | |
---|
[2] | 383 | function pwg_log( $file, $category, $picture = '' ) |
---|
| 384 | { |
---|
[13] | 385 | global $conf, $user; |
---|
[2] | 386 | |
---|
[725] | 387 | if ($conf['log']) |
---|
[2] | 388 | { |
---|
[1510] | 389 | if (($conf['history_admin']) or ((! $conf['history_admin']) |
---|
| 390 | and (!is_admin()))) |
---|
[1092] | 391 | { |
---|
[876] | 392 | $login = ($user['id'] == $conf['guest_id']) |
---|
| 393 | ? 'guest' : addslashes($user['username']); |
---|
[1086] | 394 | |
---|
[725] | 395 | $query = ' |
---|
| 396 | INSERT INTO '.HISTORY_TABLE.' |
---|
| 397 | (date,login,IP,file,category,picture) |
---|
| 398 | VALUES |
---|
| 399 | (NOW(), |
---|
[876] | 400 | \''.$login.'\', |
---|
[725] | 401 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
[868] | 402 | \''.addslashes($file).'\', |
---|
[876] | 403 | \''.addslashes(strip_tags($category)).'\', |
---|
[868] | 404 | \''.addslashes($picture).'\') |
---|
[725] | 405 | ;'; |
---|
| 406 | pwg_query($query); |
---|
[2] | 407 | } |
---|
[894] | 408 | } |
---|
[2] | 409 | } |
---|
| 410 | |
---|
[85] | 411 | // format_date returns a formatted date for display. The date given in |
---|
| 412 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
| 413 | // american format (2003-09-15). By option, you can show the time. The |
---|
| 414 | // output is internationalized. |
---|
| 415 | // |
---|
| 416 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
[599] | 417 | function format_date($date, $type = 'us', $show_time = false) |
---|
[61] | 418 | { |
---|
| 419 | global $lang; |
---|
| 420 | |
---|
[599] | 421 | list($year,$month,$day,$hour,$minute,$second) = array(0,0,0,0,0,0); |
---|
[1086] | 422 | |
---|
[61] | 423 | switch ( $type ) |
---|
| 424 | { |
---|
[599] | 425 | case 'us' : |
---|
| 426 | { |
---|
| 427 | list($year,$month,$day) = explode('-', $date); |
---|
| 428 | break; |
---|
| 429 | } |
---|
| 430 | case 'unix' : |
---|
| 431 | { |
---|
[667] | 432 | list($year,$month,$day,$hour,$minute) = |
---|
[599] | 433 | explode('.', date('Y.n.j.G.i', $date)); |
---|
| 434 | break; |
---|
| 435 | } |
---|
| 436 | case 'mysql_datetime' : |
---|
| 437 | { |
---|
| 438 | preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
| 439 | $date, $out); |
---|
| 440 | list($year,$month,$day,$hour,$minute,$second) = |
---|
| 441 | array($out[1],$out[2],$out[3],$out[4],$out[5],$out[6]); |
---|
| 442 | break; |
---|
| 443 | } |
---|
[61] | 444 | } |
---|
[599] | 445 | $formated_date = ''; |
---|
| 446 | // before 1970, Microsoft Windows can't mktime |
---|
[698] | 447 | if ($year >= 1970) |
---|
[61] | 448 | { |
---|
[618] | 449 | // we ask midday because Windows think it's prior to midnight with a |
---|
| 450 | // zero and refuse to work |
---|
| 451 | $formated_date.= $lang['day'][date('w', mktime(12,0,0,$month,$day,$year))]; |
---|
[61] | 452 | } |
---|
[599] | 453 | $formated_date.= ' '.$day; |
---|
| 454 | $formated_date.= ' '.$lang['month'][(int)$month]; |
---|
| 455 | $formated_date.= ' '.$year; |
---|
| 456 | if ($show_time) |
---|
| 457 | { |
---|
| 458 | $formated_date.= ' '.$hour.':'.$minute; |
---|
| 459 | } |
---|
[61] | 460 | |
---|
| 461 | return $formated_date; |
---|
| 462 | } |
---|
[85] | 463 | |
---|
[1487] | 464 | function pwg_stripslashes($value) |
---|
| 465 | { |
---|
| 466 | if (get_magic_quotes_gpc()) |
---|
| 467 | { |
---|
| 468 | $value = stripslashes($value); |
---|
| 469 | } |
---|
| 470 | return $value; |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | function pwg_addslashes($value) |
---|
| 474 | { |
---|
| 475 | if (!get_magic_quotes_gpc()) |
---|
| 476 | { |
---|
| 477 | $value = addslashes($value); |
---|
| 478 | } |
---|
| 479 | return $value; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | function pwg_quotemeta($value) |
---|
| 483 | { |
---|
| 484 | if (get_magic_quotes_gpc()) { |
---|
| 485 | $value = stripslashes($value); |
---|
| 486 | } |
---|
| 487 | if (function_exists('mysql_real_escape_string')) |
---|
| 488 | { |
---|
| 489 | $value = mysql_real_escape_string($value); |
---|
| 490 | } |
---|
| 491 | else |
---|
| 492 | { |
---|
| 493 | $value = mysql_escape_string($value); |
---|
| 494 | } |
---|
| 495 | return $value; |
---|
| 496 | } |
---|
| 497 | |
---|
[587] | 498 | function pwg_query($query) |
---|
[345] | 499 | { |
---|
[1033] | 500 | global $conf,$page,$debug,$t2; |
---|
[1086] | 501 | |
---|
[345] | 502 | $start = get_moment(); |
---|
[672] | 503 | $result = mysql_query($query) or my_error($query."\n"); |
---|
[1086] | 504 | |
---|
[659] | 505 | $time = get_moment() - $start; |
---|
[672] | 506 | |
---|
| 507 | if (!isset($page['count_queries'])) |
---|
| 508 | { |
---|
| 509 | $page['count_queries'] = 0; |
---|
| 510 | $page['queries_time'] = 0; |
---|
| 511 | } |
---|
[1086] | 512 | |
---|
[672] | 513 | $page['count_queries']++; |
---|
| 514 | $page['queries_time']+= $time; |
---|
[1086] | 515 | |
---|
[592] | 516 | if ($conf['show_queries']) |
---|
[587] | 517 | { |
---|
| 518 | $output = ''; |
---|
[672] | 519 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
| 520 | $output.= "\n".$query; |
---|
| 521 | $output.= "\n".'(this query time : '; |
---|
[1012] | 522 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
[672] | 523 | $output.= "\n".'(total SQL time : '; |
---|
| 524 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
[1033] | 525 | $output.= "\n".'(total time : '; |
---|
| 526 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
[1012] | 527 | $output.= "</pre>\n"; |
---|
[1086] | 528 | |
---|
[1012] | 529 | $debug .= $output; |
---|
[587] | 530 | } |
---|
[1086] | 531 | |
---|
[345] | 532 | return $result; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | function pwg_debug( $string ) |
---|
| 536 | { |
---|
[1033] | 537 | global $debug,$t2,$page; |
---|
[345] | 538 | |
---|
| 539 | $now = explode( ' ', microtime() ); |
---|
| 540 | $now2 = explode( '.', $now[0] ); |
---|
| 541 | $now2 = $now[1].'.'.$now2[1]; |
---|
| 542 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
[1012] | 543 | $debug .= '<p>'; |
---|
[345] | 544 | $debug.= '['.$time.', '; |
---|
[1033] | 545 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
[1012] | 546 | $debug.= "</p>\n"; |
---|
[345] | 547 | } |
---|
[351] | 548 | |
---|
[405] | 549 | /** |
---|
| 550 | * Redirects to the given URL |
---|
| 551 | * |
---|
| 552 | * Note : once this function called, the execution doesn't go further |
---|
| 553 | * (presence of an exit() instruction. |
---|
| 554 | * |
---|
| 555 | * @param string $url |
---|
[1156] | 556 | * @param string $title_msg |
---|
| 557 | * @param integer $refreh_time |
---|
[405] | 558 | * @return void |
---|
| 559 | */ |
---|
[1510] | 560 | function redirect( $url , $msg = '', $refresh_time = 0) |
---|
[405] | 561 | { |
---|
[1509] | 562 | global $lang_info, $lang; |
---|
[405] | 563 | |
---|
[1508] | 564 | if (!isset($lang_info)) { |
---|
| 565 | include_once(get_language_filepath('common.lang.php')); |
---|
[1298] | 566 | } |
---|
| 567 | |
---|
[1508] | 568 | $template = new Template(PHPWG_ROOT_PATH.'template-common'); |
---|
| 569 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
| 570 | if (empty($msg)) |
---|
[1156] | 571 | { |
---|
| 572 | $redirect_msg = l10n('redirect_msg'); |
---|
| 573 | } |
---|
| 574 | else |
---|
| 575 | { |
---|
| 576 | $redirect_msg = $msg; |
---|
| 577 | } |
---|
[1160] | 578 | $redirect_msg = nl2br($redirect_msg); |
---|
[688] | 579 | |
---|
[1508] | 580 | $template->assign_vars( |
---|
| 581 | array( |
---|
| 582 | 'CONTENT_ENCODING' => $lang_info['charset'], |
---|
| 583 | 'LANG'=>$lang_info['code'], |
---|
| 584 | 'DIR'=>$lang_info['direction'], |
---|
| 585 | ) |
---|
| 586 | ); |
---|
[1086] | 587 | |
---|
[1508] | 588 | $template->assign_vars( |
---|
| 589 | array( |
---|
| 590 | 'U_REDIRECT_MSG' => $redirect_msg, |
---|
| 591 | 'REFRESH_TIME' => $refresh_time, |
---|
| 592 | 'U_REFRESH' => $url |
---|
| 593 | ) |
---|
| 594 | ); |
---|
| 595 | $template->assign_block_vars('refresh', array()); |
---|
[688] | 596 | $template->parse('redirect'); |
---|
[1508] | 597 | $template->p(); |
---|
[405] | 598 | exit(); |
---|
| 599 | } |
---|
[507] | 600 | |
---|
| 601 | /** |
---|
| 602 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
| 603 | * |
---|
| 604 | * @param array $rejects |
---|
| 605 | * @returns string |
---|
| 606 | */ |
---|
| 607 | function get_query_string_diff($rejects = array()) |
---|
| 608 | { |
---|
| 609 | $query_string = ''; |
---|
[1086] | 610 | |
---|
[507] | 611 | $str = $_SERVER['QUERY_STRING']; |
---|
| 612 | parse_str($str, $vars); |
---|
[1086] | 613 | |
---|
[507] | 614 | $is_first = true; |
---|
| 615 | foreach ($vars as $key => $value) |
---|
| 616 | { |
---|
| 617 | if (!in_array($key, $rejects)) |
---|
| 618 | { |
---|
[764] | 619 | $query_string.= $is_first ? '?' : '&'; |
---|
| 620 | $is_first = false; |
---|
[507] | 621 | $query_string.= $key.'='.$value; |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | return $query_string; |
---|
| 626 | } |
---|
[512] | 627 | |
---|
[1020] | 628 | function url_is_remote($url) |
---|
| 629 | { |
---|
| 630 | if (preg_match('/^https?:\/\/[~\/\.\w-]+$/', $url)) |
---|
| 631 | { |
---|
| 632 | return true; |
---|
| 633 | } |
---|
| 634 | return false; |
---|
| 635 | } |
---|
| 636 | |
---|
[512] | 637 | /** |
---|
[1048] | 638 | * returns available template/theme |
---|
[512] | 639 | */ |
---|
[1376] | 640 | function get_pwg_themes() |
---|
[512] | 641 | { |
---|
[960] | 642 | $themes = array(); |
---|
[579] | 643 | |
---|
[1376] | 644 | $template_dir = PHPWG_ROOT_PATH.'template'; |
---|
[1086] | 645 | |
---|
[1048] | 646 | foreach (get_dirs($template_dir) as $template) |
---|
[960] | 647 | { |
---|
[1048] | 648 | foreach (get_dirs($template_dir.'/'.$template.'/theme') as $theme) |
---|
[960] | 649 | { |
---|
| 650 | array_push($themes, $template.'/'.$theme); |
---|
| 651 | } |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | return $themes; |
---|
| 655 | } |
---|
| 656 | |
---|
[579] | 657 | /** |
---|
| 658 | * returns thumbnail filepath (or distant URL if thumbnail is remote) for a |
---|
| 659 | * given element |
---|
| 660 | * |
---|
| 661 | * the returned string can represente the filepath of the thumbnail or the |
---|
| 662 | * filepath to the corresponding icon for non picture elements |
---|
| 663 | * |
---|
[606] | 664 | * @param string path |
---|
[579] | 665 | * @param string tn_ext |
---|
[1090] | 666 | * @param bool with_rewrite if true returned path can't be used from the script |
---|
[579] | 667 | * @return string |
---|
| 668 | */ |
---|
[1090] | 669 | function get_thumbnail_src($path, $tn_ext = '', $with_rewrite = true) |
---|
[579] | 670 | { |
---|
[606] | 671 | global $conf, $user; |
---|
| 672 | |
---|
[579] | 673 | if ($tn_ext != '') |
---|
| 674 | { |
---|
[1082] | 675 | $src = substr_replace( |
---|
| 676 | get_filename_wo_extension($path), |
---|
| 677 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
| 678 | strrpos($path,'/'), |
---|
| 679 | 1 |
---|
| 680 | ); |
---|
[579] | 681 | $src.= '.'.$tn_ext; |
---|
[1090] | 682 | if ($with_rewrite==true and !url_is_remote($src) ) |
---|
| 683 | { |
---|
| 684 | $src = get_root_url().$src; |
---|
| 685 | } |
---|
[579] | 686 | } |
---|
| 687 | else |
---|
| 688 | { |
---|
[1090] | 689 | $src = ($with_rewrite==true) ? get_root_url() : ''; |
---|
| 690 | $src .= get_themeconf('mime_icon_dir'); |
---|
[606] | 691 | $src.= strtolower(get_extension($path)).'.png'; |
---|
[579] | 692 | } |
---|
[1086] | 693 | |
---|
[579] | 694 | return $src; |
---|
| 695 | } |
---|
[672] | 696 | |
---|
| 697 | // my_error returns (or send to standard output) the message concerning the |
---|
| 698 | // error occured for the last mysql query. |
---|
[735] | 699 | function my_error($header) |
---|
[672] | 700 | { |
---|
[1221] | 701 | global $conf; |
---|
| 702 | |
---|
[672] | 703 | $error = '<pre>'; |
---|
| 704 | $error.= $header; |
---|
| 705 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
| 706 | $error.= mysql_error(); |
---|
| 707 | $error.= '</pre>'; |
---|
[1221] | 708 | |
---|
| 709 | if ($conf['die_on_sql_error']) |
---|
| 710 | { |
---|
| 711 | die($error); |
---|
| 712 | } |
---|
| 713 | else |
---|
| 714 | { |
---|
| 715 | echo $error; |
---|
| 716 | } |
---|
[672] | 717 | } |
---|
[755] | 718 | |
---|
| 719 | /** |
---|
| 720 | * creates an array based on a query, this function is a very common pattern |
---|
| 721 | * used here |
---|
| 722 | * |
---|
| 723 | * @param string $query |
---|
| 724 | * @param string $fieldname |
---|
| 725 | * @return array |
---|
| 726 | */ |
---|
| 727 | function array_from_query($query, $fieldname) |
---|
| 728 | { |
---|
| 729 | $array = array(); |
---|
[1086] | 730 | |
---|
[755] | 731 | $result = pwg_query($query); |
---|
| 732 | while ($row = mysql_fetch_array($result)) |
---|
| 733 | { |
---|
| 734 | array_push($array, $row[$fieldname]); |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | return $array; |
---|
| 738 | } |
---|
[762] | 739 | |
---|
| 740 | /** |
---|
| 741 | * instantiate number list for days in a template block |
---|
| 742 | * |
---|
| 743 | * @param string blockname |
---|
| 744 | * @param string selection |
---|
| 745 | */ |
---|
| 746 | function get_day_list($blockname, $selection) |
---|
| 747 | { |
---|
| 748 | global $template; |
---|
[1086] | 749 | |
---|
[762] | 750 | $template->assign_block_vars( |
---|
| 751 | $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); |
---|
[1086] | 752 | |
---|
[762] | 753 | for ($i = 1; $i <= 31; $i++) |
---|
| 754 | { |
---|
| 755 | $selected = ''; |
---|
| 756 | if ($i == (int)$selection) |
---|
| 757 | { |
---|
| 758 | $selected = 'selected="selected"'; |
---|
| 759 | } |
---|
| 760 | $template->assign_block_vars( |
---|
| 761 | $blockname, array('SELECTED' => $selected, |
---|
| 762 | 'VALUE' => $i, |
---|
| 763 | 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); |
---|
| 764 | } |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | /** |
---|
| 768 | * instantiate month list in a template block |
---|
| 769 | * |
---|
| 770 | * @param string blockname |
---|
| 771 | * @param string selection |
---|
| 772 | */ |
---|
| 773 | function get_month_list($blockname, $selection) |
---|
| 774 | { |
---|
| 775 | global $template, $lang; |
---|
[1086] | 776 | |
---|
[762] | 777 | $template->assign_block_vars( |
---|
| 778 | $blockname, array('SELECTED' => '', |
---|
| 779 | 'VALUE' => 0, |
---|
| 780 | 'OPTION' => '------------')); |
---|
| 781 | |
---|
| 782 | for ($i = 1; $i <= 12; $i++) |
---|
| 783 | { |
---|
| 784 | $selected = ''; |
---|
| 785 | if ($i == (int)$selection) |
---|
| 786 | { |
---|
| 787 | $selected = 'selected="selected"'; |
---|
| 788 | } |
---|
| 789 | $template->assign_block_vars( |
---|
| 790 | $blockname, array('SELECTED' => $selected, |
---|
| 791 | 'VALUE' => $i, |
---|
| 792 | 'OPTION' => $lang['month'][$i])); |
---|
| 793 | } |
---|
| 794 | } |
---|
[764] | 795 | |
---|
| 796 | /** |
---|
| 797 | * fill the current user caddie with given elements, if not already in |
---|
| 798 | * caddie |
---|
| 799 | * |
---|
| 800 | * @param array elements_id |
---|
| 801 | */ |
---|
| 802 | function fill_caddie($elements_id) |
---|
| 803 | { |
---|
| 804 | global $user; |
---|
[1086] | 805 | |
---|
[764] | 806 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
[1086] | 807 | |
---|
[764] | 808 | $query = ' |
---|
| 809 | SELECT element_id |
---|
| 810 | FROM '.CADDIE_TABLE.' |
---|
| 811 | WHERE user_id = '.$user['id'].' |
---|
| 812 | ;'; |
---|
| 813 | $in_caddie = array_from_query($query, 'element_id'); |
---|
| 814 | |
---|
| 815 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
| 816 | |
---|
| 817 | $datas = array(); |
---|
| 818 | |
---|
| 819 | foreach ($caddiables as $caddiable) |
---|
| 820 | { |
---|
| 821 | array_push($datas, array('element_id' => $caddiable, |
---|
| 822 | 'user_id' => $user['id'])); |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | if (count($caddiables) > 0) |
---|
| 826 | { |
---|
| 827 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
| 828 | } |
---|
| 829 | } |
---|
[793] | 830 | |
---|
| 831 | /** |
---|
| 832 | * returns the element name from its filename |
---|
| 833 | * |
---|
| 834 | * @param string filename |
---|
| 835 | * @return string name |
---|
| 836 | */ |
---|
| 837 | function get_name_from_file($filename) |
---|
| 838 | { |
---|
| 839 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | /** |
---|
| 843 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
| 844 | * returned |
---|
| 845 | * |
---|
| 846 | * @param string key |
---|
| 847 | * @return string |
---|
| 848 | */ |
---|
[1290] | 849 | function l10n($key) |
---|
[793] | 850 | { |
---|
[808] | 851 | global $lang, $conf; |
---|
[793] | 852 | |
---|
[862] | 853 | if ($conf['debug_l10n'] and !isset($lang[$key])) |
---|
[808] | 854 | { |
---|
| 855 | echo '[l10n] language key "'.$key.'" is not defined<br />'; |
---|
| 856 | } |
---|
[1086] | 857 | |
---|
[808] | 858 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
[793] | 859 | } |
---|
[960] | 860 | |
---|
| 861 | /** |
---|
[1361] | 862 | * Translate string in string ascii7bits |
---|
[1364] | 863 | * It's possible to do that with iconv_substr |
---|
| 864 | * but this fonction is not avaible on all the providers. |
---|
[1361] | 865 | * |
---|
| 866 | * @param string str |
---|
| 867 | * @return string |
---|
| 868 | */ |
---|
| 869 | function str_translate_to_ascii7bits($str) |
---|
| 870 | { |
---|
| 871 | global $lang_table_translate_ascii7bits; |
---|
| 872 | |
---|
| 873 | $src_table = array_keys($lang_table_translate_ascii7bits); |
---|
| 874 | $dst_table = array_values($lang_table_translate_ascii7bits); |
---|
| 875 | |
---|
| 876 | return str_replace($src_table , $dst_table, $str); |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | /** |
---|
[1008] | 880 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
| 881 | * key is returned |
---|
[960] | 882 | * |
---|
| 883 | * @param string key |
---|
| 884 | * @return string |
---|
| 885 | */ |
---|
| 886 | function get_themeconf($key) |
---|
| 887 | { |
---|
| 888 | global $themeconf; |
---|
| 889 | |
---|
[1400] | 890 | return isset($themeconf[$key]) ? $themeconf[$key] : ''; |
---|
[960] | 891 | } |
---|
[1008] | 892 | |
---|
| 893 | /** |
---|
[1021] | 894 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
| 895 | * |
---|
| 896 | * @return string |
---|
| 897 | */ |
---|
| 898 | function get_webmaster_mail_address() |
---|
| 899 | { |
---|
| 900 | global $conf; |
---|
| 901 | |
---|
| 902 | $query = ' |
---|
| 903 | SELECT '.$conf['user_fields']['email'].' |
---|
| 904 | FROM '.USERS_TABLE.' |
---|
| 905 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
| 906 | ;'; |
---|
| 907 | list($email) = mysql_fetch_array(pwg_query($query)); |
---|
| 908 | |
---|
| 909 | return $email; |
---|
| 910 | } |
---|
[1027] | 911 | |
---|
| 912 | /** |
---|
| 913 | * which upgrades are available ? |
---|
| 914 | * |
---|
| 915 | * @return array |
---|
| 916 | */ |
---|
| 917 | function get_available_upgrade_ids() |
---|
| 918 | { |
---|
| 919 | $upgrades_path = PHPWG_ROOT_PATH.'install/db'; |
---|
| 920 | |
---|
| 921 | $available_upgrade_ids = array(); |
---|
[1086] | 922 | |
---|
[1027] | 923 | if ($contents = opendir($upgrades_path)) |
---|
| 924 | { |
---|
| 925 | while (($node = readdir($contents)) !== false) |
---|
| 926 | { |
---|
| 927 | if (is_file($upgrades_path.'/'.$node) |
---|
| 928 | and preg_match('/^(.*?)-database\.php$/', $node, $match)) |
---|
| 929 | { |
---|
| 930 | array_push($available_upgrade_ids, $match[1]); |
---|
| 931 | } |
---|
| 932 | } |
---|
| 933 | } |
---|
| 934 | natcasesort($available_upgrade_ids); |
---|
| 935 | |
---|
| 936 | return $available_upgrade_ids; |
---|
| 937 | } |
---|
[1156] | 938 | |
---|
[1284] | 939 | /** |
---|
| 940 | * Add configuration parameters from database to global $conf array |
---|
| 941 | * |
---|
| 942 | * @return void |
---|
| 943 | */ |
---|
| 944 | function load_conf_from_db() |
---|
| 945 | { |
---|
| 946 | global $conf; |
---|
| 947 | |
---|
| 948 | $query = ' |
---|
| 949 | SELECT param,value |
---|
| 950 | FROM '.CONFIG_TABLE.' |
---|
| 951 | ;'; |
---|
| 952 | $result = pwg_query($query); |
---|
| 953 | |
---|
| 954 | if (mysql_num_rows($result) == 0) |
---|
| 955 | { |
---|
| 956 | die('No configuration data'); |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | while ($row = mysql_fetch_array($result)) |
---|
| 960 | { |
---|
| 961 | $conf[ $row['param'] ] = isset($row['value']) ? $row['value'] : ''; |
---|
| 962 | |
---|
| 963 | // If the field is true or false, the variable is transformed into a |
---|
| 964 | // boolean value. |
---|
| 965 | if ($conf[$row['param']] == 'true' or $conf[$row['param']] == 'false') |
---|
| 966 | { |
---|
| 967 | $conf[ $row['param'] ] = get_boolean($conf[ $row['param'] ]); |
---|
| 968 | } |
---|
| 969 | } |
---|
| 970 | } |
---|
[1119] | 971 | ?> |
---|