thomascube
2010-04-01 1d773d71414316e0b9836a15c35576593427ee21
program/include/rcube_shared.inc
@@ -39,9 +39,11 @@
  header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
  header("Cache-Control: private, must-revalidate, post-check=0, pre-check=0");
  header("Pragma: no-cache");
  // Request browser to disable DNS prefetching (CVE-2010-0464)
  header("X-DNS-Prefetch-Control: off");
  
  // We need to set the following headers to make downloads work using IE in HTTPS mode.
  if (isset($_SERVER['HTTPS'])) {
  if (rcube_https_check()) {
    header('Pragma: ');
    header('Cache-Control: ');
  }
@@ -93,7 +95,7 @@
  else
    header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mdate)." GMT");
  
  header("Cache-Control: max-age=0");
  header("Cache-Control: private, must-revalidate, max-age=0");
  header("Expires: ");
  header("Pragma: ");
  
@@ -105,97 +107,6 @@
    ob_end_clean();
    exit;
    }
}
/**
 * Returns whether an $str is a reserved word for any of the version of Javascript or ECMAScript
 * @param str String to check
 * @return boolean True if $str is a reserver word, False if not
 */
function is_js_reserved_word($str)
{
  return in_array($str, array(
    // ECMASript ver 4 reserved words
    'as','break','case','catch','class','const','continue',
    'default','delete','do','else','export','extends','false','finally','for','function',
    'if','import','in','instanceof','is','namespace','new','null','package','private',
    'public','return','super','switch','this','throw','true','try','typeof','use','var',
    'void','while','with',
    // ECMAScript ver 4 future reserved words
    'abstract','debugger','enum','goto','implements','interface','native','protected',
    'synchronized','throws','transient','volatile',
    // special meaning in some contexts
    'get','set',
    // were reserved in ECMAScript ver 3
    'boolean','byte','char','double','final','float','int','long','short','static'
  ));
}
/**
 * Convert a variable into a javascript object notation
 *
 * @param mixed Input value
 * @return string Serialized JSON string
 */
function json_serialize($var)
{
  if (is_object($var))
    $var = get_object_vars($var);
  if (is_array($var))
  {
    // empty array
    if (!sizeof($var))
      return '[]';
    else
    {
      $keys_arr = array_keys($var);
      $is_assoc = $have_numeric = 0;
      for ($i=0; $i<sizeof($keys_arr); ++$i)
      {
        if (is_numeric($keys_arr[$i]))
          $have_numeric = 1;
        if (!is_numeric($keys_arr[$i]) || $keys_arr[$i] != $i)
          $is_assoc = 1;
        if ($is_assoc && $have_numeric)
          break;
      }
      $brackets = $is_assoc ? '{}' : '[]';
      $pairs = array();
      foreach ($var as $key => $value)
      {
        // enclose key with quotes if it is not variable-name conform
        if (!preg_match('/^[_a-zA-Z]{1}[_a-zA-Z0-9]*$/', $key) || is_js_reserved_word($key))
          $key = "'$key'";
        $pairs[] = sprintf("%s%s", $is_assoc ? "$key:" : '', json_serialize($value));
      }
      return $brackets{0} . implode(',', $pairs) . $brackets{1};
    }
  }
  else if (!is_string($var) && strval(intval($var)) === strval($var))
    return $var;
  else if (is_bool($var))
    return $var ? '1' : '0';
  else
    return "'".JQ($var)."'";
}
/**
 * Function to convert an array to a javascript array
 * Actually an alias function for json_serialize()
 * @deprecated
 */
