]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcube_shared.inc
Imported Upstream version 0.2.1
[roundcube.git] / program / include / rcube_shared.inc
index 345f75e99b9076701a1ca134130863008655ba44..370b4bc33e3dae74d48d0ae8575872da869b265c 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 2313 2009-02-27 10:18:18Z thomasb $
 
 */
 
@@ -309,7 +309,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, '/'));
@@ -535,6 +535,7 @@ function get_offset_time($offset_str, $factor=1)
  * 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 +543,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 +609,30 @@ 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;
+}
+
 ?>