| | |
| | | |
| | | |
| | | /** |
| | | * Read a specific HTTP request header. |
| | | * |
| | | * @param string $name Header name |
| | | * |
| | | * @return mixed Header value or null if not available |
| | | */ |
| | | function rcube_request_header($name) |
| | | { |
| | | if (function_exists('getallheaders')) { |
| | | $hdrs = array_change_key_case(getallheaders(), CASE_UPPER); |
| | | $key = strtoupper($name); |
| | | } |
| | | else { |
| | | $key = 'HTTP_' . strtoupper(strtr($name, '-', '_')); |
| | | $hdrs = array_change_key_case($_SERVER, CASE_UPPER); |
| | | } |
| | | |
| | | return $hdrs[$key]; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Make sure the string ends with a slash |
| | | */ |
| | | function slashify($str) |
| | |
| | | |
| | | |
| | | /** |
| | | * Create a unix timestamp with a specified offset from now. |
| | | * Returns number of seconds for a specified offset string. |
| | | * |
| | | * @param string $offset_str String representation of the offset (e.g. 20min, 5h, 2days) |
| | | * @param int $factor Factor to multiply with the offset |
| | | * @param string $str String representation of the offset (e.g. 20min, 5h, 2days, 1week) |
| | | * |
| | | * @return int Unix timestamp |
| | | * @return int Number of seconds |
| | | */ |
| | | function get_offset_time($offset_str, $factor=1) |
| | | function get_offset_sec($str) |
| | | { |
| | | if (preg_match('/^([0-9]+)\s*([smhdw])/i', $offset_str, $regs)) { |
| | | $amount = (int)$regs[1]; |
| | | if (preg_match('/^([0-9]+)\s*([smhdw])/i', $str, $regs)) { |
| | | $amount = (int) $regs[1]; |
| | | $unit = strtolower($regs[2]); |
| | | } |
| | | else { |
| | | $amount = (int)$offset_str; |
| | | $amount = (int) $str; |
| | | $unit = 's'; |
| | | } |
| | | |
| | | $ts = mktime(); |
| | | switch ($unit) { |
| | | case 'w': |
| | | $amount *= 7; |
| | |
| | | $amount *= 60; |
| | | case 'm': |
| | | $amount *= 60; |
| | | case 's': |
| | | $ts += $amount * $factor; |
| | | } |
| | | |
| | | return $ts; |
| | | return $amount; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Create a unix timestamp with a specified offset from now. |
| | | * |
| | | * @param string $offset_str String representation of the offset (e.g. 20min, 5h, 2days) |
| | | * @param int $factor Factor to multiply with the offset |
| | | * |
| | | * @return int Unix timestamp |
| | | */ |
| | | function get_offset_time($offset_str, $factor=1) |
| | | { |
| | | return time() + get_offset_sec($offset_str) * $factor; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | return $str; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Explode quoted string |
| | | * |
| | | * @param string Delimiter expression string for preg_match() |
| | | * @param string Input string |
| | | */ |
| | | function rcube_explode_quoted_string($delimiter, $string) |
| | | { |
| | | $result = array(); |
| | | $strlen = strlen($string); |
| | | |
| | | for ($q=$p=$i=0; $i < $strlen; $i++) { |
| | | if ($string[$i] == "\"" && $string[$i-1] != "\\") { |
| | | $q = $q ? false : true; |
| | | } |
| | | else if (!$q && preg_match("/$delimiter/", $string[$i])) { |
| | | $result[] = substr($string, $p, $i - $p); |
| | | $p = $i + 1; |
| | | } |
| | | } |
| | | |
| | | $result[] = substr($string, $p); |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | |
| | |
| | | function strip_newlines($str) |
| | | { |
| | | return preg_replace('/[\r\n]/', '', $str); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Improved equivalent to strtotime() |
| | | * |
| | | * @param string $date Date string |
| | | * |
| | | * @return int Unix timestamp |
| | | */ |
| | | function rcube_strtotime($date) |
| | | { |
| | | // check for MS Outlook vCard date format YYYYMMDD |
| | | if (preg_match('/^([12][90]\d\d)([01]\d)(\d\d)$/', trim($date), $matches)) { |
| | | return mktime(0,0,0, intval($matches[2]), intval($matches[3]), intval($matches[1])); |
| | | } |
| | | else if (is_numeric($date)) { |
| | | return $date; |
| | | } |
| | | |
| | | // support non-standard "GMTXXXX" literal |
| | | $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date); |
| | | |
| | | // if date parsing fails, we have a date in non-rfc format. |
| | | // remove token from the end and try again |
| | | while ((($ts = @strtotime($date)) === false) || ($ts < 0)) { |
| | | $d = explode(' ', $date); |
| | | array_pop($d); |
| | | if (!$d) { |
| | | break; |
| | | } |
| | | $date = implode(' ', $d); |
| | | } |
| | | |
| | | return $ts; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /* |
| | | * Idn_to_ascii wrapper. |
| | | * Intl/Idn modules version of this function doesn't work with e-mail address |
| | | */ |
| | | function rcube_idn_to_ascii($str) |
| | | { |
| | | return rcube_idn_convert($str, true); |
| | | } |
| | | |
| | | /* |
| | | * Idn_to_ascii wrapper. |
| | | * Intl/Idn modules version of this function doesn't work with e-mail address |
| | | */ |
| | | function rcube_idn_to_utf8($str) |
| | | { |
| | | return rcube_idn_convert($str, false); |
| | | } |
| | | |
| | | function rcube_idn_convert($input, $is_utf=false) |
| | | { |
| | | if ($at = strpos($input, '@')) { |
| | | $user = substr($input, 0, $at); |
| | | $domain = substr($input, $at+1); |
| | | } |
| | | else { |
| | | $domain = $input; |
| | | } |
| | | |
| | | $domain = $is_utf ? idn_to_ascii($domain) : idn_to_utf8($domain); |
| | | |
| | | if ($domain === false) { |
| | | return ''; |
| | | } |
| | | |
| | | return $at ? $user . '@' . $domain : $domain; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Use PHP5 autoload for dynamic class loading |
| | | * |
| | |
| | | |
| | | if ($fp = @fopen("$filename.php", 'r', true)) { |
| | | fclose($fp); |
| | | include_once("$filename.php"); |
| | | include_once "$filename.php"; |
| | | return true; |
| | | } |
| | | |