Horde_Share -> Horde_Share_Base.
authorJan Schneider <jan@horde.org>
Thu, 16 Dec 2010 18:15:17 +0000 (19:15 +0100)
committerJan Schneider <jan@horde.org>
Thu, 16 Dec 2010 23:17:09 +0000 (00:17 +0100)
ansel/lib/Storage.php
framework/Core/lib/Horde/Core/Factory/Share.php
framework/Core/lib/Horde/Core/Factory/ShareBase.php
framework/Core/lib/Horde/Core/Share/Driver.php
framework/Share/lib/Horde/Share.php [deleted file]
framework/Share/lib/Horde/Share/Base.php [new file with mode: 0644]
framework/Share/lib/Horde/Share/Datatree.php
framework/Share/lib/Horde/Share/Kolab.php
framework/Share/lib/Horde/Share/Sql.php
framework/Share/package.xml

index 237983c..5a49f74 100644 (file)
@@ -742,7 +742,7 @@ class Ansel_Storage
 
     /**
      * Check if a gallery exists. Need to do this here instead of Horde_Share
-     * since Horde_Share::exists() takes a share_name, not a share_id. We
+     * since Horde_Share_Base::exists() takes a share_name, not a share_id. We
      * might also be checking by gallery_slug and this is more efficient than
      * a listShares() call for one gallery.
      *
index 28367e3..5774b7e 100644 (file)
@@ -8,7 +8,7 @@
  */
 
 /**
- * A Horde_Injector:: based Horde_Share:: factory.
+ * A Horde_Injector:: based Horde_Share factory.
  *
  * Copyright 2010 The Horde Project (http://www.horde.org/)
  *
@@ -39,7 +39,7 @@ class Horde_Core_Factory_Share
     }
 
     /**
-     * Return the Horde_Share:: instance.
+     * Returns the Horde_Share_Base instance.
      *
      * @param string $app     The application scope to use, if not the current
      *                        app.
index 4f1558f..6889298 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A Horde_Injector:: based Horde_Share:: factory.
+ * A Horde_Injector based Horde_Share factory.
  *
  * @category Horde
  * @package  Core
@@ -8,7 +8,7 @@
  */
 
 /**
- * A Horde_Injector:: based Horde_Share:: factory.
+ * A Horde_Injector based Horde_Share factory.
  *
  * Copyright 2010 The Horde Project (http://www.horde.org/)
  *
index 9dbefdd..e4a4707 100644 (file)
@@ -17,7 +17,7 @@ class Horde_Core_Share_Driver
     /**
      * The composed Horde_Share driver
      *
-     * @var Horde_Share
+     * @var Horde_Share_Base
      */
     protected $_share;
 
@@ -33,7 +33,7 @@ class Horde_Core_Share_Driver
 
     /**
      */
