[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[675] | 5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[362] | 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2006-01-21 20:32:09 +0000 (Sat, 21 Jan 2006) $ |
---|
| 10 | // | last modifier : $Author: nikrou $ |
---|
| 11 | // | revision : $Revision: 1011 $ |
---|
| 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' ); |
---|
[2] | 34 | |
---|
| 35 | //----------------------------------------------------------- generic functions |
---|
| 36 | |
---|
[777] | 37 | /** |
---|
| 38 | * returns an array containing the possible values of an enum field |
---|
| 39 | * |
---|
| 40 | * @param string tablename |
---|
| 41 | * @param string fieldname |
---|
| 42 | */ |
---|
| 43 | function get_enums($table, $field) |
---|
| 44 | { |
---|
| 45 | // retrieving the properties of the table. Each line represents a field : |
---|
| 46 | // columns are 'Field', 'Type' |
---|
| 47 | $result = pwg_query('desc '.$table); |
---|
| 48 | while ($row = mysql_fetch_array($result)) |
---|
| 49 | { |
---|
| 50 | // we are only interested in the the field given in parameter for the |
---|
| 51 | // function |
---|
| 52 | if ($row['Field'] == $field) |
---|
| 53 | { |
---|
| 54 | // retrieving possible values of the enum field |
---|
| 55 | // enum('blue','green','black') |
---|
| 56 | $options = explode(',', substr($row['Type'], 5, -1)); |
---|
| 57 | foreach ($options as $i => $option) |
---|
| 58 | { |
---|
| 59 | $options[$i] = str_replace("'", '',$option); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | mysql_free_result($result); |
---|
| 64 | return $options; |
---|
| 65 | } |
---|
| 66 | |
---|
[6] | 67 | // get_boolean transforms a string to a boolean value. If the string is |
---|
| 68 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
| 69 | // any other case, true is returned. |
---|
[2] | 70 | function get_boolean( $string ) |
---|
| 71 | { |
---|
| 72 | $boolean = true; |
---|
| 73 | if ( preg_match( '/^false$/i', $string ) ) |
---|
| 74 | { |
---|
| 75 | $boolean = false; |
---|
| 76 | } |
---|
| 77 | return $boolean; |
---|
| 78 | } |
---|
| 79 | |
---|
[661] | 80 | /** |
---|
| 81 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
| 82 | * |
---|
| 83 | * @param mixed $var |
---|
| 84 | * @return mixed |
---|
| 85 | */ |
---|
| 86 | function boolean_to_string($var) |
---|
| 87 | { |
---|
| 88 | if (is_bool($var)) |
---|
| 89 | { |
---|
| 90 | if ($var) |
---|
| 91 | { |
---|
| 92 | return 'true'; |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | return 'false'; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
| 101 | return $var; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
[2] | 105 | // The function get_moment returns a float value coresponding to the number |
---|
| 106 | // of seconds since the unix epoch (1st January 1970) and the microseconds |
---|
| 107 | // are precised : e.g. 1052343429.89276600 |
---|
| 108 | function get_moment() |
---|
| 109 | { |
---|
[9] | 110 | $t1 = explode( ' ', microtime() ); |
---|
| 111 | $t2 = explode( '.', $t1[0] ); |
---|
| 112 | $t2 = $t1[1].'.'.$t2[1]; |
---|
[2] | 113 | return $t2; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | // The function get_elapsed_time returns the number of seconds (with 3 |
---|
| 117 | // decimals precision) between the start time and the end time given. |
---|
| 118 | function get_elapsed_time( $start, $end ) |
---|
| 119 | { |
---|
| 120 | return number_format( $end - $start, 3, '.', ' ').' s'; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | // - The replace_space function replaces space and '-' characters |
---|
| 124 | // by their HTML equivalent &nbsb; and − |
---|
| 125 | // - The function does not replace characters in HTML tags |
---|
| 126 | // - This function was created because IE5 does not respect the |
---|
| 127 | // CSS "white-space: nowrap;" property unless space and minus |
---|
| 128 | // characters are replaced like this function does. |
---|
[15] | 129 | // - Example : |
---|
| 130 | // <div class="foo">My friend</div> |
---|
| 131 | // ( 01234567891111111111222222222233 ) |
---|
| 132 | // ( 0123456789012345678901 ) |
---|
| 133 | // becomes : |
---|
| 134 | // <div class="foo">My friend</div> |
---|
[2] | 135 | function replace_space( $string ) |
---|
| 136 | { |
---|
[15] | 137 | //return $string; |
---|
| 138 | $return_string = ''; |
---|
| 139 | // $remaining is the rest of the string where to replace spaces characters |
---|
[2] | 140 | $remaining = $string; |
---|
[15] | 141 | // $start represents the position of the next '<' character |
---|
| 142 | // $end represents the position of the next '>' character |
---|
[2] | 143 | $start = 0; |
---|
| 144 | $end = 0; |
---|
[15] | 145 | $start = strpos ( $remaining, '<' ); // -> 0 |
---|
| 146 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
| 147 | // as long as a '<' and his friend '>' are found, we loop |
---|
[2] | 148 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
| 149 | { |
---|
[15] | 150 | // $treatment is the part of the string to treat |
---|
| 151 | // In the first loop of our example, this variable is empty, but in the |
---|
| 152 | // second loop, it equals 'My friend' |
---|
[2] | 153 | $treatment = substr ( $remaining, 0, $start ); |
---|
[15] | 154 | // Replacement of ' ' by his equivalent ' ' |
---|
[6] | 155 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
| 156 | $treatment = str_replace( '-', '−', $treatment ); |
---|
[15] | 157 | // composing the string to return by adding the treated string and the |
---|
| 158 | // following HTML tag -> 'My friend</div>' |
---|
| 159 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
| 160 | // the remaining string is deplaced to the part after the '>' of this |
---|
| 161 | // loop |
---|
[2] | 162 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
[6] | 163 | $start = strpos ( $remaining, '<' ); |
---|
| 164 | $end = strpos ( $remaining, '>' ); |
---|
[2] | 165 | } |
---|
[6] | 166 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
| 167 | $treatment = str_replace( '-', '−', $treatment ); |
---|
[2] | 168 | $return_string.= $treatment; |
---|
[15] | 169 | |
---|
[2] | 170 | return $return_string; |
---|
| 171 | } |
---|
| 172 | |
---|
[13] | 173 | // get_extension returns the part of the string after the last "." |
---|
| 174 | function get_extension( $filename ) |
---|
| 175 | { |
---|
| 176 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | // get_filename_wo_extension returns the part of the string before the last |
---|
| 180 | // ".". |
---|
| 181 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
| 182 | function get_filename_wo_extension( $filename ) |
---|
| 183 | { |
---|
| 184 | return substr( $filename, 0, strrpos( $filename, '.' ) ); |
---|
| 185 | } |
---|
| 186 | |
---|
[345] | 187 | /** |
---|
[512] | 188 | * returns an array contening sub-directories, excluding "CVS" |
---|
[345] | 189 | * |
---|
| 190 | * @param string $dir |
---|
| 191 | * @return array |
---|
| 192 | */ |
---|
[512] | 193 | function get_dirs($directory) |
---|
[2] | 194 | { |
---|
[345] | 195 | $sub_dirs = array(); |
---|
[2] | 196 | |
---|
[512] | 197 | if ($opendir = opendir($directory)) |
---|
[2] | 198 | { |
---|
[512] | 199 | while ($file = readdir($opendir)) |
---|
[2] | 200 | { |
---|
[512] | 201 | if ($file != '.' |
---|
| 202 | and $file != '..' |
---|
| 203 | and is_dir($directory.'/'.$file) |
---|
| 204 | and $file != 'CVS') |
---|
[2] | 205 | { |
---|
[512] | 206 | array_push($sub_dirs, $file); |
---|
[2] | 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
[345] | 210 | return $sub_dirs; |
---|
[2] | 211 | } |
---|
| 212 | |
---|
| 213 | // The get_picture_size function return an array containing : |
---|
| 214 | // - $picture_size[0] : final width |
---|
| 215 | // - $picture_size[1] : final height |
---|
| 216 | // The final dimensions are calculated thanks to the original dimensions and |
---|
| 217 | // the maximum dimensions given in parameters. get_picture_size respects |
---|
| 218 | // the width/height ratio |
---|
| 219 | function get_picture_size( $original_width, $original_height, |
---|
| 220 | $max_width, $max_height ) |
---|
| 221 | { |
---|
| 222 | $width = $original_width; |
---|
| 223 | $height = $original_height; |
---|
| 224 | $is_original_size = true; |
---|
| 225 | |
---|
| 226 | if ( $max_width != "" ) |
---|
| 227 | { |
---|
| 228 | if ( $original_width > $max_width ) |
---|
| 229 | { |
---|
| 230 | $width = $max_width; |
---|
| 231 | $height = floor( ( $width * $original_height ) / $original_width ); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | if ( $max_height != "" ) |
---|
| 235 | { |
---|
| 236 | if ( $original_height > $max_height ) |
---|
| 237 | { |
---|
| 238 | $height = $max_height; |
---|
| 239 | $width = floor( ( $height * $original_width ) / $original_height ); |
---|
| 240 | $is_original_size = false; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | if ( is_numeric( $max_width ) and is_numeric( $max_height ) |
---|
| 244 | and $max_width != 0 and $max_height != 0 ) |
---|
| 245 | { |
---|
| 246 | $ratioWidth = $original_width / $max_width; |
---|
| 247 | $ratioHeight = $original_height / $max_height; |
---|
| 248 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
| 249 | { |
---|
| 250 | if ( $ratioWidth < $ratioHeight ) |
---|
| 251 | { |
---|
| 252 | $width = floor( $original_width / $ratioHeight ); |
---|
| 253 | $height = $max_height; |
---|
| 254 | } |
---|
| 255 | else |
---|
| 256 | { |
---|
| 257 | $width = $max_width; |
---|
| 258 | $height = floor( $original_height / $ratioWidth ); |
---|
| 259 | } |
---|
| 260 | $is_original_size = false; |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | $picture_size = array(); |
---|
| 264 | $picture_size[0] = $width; |
---|
| 265 | $picture_size[1] = $height; |
---|
| 266 | return $picture_size; |
---|
| 267 | } |
---|
| 268 | //-------------------------------------------- PhpWebGallery specific functions |
---|
| 269 | |
---|
[512] | 270 | /** |
---|
| 271 | * returns an array with a list of {language_code => language_name} |
---|
| 272 | * |
---|
| 273 | * @returns array |
---|
| 274 | */ |
---|
| 275 | function get_languages() |
---|
[2] | 276 | { |
---|
[512] | 277 | $dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
[2] | 278 | $languages = array(); |
---|
[512] | 279 | |
---|
| 280 | while ($file = readdir($dir)) |
---|
[2] | 281 | { |
---|
[749] | 282 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
[512] | 283 | if (is_dir($path) and !is_link($path) and file_exists($path.'/iso.txt')) |
---|
[2] | 284 | { |
---|
[512] | 285 | list($language_name) = @file($path.'/iso.txt'); |
---|
[528] | 286 | $languages[$file] = $language_name; |
---|
[2] | 287 | } |
---|
| 288 | } |
---|
[512] | 289 | closedir($dir); |
---|
| 290 | @asort($languages); |
---|
| 291 | @reset($languages); |
---|
| 292 | |
---|
[2] | 293 | return $languages; |
---|
| 294 | } |
---|
| 295 | |
---|
[409] | 296 | /** |
---|
| 297 | * replaces the $search into <span style="$style">$search</span> in the |
---|
| 298 | * given $string. |
---|
| 299 | * |
---|
| 300 | * case insensitive replacements, does not replace characters in HTML tags |
---|
| 301 | * |
---|
| 302 | * @param string $string |
---|
| 303 | * @param string $search |
---|
| 304 | * @param string $style |
---|
| 305 | * @return string |
---|
| 306 | */ |
---|
[17] | 307 | function add_style( $string, $search, $style ) |
---|
[2] | 308 | { |
---|
| 309 | //return $string; |
---|
[17] | 310 | $return_string = ''; |
---|
[2] | 311 | $remaining = $string; |
---|
| 312 | |
---|
| 313 | $start = 0; |
---|
| 314 | $end = 0; |
---|
[6] | 315 | $start = strpos ( $remaining, '<' ); |
---|
| 316 | $end = strpos ( $remaining, '>' ); |
---|
[2] | 317 | while ( is_numeric( $start ) and is_numeric( $end ) ) |
---|
| 318 | { |
---|
| 319 | $treatment = substr ( $remaining, 0, $start ); |
---|
[409] | 320 | $treatment = preg_replace( '/('.$search.')/i', |
---|
| 321 | '<span style="'.$style.'">\\0</span>', |
---|
| 322 | $treatment ); |
---|
[17] | 323 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
[2] | 324 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
[6] | 325 | $start = strpos ( $remaining, '<' ); |
---|
| 326 | $end = strpos ( $remaining, '>' ); |
---|
[2] | 327 | } |
---|
[409] | 328 | $treatment = preg_replace( '/('.$search.')/i', |
---|
| 329 | '<span style="'.$style.'">\\0</span>', |
---|
| 330 | $remaining ); |
---|
[2] | 331 | $return_string.= $treatment; |
---|
| 332 | |
---|
| 333 | return $return_string; |
---|
| 334 | } |
---|
| 335 | |
---|
[17] | 336 | // replace_search replaces a searched words array string by the search in |
---|
| 337 | // another style for the given $string. |
---|
| 338 | function replace_search( $string, $search ) |
---|
| 339 | { |
---|
[717] | 340 | // FIXME : with new advanced search, this function needs a rewrite |
---|
| 341 | return $string; |
---|
| 342 | |
---|
[17] | 343 | $words = explode( ',', $search ); |
---|
| 344 | $style = 'background-color:white;color:red;'; |
---|
| 345 | foreach ( $words as $word ) { |
---|
| 346 | $string = add_style( $string, $word, $style ); |
---|
| 347 | } |
---|
| 348 | return $string; |
---|
| 349 | } |
---|
| 350 | |
---|
[2] | 351 | function pwg_log( $file, $category, $picture = '' ) |
---|
| 352 | { |
---|
[13] | 353 | global $conf, $user; |
---|
[2] | 354 | |
---|
[725] | 355 | if ($conf['log']) |
---|
[2] | 356 | { |
---|
[894] | 357 | if ( ($conf['history_admin'] ) or ( (! $conf['history_admin']) and ($user['status'] != 'admin') ) ) |
---|
| 358 | { |
---|
[876] | 359 | $login = ($user['id'] == $conf['guest_id']) |
---|
| 360 | ? 'guest' : addslashes($user['username']); |
---|
| 361 | |
---|
[725] | 362 | $query = ' |
---|
| 363 | INSERT INTO '.HISTORY_TABLE.' |
---|
| 364 | (date,login,IP,file,category,picture) |
---|
| 365 | VALUES |
---|
| 366 | (NOW(), |
---|
[876] | 367 | \''.$login.'\', |
---|
[725] | 368 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
[868] | 369 | \''.addslashes($file).'\', |
---|
[876] | 370 | \''.addslashes(strip_tags($category)).'\', |
---|
[868] | 371 | \''.addslashes($picture).'\') |
---|
[725] | 372 | ;'; |
---|
| 373 | pwg_query($query); |
---|
[2] | 374 | } |
---|
[894] | 375 | } |
---|
[2] | 376 | } |
---|
| 377 | |
---|
[85] | 378 | // format_date returns a formatted date for display. The date given in |
---|
| 379 | // argument can be a unixdate (number of seconds since the 01.01.1970) or an |
---|
| 380 | // american format (2003-09-15). By option, you can show the time. The |
---|
| 381 | // output is internationalized. |
---|
| 382 | // |
---|
| 383 | // format_date( "2003-09-15", 'us', true ) -> "Monday 15 September 2003 21:52" |
---|
[599] | 384 | function format_date($date, $type = 'us', $show_time = false) |
---|
[61] | 385 | { |
---|
| 386 | global $lang; |
---|
| 387 | |
---|
[599] | 388 | list($year,$month,$day,$hour,$minute,$second) = array(0,0,0,0,0,0); |
---|
| 389 | |
---|
[61] | 390 | switch ( $type ) |
---|
| 391 | { |
---|
[599] | 392 | case 'us' : |
---|
| 393 | { |
---|
| 394 | list($year,$month,$day) = explode('-', $date); |
---|
| 395 | break; |
---|
| 396 | } |
---|
| 397 | case 'unix' : |
---|
| 398 | { |
---|
[667] | 399 | list($year,$month,$day,$hour,$minute) = |
---|
[599] | 400 | explode('.', date('Y.n.j.G.i', $date)); |
---|
| 401 | break; |
---|
| 402 | } |
---|
| 403 | case 'mysql_datetime' : |
---|
| 404 | { |
---|
| 405 | preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', |
---|
| 406 | $date, $out); |
---|
| 407 | list($year,$month,$day,$hour,$minute,$second) = |
---|
| 408 | array($out[1],$out[2],$out[3],$out[4],$out[5],$out[6]); |
---|
| 409 | break; |
---|
| 410 | } |
---|
[61] | 411 | } |
---|
[599] | 412 | $formated_date = ''; |
---|
| 413 | // before 1970, Microsoft Windows can't mktime |
---|
[698] | 414 | if ($year >= 1970) |
---|
[61] | 415 | { |
---|
[618] | 416 | // we ask midday because Windows think it's prior to midnight with a |
---|
| 417 | // zero and refuse to work |
---|
| 418 | $formated_date.= $lang['day'][date('w', mktime(12,0,0,$month,$day,$year))]; |
---|
[61] | 419 | } |
---|
[599] | 420 | $formated_date.= ' '.$day; |
---|
| 421 | $formated_date.= ' '.$lang['month'][(int)$month]; |
---|
| 422 | $formated_date.= ' '.$year; |
---|
| 423 | if ($show_time) |
---|
| 424 | { |
---|
| 425 | $formated_date.= ' '.$hour.':'.$minute; |
---|
| 426 | } |
---|
[61] | 427 | |
---|
| 428 | return $formated_date; |
---|
| 429 | } |
---|
[85] | 430 | |
---|
[587] | 431 | function pwg_query($query) |
---|
[345] | 432 | { |
---|
[1011] | 433 | global $conf,$page,$debug; |
---|
[592] | 434 | |
---|
[345] | 435 | $start = get_moment(); |
---|
[672] | 436 | $result = mysql_query($query) or my_error($query."\n"); |
---|
[659] | 437 | |
---|
| 438 | $time = get_moment() - $start; |
---|
[672] | 439 | |
---|
| 440 | if (!isset($page['count_queries'])) |
---|
| 441 | { |
---|
| 442 | $page['count_queries'] = 0; |
---|
| 443 | $page['queries_time'] = 0; |
---|
| 444 | } |
---|
[659] | 445 | |
---|
[672] | 446 | $page['count_queries']++; |
---|
| 447 | $page['queries_time']+= $time; |
---|
| 448 | |
---|
[592] | 449 | if ($conf['show_queries']) |
---|
[587] | 450 | { |
---|
| 451 | $output = ''; |
---|
[672] | 452 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
| 453 | $output.= "\n".$query; |
---|
| 454 | $output.= "\n".'(this query time : '; |
---|
[1011] | 455 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
[672] | 456 | $output.= "\n".'(total SQL time : '; |
---|
| 457 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
[1011] | 458 | $output.= "</pre>\n"; |
---|
[659] | 459 | |
---|
[1011] | 460 | $debug .= $output; |
---|
[587] | 461 | } |
---|
[345] | 462 | |
---|
| 463 | return $result; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | function pwg_debug( $string ) |
---|
| 467 | { |
---|
| 468 | global $debug,$t2,$count_queries; |
---|
| 469 | |
---|
| 470 | $now = explode( ' ', microtime() ); |
---|
| 471 | $now2 = explode( '.', $now[0] ); |
---|
| 472 | $now2 = $now[1].'.'.$now2[1]; |
---|
| 473 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
[1011] | 474 | $debug .= '<p>'; |
---|
[345] | 475 | $debug.= '['.$time.', '; |
---|
| 476 | $debug.= $count_queries.' queries] : '.$string; |
---|
[1011] | 477 | $debug.= "</p>\n"; |
---|
[345] | 478 | } |
---|
[351] | 479 | |
---|
[405] | 480 | /** |
---|
| 481 | * Redirects to the given URL |
---|
| 482 | * |
---|
| 483 | * Note : once this function called, the execution doesn't go further |
---|
| 484 | * (presence of an exit() instruction. |
---|
| 485 | * |
---|
| 486 | * @param string $url |
---|
| 487 | * @return void |
---|
| 488 | */ |
---|
| 489 | function redirect( $url ) |
---|
| 490 | { |
---|
[688] | 491 | global $user, $template, $lang_info, $conf, $lang, $t2, $page; |
---|
[405] | 492 | |
---|
| 493 | // $refresh, $url_link and $title are required for creating an automated |
---|
| 494 | // refresh page in header.tpl |
---|
[686] | 495 | $refresh = 0; |
---|
[405] | 496 | $url_link = $url; |
---|
| 497 | $title = 'redirection'; |
---|
[688] | 498 | |
---|
[405] | 499 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
| 500 | |
---|
| 501 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
[688] | 502 | $template->parse('redirect'); |
---|
[405] | 503 | |
---|
| 504 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
| 505 | |
---|
| 506 | exit(); |
---|
| 507 | } |
---|
[507] | 508 | |
---|
| 509 | /** |
---|
| 510 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
| 511 | * |
---|
| 512 | * @param array $rejects |
---|
| 513 | * @returns string |
---|
| 514 | */ |
---|
| 515 | function get_query_string_diff($rejects = array()) |
---|
| 516 | { |
---|
| 517 | $query_string = ''; |
---|
| 518 | |
---|
| 519 | $str = $_SERVER['QUERY_STRING']; |
---|
| 520 | parse_str($str, $vars); |
---|
| 521 | |
---|
| 522 | $is_first = true; |
---|
| 523 | foreach ($vars as $key => $value) |
---|
| 524 | { |
---|
| 525 | if (!in_array($key, $rejects)) |
---|
| 526 | { |
---|
[764] | 527 | $query_string.= $is_first ? '?' : '&'; |
---|
| 528 | $is_first = false; |
---|
[507] | 529 | $query_string.= $key.'='.$value; |
---|
| 530 | } |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | return $query_string; |
---|
| 534 | } |
---|
[512] | 535 | |
---|
| 536 | /** |
---|
| 537 | * returns available templates |
---|
| 538 | */ |
---|
| 539 | function get_templates() |
---|
| 540 | { |
---|
| 541 | return get_dirs(PHPWG_ROOT_PATH.'template'); |
---|
| 542 | } |
---|
[579] | 543 | |
---|
| 544 | /** |
---|
| 545 | * returns thumbnail filepath (or distant URL if thumbnail is remote) for a |
---|
| 546 | * given element |
---|
| 547 | * |
---|
| 548 | * the returned string can represente the filepath of the thumbnail or the |
---|
| 549 | * filepath to the corresponding icon for non picture elements |
---|
| 550 | * |
---|
[606] | 551 | * @param string path |
---|
[579] | 552 | * @param string tn_ext |
---|
| 553 | * @return string |
---|
| 554 | */ |
---|
[606] | 555 | function get_thumbnail_src($path, $tn_ext = '') |
---|
[579] | 556 | { |
---|
[606] | 557 | global $conf, $user; |
---|
| 558 | |
---|
[579] | 559 | if ($tn_ext != '') |
---|
| 560 | { |
---|
[606] | 561 | $src = substr_replace(get_filename_wo_extension($path), |
---|
| 562 | '/thumbnail/'.$conf['prefix_thumbnail'], |
---|
| 563 | strrpos($path,'/'), |
---|
| 564 | 1); |
---|
[579] | 565 | $src.= '.'.$tn_ext; |
---|
| 566 | } |
---|
| 567 | else |
---|
| 568 | { |
---|
| 569 | $src = PHPWG_ROOT_PATH; |
---|
| 570 | $src.= 'template/'.$user['template'].'/mimetypes/'; |
---|
[606] | 571 | $src.= strtolower(get_extension($path)).'.png'; |
---|
[579] | 572 | } |
---|
[606] | 573 | |
---|
[579] | 574 | return $src; |
---|
| 575 | } |
---|
[672] | 576 | |
---|
| 577 | // my_error returns (or send to standard output) the message concerning the |
---|
| 578 | // error occured for the last mysql query. |
---|
[735] | 579 | function my_error($header) |
---|
[672] | 580 | { |
---|
| 581 | $error = '<pre>'; |
---|
| 582 | $error.= $header; |
---|
| 583 | $error.= '[mysql error '.mysql_errno().'] '; |
---|
| 584 | $error.= mysql_error(); |
---|
| 585 | $error.= '</pre>'; |
---|
[735] | 586 | die ($error); |
---|
[672] | 587 | } |
---|
[755] | 588 | |
---|
| 589 | /** |
---|
| 590 | * creates an array based on a query, this function is a very common pattern |
---|
| 591 | * used here |
---|
| 592 | * |
---|
| 593 | * @param string $query |
---|
| 594 | * @param string $fieldname |
---|
| 595 | * @return array |
---|
| 596 | */ |
---|
| 597 | function array_from_query($query, $fieldname) |
---|
| 598 | { |
---|
| 599 | $array = array(); |
---|
| 600 | |
---|
| 601 | $result = pwg_query($query); |
---|
| 602 | while ($row = mysql_fetch_array($result)) |
---|
| 603 | { |
---|
| 604 | array_push($array, $row[$fieldname]); |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | return $array; |
---|
| 608 | } |
---|
[762] | 609 | |
---|
| 610 | /** |
---|
| 611 | * instantiate number list for days in a template block |
---|
| 612 | * |
---|
| 613 | * @param string blockname |
---|
| 614 | * @param string selection |
---|
| 615 | */ |
---|
| 616 | function get_day_list($blockname, $selection) |
---|
| 617 | { |
---|
| 618 | global $template; |
---|
| 619 | |
---|
| 620 | $template->assign_block_vars( |
---|
| 621 | $blockname, array('SELECTED' => '', 'VALUE' => 0, 'OPTION' => '--')); |
---|
| 622 | |
---|
| 623 | for ($i = 1; $i <= 31; $i++) |
---|
| 624 | { |
---|
| 625 | $selected = ''; |
---|
| 626 | if ($i == (int)$selection) |
---|
| 627 | { |
---|
| 628 | $selected = 'selected="selected"'; |
---|
| 629 | } |
---|
| 630 | $template->assign_block_vars( |
---|
| 631 | $blockname, array('SELECTED' => $selected, |
---|
| 632 | 'VALUE' => $i, |
---|
| 633 | 'OPTION' => str_pad($i, 2, '0', STR_PAD_LEFT))); |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | /** |
---|
| 638 | * instantiate month list in a template block |
---|
| 639 | * |
---|
| 640 | * @param string blockname |
---|
| 641 | * @param string selection |
---|
| 642 | */ |
---|
| 643 | function get_month_list($blockname, $selection) |
---|
| 644 | { |
---|
| 645 | global $template, $lang; |
---|
| 646 | |
---|
| 647 | $template->assign_block_vars( |
---|
| 648 | $blockname, array('SELECTED' => '', |
---|
| 649 | 'VALUE' => 0, |
---|
| 650 | 'OPTION' => '------------')); |
---|
| 651 | |
---|
| 652 | for ($i = 1; $i <= 12; $i++) |
---|
| 653 | { |
---|
| 654 | $selected = ''; |
---|
| 655 | if ($i == (int)$selection) |
---|
| 656 | { |
---|
| 657 | $selected = 'selected="selected"'; |
---|
| 658 | } |
---|
| 659 | $template->assign_block_vars( |
---|
| 660 | $blockname, array('SELECTED' => $selected, |
---|
| 661 | 'VALUE' => $i, |
---|
| 662 | 'OPTION' => $lang['month'][$i])); |
---|
| 663 | } |
---|
| 664 | } |
---|
[764] | 665 | |
---|
| 666 | /** |
---|
| 667 | * fill the current user caddie with given elements, if not already in |
---|
| 668 | * caddie |
---|
| 669 | * |
---|
| 670 | * @param array elements_id |
---|
| 671 | */ |
---|
| 672 | function fill_caddie($elements_id) |
---|
| 673 | { |
---|
| 674 | global $user; |
---|
| 675 | |
---|
| 676 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 677 | |
---|
| 678 | $query = ' |
---|
| 679 | SELECT element_id |
---|
| 680 | FROM '.CADDIE_TABLE.' |
---|
| 681 | WHERE user_id = '.$user['id'].' |
---|
| 682 | ;'; |
---|
| 683 | $in_caddie = array_from_query($query, 'element_id'); |
---|
| 684 | |
---|
| 685 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
| 686 | |
---|
| 687 | $datas = array(); |
---|
| 688 | |
---|
| 689 | foreach ($caddiables as $caddiable) |
---|
| 690 | { |
---|
| 691 | array_push($datas, array('element_id' => $caddiable, |
---|
| 692 | 'user_id' => $user['id'])); |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | if (count($caddiables) > 0) |
---|
| 696 | { |
---|
| 697 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
| 698 | } |
---|
| 699 | } |
---|
[793] | 700 | |
---|
| 701 | /** |
---|
| 702 | * returns the element name from its filename |
---|
| 703 | * |
---|
| 704 | * @param string filename |
---|
| 705 | * @return string name |
---|
| 706 | */ |
---|
| 707 | function get_name_from_file($filename) |
---|
| 708 | { |
---|
| 709 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | /** |
---|
| 713 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
| 714 | * returned |
---|
| 715 | * |
---|
| 716 | * @param string key |
---|
| 717 | * @return string |
---|
| 718 | */ |
---|
| 719 | function l10n($key) |
---|
| 720 | { |
---|
[808] | 721 | global $lang, $conf; |
---|
[793] | 722 | |
---|
[862] | 723 | if ($conf['debug_l10n'] and !isset($lang[$key])) |
---|
[808] | 724 | { |
---|
| 725 | echo '[l10n] language key "'.$key.'" is not defined<br />'; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | return isset($lang[$key]) ? $lang[$key] : $key; |
---|
[793] | 729 | } |
---|
[587] | 730 | ?> |
---|