| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | rcube_shared.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube PHP suite | |
|---|
| 8 | | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | CONTENTS: | |
|---|
| 12 | | Shared functions and classes used in PHP projects | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * RoundCube shared functions |
|---|
| 25 | * |
|---|
| 26 | * @package Core |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Send HTTP headers to prevent caching this page |
|---|
| 32 | */ |
|---|
| 33 | function send_nocacheing_headers() |
|---|
| 34 | { |
|---|
| 35 | if (headers_sent()) |
|---|
| 36 | return; |
|---|
| 37 | |
|---|
| 38 | header("Expires: ".gmdate("D, d M Y H:i:s")." GMT"); |
|---|
| 39 | header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); |
|---|
| 40 | header("Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); |
|---|
| 41 | header("Pragma: no-cache"); |
|---|
| 42 | |
|---|
| 43 | // We need to set the following headers to make downloads work using IE in HTTPS mode. |
|---|
| 44 | if (isset($_SERVER['HTTPS'])) { |
|---|
| 45 | header('Pragma: '); |
|---|
| 46 | header('Cache-Control: '); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Send header with expire date 30 days in future |
|---|
| 53 | * |
|---|
| 54 | * @param int Expiration time in seconds |
|---|
| 55 | */ |
|---|
| 56 | function send_future_expire_header($offset=2600000) |
|---|
| 57 | { |
|---|
| 58 | if (headers_sent()) |
|---|
| 59 | return; |
|---|
| 60 | |
|---|
| 61 | header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+$offset)." GMT"); |
|---|
| 62 | header("Cache-Control: max-age=$offset"); |
|---|
| 63 | header("Pragma: "); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Check request for If-Modified-Since and send an according response. |
|---|
| 69 | * This will terminate the current script if headers match the given values |
|---|
| 70 | * |
|---|
| 71 | * @param int Modified date as unix timestamp |
|---|
| 72 | * @param string Etag value for caching |
|---|
| 73 | */ |
|---|
| 74 | function send_modified_header($mdate, $etag=null, $skip_check=false) |
|---|
| 75 | { |
|---|
| 76 | if (headers_sent()) |
|---|
| 77 | return; |
|---|
| 78 | |
|---|
| 79 | $iscached = false; |
|---|
| 80 | $etag = $etag ? "\"$etag\"" : null; |
|---|
| 81 | |
|---|
| 82 | if (!$skip_check) |
|---|
| 83 | { |
|---|
| 84 | if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mdate) |
|---|
| 85 | $iscached = true; |
|---|
| 86 | |
|---|
| 87 | if ($etag) |
|---|
| 88 | $iscached = ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | if ($iscached) |
|---|
| 92 | header("HTTP/1.x 304 Not Modified"); |
|---|
| 93 | else |
|---|
| 94 | header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mdate)." GMT"); |
|---|
| 95 | |
|---|
| 96 | header("Cache-Control: max-age=0"); |
|---|
| 97 | header("Expires: "); |
|---|
| 98 | header("Pragma: "); |
|---|
| 99 | |
|---|
| 100 | if ($etag) |
|---|
| 101 | header("Etag: $etag"); |
|---|
| 102 | |
|---|
| 103 | if ($iscached) |
|---|
| 104 | { |
|---|
| 105 | ob_end_clean(); |
|---|
| 106 | exit; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Returns whether an $str is a reserved word for any of the version of Javascript or ECMAScript |
|---|
| 113 | * @param str String to check |
|---|
| 114 | * @return boolean True if $str is a reserver word, False if not |
|---|
| 115 | */ |
|---|
| 116 | function is_js_reserved_word($str) |
|---|
| 117 | { |
|---|
| 118 | return in_array($str, array( |
|---|
| 119 | // ECMASript ver 4 reserved words |
|---|
| 120 | 'as','break','case','catch','class','const','continue', |
|---|
| 121 | 'default','delete','do','else','export','extends','false','finally','for','function', |
|---|
| 122 | 'if','import','in','instanceof','is','namespace','new','null','package','private', |
|---|
| 123 | 'public','return','super','switch','this','throw','true','try','typeof','use','var', |
|---|
| 124 | 'void','while','with', |
|---|
| 125 | // ECMAScript ver 4 future reserved words |
|---|
| 126 | 'abstract','debugger','enum','goto','implements','interface','native','protected', |
|---|
| 127 | 'synchronized','throws','transient','volatile', |
|---|
| 128 | // special meaning in some contexts |
|---|
| 129 | 'get','set', |
|---|
| 130 | // were reserved in ECMAScript ver 3 |
|---|
| 131 | 'boolean','byte','char','double','final','float','int','long','short','static' |
|---|
| 132 | )); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Convert a variable into a javascript object notation |
|---|
| 138 | * |
|---|
| 139 | * @param mixed Input value |
|---|
| 140 | * @return string Serialized JSON string |
|---|
| 141 | */ |
|---|
| 142 | function json_serialize($var) |
|---|
| 143 | { |
|---|
| 144 | if (is_object($var)) |
|---|
| 145 | $var = get_object_vars($var); |
|---|
| 146 | |
|---|
| 147 | if (is_array($var)) |
|---|
| 148 | { |
|---|
| 149 | // empty array |
|---|
| 150 | if (!sizeof($var)) |
|---|
| 151 | return '[]'; |
|---|
| 152 | else |
|---|
| 153 | { |
|---|
| 154 | $keys_arr = array_keys($var); |
|---|
| 155 | $is_assoc = $have_numeric = 0; |
|---|
| 156 | |
|---|
| 157 | for ($i=0; $i<sizeof($keys_arr); ++$i) |
|---|
| 158 | { |
|---|
| 159 | if (is_numeric($keys_arr[$i])) |
|---|
| 160 | $have_numeric = 1; |
|---|
| 161 | if (!is_numeric($keys_arr[$i]) || $keys_arr[$i] != $i) |
|---|
| 162 | $is_assoc = 1; |
|---|
| 163 | if ($is_assoc && $have_numeric) |
|---|
| 164 | break; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | $brackets = $is_assoc ? '{}' : '[]'; |
|---|
| 168 | $pairs = array(); |
|---|
| 169 | |
|---|
| 170 | foreach ($var as $key => $value) |
|---|
| 171 | { |
|---|
| 172 | // enclose key with quotes if it is not variable-name conform |
|---|
| 173 | if (!ereg("^[_a-zA-Z]{1}[_a-zA-Z0-9]*$", $key) || is_js_reserved_word($key)) |
|---|
| 174 | $key = "'$key'"; |
|---|
| 175 | |
|---|
| 176 | $pairs[] = sprintf("%s%s", $is_assoc ? "$key:" : '', json_serialize($value)); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | return $brackets{0} . implode(',', $pairs) . $brackets{1}; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | else if (is_numeric($var) && strval(intval($var)) === strval($var)) |
|---|
| 183 | return $var; |
|---|
| 184 | else if (is_bool($var)) |
|---|
| 185 | return $var ? '1' : '0'; |
|---|
| 186 | else |
|---|
| 187 | return "'".JQ($var)."'"; |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | * Function to convert an array to a javascript array |
|---|
| 194 | * Actually an alias function for json_serialize() |
|---|
| 195 | * @deprecated |
|---|
| 196 | */ |
|---|
| 197 | function array2js($arr, $type='') |
|---|
| 198 | { |
|---|
| 199 | return json_serialize($arr); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * Similar function as in_array() but case-insensitive |
|---|
| 205 | * |
|---|
| 206 | * @param mixed Needle value |
|---|
| 207 | * @param array Array to search in |
|---|
| 208 | * @return boolean True if found, False if not |
|---|
| 209 | */ |
|---|
| 210 | function in_array_nocase($needle, $haystack) |
|---|
| 211 | { |
|---|
| 212 | foreach ($haystack as $value) |
|---|
| 213 | if (rc_strtolower($needle)===rc_strtolower($value)) |
|---|
| 214 | return true; |
|---|
| 215 | |
|---|
| 216 | return false; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * Find out if the string content means TRUE or FALSE |
|---|
| 222 | * |
|---|
| 223 | * @param string Input value |
|---|
| 224 | * @return boolean Imagine what! |
|---|
| 225 | */ |
|---|
| 226 | function get_boolean($str) |
|---|
| 227 | { |
|---|
| 228 | $str = strtolower($str); |
|---|
| 229 | if(in_array($str, array('false', '0', 'no', 'nein', ''), TRUE)) |
|---|
| 230 | return FALSE; |
|---|
| 231 | else |
|---|
| 232 | return TRUE; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * Parse a human readable string for a number of bytes |
|---|
| 238 | * |
|---|
| 239 | * @param string Input string |
|---|
| 240 | * @return int Number of bytes |
|---|
| 241 | */ |
|---|
| 242 | function parse_bytes($str) |
|---|
| 243 | { |
|---|
| 244 | if (is_numeric($str)) |
|---|
| 245 | return intval($str); |
|---|
| 246 | |
|---|
| 247 | if (preg_match('/([0-9]+)([a-z])/i', $str, $regs)) |
|---|
| 248 | { |
|---|
| 249 | $bytes = floatval($regs[1]); |
|---|
| 250 | switch (strtolower($regs[2])) |
|---|
| 251 | { |
|---|
| 252 | case 'g': |
|---|
| 253 | $bytes *= 1073741824; |
|---|
| 254 | break; |
|---|
| 255 | case 'm': |
|---|
| 256 | $bytes *= 1048576; |
|---|
| 257 | break; |
|---|
| 258 | case 'k': |
|---|
| 259 | $bytes *= 1024; |
|---|
| 260 | break; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | return intval($bytes); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * Create a human readable string for a number of bytes |
|---|
| 269 | * |
|---|
| 270 | * @param int Number of bytes |
|---|
| 271 | * @return string Byte string |
|---|
| 272 | */ |
|---|
| 273 | function show_bytes($bytes) |
|---|
| 274 | { |
|---|
| 275 | if ($bytes > 1073741824) |
|---|
| 276 | { |
|---|
| 277 | $gb = $bytes/1073741824; |
|---|
| 278 | $str = sprintf($gb>=10 ? "%d " : "%.1f ", $gb) . rcube_label('GB'); |
|---|
| 279 | } |
|---|
| 280 | else if ($bytes > 1048576) |
|---|
| 281 | { |
|---|
| 282 | $mb = $bytes/1048576; |
|---|
| 283 | $str = sprintf($mb>=10 ? "%d " : "%.1f ", $mb) . rcube_label('MB'); |
|---|
| 284 | } |
|---|
| 285 | else if ($bytes > 1024) |
|---|
| 286 | $str = sprintf("%d ", round($bytes/1024)) . rcube_label('KB'); |
|---|
| 287 | else |
|---|
| 288 | $str = sprintf('%d ', $bytes) . rcube_label('B'); |
|---|
| 289 | |
|---|
| 290 | return $str; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * Convert paths like ../xxx to an absolute path using a base url |
|---|
| 296 | * |
|---|
| 297 | * @param string Relative path |
|---|
| 298 | * @param string Base URL |
|---|
| 299 | * @return string Absolute URL |
|---|
| 300 | */ |
|---|
| 301 | function make_absolute_url($path, $base_url) |
|---|
| 302 | { |
|---|
| 303 | $host_url = $base_url; |
|---|
| 304 | $abs_path = $path; |
|---|
| 305 | |
|---|
| 306 | // check if path is an absolute URL |
|---|
| 307 | if (preg_match('/^[fhtps]+:\/\//', $path)) |
|---|
| 308 | return $path; |
|---|
| 309 | |
|---|
| 310 | // cut base_url to the last directory |
|---|
| 311 | if (strpos($base_url, '/')>7) |
|---|
| 312 | { |
|---|
| 313 | $host_url = substr($base_url, 0, strpos($base_url, '/')); |
|---|
| 314 | $base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | // $path is absolute |
|---|
| 318 | if ($path{0}=='/') |
|---|
| 319 | $abs_path = $host_url.$path; |
|---|
| 320 | else |
|---|
| 321 | { |
|---|
| 322 | // strip './' because its the same as '' |
|---|
| 323 | $path = preg_replace('/^\.\//', '', $path); |
|---|
| 324 | |
|---|
| 325 | if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) |
|---|
| 326 | foreach ($matches as $a_match) |
|---|
| 327 | { |
|---|
| 328 | if (strrpos($base_url, '/')) |
|---|
| 329 | $base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|---|
| 330 | |
|---|
| 331 | $path = substr($path, 3); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | $abs_path = $base_url.'/'.$path; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | return $abs_path; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | /** |
|---|
| 342 | * Wrapper function for strlen |
|---|
| 343 | */ |
|---|
| 344 | function rc_strlen($str) |
|---|
| 345 | { |
|---|
| 346 | if (function_exists('mb_strlen')) |
|---|
| 347 | return mb_strlen($str); |
|---|
| 348 | else |
|---|
| 349 | return strlen($str); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | /** |
|---|
| 353 | * Wrapper function for strtolower |
|---|
| 354 | */ |
|---|
| 355 | function rc_strtolower($str) |
|---|
| 356 | { |
|---|
| 357 | if (function_exists('mb_strtolower')) |
|---|
| 358 | return mb_strtolower($str); |
|---|
| 359 | else |
|---|
| 360 | return strtolower($str); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | /** |
|---|
| 364 | * Wrapper function for substr |
|---|
| 365 | */ |
|---|
| 366 | function rc_substr($str, $start, $len=null) |
|---|
| 367 | { |
|---|
| 368 | if (function_exists('mb_substr')) |
|---|
| 369 | return mb_substr($str, $start, $len); |
|---|
| 370 | else |
|---|
| 371 | return substr($str, $start, $len); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * Wrapper function for strpos |
|---|
| 376 | */ |
|---|
| 377 | function rc_strpos($haystack, $needle, $offset=0) |
|---|
| 378 | { |
|---|
| 379 | if (function_exists('mb_strpos')) |
|---|
| 380 | return mb_strpos($haystack, $needle, $offset); |
|---|
| 381 | else |
|---|
| 382 | return strpos($haystack, $needle, $offset); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | /** |
|---|
| 386 | * Wrapper function for strrpos |
|---|
| 387 | */ |
|---|
| 388 | function rc_strrpos($haystack, $needle, $offset=0) |
|---|
| 389 | { |
|---|
| 390 | if (function_exists('mb_strrpos')) |
|---|
| 391 | return mb_strrpos($haystack, $needle, $offset); |
|---|
| 392 | else |
|---|
| 393 | return strrpos($haystack, $needle, $offset); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | /** |
|---|
| 398 | * Read a specific HTTP request header |
|---|
| 399 | * |
|---|
| 400 | * @access static |
|---|
| 401 | * @param string $name Header name |
|---|
| 402 | * @return mixed Header value or null if not available |
|---|
| 403 | */ |
|---|
| 404 | function rc_request_header($name) |
|---|
| 405 | { |
|---|
| 406 | if (function_exists('getallheaders')) |
|---|
| 407 | { |
|---|
| 408 | $hdrs = array_change_key_case(getallheaders(), CASE_UPPER); |
|---|
| 409 | $key = strtoupper($name); |
|---|
| 410 | } |
|---|
| 411 | else |
|---|
| 412 | { |
|---|
| 413 | $key = 'HTTP_' . strtoupper(strtr($name, '-', '_')); |
|---|
| 414 | $hdrs = array_change_key_case($_SERVER, CASE_UPPER); |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | return $hdrs[$key]; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | /** |
|---|
| 422 | * Replace the middle part of a string with ... |
|---|
| 423 | * if it is longer than the allowed length |
|---|
| 424 | * |
|---|
| 425 | * @param string Input string |
|---|
| 426 | * @param int Max. length |
|---|
| 427 | * @param string Replace removed chars with this |
|---|
| 428 | * @return string Abbreviated string |
|---|
| 429 | */ |
|---|
| 430 | function abbreviate_string($str, $maxlength, $place_holder='...') |
|---|
| 431 | { |
|---|
| 432 | $length = rc_strlen($str); |
|---|
| 433 | $first_part_length = floor($maxlength/2) - rc_strlen($place_holder); |
|---|
| 434 | |
|---|
| 435 | if ($length > $maxlength) |
|---|
| 436 | { |
|---|
| 437 | $second_starting_location = $length - $maxlength + $first_part_length + 1; |
|---|
| 438 | $str = rc_substr($str, 0, $first_part_length) . $place_holder . rc_substr($str, $second_starting_location, $length); |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | return $str; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | /** |
|---|
| 446 | * Make sure the string ends with a slash |
|---|
| 447 | */ |
|---|
| 448 | function slashify($str) |
|---|
| 449 | { |
|---|
| 450 | return unslashify($str).'/'; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | /** |
|---|
| 455 | * Remove slash at the end of the string |
|---|
| 456 | */ |
|---|
| 457 | function unslashify($str) |
|---|
| 458 | { |
|---|
| 459 | return preg_replace('/\/$/', '', $str); |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | /** |
|---|
| 464 | * Delete all files within a folder |
|---|
| 465 | * |
|---|
| 466 | * @param string Path to directory |
|---|
| 467 | * @return boolean True on success, False if directory was not found |
|---|
| 468 | */ |
|---|
| 469 | function clear_directory($dir_path) |
|---|
| 470 | { |
|---|
| 471 | $dir = @opendir($dir_path); |
|---|
| 472 | if(!$dir) return FALSE; |
|---|
| 473 | |
|---|
| 474 | while ($file = readdir($dir)) |
|---|
| 475 | if (strlen($file)>2) |
|---|
| 476 | unlink("$dir_path/$file"); |
|---|
| 477 | |
|---|
| 478 | closedir($dir); |
|---|
| 479 | return TRUE; |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | /** |
|---|
| 484 | * Create a unix timestamp with a specified offset from now |
|---|
| 485 | * |
|---|
| 486 | * @param string String representation of the offset (e.g. 20min, 5h, 2days) |
|---|
| 487 | * @param int Factor to multiply with the offset |
|---|
| 488 | * @return int Unix timestamp |
|---|
| 489 | */ |
|---|
| 490 | function get_offset_time($offset_str, $factor=1) |
|---|
| 491 | { |
|---|
| 492 | if (preg_match('/^([0-9]+)\s*([smhdw])/i', $offset_str, $regs)) |
|---|
| 493 | { |
|---|
| 494 | $amount = (int)$regs[1]; |
|---|
| 495 | $unit = strtolower($regs[2]); |
|---|
| 496 | } |
|---|
| 497 | else |
|---|
| 498 | { |
|---|
| 499 | $amount = (int)$offset_str; |
|---|
| 500 | $unit = 's'; |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | $ts = mktime(); |
|---|
| 504 | switch ($unit) |
|---|
| 505 | { |
|---|
| 506 | case 'w': |
|---|
| 507 | $amount *= 7; |
|---|
| 508 | case 'd': |
|---|
| 509 | $amount *= 24; |
|---|
| 510 | case 'h': |
|---|
| 511 | $amount *= 60; |
|---|
| 512 | case 'm': |
|---|
| 513 | $amount *= 60; |
|---|
| 514 | case 's': |
|---|
| 515 | $ts += $amount * $factor; |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | return $ts; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | /** |
|---|
| 523 | * A method to guess the mime_type of an attachment. |
|---|
| 524 | * |
|---|
| 525 | * @param string $path Path to the file. |
|---|
| 526 | * @param string $failover Mime type supplied for failover. |
|---|
| 527 | * |
|---|
| 528 | * @return string |
|---|
| 529 | * @author Till Klampaeckel <till@php.net> |
|---|
| 530 | * @see http://de2.php.net/manual/en/ref.fileinfo.php |
|---|
| 531 | * @see http://de2.php.net/mime_content_type |
|---|
| 532 | */ |
|---|
| 533 | function rc_mime_content_type($path, $failover = 'application/octet-stream') |
|---|
| 534 | { |
|---|
| 535 | $mime_type = null; |
|---|
| 536 | $mime_magic = rcmail::get_instance()->config->get('mime_magic'); |
|---|
| 537 | |
|---|
| 538 | if (!extension_loaded('fileinfo')) { |
|---|
| 539 | @dl('fileinfo.' . PHP_SHLIB_SUFFIX); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | if (function_exists('finfo_open')) { |
|---|
| 543 | if ($finfo = finfo_open(FILEINFO_MIME, $mime_magic)) { |
|---|
| 544 | $mime_type = finfo_file($finfo, $path); |
|---|
| 545 | finfo_close($finfo); |
|---|
| 546 | } |
|---|
| 547 | } |
|---|
| 548 | if (!$mime_type && function_exists('mime_content_type')) { |
|---|
| 549 | $mime_type = mime_content_type($path); |
|---|
| 550 | } |
|---|
| 551 | |
|---|
| 552 | if (!$mime_type) { |
|---|
| 553 | $mime_type = $failover; |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | return $mime_type; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | /** |
|---|
| 561 | * A method to guess encoding of a string. |
|---|
| 562 | * |
|---|
| 563 | * @param string $string String. |
|---|
| 564 | * @param string $failover Default result for failover. |
|---|
| 565 | * |
|---|
| 566 | * @return string |
|---|
| 567 | */ |
|---|
| 568 | function rc_detect_encoding($string, $failover='') |
|---|
| 569 | { |
|---|
| 570 | if (!function_exists('mb_detect_encoding')) { |
|---|
| 571 | return $failover; |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | // FIXME: the order is important, because sometimes |
|---|
| 575 | // iso string is detected as euc-jp and etc. |
|---|
| 576 | $enc = array( |
|---|
| 577 | 'SJIS', 'BIG5', 'GB2312', 'UTF-8', |
|---|
| 578 | 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', |
|---|
| 579 | 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', |
|---|
| 580 | 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', |
|---|
| 581 | 'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', |
|---|
| 582 | 'ISO-2022-KR', 'ISO-2022-JP' |
|---|
| 583 | ); |
|---|
| 584 | |
|---|
| 585 | $result = mb_detect_encoding($string, join(',', $enc)); |
|---|
| 586 | |
|---|
| 587 | return $result ? $result : $failover; |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | ?> |
|---|