-    public function __construct(Horde_Share $share)
+    public function __construct(Horde_Share_Base $share)
     {
         $this->_share = $share;
         $this->_share->setStorage($GLOBALS['injector']->getInstance($this->_storageMap[get_class($this->_share)]));
@@ -217,7 +217,7 @@ class Horde_Core_Share_Driver
     /**
      * Calls the share_remove hook before delegating to the share object.
      *
-     * @see Horde_Share::removeShare
+     * @see Horde_Share_Base::removeShare()
      */
     public function shareRemoveCallback(Horde_Share_Object $share)
     {
diff --git a/framework/Share/lib/Horde/Share.php b/framework/Share/lib/Horde/Share.php
deleted file mode 100644 (file)
index 4f77bf4..0000000
+++ /dev/null
@@ -1,630 +0,0 @@
-<?php
-/**
- * Horde_Share:: provides an interface to all shares a user might have.  Its
- * methods take care of any site-specific restrictions configured in in the
- * application's prefs.php and conf.php files.
- *
- * Copyright 2002-2010 The Horde Project (http://www.horde.org/)
- * Copyright 2002-2007 Infoteck Internet <webmaster@infoteck.qc.ca>
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
- *
- * @author  Joel Vandal <joel@scopserv.com>
- * @author  Mike Cochrame <mike@graftonhall.co.nz>
- * @author  Chuck Hagenbuch <chuck@horde.org>
- * @author  Jan Schneider <jan@horde.org>
- * @author  Gunnar Wrobel <wrobel@pardus.de>
- * @author  Michael J. Rubinsky <mrubinsk@horde.org>
- * @package Horde_Share
- */
-class Horde_Share
-{
-    /**
-     * The application we're managing shares for.
-     *
-     * @var string
-     */
-    protected $_app;
-
-    /**
-     * The root of the Share tree.
-     *
-     * @var mixed
-     */
-    protected $_root = null;
-
-    /**
-     * A cache of all shares that have been retrieved, so we don't hit the
-     * backend again and again for them.
-     *
-     * @var array
-     */
-    protected $_cache = array();
-
-    /**
-     * Id-name-map of already cached share objects.
-     *
-     * @var array
-     */
-    protected $_shareMap = array();
-
-    /**
-     * Cache used for listShares().
-     *
-     * @var array
-     */
-    protected $_listcache = array();
-
-    /**
-     * A list of objects that we're currently sorting, for reference during the
-     * sorting algorithm.
-     *
-     * @var array
-     */
-    protected $_sortList;
-
-    /**
-     * The Horde_Share_Object subclass to instantiate objects as
-     *
-     * @var string
-     */
-    protected $_shareObject;
-
-    /**
-     * The Horde_Perms object
-     *
-     * @var Horde_Perms
-     */
-    protected $_permsObject;
-
-    /**
-     * The current user
-     *
-     * @var string
-     */
-    protected $_user;
-
-    /**
-     * The Horde_Group driver
-     *
-     * @var Horde_Group
-     */
-    protected $_groups;
-
-    /**
-     * A callback that is passed to the share objects for setting the objects'
-     * Horde_Share object.
-     *
-     * @var callback
-     */
-    protected $_shareCallback;
-
-    /**
-     * Logger
-     *
-     * @var Horde_Log_Logger
-     */
-    protected $_logger;
-
-    /**
-     * Configured callbacks. We currently support:
-     *<pre>
-     * add      - Called immediately before a new share is added. Receives the
-     *            share object as a parameter.
-     * modify   - Called immediately before a share object's changes are saved
-     *            to storage. Receives the share object as a parameter.
-     * remove   - Called immediately before a share is removed from storage.
-     *            Receives the share object as a parameter.
-     * list     - Called immediately after a list of shares is received from
-     *            storage. Passed the userid, share list, and any parameters
-     *            passed to the listShare call. Should return the (possibly
-     *            modified) share list. @see Horde_Share::listShares() for more
-     *            info.
-     *</pre>
-     *
-     * @var array
-     */
-    protected $_callbacks;
-
-    /**
-     * Constructor.
-     *
-     * @param string $app          The application that the shares belong to
-     * @param string $user         The current user
-     * @param Horde_Perms $perms   The permissions object
-     * @param Horde_Group $groups  The Horde_Group object
-     *
-     */
-    public function __construct($app, $user, Horde_Perms $perms, Horde_Group $groups)
-    {
-        $this->_app = $app;
-        $this->_user = $user;
-        $this->_permsObject = $perms;
-        $this->_groups = $groups;
-        $this->_logger = new Horde_Support_Stub();
-    }
-
-    /**
-     * Set a logger object.
-     *
-     * @inject
-     *
-     * @var Horde_Log_Logger $logger  The logger object.
-     */
-    public function setLogger(Horde_Log_Logger $logger)
-    {
-        $this->_logger = $logger;
-    }
-
-    /**
-     * (re)connect the share object to this share driver.
-     *
-     * @param Horde_Share_Object $object
-     */
-    public function initShareObject(Horde_Share_Object $object)
-    {
-        $object->setShareOb($this->_shareCallback);
-        $this->_initShareObject($object);
-    }
-
-    protected function _initShareObject(Horde_Share_Object $object)
-    {
-        // noop here
-    }
-
-    public function setShareCallback($callback)
-    {
-        $this->_shareCallback = $callback;
-    }
-
-    /**
-     * Returns the application we're managing shares for.
-     *
-     * @return string  The application this share belongs to.
-     */
-    public function getApp()
-    {
-        return $this->_app;
-    }
-
-    /**
-     * Returns a Horde_Share_Object object corresponding to the given share
-     * name, with the details retrieved appropriately.
-     *
-     * @param string $name  The name of the share to retrieve.
-     *
-     * @return Horde_Share_Object  The requested share.
-     */
-    public function getShare($name)
-    {
-        if (isset($this->_cache[$name])) {
-            return $this->_cache[$name];
-        }
-
-        $share = $this->_getShare($name);
-        $this->_shareMap[$share->getId()] = $name;
-        $this->_cache[$name] = $share;
-
-        return $share;
-    }
-
-    /**
-     * Returns a Horde_Share_Object object corresponding to the given unique
-     * ID, with the details retrieved appropriately.
-     *
-     * @param string $cid  The id of the share to retrieve.
-     *
-     * @return Horde_Share_Object  The requested share.
-     */
-    public function getShareById($cid)
-    {
-        if (!isset($this->_shareMap[$cid])) {
-            $share = $this->_getShareById($cid);
-            $name = $share->getName();
-            $this->_cache[$name] = $share;
-            $this->_shareMap[$cid] = $name;
-        }
-
-        return $this->_cache[$this->_shareMap[$cid]];
-    }
-
-    /**
-     * Returns an array of Horde_Share_Object objects corresponding to the
-     * given set of unique IDs, with the details retrieved appropriately.
-     *
-     * @param array $cids  The array of ids to retrieve.
-     *
-     * @return array  The requested shares.
-     */
-    public function getShares($cids)
-    {
-        $all_shares = $missing_ids = array();
-        foreach ($cids as $cid) {
-            if (isset($this->_shareMap[$cid])) {
-                $all_shares[$this->_shareMap[$cid]] = $this->_cache[$this->_shareMap[$cid]];
-            } else {
-                $missing_ids[] = $cid;
-            }
-        }
-
-        if (count($missing_ids)) {
-            $shares = $this->_getShares($missing_ids);
-            foreach (array_keys($shares) as $key) {
-                $this->_cache[$key] = $shares[$key];
-                $this->_shareMap[$shares[$key]->getId()] = $key;
-                $all_shares[$key] = $this->_cache[$key];
-            }
-        }
-
-        return $all_shares;
-    }
-
-    /**
-     * Lists *all* shares for the current app/share, regardless of
-     * permissions.
-     *
-     * This is for admin functionality and scripting tools, and shouldn't be
-     * called from user-level code!
-     *
-     * @return array  All shares for the current app/share.
-     */
-    public function listAllShares()
-    {
-        $shares = $this->_listAllShares();
-        $this->_sortList = $shares;
-        uasort($shares, array($this, '_sortShares'));
-        $this->_sortList = null;
-
-        return $shares;
-    }
-
-    /**
-     * Returns an array of all shares that $userid has access to.
-     *
-     * @param string $userid  The userid of the user to check access for.
-     * @param array $params   Additional parameters for the search.
-     *<pre>
-     *  'perm'        Require this level of permissions. Horde_Perms constant.
-     *  'attributes'  Restrict shares to these attributes. A hash or username.
-     *  'from'        Offset. Start at this share
-     *  'count'       Limit.  Only return this many.
-     *  'sort_by'     Sort by attribute.
-     *  'direction'   Sort by direction.
-     *</pre>
-     *
-     * @return array  The shares the user has access to.
-     */
-    public function listShares($userid, $params = array())
-    {
-        $params = array_merge(array('perm' => Horde_Perms::SHOW,
-                                    'attributes' => null,
-                                    'from' => 0,
-                                    'count' => 0,
-                                    'sort_by' => null,
-                                    'direction' => 0),
-                              $params);
-
-        $shares = $this->_listShares($userid, $params);
-        if (!count($shares)) {
-            return $shares;
-        }
-
-        $shares = $this->getShares($shares);
-        if (is_null($sort_by)) {
-            $this->_sortList = $shares;
-            uasort($shares, array($this, '_sortShares'));
-            $this->_sortList = null;
-        }
-
-        // Run the results through the callback, if configured.
-        if (!empty($this->_callbacks['list'])) {
-            return $this->runCallback('list', array($userid, $shares, $params));
-        }
-
-        return $shares;
-    }
-
-    /**
-     * Returns an array of all system shares.
-     *
-     * @return array  All system shares.
-     */
-    public function listSystemShares()
-    {
-        return array();
-    }
-
-    /**
-     * Returns the number of shares that $userid has access to.
-     *
-     * @param string $userid     The userid of the user to check access for.
-     * @param integer $perm      The level of permissions required.
-     * @param mixed $attributes  Restrict the shares counted to those
-     *                           matching $attributes. An array of
-     *                           attribute/values pairs or a share owner
-     *                           username.
-     *
-     * @return integer  The number of shares
-     */
-    public function countShares($userid, $perm = Horde_Perms::SHOW, $attributes = null)
-    {
-        return $this->_countShares($userid, $perm, $attributes);
-    }
-
-    /**
-     * Returns a new share object.
-     *
-     * @param string $owner The share owner name.
-     * @param string $name  The share's name.
-     *
-     * @return Horde_Share_Object  A new share object.
-     * @throws Horde_Share_Exception
-     */
-    public function newShare($owner, $name = '')
-    {
-        $share = $this->_newShare($name);
-        $share->set('owner', $owner);
-        $share->setShareOb(empty($this->_shareCallback) ? $this : $this->_shareCallback);
-
-        return $share;
-    }
-
-    /**
-     * Adds a share to the shares system.
-     *
-     * The share must first be created with Horde_Share::newShare(), and have
-     * any initial details added to it, before this function is called.
-     *
-     * @param Horde_Share_Object $share  The new share object.
-     *
-     * @return boolean
-     * @throws Horde_Share_Exception
-     */
-    public function addShare(Horde_Share_Object $share)
-    {
-        // Run the results through the callback, if configured.
-        $this->runCallback('add', array($share));
-        $result = $this->_addShare($share);
-
-        /* Store new share in the caches. */
-        $id = $share->getId();
-        $name = $share->getName();
-        $this->_cache[$name] = $share;
-        $this->_shareMap[$id] = $name;
-
-        /* Reset caches that depend on unknown criteria. */
-        $this->_listcache = array();
-
-        return $result;
-    }
-
-    /**
-     * Removes a share from the shares system permanently.
-     *
-     * @param Horde_Share_Object $share  The share to remove.
-     *
-     * @throws Horde_Share_Exception
-     */
-    public function removeShare(Horde_Share_Object $share)
-    {
-        // Run the results through the callback, if configured.
-        $this->runCallback('remove', array($share));
-
-        /* Remove share from the caches. */
-        $id = $share->getId();
-        unset($this->_shareMap[$id]);
-        unset($this->_cache[$share->getName()]);
-
-        /* Reset caches that depend on unknown criteria. */
-        $this->_listcache = array();
-
-        return $this->_removeShare($share);
-    }
-
-    /**
-     * Checks if a share exists in the system.
-     *
-     * @param string $share  The share to check.
-     *
-     * @return boolean  True if the share exists.
-     */
-    public function exists($share)
-    {
-        if (isset($this->_cache[$share])) {
-            return true;
-        }
-
-        return $this->_exists($share);
-    }
-
-    /**
-     * Finds out what rights the given user has to this object.
-     *
-     * @see Horde_Perms::getPermissions
-     *
-     * @param mixed $share  The share that should be checked for the users
-     *                      permissions.
-     * @param string $user  The user to check for.
-     *
-     * @return mixed  A bitmask of permissions, a permission value, or an array
-     *                of permission values the user has, depending on the
-     *                permission type and whether the permission value is
-     *                ambiguous. False if there is no such permsission.
-     */
-    public function getPermissions($share, $user = null)
-    {
-        if (!($share instanceof Horde_Share_Object)) {
-            $share = $this->getShare($share);
-        }
-
-        return $this->_permsObject->getPermissions($share->getPermission(), $user);
-    }
-
-    /**
-     * Set the class type to use for creating share objects.
-     *
-     * @var string $classname  The classname to use.
-     */
-    public function setShareClass($classname)
-    {
-        $this->_shareObject = $classname;
-    }
-
-    /**
-     * Getter for Horde_Perms object
-     *
-     * @return Horde_Perms
-     */
-    public function getPermsObject()
-    {
-        return $this->_permsObject;
-    }
-
-    /**
-     * Convert TO the storage driver's charset. Individual share objects should
-     * implement this method if needed.
-     *
-     * @param array $data  Data to be converted.
-     */
-    public function toDriverCharset($data)
-    {
-        // noop
-    }
-
-    /**
-     * Add a callback to the collection
-     *
-     * @param string $type
-     * @param array $callback
-     */
-    public function addCallback($type, $callback)
-    {
-        $this->_callbacks[$type] = $callback;
-    }
-
-    /**
-     * Returns the share's list cache.
-     *
-     * @return array
-     */
-    public function getListCache()
-    {
-        return $this->_listcache;
-    }
-
-    /**
-     * Set the list cache.
-     *
-     * @param array $cache
-     */
-    public function setListCache($cache)
-    {
-        $this->_listcache = $cache;
-    }
-
-    /**
-     * Give public access to call the share callbacks. Needed to run the
-     * callbacks from the Horde_Share_Object objects.
-     *
-     * @param string $type   The callback to run
-     * @param array $params  The parameters to pass to the callback.
-     *
-     * @return mixed
-     */
-    public function runCallback($type, $params)
-    {
-        if (!empty($this->_callbacks[$type])) {
-            return call_user_func_array($this->_callbacks[$type], $params);
-        }
-    }
-
-    /**
-     * Expire the current list cache. This would be needed anytime a share is
-     * either added, deleted, had a change in owner, parent, or perms.
-     *
-     */
-    public function expireListCache()
-    {
-        $this->_listcache = array();
-    }
-
-    /**
-     * Utility function to be used with uasort() for sorting arrays of
-     * Horde_Share objects.
-     *
-     * Example:
-     * <code>
-     * uasort($list, array('Horde_Share', '_sortShares'));
-     * </code>
-     */
-    protected function _sortShares($a, $b)
-    {
-        $aParts = explode(':', $a->getName());
-        $bParts = explode(':', $b->getName());
-
-        $min = min(count($aParts), count($bParts));
-        $idA = '';
-        $idB = '';
-        for ($i = 0; $i < $min; $i++) {
-            if ($idA) {
-                $idA .= ':';
-                $idB .= ':';
-            }
-            $idA .= $aParts[$i];
-            $idB .= $bParts[$i];
-
-            if ($idA != $idB) {
-                $curA = isset($this->_sortList[$idA]) ? $this->_sortList[$idA]->get('name') : '';
-                $curB = isset($this->_sortList[$idB]) ? $this->_sortList[$idB]->get('name') : '';
-                return strnatcasecmp($curA, $curB);
-            }
-        }
-
-        return count($aParts) > count($bParts);
-    }
-
-    /**
-     * Logs a debug entry
-     *
-     * @param string $sql     The log statement.
-     * @param string $name    TODO
-     * @param float $runtime  Runtime interval.
-     */
-    protected function _logDebug($entry, $name, $runtime = null)
-    {
-        /*@TODO */
-        $name = (empty($name) ? '' : $name)
-              . (empty($runtime) ? '' : sprintf(" (%.4fs)", $runtime));
-        $this->_logger->debug($this->_formatLogEntry($name, $entry));
-    }
-
-    /**
-     * Logs an error entry.
-     *
-     * @param string $error   The error statement.
-     * @param string $name    TODO
-     * @param float $runtime  Runtime interval.
-     */
-    protected function _logError($error, $name, $runtime = null)
-    {
-        /*@TODO */
-        $name = (empty($name) ? '' : $name)
-              . (empty($runtime) ? '' : sprintf(" (%.4fs)", $runtime));
-        $this->_logger->err($this->_formatLogEntry($name, $error));
-    }
-
-    /**
-     * Formats the log entry.
-     *
-     * @param string $message  Message.
-     * @param string $sql      SQL statment.
-     *
-     * @return string  Formatted log entry.
-     */
-    protected function _formatLogEntry($message, $sql)
-    {
-        return "SQL $message  \n\t" . wordwrap(preg_replace("/\s+/", ' ', $sql), 70, "\n\t  ", 1);
-    }
-
-}
diff --git a/framework/Share/lib/Horde/Share/Base.php b/framework/Share/lib/Horde/Share/Base.php
new file mode 100644 (file)
index 0000000..eba2716
--- /dev/null
@@ -0,0 +1,628 @@
+<?php
+/**
+ * Base class for all Horde_Share drivers.
+ *
+ * Copyright 2002-2010 The Horde Project (http://www.horde.org/)
+ * Copyright 2002-2007 Infoteck Internet <webmaster@infoteck.qc.ca>
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author  Joel Vandal <joel@scopserv.com>
+ * @author  Mike Cochrame <mike@graftonhall.co.nz>
+ * @author  Chuck Hagenbuch <chuck@horde.org>
+ * @author  Jan Schneider <jan@horde.org>
+ * @author  Gunnar Wrobel <wrobel@pardus.de>
+ * @author  Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Horde_Share
+ */
+class Horde_Share_Base
+{
+    /**
+     * The application we're managing shares for.
+     *
+     * @var string
+     */
+    protected $_app;
+
+    /**
+     * The root of the Share tree.
+     *
+     * @var mixed
+     */
+    protected $_root = null;
+
+    /**
+     * A cache of all shares that have been retrieved, so we don't hit the
+     * backend again and again for them.
+     *
+     * @var array
+     */
+    protected $_cache = array();
+
+    /**
+     * Id-name-map of already cached share objects.
+     *
+     * @var array
+     */
+    protected $_shareMap = array();
+
+    /**
+     * Cache used for listShares().
+     *
+     * @var array
+     */
+    protected $_listcache = array();
+
+    /**
+     * A list of objects that we're currently sorting, for reference during the
+     * sorting algorithm.
+     *
+     * @var array
+     */
+    protected $_sortList;
+
+    /**
+     * The Horde_Share_Object subclass to instantiate objects as
+     *
+     * @var string
+     */
+    protected $_shareObject;
+
+    /**
+     * The Horde_Perms object
+     *
+     * @var Horde_Perms
+     */
+    protected $_permsObject;
+
+    /**
+     * The current user
+     *
+     * @var string
+     */
+    protected $_user;
+
+    /**
+     * The Horde_Group driver
+     *
+     * @var Horde_Group
+     */
+    protected $_groups;
+
+    /**
+     * A callback that is passed to the share objects for setting the objects'
+     * Horde_Share object.
+     *
+     * @var callback
+     */
+    protected $_shareCallback;
+
+    /**
+     * Logger
+     *
+     * @var Horde_Log_Logger
+     */
+    protected $_logger;
+
+    /**
+     * Configured callbacks. We currently support:
+     *<pre>
+     * add      - Called immediately before a new share is added. Receives the
+     *            share object as a parameter.
+     * modify   - Called immediately before a share object's changes are saved
+     *            to storage. Receives the share object as a parameter.
+     * remove   - Called immediately before a share is removed from storage.
+     *            Receives the share object as a parameter.
+     * list     - Called immediately after a list of shares is received from
+     *            storage. Passed the userid, share list, and any parameters
+     *            passed to the listShare call. Should return the (possibly
+     *            modified) share list. @see listShares() for more
+     *            info.
+     *</pre>
+     *
+     * @var array
+     */
+    protected $_callbacks;
+
+    /**
+     * Constructor.
+     *
+     * @param string $app          The application that the shares belong to
+     * @param string $user         The current user
+     * @param Horde_Perms $perms   The permissions object
+     * @param Horde_Group $groups  The Horde_Group object
+     *
+     */
+    public function __construct($app, $user, Horde_Perms $perms, Horde_Group $groups)
+    {
+        $this->_app = $app;
+        $this->_user = $user;
+        $this->_permsObject = $perms;
+        $this->_groups = $groups;
+        $this->_logger = new Horde_Support_Stub();
+    }
+
+    /**
+     * Set a logger object.
+     *
+     * @inject
+     *
+     * @var Horde_Log_Logger $logger  The logger object.
+     */
+    public function setLogger(Horde_Log_Logger $logger)
+    {
+        $this->_logger = $logger;
+    }
+
+    /**
+     * (re)connect the share object to this share driver.
+     *
+     * @param Horde_Share_Object $object
+     */
+    public function initShareObject(Horde_Share_Object $object)
+    {
+        $object->setShareOb($this->_shareCallback);
+        $this->_initShareObject($object);
+    }
+
+    protected function _initShareObject(Horde_Share_Object $object)
+    {
+        // noop here
+    }
+
+    public function setShareCallback($callback)
+    {
+        $this->_shareCallback = $callback;
+    }
+
+    /**
+     * Returns the application we're managing shares for.
+     *
+     * @return string  The application this share belongs to.
+     */
+    public function getApp()
+    {
+        return $this->_app;
+    }
+
+    /**
+     * Returns a Horde_Share_Object object corresponding to the given share
+     * name, with the details retrieved appropriately.
+     *
+     * @param string $name  The name of the share to retrieve.
+     *
+     * @return Horde_Share_Object  The requested share.
+     */
+    public function getShare($name)
+    {
+        if (isset($this->_cache[$name])) {
+            return $this->_cache[$name];
+        }
+
+        $share = $this->_getShare($name);
+        $this->_shareMap[$share->getId()] = $name;
+        $this->_cache[$name] = $share;
+
+        return $share;
+    }
+
+    /**
+     * Returns a Horde_Share_Object object corresponding to the given unique
+     * ID, with the details retrieved appropriately.
+     *
+     * @param string $cid  The id of the share to retrieve.
+     *
+     * @return Horde_Share_Object  The requested share.
+     */
+    public function getShareById($cid)
+    {
+        if (!isset($this->_shareMap[$cid])) {
+            $share = $this->_getShareById($cid);
+            $name = $share->getName();
+            $this->_cache[$name] = $share;
+            $this->_shareMap[$cid] = $name;
+        }
+
+        return $this->_cache[$this->_shareMap[$cid]];
+    }
+
+    /**
+     * Returns an array of Horde_Share_Object objects corresponding to the
+     * given set of unique IDs, with the details retrieved appropriately.
+     *
+     * @param array $cids  The array of ids to retrieve.
+     *
+     * @return array  The requested shares.
+     */
+    public function getShares($cids)
+    {
+        $all_shares = $missing_ids = array();
+        foreach ($cids as $cid) {
+            if (isset($this->_shareMap[$cid])) {
+                $all_shares[$this->_shareMap[$cid]] = $this->_cache[$this->_shareMap[$cid]];
+            } else {
+                $missing_ids[] = $cid;
+            }
+        }
+
+        if (count($missing_ids)) {
+            $shares = $this->_getShares($missing_ids);
+            foreach (array_keys($shares) as $key) {
+                $this->_cache[$key] = $shares[$key];
+                $this->_shareMap[$shares[$key]->getId()] = $key;
+                $all_shares[$key] = $this->_cache[$key];
+            }
+        }
+
+        return $all_shares;
+    }
+
+    /**
+     * Lists *all* shares for the current app/share, regardless of
+     * permissions.
+     *
+     * This is for admin functionality and scripting tools, and shouldn't be
+     * called from user-level code!
+     *
+     * @return array  All shares for the current app/share.
+     */
+    public function listAllShares()
+    {
+        $shares = $this->_listAllShares();
+        $this->_sortList = $shares;
+        uasort($shares, array($this, '_sortShares'));
+        $this->_sortList = null;
+
+        return $shares;
+    }
+
+    /**
+     * Returns an array of all shares that $userid has access to.
+     *
+     * @param string $userid  The userid of the user to check access for.
+     * @param array $params   Additional parameters for the search.
+     *<pre>
+     *  'perm'        Require this level of permissions. Horde_Perms constant.
+     *  'attributes'  Restrict shares to these attributes. A hash or username.
+     *  'from'        Offset. Start at this share
+     *  'count'       Limit.  Only return this many.
+     *  'sort_by'     Sort by attribute.
+     *  'direction'   Sort by direction.
+     *</pre>
+     *
+     * @return array  The shares the user has access to.
+     */
+    public function listShares($userid, $params = array())
+    {
+        $params = array_merge(array('perm' => Horde_Perms::SHOW,
+                                    'attributes' => null,
+                                    'from' => 0,
+                                    'count' => 0,
+                                    'sort_by' => null,
+                                    'direction' => 0),
+                              $params);
+
+        $shares = $this->_listShares($userid, $params);
+        if (!count($shares)) {
+            return $shares;
+        }
+
+        $shares = $this->getShares($shares);
+        if (is_null($sort_by)) {
+            $this->_sortList = $shares;
+            uasort($shares, array($this, '_sortShares'));
+            $this->_sortList = null;
+        }
+
+        // Run the results through the callback, if configured.
+        if (!empty($this->_callbacks['list'])) {
+            return $this->runCallback('list', array($userid, $shares, $params));
+        }
+
+        return $shares;
+    }
+
+    /**
+     * Returns an array of all system shares.
+     *
+     * @return array  All system shares.
+     */
+    public function listSystemShares()
+    {
+        return array();
+    }
+
+    /**
+     * Returns the number of shares that $userid has access to.
+     *
+     * @param string $userid     The userid of the user to check access for.
+     * @param integer $perm      The level of permissions required.
+     * @param mixed $attributes  Restrict the shares counted to those
+     *                           matching $attributes. An array of
+     *                           attribute/values pairs or a share owner
+     *                           username.
+     *
+     * @return integer  The number of shares
+     */
+    public function countShares($userid, $perm = Horde_Perms::SHOW, $attributes = null)
+    {
+        return $this->_countShares($userid, $perm, $attributes);
+    }
+
+    /**
+     * Returns a new share object.
+     *
+     * @param string $owner The share owner name.
+     * @param string $name  The share's name.
+     *
+     * @return Horde_Share_Object  A new share object.
+     * @throws Horde_Share_Exception
+     */
+    public function newShare($owner, $name = '')
+    {
+        $share = $this->_newShare($name);
+        $share->set('owner', $owner);
+        $share->setShareOb(empty($this->_shareCallback) ? $this : $this->_shareCallback);
+
+        return $share;
+    }
+
+    /**
+     * Adds a share to the shares system.
+     *
+     * The share must first be created with newShare(), and have any initial
+     * details added to it, before this function is called.
+     *
+     * @param Horde_Share_Object $share  The new share object.
+     *
+     * @return boolean
+     * @throws Horde_Share_Exception
+     */
+    public function addShare(Horde_Share_Object $share)
+    {
+        // Run the results through the callback, if configured.
+        $this->runCallback('add', array($share));
+        $result = $this->_addShare($share);
+
+        /* Store new share in the caches. */
+        $id = $share->getId();
+        $name = $share->getName();
+        $this->_cache[$name] = $share;
+        $this->_shareMap[$id] = $name;
+
+        /* Reset caches that depend on unknown criteria. */
+        $this->_listcache = array();
+
+        return $result;
+    }
+
+    /**
+     * Removes a share from the shares system permanently.
+     *
+     * @param Horde_Share_Object $share  The share to remove.
+     *
+     * @throws Horde_Share_Exception
+     */
+    public function removeShare(Horde_Share_Object $share)
+    {
+        // Run the results through the callback, if configured.
+        $this->runCallback('remove', array($share));
+
+        /* Remove share from the caches. */
+        $id = $share->getId();
+        unset($this->_shareMap[$id]);
+        unset($this->_cache[$share->getName()]);
+
+        /* Reset caches that depend on unknown criteria. */
+        $this->_listcache = array();
+
+        return $this->_removeShare($share);
+    }
+
+    /**
+     * Checks if a share exists in the system.
+     *
+     * @param string $share  The share to check.
+     *
+     * @return boolean  True if the share exists.
+     */
+    public function exists($share)
+    {
+        if (isset($this->_cache[$share])) {
+            return true;
+        }
+
+        return $this->_exists($share);
+    }
+
+    /**
+     * Finds out what rights the given user has to this object.
+     *
+     * @see Horde_Perms::getPermissions
+     *
+     * @param mixed $share  The share that should be checked for the users
+     *                      permissions.
+     * @param string $user  The user to check for.
+     *
+     * @return mixed  A bitmask of permissions, a permission value, or an array
+     *                of permission values the user has, depending on the
+     *                permission type and whether the permission value is
+     *                ambiguous. False if there is no such permsission.
+     */
+    public function getPermissions($share, $user = null)
+    {
+        if (!($share instanceof Horde_Share_Object)) {
+            $share = $this->getShare($share);
+        }
+
+        return $this->_permsObject->getPermissions($share->getPermission(), $user);
+    }
+
+    /**
+     * Set the class type to use for creating share objects.
+     *
+     * @var string $classname  The classname to use.
+     */
+    public function setShareClass($classname)
+    {
+        $this->_shareObject = $classname;
+    }
+
+    /**
+     * Getter for Horde_Perms object
+     *
+     * @return Horde_Perms
+     */
+    public function getPermsObject()
+    {
+        return $this->_permsObject;
+    }
+
+    /**
+     * Convert TO the storage driver's charset. Individual share objects should
+     * implement this method if needed.
+     *
+     * @param array $data  Data to be converted.
+     */
+    public function toDriverCharset($data)
+    {
+        // noop
+    }
+
+    /**
+     * Add a callback to the collection
+     *
+     * @param string $type
+     * @param array $callback
+     */
+    public function addCallback($type, $callback)
+    {
+        $this->_callbacks[$type] = $callback;
+    }
+
+    /**
+     * Returns the share's list cache.
+     *
+     * @return array
+     */
+    public function getListCache()
+    {
+        return $this->_listcache;
+    }
+
+    /**
+     * Set the list cache.
+     *
+     * @param array $cache
+     */
+    public function setListCache($cache)
+    {
+        $this->_listcache = $cache;
+    }
+
+    /**
+     * Give public access to call the share callbacks. Needed to run the
+     * callbacks from the Horde_Share_Object objects.
+     *
+     * @param string $type   The callback to run
+     * @param array $params  The parameters to pass to the callback.
+     *
+     * @return mixed
+     */
+    public function runCallback($type, $params)
+    {
+        if (!empty($this->_callbacks[$type])) {
+            return call_user_func_array($this->_callbacks[$type], $params);
+        }
+    }
+
+    /**
+     * Expire the current list cache. This would be needed anytime a share is
+     * either added, deleted, had a change in owner, parent, or perms.
+     *
+     */
+    public function expireListCache()
+    {
+        $this->_listcache = array();
+    }
+
+    /**
+     * Utility function to be used with uasort() for sorting arrays of
+     * Horde_Share objects.
+     *
+     * Example:
+     * <code>
+     * uasort($list, array('Horde_Share', '_sortShares'));
+     * </code>
+     */
+    protected function _sortShares($a, $b)
+    {
+        $aParts = explode(':', $a->getName());
+        $bParts = explode(':', $b->getName());
+
+        $min = min(count($aParts), count($bParts));
+        $idA = '';
+        $idB = '';
+        for ($i = 0; $i < $min; $i++) {
+            if ($idA) {
+                $idA .= ':';
+                $idB .= ':';
+            }
+            $idA .= $aParts[$i];
+            $idB .= $bParts[$i];
+
+            if ($idA != $idB) {
+                $curA = isset($this->_sortList[$idA]) ? $this->_sortList[$idA]->get('name') : '';
+                $curB = isset($this->_sortList[$idB]) ? $this->_sortList[$idB]->get('name') : '';
+                return strnatcasecmp($curA, $curB);
+            }
+        }
+
+        return count($aParts) > count($bParts);
+    }
+
+    /**
+     * Logs a debug entry
+     *
+     * @param string $sql     The log statement.
+     * @param string $name    TODO
+     * @param float $runtime  Runtime interval.
+     */
+    protected function _logDebug($entry, $name, $runtime = null)
+    {
+        /*@TODO */
+        $name = (empty($name) ? '' : $name)
+              . (empty($runtime) ? '' : sprintf(" (%.4fs)", $runtime));
+        $this->_logger->debug($this->_formatLogEntry($name, $entry));
+    }
+
+    /**
+     * Logs an error entry.
+     *
+     * @param string $error   The error statement.
+     * @param string $name    TODO
+     * @param float $runtime  Runtime interval.
+     */
+    protected function _logError($error, $name, $runtime = null)
+    {
+        /*@TODO */
+        $name = (empty($name) ? '' : $name)
+              . (empty($runtime) ? '' : sprintf(" (%.4fs)", $runtime));
+        $this->_logger->err($this->_formatLogEntry($name, $error));
+    }
+
+    /**
+     * Formats the log entry.
+     *
+     * @param string $message  Message.
+     * @param string $sql      SQL statment.
+     *
+     * @return string  Formatted log entry.
+     */
+    protected function _formatLogEntry($message, $sql)
+    {
+        return "SQL $message  \n\t" . wordwrap(preg_replace("/\s+/", ' ', $sql), 70, "\n\t  ", 1);
+    }
+
+}
index 2b63e7e..a551f65 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once 'Horde/DataTree.php';
-
 /**
  * Horde_Share_datatree:: provides the datatree backend for the horde share
  * driver.
@@ -19,7 +16,7 @@ require_once 'Horde/DataTree.php';
  * @author  Gunnar Wrobel <wrobel@pardus.de>
  * @package Horde_Share
  */
-class Horde_Share_Datatree extends Horde_Share
+class Horde_Share_Datatree extends Horde_Share_Base
 {
     /**
      * The Horde_Share_Object subclass to instantiate objects as
index 2180259..1fe20a6 100644 (file)
@@ -15,7 +15,7 @@
  * @author  Gunnar Wrobel <wrobel@pardus.de>
  * @package Horde_Share
  */
-class Horde_Share_kolab extends Horde_Share
+class Horde_Share_Kolab extends Horde_Share_Base
 {
     const VERSION = 1;
 
@@ -171,7 +171,7 @@ class Horde_Share_kolab extends Horde_Share
      * Returns an array of all shares that $userid has access to.
      *
      * @param string $userid     The userid of the user to check access for.
-     * @param array  $params  @see Horde_Share::listShares
+     * @param array  $params     See listShares().
      *
      * @return array  The shares the user has access to.
      */
index 236e12b..7089f7e 100644 (file)
@@ -16,7 +16,7 @@
 /**
  * @package Horde_Share
  */
-class Horde_Share_Sql extends Horde_Share
+class Horde_Share_Sql extends Horde_Share_Base
 {
     /* Share has user perms */
     const SQL_FLAG_USERS = 1;
@@ -50,7 +50,7 @@ class Horde_Share_Sql extends Horde_Share
 
     /**
      *
-     * @see Horde_Share::__construct()
+     * @see Horde_Share_Base::__construct()
      */
     public function __construct($app, $user, Horde_Perms $perms, Horde_Group $groups)
     {
@@ -630,7 +630,7 @@ class Horde_Share_Sql extends Horde_Share
                         . ' AND (' . Horde_SQL::buildClause($this->_db, 'g.perm', '&', $perm) . '))';
                 }
             } catch (Horde_Group_Exception $e) {
-                $this->_logError($e, 'Horde_Share::getShareCriteria()');
+                $this->_logError($e, 'Horde_Share_Sql::getShareCriteria()');
             }
         } else {
             $where = '(' . Horde_SQL::buildClause($this->_db, 's.perm_guest', '&', $perm) . ')';
index 39a5324..35b8c1d 100644 (file)
@@ -3,16 +3,16 @@
  <name>Horde_Share</name>
  <channel>pear.horde.org</channel>
  <summary>Horde Shared Permissions System</summary>
- <description>Horde_Share:: This class provides an interface to all shared
-resources a user owns or has access to.</description>
+ <description>Horde_Share provides an interface to all shared resources a user
+owns or has access to.</description>
  <lead>
   <name>Chuck Hagenbuch</name>
   <user>chuck</user>
   <email>chuck@horde.org</email>
   <active>yes</active>
  </lead>
- <date>2010-12-07</date>
- <time>17:46:01</time>
+ <date>2010-12-16</date>
+ <time>19:14:43</time>
  <version>
   <release>0.0.4</release>
   <api>0.0.4</api>
@@ -44,6 +44,7 @@ resources a user owns or has access to.</description>
       <dir name="Sql">
        <file name="Hierarchical.php" role="php" />
       </dir> <!-- /lib/Horde/Share/Sql -->
+      <file name="Base.php" role="php" />
       <file name="Datatree.php" role="php" />
       <file name="Exception.php" role="php" />
       <file name="Kolab.php" role="php" />
@@ -53,7 +54,6 @@ resources a user owns or has access to.</description>
        <tasks:replace from="@data_dir@" to="data_dir" type="pear-config" />
       </file>
      </dir> <!-- /lib/Horde/Share -->
-     <file name="Share.php" role="php" />
     </dir> <!-- /lib/Horde -->
    </dir> <!-- /lib -->
    <dir name="locale">
@@ -346,7 +346,7 @@ resources a user owns or has access to.</description>
  </dependencies>
  <phprelease>
   <filelist>
-   <install as="Horde/Share.php" name="lib/Horde/Share.php" />
+   <install as="Horde/Share/Base.php" name="lib/Horde/Share/Base.php" />
    <install as="Horde/Share/Datatree.php" name="lib/Horde/Share/Datatree.php" />
    <install as="Horde/Share/Exception.php" name="lib/Horde/Share/Exception.php" />
    <install as="Horde/Share/Kolab.php" name="lib/Horde/Share/Kolab.php" />
@@ -515,7 +515,7 @@ Initial release as a PEAR package
     <release>beta</release>
     <api>beta</api>
    </stability>
-   <date>2010-12-07</date>
+   <date>2010-12-16</date>
    <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
    <notes>
 * Converted to Horde 4 coding standards