]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcube_shared.inc
Imported Upstream version 0.3
[roundcube.git] / program / include / rcube_shared.inc
index 345f75e99b9076701a1ca134130863008655ba44..e99d324e5550847b384f2c393b7b2dda89b1b2ff 100644 (file)
@@ -15,7 +15,7 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
- $Id: rcube_shared.inc 2147 2008-12-11 17:29:50Z alec $
+ $Id: rcube_shared.inc 2789 2009-07-23 12:14:17Z alec $
 
 */
 
@@ -170,7 +170,7 @@ function json_serialize($var)
       foreach ($var as $key => $value)
       {
         // enclose key with quotes if it is not variable-name conform
-        if (!ereg("^[_a-zA-Z]{1}[_a-zA-Z0-9]*$", $key) || is_js_reserved_word($key))
+        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));
@@ -185,7 +185,6 @@ function json_serialize($var)
     return $var ? '1' : '0';
   else
     return "'".JQ($var)."'";
-
 }
 
 
@@ -209,9 +208,9 @@ function array2js($arr, $type='')
  */
 function in_array_nocase($needle, $haystack)
 {
-  $needle = rc_strtolower($needle);
+  $needle = mb_strtolower($needle);
   foreach ($haystack as $value)
-    if ($needle===rc_strtolower($value))
+    if ($needle===mb_strtolower($value))
       return true;
   
   return false;
@@ -227,7 +226,7 @@ function in_array_nocase($needle, $haystack)
 function get_boolean($str)
 {
   $str = strtolower($str);
-  if(in_array($str, array('false', '0', 'no', 'nein', ''), TRUE))
+  if (in_array($str, array('false', '0', 'no', 'nein', ''), TRUE))
     return FALSE;
   else
     return TRUE;
@@ -238,12 +237,12 @@ function get_boolean($str)
  * Parse a human readable string for a number of bytes
  *
  * @param string Input string
- * @return int Number of bytes
+ * @return float Number of bytes
  */
 function parse_bytes($str)
 {
   if (is_numeric($str))
-    return intval($str);
+    return floatval($str);
     
   if (preg_match('/([0-9]+)([a-z])/i', $str, $regs))
   {
@@ -262,7 +261,7 @@ function parse_bytes($str)
     }
   }
 
-  return intval($bytes);
+  return floatval($bytes);
 }
     
 /**
@@ -309,7 +308,7 @@ function make_absolute_url($path, $base_url)
     return $path;
 
   // cut base_url to the last directory
-  if (strpos($base_url, '/')>7)
+  if (strrpos($base_url, '/')>7)
   {
     $host_url = substr($base_url, 0, strpos($base_url, '/'));
     $base_url = substr($base_url, 0, strrpos($base_url, '/'));
@@ -338,74 +337,52 @@ function make_absolute_url($path, $base_url)
   return $abs_path;
 }
 
-
-/**
- * Wrapper function for strlen
- */
-function rc_strlen($str)
-{
-  if (function_exists('mb_strlen'))
-    return mb_strlen($str);
-  else
-    return strlen($str);
-}
-  
-/**
- * Wrapper function for strtolower
- */
-function rc_strtolower($str)
-{
-  if (function_exists('mb_strtolower'))
-    return mb_strtolower($str);
-  else
-    return strtolower($str);
-}
-
-/**
- * Wrapper function for strtoupper
- */
-function rc_strtoupper($str)
-{
-  if (function_exists('mb_strtoupper'))
-    return mb_strtoupper($str);
-  else
-    return strtoupper($str);
-}
-
 /**
- * Wrapper function for substr
+ * Wrapper function for wordwrap
  */
-function rc_substr($str, $start, $len=null)
+function rc_wordwrap($string, $width=75, $break="\n", $cut=false)
 {
-  if (function_exists('mb_substr'))
-    return mb_substr($str, $start, $len);
-  else
-    return substr($str, $start, $len);
-}
-
-/**
- * Wrapper function for strpos
- */
-function rc_strpos($haystack, $needle, $offset=0)
-{
-  if (function_exists('mb_strpos'))
-    return mb_strpos($haystack, $needle, $offset);
-  else
-    return strpos($haystack, $needle, $offset);
-}
-
-/**
- * Wrapper function for strrpos
- */
-function rc_strrpos($haystack, $needle, $offset=0)
-{
-  if (function_exists('mb_strrpos'))
-    return mb_strrpos($haystack, $needle, $offset);
-  else
-    return strrpos($haystack, $needle, $offset);
+  $para = explode($break, $string);
+  $string = '';
+  while (count($para)) {
+    $list = explode(' ', array_shift($para));
+    $len = 0;
+    while (count($list)) {
+      $line = array_shift($list);
+      $l = mb_strlen($line);
+      $newlen = $len + $l + ($len ? 1 : 0);
+
+      if ($newlen <= $width) {
+        $string .= ($len ? ' ' : '').$line;
+        $len += (1 + $l);
+      } else {
+       if ($l > $width) {
+         if ($cut) {
+           $start = 0;
+           while ($l) {
+             $str = mb_substr($line, $start, $width);
+             $strlen = mb_strlen($str);
+             $string .= ($len ? $break : '').$str;
+             $start += $strlen;
+             $l -= $strlen;
+             $len = $strlen;
+           }
+         } else {
+            $string .= ($len ? $break : '').$line;
+           if (count($list)) $string .= $break;
+           $len = 0;
+         }
+       } else {
+          $string .= $break.$line;
+         $len = $l;
+        }
+      }
+    }
+    if (count($para)) $string .= $break;
+  }
+  return $string;
 }
 
-
 /**
  * Read a specific HTTP request header
  *
@@ -430,30 +407,6 @@ function rc_request_header($name)
   }
 
 
-/**
- * Replace the middle part of a string with ...
- * if it is longer than the allowed length
- *
- * @param string Input string
- * @param int    Max. length
- * @param string Replace removed chars with this
- * @return string Abbreviated string
- */
-function abbreviate_string($str, $maxlength, $place_holder='...')
-{
-  $length = rc_strlen($str);
-  $first_part_length = floor($maxlength/2) - rc_strlen($place_holder);
-  
-  if ($length > $maxlength)
-  {
-    $second_starting_location = $length - $maxlength + $first_part_length + 1;
-    $str = rc_substr($str, 0, $first_part_length) . $place_holder . rc_substr($str, $second_starting_location, $length);
-  }
-
-  return $str;
-}
-
-
 /**
  * Make sure the string ends with a slash
  */
@@ -531,10 +484,34 @@ function get_offset_time($offset_str, $factor=1)
 }
 
 
