| 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 | * Provide details about the client's browser |
|---|
| 32 | * |
|---|
| 33 | * @return array Key-value pairs of browser properties |
|---|
| 34 | */ |
|---|
| 35 | function rcube_browser() |
|---|
| 36 | { |
|---|
| 37 | $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT']; |
|---|
| 38 | |
|---|
| 39 | $bw['ver'] = 0; |
|---|
| 40 | $bw['win'] = stristr($HTTP_USER_AGENT, 'win'); |
|---|
| 41 | $bw['mac'] = stristr($HTTP_USER_AGENT, 'mac'); |
|---|
| 42 | $bw['linux'] = stristr($HTTP_USER_AGENT, 'linux'); |
|---|
| 43 | $bw['unix'] = stristr($HTTP_USER_AGENT, 'unix'); |
|---|
| 44 | |
|---|
| 45 | $bw['ns4'] = stristr($HTTP_USER_AGENT, 'mozilla/4') && !stristr($HTTP_USER_AGENT, 'msie'); |
|---|
| 46 | $bw['ns'] = ($bw['ns4'] || stristr($HTTP_USER_AGENT, 'netscape')); |
|---|
| 47 | $bw['ie'] = stristr($HTTP_USER_AGENT, 'msie'); |
|---|
| 48 | $bw['mz'] = stristr($HTTP_USER_AGENT, 'mozilla/5'); |
|---|
| 49 | $bw['opera'] = stristr($HTTP_USER_AGENT, 'opera'); |
|---|
| 50 | $bw['safari'] = stristr($HTTP_USER_AGENT, 'safari'); |
|---|
| 51 | |
|---|
| 52 | if($bw['ns']) |
|---|
| 53 | { |
|---|
| 54 | $test = eregi("mozilla\/([0-9\.]+)", $HTTP_USER_AGENT, $regs); |
|---|
| 55 | $bw['ver'] = $test ? (float)$regs[1] : 0; |
|---|
| 56 | } |
|---|
| 57 | if($bw['mz']) |
|---|
| 58 | { |
|---|
| 59 | $test = ereg("rv:([0-9\.]+)", $HTTP_USER_AGENT, $regs); |
|---|
| 60 | $bw['ver'] = $test ? (float)$regs[1] : 0; |
|---|
| 61 | } |
|---|
| 62 | if($bw['ie']) |
|---|
| 63 | { |
|---|
| 64 | $test = eregi("msie ([0-9\.]+)", $HTTP_USER_AGENT, $regs); |
|---|
| 65 | $bw['ver'] = $test ? (float)$regs[1] : 0; |
|---|
| 66 | } |
|---|
| 67 | if($bw['opera']) |
|---|
| 68 | { |
|---|
| 69 | $test = eregi("opera ([0-9\.]+)", $HTTP_USER_AGENT, $regs); |
|---|
| 70 | $bw['ver'] = $test ? (float)$regs[1] : 0; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | if(eregi(" ([a-z]{2})-([a-z]{2})", $HTTP_USER_AGENT, $regs)) |
|---|
| 74 | $bw['lang'] = $regs[1]; |
|---|
| 75 | else |
|---|
| 76 | $bw['lang'] = 'en'; |
|---|
| 77 | |
|---|
| 78 | $bw['dom'] = ($bw['mz'] || $bw['safari'] || ($bw['ie'] && $bw['ver']>=5) || ($bw['opera'] && $bw['ver']>=7)); |
|---|
| 79 | $bw['pngalpha'] = $bw['mz'] || $bw['safari'] || ($bw['ie'] && $bw['ver']>=5.5) || |
|---|
| 80 | ($bw['ie'] && $bw['ver']>=5 && $bw['mac']) || ($bw['opera'] && $bw['ver']>=7) ? TRUE : FALSE; |
|---|
| 81 | |
|---|
| 82 | return $bw; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Get localized text in the desired language |
|---|
| 88 | * |
|---|
| 89 | * @param mixed Named parameters array or label name |
|---|
| 90 | * @return string Localized text |
|---|
| 91 | */ |
|---|
| 92 | function rcube_label($attrib) |
|---|
| 93 | { |
|---|
| 94 | global $sess_user_lang, $INSTALL_PATH, $OUTPUT; |
|---|
| 95 | static $sa_text_data, $s_language, $utf8_decode; |
|---|
| 96 | |
|---|
| 97 | // extract attributes |
|---|
| 98 | if (is_string($attrib)) |
|---|
| 99 | $attrib = array('name' => $attrib); |
|---|
| 100 | |
|---|
| 101 | $nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1; |
|---|
| 102 | $vars = isset($attrib['vars']) ? $attrib['vars'] : ''; |
|---|
| 103 | |
|---|
| 104 | $command_name = !empty($attrib['command']) ? $attrib['command'] : NULL; |
|---|
| 105 | $alias = $attrib['name'] ? $attrib['name'] : ($command_name && $command_label_map[$command_name] ? $command_label_map[$command_name] : ''); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | // load localized texts |
|---|
| 109 | if (!$sa_text_data || $s_language != $sess_user_lang) |
|---|
| 110 | { |
|---|
| 111 | $sa_text_data = array(); |
|---|
| 112 | |
|---|
| 113 | // get english labels (these should be complete) |
|---|
| 114 | @include($INSTALL_PATH.'program/localization/en_US/labels.inc'); |
|---|
| 115 | @include($INSTALL_PATH.'program/localization/en_US/messages.inc'); |
|---|
| 116 | |
|---|
| 117 | if (is_array($labels)) |
|---|
| 118 | $sa_text_data = $labels; |
|---|
| 119 | if (is_array($messages)) |
|---|
| 120 | $sa_text_data = array_merge($sa_text_data, $messages); |
|---|
| 121 | |
|---|
| 122 | // include user language files |
|---|
| 123 | if ($sess_user_lang!='en' && is_dir($INSTALL_PATH.'program/localization/'.$sess_user_lang)) |
|---|
| 124 | { |
|---|
| 125 | include_once($INSTALL_PATH.'program/localization/'.$sess_user_lang.'/labels.inc'); |
|---|
| 126 | include_once($INSTALL_PATH.'program/localization/'.$sess_user_lang.'/messages.inc'); |
|---|
| 127 | |
|---|
| 128 | if (is_array($labels)) |
|---|
| 129 | $sa_text_data = array_merge($sa_text_data, $labels); |
|---|
| 130 | if (is_array($messages)) |
|---|
| 131 | $sa_text_data = array_merge($sa_text_data, $messages); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | $s_language = $sess_user_lang; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | // text does not exist |
|---|
| 138 | if (!($text_item = $sa_text_data[$alias])) |
|---|
| 139 | { |
|---|
| 140 | /* |
|---|
| 141 | raise_error(array( |
|---|
| 142 | 'code' => 500, |
|---|
| 143 | 'type' => 'php', |
|---|
| 144 | 'line' => __LINE__, |
|---|
| 145 | 'file' => __FILE__, |
|---|
| 146 | 'message' => "Missing localized text for '$alias' in '$sess_user_lang'"), TRUE, FALSE); |
|---|
| 147 | */ |
|---|
| 148 | return "[$alias]"; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | // make text item array |
|---|
| 152 | $a_text_item = is_array($text_item) ? $text_item : array('single' => $text_item); |
|---|
| 153 | |
|---|
| 154 | // decide which text to use |
|---|
| 155 | if ($nr==1) |
|---|
| 156 | $text = $a_text_item['single']; |
|---|
| 157 | else if ($nr>0) |
|---|
| 158 | $text = $a_text_item['multiple']; |
|---|
| 159 | else if ($nr==0) |
|---|
| 160 | { |
|---|
| 161 | if ($a_text_item['none']) |
|---|
| 162 | $text = $a_text_item['none']; |
|---|
| 163 | else if ($a_text_item['single']) |
|---|
| 164 | $text = $a_text_item['single']; |
|---|
| 165 | else if ($a_text_item['multiple']) |
|---|
| 166 | $text = $a_text_item['multiple']; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | // default text is single |
|---|
| 170 | if ($text=='') |
|---|
| 171 | $text = $a_text_item['single']; |
|---|
| 172 | |
|---|
| 173 | // replace vars in text |
|---|
| 174 | if (is_array($attrib['vars'])) |
|---|
| 175 | { |
|---|
| 176 | foreach ($attrib['vars'] as $var_key=>$var_value) |
|---|
| 177 | $a_replace_vars[substr($var_key, 0, 1)=='$' ? substr($var_key, 1) : $var_key] = $var_value; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | if ($a_replace_vars) |
|---|
| 181 | $text = preg_replace('/\${?([_a-z]{1}[_a-z0-9]*)}?/ei', '$a_replace_vars["\1"]', $text); |
|---|
| 182 | |
|---|
| 183 | // remove variables in text which were not available in arg $vars and $nr |
|---|
| 184 | eval("\$text = <<<EOF |
|---|
| 185 | $text |
|---|
| 186 | EOF; |
|---|
| 187 | "); |
|---|
| 188 | |
|---|
| 189 | // format output |
|---|
| 190 | if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) |
|---|
| 191 | return ucfirst($text); |
|---|
| 192 | else if ($attrib['uppercase']) |
|---|
| 193 | return strtoupper($text); |
|---|
| 194 | else if ($attrib['lowercase']) |
|---|
| 195 | return strtolower($text); |
|---|
| 196 | |
|---|
| 197 | return $text; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | /** |
|---|
| 202 | * Send HTTP headers to prevent caching this page |
|---|
| 203 | */ |
|---|
| 204 | function send_nocacheing_headers() |
|---|
| 205 | { |
|---|
| 206 | if (headers_sent()) |
|---|
| 207 | return; |
|---|
| 208 | |
|---|
| 209 | header("Expires: ".gmdate("D, d M Y H:i:s")." GMT"); |
|---|
| 210 | header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); |
|---|
| 211 | header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); |
|---|
| 212 | header("Pragma: no-cache"); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * Send header with expire date 30 days in future |
|---|
| 218 | * |
|---|
| 219 | * @param int Expiration time in seconds |
|---|
| 220 | */ |
|---|
| 221 | function send_future_expire_header($offset=2600000) |
|---|
| 222 | { |
|---|
| 223 | if (headers_sent()) |
|---|
| 224 | return; |
|---|
| 225 | |
|---|
| 226 | header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+$offset)." GMT"); |
|---|
| 227 | header("Cache-Control: max-age=$offset"); |
|---|
| 228 | header("Pragma: "); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | /** |
|---|
| 233 | * Check request for If-Modified-Since and send an according response. |
|---|
| 234 | * This will terminate the current script if headers match the given values |
|---|
| 235 | * |
|---|
| 236 | * @param int Modified date as unix timestamp |
|---|
| 237 | * @param string Etag value for caching |
|---|
| 238 | */ |
|---|
| 239 | function send_modified_header($mdate, $etag=null) |
|---|
| 240 | { |
|---|
| 241 | if (headers_sent()) |
|---|
| 242 | return; |
|---|
| 243 | |
|---|
| 244 | $iscached = false; |
|---|
| 245 | if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mdate) |
|---|
| 246 | $iscached = true; |
|---|
| 247 | |
|---|
| 248 | $etag = $etag ? "\"$etag\"" : null; |
|---|
| 249 | if ($etag && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) |
|---|
| 250 | $iscached = true; |
|---|
| 251 | |
|---|
| 252 | if ($iscached) |
|---|
| 253 | header("HTTP/1.x 304 Not Modified"); |
|---|
| 254 | else |
|---|
| 255 | header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mdate)." GMT"); |
|---|
| 256 | |
|---|
| 257 | header("Cache-Control: max-age=0"); |
|---|
| 258 | header("Expires: "); |
|---|
| 259 | header("Pragma: "); |
|---|
| 260 | |
|---|
| 261 | if ($etag) |
|---|
| 262 | header("Etag: $etag"); |
|---|
| 263 | |
|---|
| 264 | if ($iscached) |
|---|
| 265 | exit; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | /** |
|---|
| 270 | * Convert a variable into a javascript object notation |
|---|
| 271 | * |
|---|
| 272 | * @param mixed Input value |
|---|
| 273 | * @return string Serialized JSON string |
|---|
| 274 | */ |
|---|
| 275 | function json_serialize($var) |
|---|
| 276 | { |
|---|
| 277 | if (is_object($var)) |
|---|
| 278 | $var = get_object_vars($var); |
|---|
| 279 | |
|---|
| 280 | if (is_array($var)) |
|---|
| 281 | { |
|---|
| 282 | // empty array |
|---|
| 283 | if (!sizeof($var)) |
|---|
| 284 | return '[]'; |
|---|
| 285 | else |
|---|
| 286 | { |
|---|
| 287 | $keys_arr = array_keys($var); |
|---|
| 288 | $is_assoc = $have_numeric = 0; |
|---|
| 289 | |
|---|
| 290 | for ($i=0; $i<sizeof($keys_arr); ++$i) |
|---|
| 291 | { |
|---|
| 292 | if (is_numeric($keys_arr[$i])) |
|---|
| 293 | $have_numeric = 1; |
|---|
| 294 | if (!is_numeric($keys_arr[$i]) || $keys_arr[$i] != $i) |
|---|
| 295 | $is_assoc = 1; |
|---|
| 296 | if ($is_assoc && $have_numeric) |
|---|
| 297 | break; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | $brackets = $is_assoc ? '{}' : '[]'; |
|---|
| 301 | $pairs = array(); |
|---|
| 302 | |
|---|
| 303 | foreach ($var as $key => $value) |
|---|
| 304 | { |
|---|
| 305 | // enclose key with quotes if it is not variable-name conform |
|---|
| 306 | if (!ereg("^[_a-zA-Z]{1}[_a-zA-Z0-9]*$", $key) /* || is_js_reserved_word($key) */) |
|---|
| 307 | $key = "'$key'"; |
|---|
| 308 | |
|---|
| 309 | $pairs[] = sprintf("%s%s", $is_assoc ? "$key:" : '', json_serialize($value)); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | return $brackets{0} . implode(',', $pairs) . $brackets{1}; |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | else if (is_numeric($var) && strval(intval($var)) === strval($var)) |
|---|
| 316 | return $var; |
|---|
| 317 | else if (is_bool($var)) |
|---|
| 318 | return $var ? '1' : '0'; |
|---|
| 319 | else |
|---|
| 320 | return "'".JQ($var)."'"; |
|---|
| 321 | |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | /** |
|---|
| 325 | * Function to convert an array to a javascript array |
|---|
| 326 | * Actually an alias function for json_serialize() |
|---|
| 327 | * @deprecated |
|---|
| 328 | */ |
|---|
| 329 | function array2js($arr, $type='') |
|---|
| 330 | { |
|---|
| 331 | return json_serialize($arr); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * Similar function as in_array() but case-insensitive |
|---|
| 337 | * |
|---|
| 338 | * @param mixed Needle value |
|---|
| 339 | * @param array Array to search in |
|---|
| 340 | * @return boolean True if found, False if not |
|---|
| 341 | */ |
|---|
| 342 | function in_array_nocase($needle, $haystack) |
|---|
| 343 | { |
|---|
| 344 | foreach ($haystack as $value) |
|---|
| 345 | if (strtolower($needle)===strtolower($value)) |
|---|
| 346 | return true; |
|---|
| 347 | |
|---|
| 348 | return false; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | /** |
|---|
| 353 | * Find out if the string content means TRUE or FALSE |
|---|
| 354 | * |
|---|
| 355 | * @param string Input value |
|---|
| 356 | * @return boolean Imagine what! |
|---|
| 357 | */ |
|---|
| 358 | function get_boolean($str) |
|---|
| 359 | { |
|---|
| 360 | $str = strtolower($str); |
|---|
| 361 | if(in_array($str, array('false', '0', 'no', 'nein', ''), TRUE)) |
|---|
| 362 | return FALSE; |
|---|
| 363 | else |
|---|
| 364 | return TRUE; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | /** |
|---|
| 369 | * Parse a human readable string for a number of bytes |
|---|
| 370 | * |
|---|
| 371 | * @param string Input string |
|---|
| 372 | * @return int Number of bytes |
|---|
| 373 | */ |
|---|
| 374 | function parse_bytes($str) |
|---|
| 375 | { |
|---|
| 376 | if (is_numeric($str)) |
|---|
| 377 | return intval($str); |
|---|
| 378 | |
|---|
| 379 | if (preg_match('/([0-9]+)([a-z])/i', $str, $regs)) |
|---|
| 380 | { |
|---|
| 381 | $bytes = floatval($regs[1]); |
|---|
| 382 | switch (strtolower($regs[2])) |
|---|
| 383 | { |
|---|
| 384 | case 'g': |
|---|
| 385 | $bytes *= 1073741824; |
|---|
| 386 | break; |
|---|
| 387 | case 'm': |
|---|
| 388 | $bytes *= 1048576; |
|---|
| 389 | break; |
|---|
| 390 | case 'k': |
|---|
| 391 | $bytes *= 1024; |
|---|
| 392 | break; |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | return intval($bytes); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | /** |
|---|
| 400 | * Create a human readable string for a number of bytes |
|---|
| 401 | * |
|---|
| 402 | * @param int Number of bytes |
|---|
| 403 | * @return string Byte string |
|---|
| 404 | */ |
|---|
| 405 | function show_bytes($bytes) |
|---|
| 406 | { |
|---|
| 407 | if ($bytes > 1073741824) |
|---|
| 408 | { |
|---|
| 409 | $gb = $bytes/1073741824; |
|---|
| 410 | $str = sprintf($gb>=10 ? "%d GB" : "%.1f GB", $gb); |
|---|
| 411 | } |
|---|
| 412 | else if ($bytes > 1048576) |
|---|
| 413 | { |
|---|
| 414 | $mb = $bytes/1048576; |
|---|
| 415 | $str = sprintf($mb>=10 ? "%d MB" : "%.1f MB", $mb); |
|---|
| 416 | } |
|---|
| 417 | else if ($bytes > 1024) |
|---|
| 418 | $str = sprintf("%d KB", round($bytes/1024)); |
|---|
| 419 | else |
|---|
| 420 | $str = sprintf('%d B', $bytes); |
|---|
| 421 | |
|---|
| 422 | return $str; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | /** |
|---|
| 427 | * Convert paths like ../xxx to an absolute path using a base url |
|---|
| 428 | * |
|---|
| 429 | * @param string Relative path |
|---|
| 430 | * @param string Base URL |
|---|
| 431 | * @return string Absolute URL |
|---|
| 432 | */ |
|---|
| 433 | function make_absolute_url($path, $base_url) |
|---|
| 434 | { |
|---|
| 435 | $host_url = $base_url; |
|---|
| 436 | $abs_path = $path; |
|---|
| 437 | |
|---|
| 438 | // cut base_url to the last directory |
|---|
| 439 | if (strpos($base_url, '/')>7) |
|---|
| 440 | { |
|---|
| 441 | $host_url = substr($base_url, 0, strpos($base_url, '/')); |
|---|
| 442 | $base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | // $path is absolute |
|---|
| 446 | if ($path{0}=='/') |
|---|
| 447 | $abs_path = $host_url.$path; |
|---|
| 448 | else |
|---|
| 449 | { |
|---|
| 450 | // strip './' because its the same as '' |
|---|
| 451 | $path = preg_replace('/^\.\//', '', $path); |
|---|
| 452 | |
|---|
| 453 | if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) |
|---|
| 454 | foreach ($matches as $a_match) |
|---|
| 455 | { |
|---|
| 456 | if (strrpos($base_url, '/')) |
|---|
| 457 | $base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|---|
| 458 | |
|---|
| 459 | $path = substr($path, 3); |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | $abs_path = $base_url.'/'.$path; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | return $abs_path; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | /** |
|---|
| 470 | * Wrapper function for strlen |
|---|
| 471 | */ |
|---|
| 472 | function rc_strlen($str) |
|---|
| 473 | { |
|---|
| 474 | if (function_exists('mb_strlen')) |
|---|
| 475 | return mb_strlen($str); |
|---|
| 476 | else |
|---|
| 477 | return strlen($str); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | /** |
|---|
| 481 | * Wrapper function for strtolower |
|---|
| 482 | */ |
|---|
| 483 | function rc_strtolower($str) |
|---|
| 484 | { |
|---|
| 485 | if (function_exists('mb_strtolower')) |
|---|
| 486 | return mb_strtolower($str); |
|---|
| 487 | else |
|---|
| 488 | return strtolower($str); |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | /** |
|---|
| 492 | * Wrapper function for substr |
|---|
| 493 | */ |
|---|
| 494 | function rc_substr($str, $start, $len=null) |
|---|
| 495 | { |
|---|
| 496 | if (function_exists('mb_substr')) |
|---|
| 497 | return mb_substr($str, $start, $len); |
|---|
| 498 | else |
|---|
| 499 | return substr($str, $start, $len); |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | /** |
|---|
| 503 | * Wrapper function for strpos |
|---|
| 504 | */ |
|---|
| 505 | function rc_strpos($haystack, $needle, $offset=0) |
|---|
| 506 | { |
|---|
| 507 | if (function_exists('mb_strpos')) |
|---|
| 508 | return mb_strpos($haystack, $needle, $offset); |
|---|
| 509 | else |
|---|
| 510 | return strpos($haystack, $needle, $offset); |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | /** |
|---|
| 514 | * Wrapper function for strrpos |
|---|
| 515 | */ |
|---|
| 516 | function rc_strrpos($haystack, $needle, $offset=0) |
|---|
| 517 | { |
|---|
| 518 | if (function_exists('mb_strrpos')) |
|---|
| 519 | return mb_strrpos($haystack, $needle, $offset); |
|---|
| 520 | else |
|---|
| 521 | return strrpos($haystack, $needle, $offset); |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | /** |
|---|
| 526 | * Read a specific HTTP request header |
|---|
| 527 | * |
|---|
| 528 | * @param string Header name |
|---|
| 529 | * @return string Header value or null if not available |
|---|
| 530 | */ |
|---|
| 531 | function rc_request_header($name) |
|---|
| 532 | { |
|---|
| 533 | if (function_exists('getallheaders')) |
|---|
| 534 | { |
|---|
| 535 | $hdrs = getallheaders(); |
|---|
| 536 | return $hdrs[$name]; |
|---|
| 537 | } |
|---|
| 538 | else |
|---|
| 539 | { |
|---|
| 540 | $key = "HTTP_" . strtoupper(strtr($name, "-", "_")); |
|---|
| 541 | return $_SERVER[$key]; |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | /** |
|---|
| 547 | * Replace the middle part of a string with ... |
|---|
| 548 | * if it is longer than the allowed length |
|---|
| 549 | * |
|---|
| 550 | * @param string Input string |
|---|
| 551 | * @param int Max. length |
|---|
| 552 | * @param string Replace removed chars with this |
|---|
| 553 | * @return string Abbrevated string |
|---|
| 554 | */ |
|---|
| 555 | function abbrevate_string($str, $maxlength, $place_holder='...') |
|---|
| 556 | { |
|---|
| 557 | $length = rc_strlen($str); |
|---|
| 558 | $first_part_length = floor($maxlength/2) - rc_strlen($place_holder); |
|---|
| 559 | |
|---|
| 560 | if ($length > $maxlength) |
|---|
| 561 | { |
|---|
| 562 | $second_starting_location = $length - $maxlength + $first_part_length + 1; |
|---|
| 563 | $str = rc_substr($str, 0, $first_part_length) . $place_holder . rc_substr($str, $second_starting_location, $length); |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | return $str; |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | /** |
|---|
| 571 | * Make sure the string ends with a slash |
|---|
| 572 | */ |
|---|
| 573 | function slashify($str) |
|---|
| 574 | { |
|---|
| 575 | return unslashify($str).'/'; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | /** |
|---|
| 580 | * Remove slash at the end of the string |
|---|
| 581 | */ |
|---|
| 582 | function unslashify($str) |
|---|
| 583 | { |
|---|
| 584 | return preg_replace('/\/$/', '', $str); |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | /** |
|---|
| 589 | * Delete all files within a folder |
|---|
| 590 | * |
|---|
| 591 | * @param string Path to directory |
|---|
| 592 | * @return boolean True on success, False if directory was not found |
|---|
| 593 | */ |
|---|
| 594 | function clear_directory($dir_path) |
|---|
| 595 | { |
|---|
| 596 | $dir = @opendir($dir_path); |
|---|
| 597 | if(!$dir) return FALSE; |
|---|
| 598 | |
|---|
| 599 | while ($file = readdir($dir)) |
|---|
| 600 | if (strlen($file)>2) |
|---|
| 601 | unlink("$dir_path/$file"); |
|---|
| 602 | |
|---|
| 603 | closedir($dir); |
|---|
| 604 | return TRUE; |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | /** |
|---|
| 609 | * Create a unix timestamp with a specified offset from now |
|---|
| 610 | * |
|---|
| 611 | * @param string String representation of the offset (e.g. 20min, 5h, 2days) |
|---|
| 612 | * @param int Factor to multiply with the offset |
|---|
| 613 | * @return int Unix timestamp |
|---|
| 614 | */ |
|---|
| 615 | function get_offset_time($offset_str, $factor=1) |
|---|
| 616 | { |
|---|
| 617 | if (preg_match('/^([0-9]+)\s*([smhdw])/i', $offset_str, $regs)) |
|---|
| 618 | { |
|---|
| 619 | $amount = (int)$regs[1]; |
|---|
| 620 | $unit = strtolower($regs[2]); |
|---|
| 621 | } |
|---|
| 622 | else |
|---|
| 623 | { |
|---|
| 624 | $amount = (int)$offset_str; |
|---|
| 625 | $unit = 's'; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | $ts = mktime(); |
|---|
| 629 | switch ($unit) |
|---|
| 630 | { |
|---|
| 631 | case 'w': |
|---|
| 632 | $amount *= 7; |
|---|
| 633 | case 'd': |
|---|
| 634 | $amount *= 24; |
|---|
| 635 | case 'h': |
|---|
| 636 | $amount *= 60; |
|---|
| 637 | case 'm': |
|---|
| 638 | $amount *= 60; |
|---|
| 639 | case 's': |
|---|
| 640 | $ts += $amount * $factor; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | return $ts; |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | /** |
|---|
| 648 | * Return the last occurence of a string in another string |
|---|
| 649 | * |
|---|
| 650 | * @param haystack string string in which to search |
|---|
| 651 | * @param needle string string for which to search |
|---|
| 652 | * @return index of needle within haystack, or false if not found |
|---|
| 653 | */ |
|---|
| 654 | function strrstr($haystack, $needle) |
|---|
| 655 | { |
|---|
| 656 | $pver = phpversion(); |
|---|
| 657 | if ($pver[0] >= 5) |
|---|
| 658 | return strrpos($haystack, $needle); |
|---|
| 659 | else |
|---|
| 660 | { |
|---|
| 661 | $index = strpos(strrev($haystack), strrev($needle)); |
|---|
| 662 | if($index === false) |
|---|
| 663 | return false; |
|---|
| 664 | |
|---|
| 665 | $index = strlen($haystack) - strlen($needle) - $index; |
|---|
| 666 | return $index; |
|---|
| 667 | } |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | ?> |
|---|