function array2js($arr, $type='')
{
  return json_serialize($arr);
}
@@ -243,19 +154,22 @@
{
  if (is_numeric($str))
    return floatval($str);
  if (preg_match('/([0-9]+)([a-z])/i', $str, $regs))
  if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs))
  {
    $bytes = floatval($regs[1]);
    switch (strtolower($regs[2]))
    {
      case 'g':
      case 'gb':
        $bytes *= 1073741824;
        break;
      case 'm':
      case 'mb':
        $bytes *= 1048576;
        break;
      case 'k':
      case 'kb':
        $bytes *= 1024;
        break;
    }
@@ -496,12 +410,13 @@
function abbreviate_string($str, $maxlength, $place_holder='...')
{
  $length = mb_strlen($str);
  $first_part_length = floor($maxlength/2) - mb_strlen($place_holder);
  
  if ($length > $maxlength)
  {
    $second_starting_location = $length - $maxlength + $first_part_length + 1;
    $str = mb_substr($str, 0, $first_part_length) . $place_holder . mb_substr($str, $second_starting_location, $length);
    $place_holder_length = mb_strlen($place_holder);
    $first_part_length = floor(($maxlength - $place_holder_length)/2);
    $second_starting_location = $length - $maxlength + $first_part_length + $place_holder_length;
    $str = mb_substr($str, 0, $first_part_length) . $place_holder . mb_substr($str, $second_starting_location);
  }
  return $str;
@@ -531,20 +446,15 @@
        $mime_type = $mime_ext[$suffix];
    }
    // try fileinfo extension if available
    if (!$mime_type) {
        if (!extension_loaded('fileinfo')) {
            @dl('fileinfo.' . PHP_SHLIB_SUFFIX);
        }
        if (function_exists('finfo_open')) {
            if ($finfo = finfo_open(FILEINFO_MIME, $mime_magic)) {
                $mime_type = finfo_file($finfo, $path);
                finfo_close($finfo);
            }
    if (!$mime_type && function_exists('finfo_open')) {
        if ($finfo = finfo_open(FILEINFO_MIME, $mime_magic)) {
            $mime_type = finfo_file($finfo, $path);
            finfo_close($finfo);
        }
    }
    // try PHP's mime_content_type
    if (!$mime_type && function_exists('mime_content_type')) {
      $mime_type = mime_content_type($path);
      $mime_type = mime_content_type($path);
    }
    // fall back to user-submitted string
    if (!$mime_type) {
@@ -553,7 +463,6 @@
    return $mime_type;
}
/**
 * A method to guess encoding of a string.
@@ -585,6 +494,92 @@
    return $result ? $result : $failover;
}
/**
 * Removes non-unicode characters from input
 *
 * @param mixed $input String or array.
 * @return string
 */
function rc_utf8_clean($input)
{
  // handle input of type array
  if (is_array($input)) {
    foreach ($input as $idx => $val)
      $input[$idx] = rc_utf8_clean($val);
    return $input;
  }
  if (!is_string($input) || $input == '')
    return $input;
  // iconv/mbstring are much faster (especially with long strings)
  if (function_exists('mb_convert_encoding') && ($res = mb_convert_encoding($input, 'UTF-8', 'UTF-8')) !== false)
    return $res;
  if (function_exists('iconv') && ($res = @iconv('UTF-8', 'UTF-8//IGNORE', $input)) !== false)
    return $res;
  $regexp = '/^('.
//    '[\x00-\x7F]'.                                  // UTF8-1
    '|[\xC2-\xDF][\x80-\xBF]'.                      // UTF8-2
    '|\xE0[\xA0-\xBF][\x80-\xBF]'.                  // UTF8-3
    '|[\xE1-\xEC][\x80-\xBF][\x80-\xBF]'.           // UTF8-3
    '|\xED[\x80-\x9F][\x80-\xBF]'.                  // UTF8-3
    '|[\xEE-\xEF][\x80-\xBF][\x80-\xBF]'.           // UTF8-3
    '|\xF0[\x90-\xBF][\x80-\xBF][\x80-\xBF]'.       // UTF8-4
    '|[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]'.// UTF8-4
    '|\xF4[\x80-\x8F][\x80-\xBF][\x80-\xBF]'.       // UTF8-4
    ')$/';
  $seq = '';
  $out = '';
  for ($i = 0, $len = strlen($input); $i < $len; $i++) {
    $chr = $input[$i];
    $ord = ord($chr);
    // 1-byte character
    if ($ord <= 0x7F) {
      if ($seq)
        $out .= preg_match($regexp, $seq) ? $seq : '';
      $seq = '';
      $out .= $chr;
    // first (or second) byte of multibyte sequence
    } else if ($ord >= 0xC0) {
      if (strlen($seq)>1) {
   $out .= preg_match($regexp, $seq) ? $seq : '';
        $seq = '';
      } else if ($seq && ord($seq) < 0xC0) {
        $seq = '';
      }
      $seq .= $chr;
    // next byte of multibyte sequence
    } else if ($seq) {
      $seq .= $chr;
    }
  }
  if ($seq)
    $out .= preg_match($regexp, $seq) ? $seq : '';
  return $out;
}
/**
 * Convert a variable into a javascript object notation
 *
 * @param mixed Input value
 * @return string Serialized JSON string
 */
function json_serialize($input)
{
  $input = rc_utf8_clean($input);
  // sometimes even using rc_utf8_clean() the input contains invalid UTF-8 sequences
  // that's why we have @ here
  return @json_encode($input);
}
/**
 * Explode quoted string
@@ -613,45 +608,56 @@
/**
 * Get all keys from array (recursive)
 *
 * @param array Input array
 * @return array
 */
function array_keys_recursive($array)
{
  $keys = array();
  if (!empty($array))
    foreach ($array as $key => $child) {
      $keys[] = $key;
      if ($children = array_keys_recursive($child))
        $keys = array_merge($keys, $children);
    }
  return $keys;
}
/**
 * mbstring replacement functions
 */
if (!function_exists('mb_strlen')) {
if (!extension_loaded('mbstring'))
{
    function mb_strlen($str)
    {
   return strlen($str);
    }
}
if (!function_exists('mb_strtolower')) {
    function mb_strtolower($str)
    {
        return strtolower($str);
    }
}
if (!function_exists('mb_strtoupper')) {
    function mb_strtoupper($str)
    {
        return strtoupper($str);
    }
}
if (!function_exists('mb_substr')) {
    function mb_substr($str, $start, $len=null)
    {
        return substr($str, $start, $len);
    }
}
if (!function_exists('mb_strpos')) {
    function mb_strpos($haystack, $needle, $offset=0)
    {
        return strpos($haystack, $needle, $offset);
    }
}
if (!function_exists('mb_strrpos')) {
    function mb_strrpos($haystack, $needle, $offset=0)
    {
        return strrpos($haystack, $needle, $offset);