Adapt getOwner() to handling variable namespaces. Condense getSubpath() and getTitle().
authorGunnar Wrobel <p@rdus.de>
Mon, 15 Mar 2010 07:36:26 +0000 (08:36 +0100)
committerGunnar Wrobel <wrobel@temple.(none)>
Mon, 15 Mar 2010 10:33:23 +0000 (11:33 +0100)
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Folder.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace.php

index a99bef1..92cbc0f 100644 (file)
@@ -484,7 +484,29 @@ class Horde_Kolab_Storage_Folder
     public function getOwner()
     {
         if (!isset($this->_owner)) {
-            $this->_owner = $this->_namespace->getOwner($this->getName());
+            $owner = $this->_namespace->getOwner($this->getName());
+            /**
+             * @todo: Reconsider if this handling should really be done here
+             * rather than in a module nearer to the applications.
+             */
+            switch ($owner) {
+            case Horde_Kolab_Storage_Namespace::PRIV:
+                $this->_owner = Horde_Auth::getAuth();
+                break;
+            case Horde_Kolab_Storage_Namespace::SHARED:
+                $this->_owner = 'anonymous';
+                break;
+            default:
+                list($prefix, $user) = explode(':', $owner, 2);
+                if (strpos($user, '@') === false) {
+                    $domain = strstr(Horde_Auth::getAuth(), '@');
+                    if (!empty($domain)) {
+                        $user .= '@' . $domain;
+                    }
+                }
+                $this->_owner = $user;
+                break;
+            }
         }
         return $this->_owner;
     }
@@ -496,7 +518,7 @@ class Horde_Kolab_Storage_Folder
      *
      * @return string|PEAR_Error  The subpath of this folder.
      */
-    function getSubpath($name = null)
+    public function getSubpath($name = null)
     {
         if (!empty($name)) {
             return $this->_namespace->getSubpath($name);
index e192150..4f97ec4 100644 (file)
@@ -57,6 +57,7 @@ class Horde_Kolab_Storage_Namespace
     public function __construct()
     {
         $this->_charset = Horde_Nls::getCharset();
+        $this->_sharedPrefix = 'shared.';
     }
 
     /**
@@ -107,21 +108,8 @@ class Horde_Kolab_Storage_Namespace
      */
     public function getTitle($name)
     {
-        $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset);
         $namespace = $this->matchNamespace($name);
-        $path = explode($namespace['delimiter'], $name);
-        if ($path[0] == $namespace['namespace']) {
-            array_shift($path);
-        }
-        if ($path[0] == $namespace['delimiter']) {
-            array_shift($path);
-        }
-        if ($namespace['type'] == self::OTHER) {
-            array_shift($path);
-            if ($path[0] == $namespace['delimiter']) {
-                array_shift($path);
-            }
-        }
+        $path = $this->_subpath($name, $namespace);
         return join($path, ':');
     }
 
@@ -134,20 +122,22 @@ class Horde_Kolab_Storage_Namespace
      */
     public function getOwner($name)
     {
-        if (!preg_match(";(shared\.|INBOX[/]?|user/([^/]+)[/]?)([^@]*)(@.*)?;", $name, $matches)) {
-            throw new Horde_Kolab_Storage_Exception(
-                sprintf('Owner of folder %s cannot be determined.', $name)
-            );
+        $namespace = $this->matchNamespace($name);
+        $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset);
+        $path = explode($namespace['delimiter'], $name);
+        if ($namespace['type'] == self::PRIV) {
+            return self::PRIV;
         }
-
-        if (substr($matches[1], 0, 6) == 'INBOX/') {
-            return Horde_Auth::getAuth();
-        } elseif (substr($matches[1], 0, 5) == 'user/') {
-            $domain = strstr(Horde_Auth::getAuth(), '@');
-            $user_domain = isset($matches[4]) ? $matches[4] : $domain;
-            return $matches[2] . $user_domain;
-        } elseif ($matches[1] == 'shared.') {
-            return  'anonymous';
+        if ($namespace['type'] == self::OTHER) {
+            $user = $path[1];
+            $domain = strstr(array_pop($path), '@');
+            if (!empty($domain)) {
+                $user .= '@' . $domain;
+            }
+            return self::OTHER . ':' . $user;
+        }
+        if ($namespace['type'] == self::SHARED) {
+            return self::SHARED;
         }
     }
 
@@ -160,12 +150,9 @@ class Horde_Kolab_Storage_Namespace
      */
     public function getSubpath($name)
     {
-        if (!preg_match(";(shared\.|INBOX[/]?|user/([^/]+)[/]?)([^@]*)(@.*)?;", $name, $matches)) {
-            throw new Horde_Kolab_Storage_Exception(
-                sprintf('Subpath of folder %s cannot be determined.', $name)
-            );
-        }
-        return $matches[3];
+        $namespace = $this->matchNamespace($name);
+        $path = $this->_subpath($name, $namespace);
+        return join($path, $namespace['delimiter']);
     }
 
     /**
@@ -183,4 +170,32 @@ class Horde_Kolab_Storage_Namespace
         }
         return Horde_String::convertCharset($name, $this->_charset, 'UTF7-IMAP');
     }
+
+    /**
+     * Return an array describing the path elements of the folder.
+     *
+     * @param string $name      The name of the folder.
+     * @param array  $namespace The namespace of the folder.
+     *
+     * @return array The path elements.
+     */
+    protected function _subpath($name, array $namespace)
+    {
+        $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset);
+        $path = explode($namespace['delimiter'], $name);
+        if ($path[0] == $namespace['namespace']) {
+            array_shift($path);
+        }
+        if ($namespace['type'] == self::OTHER) {
+            array_shift($path);
+        }
+        if ($namespace['type'] == self::SHARED && 
+            !empty($this->_sharedPrefix)) {
+            if (strpos($path[0], $this->_sharedPrefix) === 0) {
+                $path[0] = substr($path[0], strlen($this->_sharedPrefix));
+            }
+        }
+        //@todo: What about the potential trailing domain?
+        return $path;
+    }
 }
\ No newline at end of file