From: Jan Schneider Date: Thu, 16 Dec 2010 18:15:17 +0000 (+0100) Subject: Horde_Share -> Horde_Share_Base. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=00cc26e77fe3b16a8daf889f7aa5eb35c5233a1e;p=horde.git Horde_Share -> Horde_Share_Base. --- diff --git a/ansel/lib/Storage.php b/ansel/lib/Storage.php index 237983c39..5a49f7438 100644 --- a/ansel/lib/Storage.php +++ b/ansel/lib/Storage.php @@ -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. * diff --git a/framework/Core/lib/Horde/Core/Factory/Share.php b/framework/Core/lib/Horde/Core/Factory/Share.php index 28367e33b..5774b7ef9 100644 --- a/framework/Core/lib/Horde/Core/Factory/Share.php +++ b/framework/Core/lib/Horde/Core/Factory/Share.php @@ -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. diff --git a/framework/Core/lib/Horde/Core/Factory/ShareBase.php b/framework/Core/lib/Horde/Core/Factory/ShareBase.php index 4f1558fde..68892988a 100644 --- a/framework/Core/lib/Horde/Core/Factory/ShareBase.php +++ b/framework/Core/lib/Horde/Core/Factory/ShareBase.php @@ -1,6 +1,6 @@ _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 index 4f77bf44a..000000000 --- a/framework/Share/lib/Horde/Share.php +++ /dev/null @@ -1,630 +0,0 @@ - - * - * 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 - * @author Mike Cochrame - * @author Chuck Hagenbuch - * @author Jan Schneider - * @author Gunnar Wrobel - * @author Michael J. Rubinsky - * @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: - *
-     * 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.
-     *
- * - * @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. - *
-     *  '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.
-     *
- * - * @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: - * - * uasort($list, array('Horde_Share', '_sortShares')); - * - */ - 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 index 000000000..eba271650 --- /dev/null +++ b/framework/Share/lib/Horde/Share/Base.php @@ -0,0 +1,628 @@ + + * + * 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 + * @author Mike Cochrame + * @author Chuck Hagenbuch + * @author Jan Schneider + * @author Gunnar Wrobel + * @author Michael J. Rubinsky + * @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: + *
+     * 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.
+     *
+ * + * @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. + *
+     *  '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.
+     *
+ * + * @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: + * + * uasort($list, array('Horde_Share', '_sortShares')); + * + */ + 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/Datatree.php b/framework/Share/lib/Horde/Share/Datatree.php index 2b63e7ec0..a551f65e0 100644 --- a/framework/Share/lib/Horde/Share/Datatree.php +++ b/framework/Share/lib/Horde/Share/Datatree.php @@ -1,7 +1,4 @@ * @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 diff --git a/framework/Share/lib/Horde/Share/Kolab.php b/framework/Share/lib/Horde/Share/Kolab.php index 218025969..1fe20a669 100644 --- a/framework/Share/lib/Horde/Share/Kolab.php +++ b/framework/Share/lib/Horde/Share/Kolab.php @@ -15,7 +15,7 @@ * @author Gunnar Wrobel * @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. */ diff --git a/framework/Share/lib/Horde/Share/Sql.php b/framework/Share/lib/Horde/Share/Sql.php index 236e12b35..7089f7e10 100644 --- a/framework/Share/lib/Horde/Share/Sql.php +++ b/framework/Share/lib/Horde/Share/Sql.php @@ -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) . ')'; diff --git a/framework/Share/package.xml b/framework/Share/package.xml index 39a5324b8..35b8c1d30 100644 --- a/framework/Share/package.xml +++ b/framework/Share/package.xml @@ -3,16 +3,16 @@ Horde_Share pear.horde.org Horde Shared Permissions System - Horde_Share:: This class provides an interface to all shared -resources a user owns or has access to. + Horde_Share provides an interface to all shared resources a user +owns or has access to. Chuck Hagenbuch chuck chuck@horde.org yes - 2010-12-07 - + 2010-12-16 + 0.0.4 0.0.4 @@ -44,6 +44,7 @@ resources a user owns or has access to. + @@ -53,7 +54,6 @@ resources a user owns or has access to. - @@ -346,7 +346,7 @@ resources a user owns or has access to. - + @@ -515,7 +515,7 @@ Initial release as a PEAR package beta beta - 2010-12-07 + 2010-12-16 LGPL * Converted to Horde 4 coding standards