+/**
+ * Replace the middle part of a string with ...
+ * if it is longer than the allowed length
+ *
+ * @param string Input string
+ * @param int    Max. length
+ * @param string Replace removed chars with this
+ * @return string Abbreviated string
+ */
+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);
+  }
+
+  return $str;
+}
+
 /**
  * A method to guess the mime_type of an attachment.
  *
  * @param string $path     Path to the file.
+ * @param string $name     File name (with suffix)
  * @param string $failover Mime type supplied for failover.
  *
  * @return string
@@ -542,25 +519,34 @@ function get_offset_time($offset_str, $factor=1)
  * @see    http://de2.php.net/manual/en/ref.fileinfo.php
  * @see    http://de2.php.net/mime_content_type
  */
-function rc_mime_content_type($path, $failover = 'application/octet-stream')
+function rc_mime_content_type($path, $name, $failover = 'application/octet-stream')
 {
     $mime_type = null;
     $mime_magic = rcmail::get_instance()->config->get('mime_magic');
+    $mime_ext = @include(RCMAIL_CONFIG_DIR . '/mimetypes.php');
+    $suffix = $name ? substr($name, strrpos($name, '.')+1) : '*';
 
-    if (!extension_loaded('fileinfo')) {
-        @dl('fileinfo.' . PHP_SHLIB_SUFFIX);
+    // use file name suffix with hard-coded mime-type map
+    if (is_array($mime_ext)) {
+        $mime_type = $mime_ext[$suffix];
     }
-
-    if (function_exists('finfo_open')) {
-        if ($finfo = finfo_open(FILEINFO_MIME, $mime_magic)) {
-            $mime_type = finfo_file($finfo, $path);
-            finfo_close($finfo);
+    // 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);
+            }
         }
     }
+    // try PHP's mime_content_type
     if (!$mime_type && function_exists('mime_content_type')) {
       $mime_type = mime_content_type($path); 
     }
-    
+    // fall back to user-submitted string
     if (!$mime_type) {
         $mime_type = $failover;
     }
@@ -599,4 +585,68 @@ function rc_detect_encoding($string, $failover='')
     return $result ? $result : $failover;
 }
 
+
+/**
+ * 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;
+}
+
+
+/**
+ * mbstring replacement functions
+ */
+
+if (!extension_loaded('mbstring'))
+{
+    function mb_strlen($str)
+    {
+       return strlen($str);
+    }
+
+    function mb_strtolower($str)
+    {
+        return strtolower($str);
+    }
+
+    function mb_strtoupper($str)
+    {
+        return strtoupper($str);
+    }
+
+    function mb_substr($str, $start, $len=null)
+    {
+        return substr($str, $start, $len);
+    }
+
+    function mb_strpos($haystack, $needle, $offset=0)
+    {
+        return strpos($haystack, $needle, $offset);
+    }
+
+    function mb_strrpos($haystack, $needle, $offset=0)
+    {
+        return strrpos($haystack, $needle, $offset);
+    }
+}
+
 ?>