From 6287faff03e67682802dc866ed46177dcac31472 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 8 Nov 2010 12:09:57 -0500 Subject: [PATCH] Horde_Share refactor for H4...merge from the H4-Shares branch. --- ansel/lib/Ansel.php | 6 +- ansel/lib/Api.php | 12 +- ansel/lib/Block/my_galleries.php | 10 +- ansel/lib/Gallery.php | 42 +- ansel/lib/Gallery/Decorator/Date.php | 7 +- ansel/lib/GalleryMode/Normal.php | 2 +- ansel/lib/ImageGenerator.php | 2 +- ansel/lib/Storage.php | 30 +- ansel/lib/View/List.php | 4 +- ansel/lib/Widget/OtherGalleries.php | 2 +- ansel/perms.php | 4 +- ansel/rss.php | 2 +- ansel/templates/group/owner.inc | 4 +- folks/lib/Driver.php | 2 +- folks/lib/Friends/shared.php | 2 +- folks/perms.php | 30 +- framework/Core/lib/Horde/Core/Factory/Share.php | 4 +- framework/Core/lib/Horde/Core/Share/Driver.php | 279 +++++++++ framework/Core/package.xml | 14 +- framework/Group/lib/Horde/Group/Contactlists.php | 3 +- framework/Net_IMSP/IMSP/Utils.php | 10 +- framework/Share/lib/Horde/Share.php | 160 +++-- framework/Share/lib/Horde/Share/Datatree.php | 4 +- framework/Share/lib/Horde/Share/Kolab.php | 160 +++-- framework/Share/lib/Horde/Share/Object.php | 143 +---- framework/Share/lib/Horde/Share/Object/Kolab.php | 54 +- framework/Share/lib/Horde/Share/Object/Sql.php | 136 ++--- .../lib/Horde/Share/Object/Sql/Hierarchical.php | 68 ++- framework/Share/lib/Horde/Share/Sql.php | 643 ++++++++------------- .../Share/lib/Horde/Share/Sql/Hierarchical.php | 247 ++++---- framework/Share/package.xml | 6 +- horde/lib/Api.php | 113 ++-- horde/services/shares/edit.php | 2 +- ingo/lib/Application.php | 5 +- ingo/lib/Ingo.php | 5 +- kronolith/lib/Api.php | 10 +- kronolith/lib/Driver/Sql.php | 2 +- kronolith/lib/Kronolith.php | 12 +- kronolith/perms.php | 2 +- kronolith/scripts/agenda.php | 13 +- kronolith/scripts/import_squirrelmail_calendar.php | 2 +- mnemo/lib/Forms/CreateNotepad.php | 2 +- mnemo/lib/Forms/DeleteNotepad.php | 2 +- mnemo/lib/Mnemo.php | 8 +- nag/lib/Api.php | 7 +- nag/lib/Nag.php | 9 +- nag/mobile.php | 2 +- nag/templates/list.html.php | 2 +- turba/lib/Api.php | 7 +- turba/lib/Application.php | 5 +- turba/lib/Turba.php | 7 +- turba/scripts/upgrades/public_to_horde_share.php | 2 +- whups/lib/Query.php | 16 +- 53 files changed, 1211 insertions(+), 1116 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Share/Driver.php diff --git a/ansel/lib/Ansel.php b/ansel/lib/Ansel.php index 5e7326e6f..1f1c2a422 100644 --- a/ansel/lib/Ansel.php +++ b/ansel/lib/Ansel.php @@ -71,10 +71,10 @@ class Ansel *
      *     (integer)selected  The gallery_id of the gallery that is selected
      *     (integer)perm      The permissions filter to use [Horde_Perms::SHOW]
-     *     (mixed)filter      Restrict the galleries returned to those matching
+     *     (mixed)attributes  Restrict the galleries returned to those matching
      *                        the filters. Can be an array of attribute/values
      *                        pairs or a gallery owner username.
-     *     (boolean)allLevels
+     *     (boolean)all_levels
      *     (integer)parent    The parent share to start listing at.
      *     (integer)from      The gallery to start listing at.
      *     (integer)count     The number of galleries to return.
@@ -85,12 +85,12 @@ class Ansel
      */
     static public function selectGalleries($params = array())
     {
-        $params = new Horde_Support_Array($params);
         $galleries = $GLOBALS['injector']
             ->getInstance('Ansel_Injector_Factory_Storage')
             ->create()
             ->listGalleries($params);
 
+        $params = new Horde_Support_Array($params);
         $tree = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Tree')->create('gallery_tree', 'Select');
 
         /* Remove the ignored gallery, make sure it's also not the selected
diff --git a/ansel/lib/Api.php b/ansel/lib/Api.php
index ce2384d72..1bd29c313 100644
--- a/ansel/lib/Api.php
+++ b/ansel/lib/Api.php
@@ -39,7 +39,7 @@ class Ansel_Api extends Horde_Registry_Api
         $storage = $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create();
         if (empty($path)) {
             $owners = array();
-            $galleries = $storage->listGalleries(array('allLevels' => false));
+            $galleries = $storage->listGalleries(array('all_levels' => false));
             foreach ($galleries  as $gallery) {
                 $owners[$gallery->data['share_owner'] ? $gallery->data['share_owner'] : '-system-'] = true;
             }
@@ -73,8 +73,8 @@ class Ansel_Api extends Horde_Registry_Api
         } else {
             if (count($parts) == 1) {
                 // This request is for all galleries owned by the requested user.
-                $galleries = $storage->listGalleries(array('filter' => $parts[0],
-                                                           'allLevels' => false));
+                $galleries = $storage->listGalleries(array('attributes' => $parts[0],
+                                                           'all_levels' => false));
                 $images = array();
             } elseif ($this->galleryExists(null, end($parts))) {
                 // This request if for a certain gallery, list all sub-galleries
@@ -87,7 +87,7 @@ class Ansel_Api extends Horde_Registry_Api
                     throw new Horde_Exception_NotFound(_("Invalid gallery specified."));
                 }
                 $galleries = $storage->listGalleries(array('parent' => $gallery_id,
-                                                           'allLevels' => false));
+                                                           'all_levels' => false));
                 $images = $this->listImages(null, $gallery_id, Horde_Perms::SHOW, 'mini');
 
             } elseif (count($parts) > 2 &&
@@ -658,11 +658,11 @@ class Ansel_Api extends Horde_Registry_Api
      *   
      *     (string)scope      The application scope, if not default.
      *     (integer)perm      The permissions filter to use [Horde_Perms::SHOW]
-     *     (mixed)filter      Restrict the galleries returned to those matching
+     *     (mixed)attributes  Restrict the galleries returned to those matching
      *                        the filters. Can be an array of attribute/values
      *                        pairs or a gallery owner username.
      *     (integer)parent    The parent share to start listing at.
-     *     (boolean)allLevels If set, return all levels below parent, not just
+     *     (boolean)all_levels If set, return all levels below parent, not just
      *                        direct children [TRUE]
      *     (integer)from      The gallery to start listing at.
      *     (integer)count     The number of galleries to return.
diff --git a/ansel/lib/Block/my_galleries.php b/ansel/lib/Block/my_galleries.php
index 0c9455db5..074bf5822 100644
--- a/ansel/lib/Block/my_galleries.php
+++ b/ansel/lib/Block/my_galleries.php
@@ -59,11 +59,11 @@ class Horde_Block_ansel_my_galleries extends Horde_Block
         try {
             $galleries = $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')
                 ->create()->listGalleries(array('perm' => Horde_Perms::EDIT,
-                                                  'filter' => $GLOBALS['registry']->getAuth(),
-                                                  'allLevels' => false,
-                                                  'count' => empty($this->_params['limit']) ? 0 : $this->_params['limit'],
-                                                  'sort_by' => 'last_modified',
-                                                  'direction' => Ansel::SORT_DESCENDING));
+                                                'attributes' => $GLOBALS['registry']->getAuth(),
+                                                 'all_levels' => false,
+                                                 'count' => empty($this->_params['limit']) ? 0 : $this->_params['limit'],
+                                                 'sort_by' => 'last_modified',
+                                                 'direction' => Ansel::SORT_DESCENDING));
 
         } catch (Ansel_Exception $e) {
             return $e->getMessage();
diff --git a/ansel/lib/Gallery.php b/ansel/lib/Gallery.php
index 775187b23..fc55b533c 100644
--- a/ansel/lib/Gallery.php
+++ b/ansel/lib/Gallery.php
@@ -29,9 +29,8 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical
     {
         /* Pass on up the chain */
         parent::__construct($attributes);
-        $this->setShareOb($GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create()->shares);
-        $mode = isset($attributes['attribute_view_mode']) ? $attributes['attribute_view_mode'] : 'Normal';
-        $this->_setModeHelper($mode);
+        $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create()->shares->initShareObject($this);
+        $this->_setModeHelper(isset($attributes['attribute_view_mode']) ? $attributes['attribute_view_mode'] : 'Normal');
     }
 
     /**
@@ -171,8 +170,8 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical
         try {
             $this->save();
         } catch (Horde_Share_Exception $e) {
-            Horde::logMessage($e->getMessage, 'ERR');
-            throw Ansel_Exception($e);
+            Horde::logMessage($e->getMessage(), 'ERR');
+            throw new Ansel_Exception($e);
         }
 
         /* Make sure we get rid of key image/stacks if no more images */
@@ -667,7 +666,7 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical
                     $galleries = $GLOBALS['injector']
                         ->getInstance('Ansel_Injector_Factory_Storage')
                         ->create()
-                        ->listGalleries(array('parent' => $this, 'allLevels' => false));
+                        ->listGalleries(array('parent' => $this, 'all_levels' => false));
 
                     foreach ($galleries as $galleryId => $gallery) {
                         if ($default_img = $gallery->getKeyImage($style)) {
@@ -933,19 +932,14 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical
 
         /* Update the backend, but only this current change */
         if ($update) {
-            $db = $this->getShareOb()->getWriteDb();
+            $db = $this->getShareOb()->getStorage();
             // Manually convert the charset since we're not going through save()
             $data = $this->getshareOb()->toDriverCharset(array($driver_key => $value));
-            $query = $db->prepare('UPDATE ' . $this->getShareOb()->getTable() . ' SET ' . $driver_key . ' = ? WHERE share_id = ?', null, MDB2_PREPARE_MANIP);
+            $sql = 'UPDATE ' . $this->getShareOb()->getTable() . ' SET ' . $driver_key . ' = ? WHERE share_id = ?';
             if ($GLOBALS['conf']['ansel_cache']['usecache']) {
                 $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $this->id);
             }
-            $result = $query->execute(array($data[$driver_key], $this->id));
-            $query->free();
-            if ($result instanceof PEAR_Error) {
-                throw new Horde_Exception($result->getMessage());
-            }
-
+           $db->update($sql, array($data[$driver_key], $this->id));
         }
 
         return true;
@@ -972,23 +966,11 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical
         return $this->_modeHelper->getGalleryCrumbData();
     }
 
-    /**
-     *
-     */
-    public function __sleep()
-    {
-        $properties = get_object_vars($this);
-        unset($properties['_shareOb']);
-        unset($properties['_modeHelper']);
-        $properties = array_keys($properties);
-        return $properties;
-    }
-
-    public function __wakeup()
+    public function unserialize($data)
     {
-        $this->setShareOb($GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create()->shares);
-        $mode = $this->get('view_mode');
-        $this->_setModeHelper($mode);
+        parent::unserialize($data);
+        $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create()->shares->initShareObject($this);
+        $this->_setModeHelper($this->get('view_mode'));
     }
 
 }
diff --git a/ansel/lib/Gallery/Decorator/Date.php b/ansel/lib/Gallery/Decorator/Date.php
index 9932f7a92..0f8218c92 100644
--- a/ansel/lib/Gallery/Decorator/Date.php
+++ b/ansel/lib/Gallery/Decorator/Date.php
@@ -162,14 +162,15 @@ class Ansel_Gallery_Decorator_Date
     /**
      * Return a count of the number of children this share has
      *
-     * @param integer $perm  A Horde_Perms::* constant
+     * @param string  $user       The user to use for checking perms
+     * @param integer $perm       A Horde_Perms::* constant
      * @param boolean $allLevels  Count grandchildren or just children
      *
      * @return mixed  The number of child shares || PEAR_Error
      */
-    public function countChildren($perm = Horde_Perms::SHOW, $allLevels = true)
+    public function countChildren($user, $perm = Horde_Perms::SHOW, $allLevels = true)
     {
-        return $this->_gallery->getShareOb()->countShares($GLOBALS['registry']->getAuth(), $perm, null, $this, $allLevels);
+        return $this->_gallery->getShareOb()->countShares($user, $perm, null, $this, $allLevels);
     }
 
     /**
diff --git a/ansel/lib/GalleryMode/Normal.php b/ansel/lib/GalleryMode/Normal.php
index 8ffa5de94..af40a6d6c 100644
--- a/ansel/lib/GalleryMode/Normal.php
+++ b/ansel/lib/GalleryMode/Normal.php
@@ -46,7 +46,7 @@ class Ansel_GalleryMode_Normal extends Ansel_GalleryMode_Base
             if ($num_galleries > $from) {
                 $galleries = $storage->listGalleries(
                         array('parent' => $this->_gallery,
-                              'allLevels' => false,
+                              'all_levels' => false,
                               'from' => $from,
                               'count' => $to));
             }
diff --git a/ansel/lib/ImageGenerator.php b/ansel/lib/ImageGenerator.php
index 1c375152a..288a2f047 100644
--- a/ansel/lib/ImageGenerator.php
+++ b/ansel/lib/ImageGenerator.php
@@ -151,7 +151,7 @@ class Ansel_ImageGenerator
         $galleries = $GLOBALS['injector']
             ->getInstance('Ansel_Injector_Factory_Storage')
             ->create()
-            ->listGalleries(array('parent' => $parent, 'allLevels' => false));
+            ->listGalleries(array('parent' => $parent, 'all_levels' => false));
 
         foreach ($galleries as $gallery) {
             if ($gallery->countImages()) {
diff --git a/ansel/lib/Storage.php b/ansel/lib/Storage.php
index adb9d56f5..2bca772ce 100644
--- a/ansel/lib/Storage.php
+++ b/ansel/lib/Storage.php
@@ -40,7 +40,7 @@ class Ansel_Storage
      *
      * @return Ansel_Storage
      */
-    public function __construct(Horde_Share_Sql_Hierarchical $shareOb)
+    public function __construct(Horde_Core_Share_Driver $shareOb)
     {
         /* This is the only supported share backend for Ansel */
         $this->_shares = $shareOb;
@@ -121,7 +121,7 @@ class Ansel_Storage
 
         /* Create the gallery */
         try {
-            $gallery = $this->_shares->newShare('');
+            $gallery = $this->_shares->newShare($GLOBALS['registry']->getAuth(), '');
         } catch (Horde_Share_Exception $e) {
             Horde::logMessage($e->getMessage, 'ERR');
             throw new Ansel_Exception($e);
@@ -396,7 +396,7 @@ class Ansel_Storage
     public function removeGallery(Ansel_Gallery $gallery)
     {
         /* Get any children and empty them */
-        $children = $gallery->getChildren(null, true);
+        $children = $gallery->getChildren(null, null, true);
         foreach ($children as $child) {
             $this->emptyGallery($child);
             $child->setTags(array());
@@ -426,7 +426,7 @@ class Ansel_Storage
 
         /* See if we need to clear the has_subgalleries field */
         if ($parent instanceof Ansel_Gallery) {
-            if (!$parent->countChildren(Horde_Perms::SHOW, false)) {
+            if (!$parent->countChildren($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW, false)) {
                 $parent->set('has_subgalleries', 0, true);
                 if ($GLOBALS['conf']['ansel_cache']['usecache']) {
                     $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $parent->id);
@@ -820,11 +820,11 @@ class Ansel_Storage
     * @param array $params  Optional parameters:
     *   
     *     (integer)perm      The permissions filter to use [Horde_Perms::SHOW]
-    *     (mixed)filter      Restrict the galleries returned to those matching
+    *     (mixed)attributes  Restrict the galleries returned to those matching
     *                        the filters. Can be an array of attribute/values
     *                        pairs or a gallery owner username.
     *     (integer)parent    The parent share to start listing at.
-    *     (boolean)allLevels If set, return all levels below parent, not just
+    *     (boolean)all_levels If set, return all levels below parent, not just
     *                        direct children [TRUE]
     *     (integer)from      The gallery to start listing at.
     *     (integer)count     The number of galleries to return.
@@ -837,19 +837,8 @@ class Ansel_Storage
     */
     public function listGalleries($params = array())
     {
-        $params = new Horde_Support_Array($params);
-
         try {
-            $shares = $this->_shares->listShares(
-                $GLOBALS['registry']->getAuth(),
-                $params->get('perm', Horde_Perms::SHOW),
-                $params->get('filter', null),
-                $params->get('from', 0),
-                $params->get('count', 0),
-                $params->get('sort_by', null),
-                $params->get('direction', Ansel::SORT_ASCENDING),
-                $params->get('parent', null),
-                $params->get('allLevels', true));
+            $shares = $this->_shares->listShares($GLOBALS['registry']->getAuth(), $params);
         } catch (Horde_Share_Exception $e) {
             throw new Ansel_Exception($e);
         }
@@ -938,13 +927,12 @@ class Ansel_Storage
      */
     public function getRandomGallery($params = array())
     {
-        $params = new Horde_Support_Array($params);
         $galleries = $this->listGalleries($params);
         if (!$galleries) {
             return false;
         }
-
         $gallery_key = array_rand($galleries);
+
         return $galleries[$gallery_key];
     }
 
@@ -1059,7 +1047,7 @@ class Ansel_Storage
     public function getRecentImagesGeodata($user = null, $start = 0, $count = 8)
     {
         $galleries = $this->listGalleries(array('perm' => Horde_Perms::EDIT,
-                                                'filter' => $user));
+                                                'attributes' => $user));
         if (empty($galleries)) {
             return array();
         }
diff --git a/ansel/lib/View/List.php b/ansel/lib/View/List.php
index 0d0d66b75..b31c5cab1 100644
--- a/ansel/lib/View/List.php
+++ b/ansel/lib/View/List.php
@@ -128,8 +128,8 @@ class Ansel_View_List extends Ansel_View_Base
             } else {
                 $this->_galleryList = $ansel_storage->listGalleries(
                     array('perm' => Horde_Perms::SHOW,
-                          'filter' => $filter,
-                          'allLevels' => false,
+                          'attributes' => $filter,
+                          'all_levels' => false,
                           'from' => $this->_page * $this->_g_perPage,
                           'count' => $this->_g_perPage,
                           'sort_by' => $this->_sortBy,
diff --git a/ansel/lib/Widget/OtherGalleries.php b/ansel/lib/Widget/OtherGalleries.php
index 4d0b1092d..292273330 100644
--- a/ansel/lib/Widget/OtherGalleries.php
+++ b/ansel/lib/Widget/OtherGalleries.php
@@ -69,7 +69,7 @@ class Ansel_Widget_OtherGalleries extends Ansel_Widget_Base
         try {
             $galleries = $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')
                     ->create()
-                    ->listGalleries(array('filter' => $owner));
+                    ->listGalleries(array('attributes' => $owner));
         } catch (Ansel_Exception $e) {
             Horde::logMessage($e, 'ERR');
             return '';
diff --git a/ansel/perms.php b/ansel/perms.php
index 6baf30b23..ec1dcd38b 100644
--- a/ansel/perms.php
+++ b/ansel/perms.php
@@ -29,8 +29,8 @@ case 'edit':
     try {
         $share = $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create()->getGallery(Horde_Util::getFormData('cid'));
         $form = 'edit.inc';
-        $perm = &$share->getPermission();
-    } catch (Horde_Share_Exception $e) {
+        $perm = $share->getPermission();
+    } catch (Horde_Exception_NotFound $e) {
         if (($share_name = Horde_Util::getFormData('share')) !== null) {
             try {
                 $share = $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Storage')->create()->shares->getShare($share_name);
diff --git a/ansel/rss.php b/ansel/rss.php
index 22d2d9241..a75535bc0 100644
--- a/ansel/rss.php
+++ b/ansel/rss.php
@@ -127,7 +127,7 @@ if (empty($rss)) {
             $shares = $GLOBALS['injector']
                 ->getInstance('Ansel_Injector_Factory_Storage')
                 ->create()
-                ->listGalleries(array('filter' => $id));
+                ->listGalleries(array('attributes' => $id));
             foreach ($shares as $gallery) {
                 if ($gallery->isOldEnough() && !$gallery->hasPasswd()) {
                     $galleries[] = $gallery->id;
diff --git a/ansel/templates/group/owner.inc b/ansel/templates/group/owner.inc
index 786d0f194..628bee0b1 100644
--- a/ansel/templates/group/owner.inc
+++ b/ansel/templates/group/owner.inc
@@ -33,12 +33,12 @@ $num_galleries = $GLOBALS['injector']->getInstance('Ansel_Injector_Factory_Stora
 if ($num_galleries < 5) {
     $mini_galleries = $GLOBALS['injector']->
         getInstance('Ansel_Injector_Factory_Storage')->
-        create()->listGalleries(array('filter' => $group, 'allLevels' => false));
+        create()->listGalleries(array('attributes' => $group, 'all_levels' => false));
 } else {
     $mini_galleries = $GLOBALS['injector']
         ->getInstance('Ansel_Injector_Factory_Storage')
         ->create()
-        ->listGalleries(array('filter' => $group,
+        ->listGalleries(array('attributes' => $group,
                               'from' => rand(0, $num_galleries - 4),
                               'count' => 4));
 }
diff --git a/folks/lib/Driver.php b/folks/lib/Driver.php
index e803adab9..72b536a55 100644
--- a/folks/lib/Driver.php
+++ b/folks/lib/Driver.php
@@ -456,7 +456,7 @@ class Folks_Driver {
         // Delete groups
         if ($GLOBALS['conf']['friends']) {
             $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
-            $groups = $shares->listShares($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW, true);
+            $groups = $shares->listShares($GLOBALS['registry']->getAuth(), array('perm' => Horde_Perms::SHOW));
             foreach ($groups as $share) {
                 $result = $shares->removeShare($share);
                 if ($result instanceof PEAR_Error) {
diff --git a/folks/lib/Friends/shared.php b/folks/lib/Friends/shared.php
index 6776358b0..6297d076b 100644
--- a/folks/lib/Friends/shared.php
+++ b/folks/lib/Friends/shared.php
@@ -58,7 +58,7 @@ class Folks_Friends_shared extends  Folks_Friends_sql {
     protected function _getGroups()
     {
         $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
-        $groups = $GLOBALS['folks_shares']->listShares($this->_user, Horde_Perms::READ);
+        $groups = $GLOBALS['folks_shares']->listShares($this->_user, array('perm' => Horde_Perms::READ));
 
         $list = array();
         foreach ($groups as $group) {
diff --git a/folks/perms.php b/folks/perms.php
index d33e940ba..7eda28201 100644
--- a/folks/perms.php
+++ b/folks/perms.php
@@ -19,33 +19,33 @@ $reload = false;
 $actionID = Horde_Util::getFormData('actionID', 'edit');
 switch ($actionID) {
 case 'edit':
-    $share = &$shares->getShareById(Horde_Util::getFormData('cid'));
-    if (!is_a($share, 'PEAR_Error')) {
-        $perm = &$share->getPermission();
-    } elseif (($category = Horde_Util::getFormData('share')) !== null) {
-        $share = &$shares->getShare($category);
-        if (!is_a($share, 'PEAR_Error')) {
-            $perm = &$share->getPermission();
+    try {
+        $share = $shares->getShareById(Horde_Util::getFormData('cid'));
+        $perm = $share->getPermission();
+    } catch (Horde_Exception_NotFound $e) {
+        if (($category = Horde_Util::getFormData('share')) !== null) {
+            $share = $shares->getShare($category);
+            $perm = $share->getPermission();
         }
     }
-    if (is_a($share, 'PEAR_Error')) {
-        $notification->push($share, 'horde.error');
-    } elseif (!$GLOBALS['registry']->getAuth() ||
-              (isset($share) && $GLOBALS['registry']->getAuth() != $share->get('owner'))) {
+    if (!$GLOBALS['registry']->getAuth() ||
+        (isset($share) && $GLOBALS['registry']->getAuth() != $share->get('owner'))) {
         exit('permission denied');
     }
     break;
 
 case 'editform':
-    $share = &$shares->getShareById(Horde_Util::getFormData('cid'));
-    if (is_a($share, 'PEAR_Error')) {
+    try {
+        $share = $shares->getShareById(Horde_Util::getFormData('cid'));
+    } catch (Horde_Exception_NotFound $e) {
         $notification->push(_("Attempt to edit a non-existent share."), 'horde.error');
-    } else {
+    }
+    if ($share) {
         if (!$GLOBALS['registry']->getAuth() ||
             $GLOBALS['registry']->getAuth() != $share->get('owner')) {
             exit('permission denied');
         }
-        $perm = &$share->getPermission();
+        $perm = $share->getPermission();
 
         // Process owner and owner permissions.
         $old_owner = $share->get('owner');
diff --git a/framework/Core/lib/Horde/Core/Factory/Share.php b/framework/Core/lib/Horde/Core/Factory/Share.php
index 38e8a8b9a..dcff45c8e 100644
--- a/framework/Core/lib/Horde/Core/Factory/Share.php
+++ b/framework/Core/lib/Horde/Core/Factory/Share.php
@@ -81,8 +81,8 @@ class Horde_Core_Factory_Share
             if (!class_exists($class)) {
                 throw new Horde_Exception(sprintf(Horde_Core_Translation::t("\"%s\" share driver not found."), $driver));
             }
-
-            $ob = new $class($app, $this->_injector->getInstance('Horde_Perms'));
+            $sob = new $class($app, $GLOBALS['registry']->getAuth(), $this->_injector->getInstance('Horde_Perms'), $this->_injector->getInstance('Horde_Group'));
+            $ob = new Horde_Core_Share_Driver($sob);
         }
 
         if (!empty($GLOBALS['conf']['share']['cache'])) {
diff --git a/framework/Core/lib/Horde/Core/Share/Driver.php b/framework/Core/lib/Horde/Core/Share/Driver.php
new file mode 100644
index 000000000..64b095ebd
--- /dev/null
+++ b/framework/Core/lib/Horde/Core/Share/Driver.php
@@ -0,0 +1,279 @@
+
+ * @category Horde
+ * @license  http://opensource.org/licenses/lgpl-2.1.php LGPL
+ * @package  Core
+ */
+class Horde_Core_Share_Driver implements Serializable
+{
+    /** Serializable version **/
+    const VERSION = 1;
+
+    /**
+     * The composed Horde_Share driver
+     *
+     * @var Horde_Share
+     */
+    protected $_share;
+
+    /**
+     * Maps the concrete share class to the required storage adapter.
+     *
+     * @var array
+     */
+    protected $_storageMap = array(
+        'Horde_Share_Sql' => 'Horde_Db_Adapter',
+        'Horde_Share_Sql_Hierarchical' => 'Horde_Db_Adapter',
+        'Horde_Share_Kolab' => 'Horde_Kolab_Storage');
+
+    /**
+     */
+    public function __construct(Horde_Share $share)
+    {
+        $this->_share = $share;
+        $this->_share->setStorage($GLOBALS['injector']->getInstance($this->_storageMap[get_class($this->_share)]));
+        $this->_share->addCallback('add', array($this, 'shareAddCallback'));
+        $this->_share->addCallback('modify', array($this, 'shareModifyCallback'));
+        $this->_share->addCallback('remove', array($this, 'shareRemoveCallback'));
+        $this->_share->addCallback('list', array($this, 'shareListCallback'));
+
+        try {
+            Horde::callHook('share_init', array($this, $this->_share->getApp()));
+        } catch (Horde_Exception_HookNotSet $e) {}
+    }
+
+    /**
+     * Delegate method calls to the composed share object.
+     *
+     * @param string $method  The method name
+     * @param array $args     The method arguments
+     *
+     * @return mixed  The result of the method call
+     */
+    public function __call($method, $args)
+    {
+        return call_user_func_array(array($this->_share, $method), $args);
+    }
+
+    /**
+     * Serializes the object.
+     *
+     * @return string  The serialized object.
+     */
+    public function serialize()
+    {
+        $data = array(
+            self::VERSION,
+            $this->_share);
+
+        return serialize($data);
+    }
+
+    /**
+     * Reconstructs object from serialized properties.
+     *
+     * @param  $serialized
+     */
+    public function unserialize($data)
+    {
+        // Rebuild the object
+        $data = @unserialize($data);
+        if (!is_array($data) ||
+            !isset($data[0]) ||
+            ($data[0] != self::VERSION)) {
+            throw new Exception('Cache version change');
+        }
+        $this->_share = $data[1];
+
+        // Set the storage adapter.
+        $this->_share->setStorage($GLOBALS['injector']->getInstance($this->_storageMap[get_class($this->_share)]));
+
+        // Call the init hook
+         try {
+            Horde::callHook('share_init', array($this, $this->_share->getApp()));
+        } catch (Horde_Exception_HookNotSet $e) {}
+    }
+
+    /**
+     * Lock an item belonging to a share, or an entire share itself.
+     *
+     * @param Horde_Lock $locks          The lock object
+     * @param Horde_Share_Object $share  The share object
+     * @param string $uid                The uid of a specific object to lock,
+     *                                   if null, entire share is locked.
+     *
+     * @return mixed  A lock ID on sucess, false if:
+     *                  - The share is already locked,
+     *                  - The item is already locked,
+     *                  - A share lock was requested and an item is already
+     *                    locked in the share.
+     */
+    public function lock(Horde_Lock $locks, $uid = null)
+    {
+        $shareid = $this->_share->getId();
+
+        // Default parameters.
+        $locktype = Horde_Lock::TYPE_EXCLUSIVE;
+        $timeout = 600;
+        $itemscope = $this->_share->getShareOb()->getApp() . ':' . $shareid;
+
+        if (!empty($uid)) {
+            // Check if the share is locked. Share locks are placed at app scope
+            try {
+                $result = $locks->getLocks($this->_share->getShareOb()->getApp(), $shareid, $locktype);
+            } catch (Horde_Lock_Exception $e) {
+                throw new Horde_Exception_Prior($e);
+            }
+            if (!empty($result)) {
+                // Lock found.
+                return false;
+            }
+
+            // Try to place the item lock at app:shareid scope.
+            return $locks->setLock($GLOBALS['registry']->getAuth(),
+                                   $itemscope,
+                                   $uid,
+                                   $timeout,
+                                   $locktype);
+        } else {
+            // Share lock requested. Check for locked items.
+            try {
+                $result = $locks->getLocks($itemscope, null, $locktype);
+            } catch (Horde_Lock_Exception $e) {
+                throw new Horde_Exception_Prior($e);
+            }
+            if (!empty($result)) {
+                // Lock found.
+                return false;
+            }
+
+            // Try to place the share lock
+            return $locks->setLock($GLOBALS['registry']->getAuth(),
+                                   $this->_share->getShareOb()->getApp(),
+                                   $shareid,
+                                   $timeout,
+                                   $locktype);
+        }
+    }
+
+    /**
+     * Removes the lock for a lock ID.
+     *
+     * @param Horde_Lock $locks  The lock object
+     * @param string $lockid     The lock ID as generated by a previous call
+     *                           to lock().
+     *
+     * @return boolean
+     */
+    public function unlock(Horde_Lock $locks, $lockid)
+    {
+        return $locks->clearLock($lockid);
+    }
+
+    /**
+     * Checks for existing locks.
+     *
+     * First this checks for share locks and if none exists, checks for item
+     * locks (if item_uid defined).  It will return the first lock found.
+     *
+     * @param Horde_Lock  $locks  The lock object.
+     * @param string $item_uid    A uid of an item from this share.
+     *
+     * @return array   Hash with the found lock information in 'lock' and the
+     *                 lock type ('share' or 'item') in 'type', or an empty
+     *                 array if there are no locks.
+     */
+    public function checkLocks(Horde_Lock $locks, $item_uid = null)
+    {
+        $shareid = $this->_share->getId();
+        $locktype = Horde_Lock::TYPE_EXCLUSIVE;
+
+        // Check for share locks
+        try {
+            $result = $locks->getLocks($this->_share->getShareOb()->getApp(), $shareid, $locktype);
+        } catch (Horde_Lock_Exception $e) {
+            Horde::logMessage($e, 'ERR');
+            throw new Horde_Exception_Prior($e);
+        }
+
+        if (empty($result) && !empty($item_uid)) {
+            // Check for item locks
+            $locktargettype = 'item';
+            try {
+                $result = $locks->getLocks($this->_share->getShareOb()->getApp() . ':' . $shareid, $item_uid, $locktype);
+            } catch (Horde_Lock_Exception $e) {
+                Horde::logMessage($e, 'ERR');
+                throw new Horde_Exception($e->getMessage());
+            }
+        } else {
+            $locktargettype = 'share';
+        }
+
+        if (empty($result)) {
+            return array();
+        }
+
+        return array('type' => $locktargettype,
+                     'lock' => reset($result));
+    }
+
+    /**
+     * share_list callback
+     *
+     * @param string $userid  The userid listShares was called with
+     * @param array  $shares  The result of the listShares() call
+     * @param array  $params  The params that listShares() was called with
+     *
+     * @return array  An array of share objects
+     */
+    public function shareListCallback($userid, $shares, $params = array())
+    {
+        try {
+            $params = new Horde_Support_Array($params);
+            return Horde::callHook('share_list', array($userid, $params['perm'], $params['attributes'], $shares));
+        } catch (Horde_Exception_HookNotSet $e) {}
+
+        return $shares;
+    }
+
+    /**
+     * Adds the share_add hook before delegating to the share object.
+     *
+     * @param Horde_Share_Object  The share object being added
+     */
+    public function shareAddCallback(Horde_Share_Object $share)
+    {
+        try {
+            Horde::callHook('share_add', array($share));
+        } catch (Horde_Exception_HookNotSet $e) {}
+    }
+
+    /**
+     * Calls the share_remove hook before delegating to the share object.
+     *
+     * @see Horde_Share::removeShare
+     */
+    public function shareRemoveCallback(Horde_Share_Object $share)
+    {
+        try {
+            Horde::callHook('share_remove', array($share));
+        } catch (Horde_Exception_HookNotSet $e) {}
+    }
+
+    public function shareModifyCallback(Horde_Share_Object $share)
+    {
+        try {
+            Horde::callHook('share_modify', array($this));
+        } catch (Horde_Exception_HookNotSet $e) {}
+    }
+
+}
\ No newline at end of file
diff --git a/framework/Core/package.xml b/framework/Core/package.xml
index ec6865fc1..176aac2ba 100644
--- a/framework/Core/package.xml
+++ b/framework/Core/package.xml
@@ -23,8 +23,8 @@ Application Framework.
   slusarz@horde.org
   yes
  
- 2010-10-25
- 
+ 2010-11-04
+ 
  
   0.1.0
   0.1.0
@@ -203,6 +203,9 @@ Application Framework.
        
        
        
+      
+       
+       
       
        
         
@@ -522,7 +525,7 @@ Application Framework.
       
       
       
-      
+      
       
      
     
@@ -792,6 +795,7 @@ Application Framework.
    
    
    
+   
    
    
    
@@ -895,7 +899,7 @@ Application Framework.
    
    
    
-   
+   
    
    
   
@@ -941,7 +945,7 @@ Initial packaging
     beta
     beta
    
-   2010-10-25
+   2010-11-04
    LGPL
    
 * Add Horde_Session.
diff --git a/framework/Group/lib/Horde/Group/Contactlists.php b/framework/Group/lib/Horde/Group/Contactlists.php
index b1a4fdbdc..9e82c2a89 100644
--- a/framework/Group/lib/Horde/Group/Contactlists.php
+++ b/framework/Group/lib/Horde/Group/Contactlists.php
@@ -340,7 +340,8 @@ class Horde_Group_Contactlists extends Horde_Group
                 if (empty($contact_shares)) {
                     $scope = $GLOBALS['registry']->hasInterface('contacts');
                     $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope);
-                    $this->_contact_shares = $shares->listShares($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW, $GLOBALS['registry']->getAuth());
+                    $this->_contact_shares = $shares->listShares($GLOBALS['registry']->getAuth(), 
+                                                                 array('perm' => Horde_Perms::SHOW, 'attributes' => $GLOBALS['registry']->getAuth()));
                 }
                 // Contruct a list of owner ids to use
                 foreach ($this->_contact_shares as $id => $share) {
diff --git a/framework/Net_IMSP/IMSP/Utils.php b/framework/Net_IMSP/IMSP/Utils.php
index 93e996643..5d0e2f086 100644
--- a/framework/Net_IMSP/IMSP/Utils.php
+++ b/framework/Net_IMSP/IMSP/Utils.php
@@ -142,7 +142,7 @@ class Net_IMSP_Utils {
      *
      * @return mixed  Array describing any shares added or removed  | PEAR_Error.
      */
-    function synchShares(&$share_obj, $serverInfo)
+    function synchShares($share_obj, $serverInfo)
     {
         $found_shares = array();
         $return = array('added' => array(), 'removed' => array());
@@ -167,7 +167,7 @@ class Net_IMSP_Utils {
             }
         }
 
-        $shares = &$share_obj->listShares($GLOBALS['registry']->getAuth());
+        $shares = $share_obj->listShares($GLOBALS['registry']->getAuth());
         // A share for each IMSP adress book we can see.
         foreach ($abooks as $abook_uid) {
             $found = false;
@@ -215,7 +215,7 @@ class Net_IMSP_Utils {
         }
 
         // Now prune any shares that no longer exist on the IMSP server.
-        $existing = $share_obj->listShares($GLOBALS['registry']->getAuth(), Horde_Perms::READ);
+        $existing = $share_obj->listShares($GLOBALS['registry']->getAuth(), array('perm' => Horde_Perms::READ));
         foreach ($existing as $key => $share) {
             $temp = unserialize($share->get('params'));
             if (is_array($temp)) {
@@ -243,9 +243,9 @@ class Net_IMSP_Utils {
      *
      * @return mixed  True | PEAR_Error
      */
-    function _createShare(&$share_obj, $params, $shareparams)
+    function _createShare($share_obj, $params, $shareparams)
     {
-        $share = &$share_obj->newShare($params['uid']);
+        $share = $share_obj->newShare($GLOBALS['registry']->getAuth(), $params['uid']);
         if (is_a($share, 'PEAR_Error')) {
             return $share;
         }
diff --git a/framework/Share/lib/Horde/Share.php b/framework/Share/lib/Horde/Share.php
index f2678e275..cd61cdaaf 100644
--- a/framework/Share/lib/Horde/Share.php
+++ b/framework/Share/lib/Horde/Share.php
@@ -15,6 +15,7 @@
  * @author  Chuck Hagenbuch 
  * @author  Jan Schneider 
  * @author  Gunnar Wrobel 
+ * @author  Michael J. Rubinsky 
  * @package Horde_Share
  */
 class Horde_Share
@@ -78,42 +79,66 @@ class Horde_Share
     protected $_permsObject;
 
     /**
-     * Constructor.
+     * The current user
      *
-     * @param string $app              The application that the shares belong
-     *                                 to.
-     * @param Horde_Perms $perms       The permissions object
+     * @var string
      */
-    public function __construct($app, Horde_Perms $perms)
-    {
-        $this->_app = $app;
-        $this->_permsObject = $perms;
-        $this->__wakeup();
-    }
+    protected $_user;
 
     /**
-     * Initializes the object.
+     * The Horde_Group driver
      *
-     * @throws Horde_Exception
+     * @var Horde_Group
      */
-    public function __wakeup()
+    protected $_groups;
+
+    /**
+     * 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) { - try { - Horde::callHook('share_init', array($this, $this->_app)); - } catch (Horde_Exception_HookNotSet $e) {} + $this->_app = $app; + $this->_user = $user; + $this->_permsObject = $perms; + $this->_groups = $groups; } /** - * Returns the properties that need to be serialized. - * - * @return array List of serializable properties. + * (re)connect the share object to this share driver. Userful for when + * share objects are unserialized from a cache separate from the share + * driver. + * + * @param Horde_Share_Object $object */ - public function __sleep() + public function initShareObject($object) { - $properties = get_object_vars($this); - unset($properties['_sortList']); - $properties = array_keys($properties); - return $properties; + // noop } /** @@ -223,20 +248,30 @@ class 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 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. + * @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.
+     *  'attribtues'  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, $perm = Horde_Perms::SHOW, $attributes = null, - $from = 0, $count = 0, $sort_by = null, $direction = 0) + public function listShares($userid, $params = array()) { - $shares = $this->_listShares($userid, $perm, $attributes, $from, - $count, $sort_by, $direction); + $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; } @@ -248,9 +283,10 @@ class Horde_Share $this->_sortList = null; } - try { - return Horde::callHook('share_list', array($userid, $perm, $attributes, $shares)); - } catch (Horde_Exception_HookNotSet $e) {} + // Run the results through the callback, if configured. + if (!empty($this->_callbacks['list'])) { + return call_user_func_array($this->_callbacks['list'], array($userid, $shares, $params)); + } return $shares; } @@ -285,19 +321,20 @@ class Horde_Share /** * 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($name) + public function newShare($owner, $name) { if (empty($name)) { throw new Horde_Share_Exception('Share names must be non-empty'); } $share = $this->_newShare($name); - $share->setShareOb($this); - $share->set('owner', $GLOBALS['registry']->getAuth()); + $this->initShareObject($share); + $share->set('owner', $owner); return $share; } @@ -315,9 +352,10 @@ class Horde_Share */ public function addShare(Horde_Share_Object $share) { - try { - Horde::callHook('share_add', array($share)); - } catch (Horde_Exception_HookNotSet $e) {} + // Run the results through the callback, if configured. + if (!empty($this->_callbacks['add'])) { + call_user_func($this->_callbacks['add'], $share); + } $result = $this->_addShare($share); @@ -342,9 +380,10 @@ class Horde_Share */ public function removeShare(Horde_Share_Object $share) { - try { - Horde::callHook('share_remove', array($share)); - } catch (Horde_Exception_HookNotSet $e) {} + // Run the results through the callback, if configured. + if (!empty($this->_callbacks['remove'])) { + return call_user_func($this->_callbacks['remove'], $share); + } /* Remove share from the caches. */ $id = $share->getId(); @@ -426,7 +465,34 @@ class Horde_Share { // noop } - + + /** + * Add a callback to the collection + * + * @param string $type + * @param array $callback + */ + public function addCallback($type, $callback) + { + $this->_callbacks[$type] = $callback; + } + + /** + * 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); + } + } + /** * Utility function to be used with uasort() for sorting arrays of * Horde_Share objects. diff --git a/framework/Share/lib/Horde/Share/Datatree.php b/framework/Share/lib/Horde/Share/Datatree.php index eedb13efa..c94949904 100644 --- a/framework/Share/lib/Horde/Share/Datatree.php +++ b/framework/Share/lib/Horde/Share/Datatree.php @@ -229,7 +229,7 @@ class Horde_Share_Datatree extends Horde_Share * * @param Horde_Share_Object_datatree $share The new share object. */ - protected function _addShare(&$share) + protected function _addShare($share) { return $this->_datatree->add($share->datatreeObject); } @@ -239,7 +239,7 @@ class Horde_Share_Datatree extends Horde_Share * * @param Horde_Share_Object_datatree $share The share to remove. */ - protected function _removeShare(&$share) + protected function _removeShare($share) { return $this->_datatree->remove($share->datatreeObject); } diff --git a/framework/Share/lib/Horde/Share/Kolab.php b/framework/Share/lib/Horde/Share/Kolab.php index 6ad07bd8c..c762f784f 100644 --- a/framework/Share/lib/Horde/Share/Kolab.php +++ b/framework/Share/lib/Horde/Share/Kolab.php @@ -17,6 +17,8 @@ */ class Horde_Share_kolab extends Horde_Share { + const VERSION = 1; + /** * Our Kolab folder list handler * @@ -45,26 +47,6 @@ class Horde_Share_kolab extends Horde_Share */ protected $_session; - /** - * Initializes the object. - * - * @throws Horde_Exception - */ - public function __wakeup() - { - if (empty($GLOBALS['conf']['kolab']['enabled'])) { - throw new Horde_Exception('You must enable the kolab settings to use the Kolab Share driver.'); - } - - $this->_type = $this->_getFolderType($this->_app); - if ($this->_type instanceof PEAR_Error) { - return $this->_type; - } - - $this->_list = $GLOBALS['injector']->getInstance('Horde_Kolab_Storage'); - - parent::__wakeup(); - } private function _getFolderType($app) { @@ -82,17 +64,113 @@ class Horde_Share_kolab extends Horde_Share } } +// /** +// * Returns the properties that need to be serialized. +// * +// * @return array List of serializable properties. +// */ +// public function __sleep() +// { +// $properties = get_object_vars($this); +// unset($properties['_sortList'], $properties['_list']); +// $properties = array_keys($properties); +// return $properties; +// } +// /** +// * Initializes the object. +// * +// * @throws Horde_Exception +// */ +// public function __wakeup() +// { +// if (empty($GLOBALS['conf']['kolab']['enabled'])) { +// throw new Horde_Exception('You must enable the kolab settings to use the Kolab Share driver.'); +// } +// $this->_type = $this->_getFolderType($this->_app); +// $this->_list = $GLOBALS['injector']->getInstance('Horde_Kolab_Storage'); +// +// parent::__wakeup(); +// } + + /** + * Serialize the object. You *MUST* call setStorage() after unserialized. + * + * @return string + */ + public function serialize() + { + $data = array( + self::VERSION, + $this->_app, + $this->_root, + $this->_cache, + $this->_shareMap, + $this->_listcache, + $this->_shareObject, + $this->_permsObject, + $this->_type, + $this->_listCacheValidity, + $this->_session); + + return serialize($data); + } + + /** + * Reconstruct object from serialized data. You MUST call setStorage() + * after unserialize. + * + * @param $data + */ + public function unserialize($data) + { + // Rebuild the object + $data = @unserialize($data); + if (!is_array($data) || + !isset($data[0]) || + ($data[0] != self::VERSION)) { + throw new Exception('Cache version change'); + } + $this->_app = $data[1]; + $this->_root = $data[2]; + $this->_cache = $data[3]; + $this->_shareMap = $data[4]; + $this->_listcache = $data[5]; + $this->_shareObject = $data[6]; + $this->_permsObject = $data [7]; + $this->_type = $data[8]; + $this->_listCacheValidity = $data[9]; + $this->_session = $data[10]; + + foreach (array_keys($this->_cache) as $name) { + $this->_initShareObject($this->_cache[$name]); + } + } + + /** + * (re)connect the share object to this share driver. Userful for when + * share objects are unserialized from a cache separate from the share + * driver. + * + * @param Horde_Share_Object $object + */ + public function initShareObject($object) + { + $object->setShareOb($this); + } + /** - * Returns the properties that need to be serialized. + * Set the kolab storage backend. * - * @return array List of serializable properties. + * @param Horde_Kolab_Storage $driver */ - public function __sleep() + public function setStorage(Horde_Kolab_Storage $driver) + { + $this->_list = $driver; + } + + public function getStorage() { - $properties = get_object_vars($this); - unset($properties['_sortList'], $properties['_list']); - $properties = array_keys($properties); - return $properties; + return $this->_list; } /** @@ -175,19 +253,13 @@ 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 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. + * @param array $params @see Horde_Share::listShares * * @return array The shares the user has access to. */ - protected function _listShares($userid, $perm = Horde_Perms::SHOW, - $attributes = null, $from = 0, $count = 0, - $sort_by = null, $direction = 0) + protected function _listShares($userid, $params = array()) { - $key = serialize(array($this->_type, $userid, $perm, $attributes)); + $key = serialize(array($this->_type, $userid, $params['perm'], $params['attributes'])); if ($this->_list === false) { $this->_listCache[$key] = array(); } else if (empty($this->_listCache[$key]) @@ -200,20 +272,20 @@ class Horde_Share_kolab extends Horde_Share $shares = array(); foreach ($sharelist as $folder) { $id = $folder->getShareId(); - $share = &$this->getShare($id); + $share = $this->getShare($id); $keep = true; - if (!$share->hasPermission($userid, $perm)) { + if (!$share->hasPermission($userid, $params['perm'])) { $keep = false; } - if (isset($attributes) && $keep) { - if (is_array($attributes)) { - foreach ($attributes as $key => $value) { + if (isset($params['attributes']) && $keep) { + if (is_array($params['attributes'])) { + foreach ($params['attributes'] as $key => $value) { if (!$share->get($key) == $value) { $keep = false; break; } } - } elseif (!$share->get('owner') == $attributes) { + } elseif (!$share->get('owner') == $params['attributes']) { $keep = false; } } @@ -243,7 +315,7 @@ class Horde_Share_kolab extends Horde_Share protected function _countShares($userid, $perm = Horde_Perms::SHOW, $attributes = null) { - $shares = $this->_listShares($userid, $perm, $attributes); + $shares = $this->_listShares($userid, array('perm' => $perm, 'attributes' => $attributes)); return count($shares); } @@ -323,7 +395,7 @@ class Horde_Share_kolab extends Horde_Share } /** Okay, no default folder yet */ - $share = $this->newShare($GLOBALS['registry']->getAuth()); + $share = $this->newShare($GLOBALS['registry']->getAuth(), $GLOBALS['registry']->getAuth()); /** The value does not matter here as the share will rewrite it */ $share->set('name', ''); diff --git a/framework/Share/lib/Horde/Share/Object.php b/framework/Share/lib/Horde/Share/Object.php index 820d78411..64d691bb9 100644 --- a/framework/Share/lib/Horde/Share/Object.php +++ b/framework/Share/lib/Horde/Share/Object.php @@ -12,34 +12,30 @@ class Horde_Share_Object { /** - * The Horde_Share object which this share came from - needed for updating - * data in the backend to make changes stick, etc. + * The Horde_Share object which this share is associated with. * * @var Horde_Share */ protected $_shareOb; /** - * Returns the properties that need to be serialized. + * Associates a Share object with this share. * - * @return array List of serializable properties. + * @param Horde_Share $shareOb The Share object. */ - public function __sleep() + public function setShareOb(Horde_Share $shareOb) { - $properties = get_object_vars($this); - unset($properties['_shareOb']); - $properties = array_keys($properties); - return $properties; + $this->_shareOb = $shareOb; } /** - * Associates a Share object with this share. + * Sets any additional storage driver this object may need. * - * @param Horde_Share $shareOb The Share object. + * @param mixed $driver The storage driver. */ - public function setShareOb(Horde_Share $shareOb) + public function setStorage($driver) { - $this->_shareOb = $shareOb; + // Noop } /** @@ -58,8 +54,7 @@ class Horde_Share_Object * @param string $attribute The attribute to set. * @param mixed $value The value for $attribute. * - * @return mixed True if setting the attribute did succeed, a PEAR_Error - * otherwise. + * @return boolean */ public function set($attribute, $value) { @@ -106,10 +101,7 @@ class Horde_Share_Object */ public function save() { - try { - Horde::callHook('share_modify', array($this)); - } catch (Horde_Exception_HookNotSet $e) {} - + $this->_shareOb->runCallback('modify', array($this)); return $this->_save(); } @@ -235,118 +227,13 @@ class Horde_Share_Object } /** - * Locks an item from this share, or the entire share if no item defined. - * - * @param Horde_Lock $locks The Horde_Lock object - * @param string $item_uid A uid of an item from this share. - * - * @return mixed A lock ID on success, PEAR_Error on failure, false if: - * - The share is already locked - * - The item is already locked - * - A share lock was requested and an item is already - * locked in the share - * @throws Horde_Share_Exception - */ - public function lock(Horde_Lock $locks, $item_uid = null) - { - $shareid = $this->getId(); - - // Default parameters. - $locktype = Horde_Lock::TYPE_EXCLUSIVE; - $timeout = 600; - $itemscope = $this->_shareOb->getApp() . ':' . $shareid; - - if (!empty($item_uid)) { - // Check if the share is locked. Share locks are placed at app - // scope. - try { - $result = $locks->getLocks($this->_shareOb->getApp(), $shareid, $locktype); - } catch (Horde_Lock_Exception $e) { - throw new Horde_Share_Exception($e); - } - if (!empty($result)) { - // Lock found. - return false; - } - // Try to place the item lock at app:shareid scope. - return $locks->setLock($GLOBALS['registry']->getAuth(), $itemscope, $item_uid, - $timeout, $locktype); - } else { - // Share lock requested. Check for locked items. - try { - $result = $locks->getLocks($itemscope, null, $locktype); - } catch (Horde_Lock_Exception $e) { - throw new Horde_Share_Exception($e); - } - if (!empty($result)) { - // Lock found. - return false; - } - // Try to place the share lock - return $locks->setLock($GLOBALS['registry']->getAuth(), $this->_shareOb->getApp(), - $shareid, $timeout, $locktype); - } - } - - /** - * Removes the lock for a lock ID. - * - * @param Horde_Lock $locks The lock object - * @param string $lockid The lock ID as generated by a previous call - * to lock(). - * - * @return mixed True on success, PEAR_Error on failure. - */ - public function unlock(Horde_Lock $locks, $lockid) - { - return $locks->clearLock($lockid); - } - - /** - * Checks for existing locks. + * Returns the permission of this share. * - * First this checks for share locks and if none exists, checks for item - * locks (if item_uid defined). It will return the first lock found. - * - * @param Horde_Lock $locks The lock object. - * @param string $item_uid A uid of an item from this share. - * - * @return mixed Hash with the found lock information in 'lock' and the - * lock type ('share' or 'item') in 'type', or an empty - * array if there are no locks, or a PEAR_Error on failure. + * @return Horde_Perms_Permission Permission object that represents the + * permissions on this share. */ - public function checkLocks(Horde_Lock $locks, $item_uid = null) + public function getPermission() { - $shareid = $this->getId(); - $locktype = Horde_Lock::TYPE_EXCLUSIVE; - - // Check for share locks - try { - $result = $locks->getLocks($this->_shareOb->getApp(), $shareid, $locktype); - } catch (Horde_Lock_Exception $e) { - Horde::logMessage($e, 'ERR'); - throw new Horde_Share_Exception($e); - } - - if (empty($result) && !empty($item_uid)) { - // Check for item locks - $locktargettype = 'item'; - try { - $result = $locks->getLocks($this->_shareOb->getApp() . ':' . $shareid, $item_uid, $locktype); - } catch (Horde_Lock_Exception $e) { - Horde::logMessage($e, 'ERR'); - throw new Horde_Share_Exception($e->getMessage()); - } - } else { - $locktargettype = 'share'; - } - - if (empty($result)) { - return array(); - } - - return array('type' => $locktargettype, - 'lock' => reset($result)); } } diff --git a/framework/Share/lib/Horde/Share/Object/Kolab.php b/framework/Share/lib/Horde/Share/Object/Kolab.php index c20867f14..9f8324992 100644 --- a/framework/Share/lib/Horde/Share/Object/Kolab.php +++ b/framework/Share/lib/Horde/Share/Object/Kolab.php @@ -7,8 +7,11 @@ * @author Gunnar Wrobel * @package Horde_Share */ -class Horde_Share_Object_Kolab extends Horde_Share_Object +class Horde_Share_Object_Kolab extends Horde_Share_Object implements Serializable { + /** Serializable version **/ + const VERSION = 1; + /** * The Kolab folder this share is based on. * @@ -38,13 +41,6 @@ class Horde_Share_Object_Kolab extends Horde_Share_Object protected $_list; /** - * The session handler. - * - * @var Horde_Kolab_Session - */ - private $_session; - - /** * Constructor. * * Sets the folder name. @@ -72,32 +68,46 @@ class Horde_Share_Object_Kolab extends Horde_Share_Object */ public function setShareOb($shareOb) { - /** Ignore the parent as we don't need it */ + $this->_list = $shareOb->getStorage(); + if (isset($this->_folder_name)) { + $this->_folder = $this->_list->getFolder($this->_folder_name); + } + } + + public function serialize() + { + $data = array( + self::VERSION, + $this->_data, + $this->_folder_name + ); } /** - * Initializes the object. + * Unserialize object. You MUST call setShareOb() after unserializtion. + * @param $data */ - public function __wakeup() + public function unserialize($data) { - $this->_list = $GLOBALS['injector']->getInstance('Horde_Kolab_Storage'); - if (isset($this->_folder_name)) { - $this->_folder = $this->_list->getFolder($this->_folder_name); + $data = @unserialize($data); + if (!is_array($data) || + !isset($data[0]) || + ($data[0] != self::VERSION)) { + throw new Exception('Cache version change'); } + + $this->_data = $data[1]; + $this->_folder_name = $data[2]; } /** - * Returns the properties that need to be serialized. + * Sets the kolab storage object. * - * @return array List of serializable properties. + * @param Horde_Kolab_Storage $driver */ - public function __sleep() + public function setStorage($driver) { - $properties = get_object_vars($this); - unset($properties['_shareOb'], $properties['_list'], - $properties['_folder']); - $properties = array_keys($properties); - return $properties; + $this->_list = $driver; } /** diff --git a/framework/Share/lib/Horde/Share/Object/Sql.php b/framework/Share/lib/Horde/Share/Object/Sql.php index 5b648ad8d..1e5bb2311 100644 --- a/framework/Share/lib/Horde/Share/Object/Sql.php +++ b/framework/Share/lib/Horde/Share/Object/Sql.php @@ -4,15 +4,19 @@ * the sql driver. * * @author Duck + * @author Michael J. Rubinsky * @package Horde_Share */ class Horde_Share_Object_Sql extends Horde_Share_Object { + /** Serializable version **/ + const VERSION = 1; + /** * The actual storage object that holds the data. * * @TODO: Check visibility - should be protected/private - * @var mixed + * @var array */ public $data = array(); @@ -42,13 +46,46 @@ class Horde_Share_Object_Sql extends Horde_Share_Object } /** + * Serialize this object. You *MUST* call setShareOb() after unserializing. + * + * @return string The serialized data. + */ + public function serialize() + { + $data = array( + self::VERSION, + $data + ); + + return serialize($data); + } + + /** + * Reconstruct the object from serialized data. + * + * You *MUST* call setShareOb() after unserializing. + * + * @param string $data The serialized data. + */ + public function unserialize($data) + { + $data = @unserialize($data); + if (!is_array($data) || + !isset($data[0]) || + ($data[0] != self::VERSION)) { + throw new Exception('Cache version change'); + } + + $this->data = $data[1]; + } + + /** * Sets an attribute value in this object. * * @param string $attribute The attribute to set. * @param mixed $value The value for $attribute. * - * @return mixed True if setting the attribute did succeed, a PEAR_Error - * otherwise. + * @return boolean */ public function _set($attribute, $value) { @@ -101,12 +138,13 @@ class Horde_Share_Object_Sql extends Horde_Share_Object */ protected function _save() { - $db = $this->_shareOb->getWriteDb(); + $db = $this->_shareOb->getStorage(); $table = $this->_shareOb->getTable(); $fields = array(); $params = array(); + // Build the parameter arrays for the sql statement. foreach ($this->_shareOb->toDriverCharset($this->data) as $key => $value) { if ($key != 'share_id' && $key != 'perm' && $key != 'share_flags') { $fields[] = $key; @@ -133,91 +171,39 @@ class Horde_Share_Object_Sql extends Horde_Share_Object } $params[] = $flags; + // Insert new share record, or update existing if (empty($this->data['share_id'])) { - $share_id = $db->nextId($table); - if ($share_id instanceof PEAR_Error) { - Horde::logMessage($share_id, 'ERR'); - throw new Horde_Share_Exception($share_id->getMessage()); + $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (?' . str_repeat(', ?', count($fields) - 1) . ')'; + try { + $this->data['share_id'] = $db->insert($sql, $params); + } catch (Horde_Db_Exception $e) { + throw new Horde_Share_Exception($e); } - - $this->data['share_id'] = $share_id; - $fields[] = 'share_id'; - $params[] = $this->data['share_id']; - - $query = 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (?' . str_repeat(', ?', count($fields) - 1) . ')'; } else { - $query = 'UPDATE ' . $table . ' SET ' . implode(' = ?, ', $fields) . ' = ? WHERE share_id = ?'; + $sql = 'UPDATE ' . $table . ' SET ' . implode(' = ?, ', $fields) . ' = ? WHERE share_id = ?'; $params[] = $this->data['share_id']; + try { + $db->update($sql, $params); + } catch (Horde_Db_Exception $e) { + throw new Horde_Share_Exception($e); + } } - $stmt = $db->prepare($query, null, MDB2_PREPARE_MANIP); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute($params); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } - $stmt->free(); // Update the share's user permissions - $stmt = $db->prepare('DELETE FROM ' . $table . '_users WHERE share_id = ?', null, MDB2_PREPARE_MANIP); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($this->data['share_id'])); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } - $stmt->free(); - + $db->delete('DELETE FROM ' . $table . '_users WHERE share_id = ?', array($this->data['share_id'])); if (!empty($this->data['perm']['users'])) { $data = array(); foreach ($this->data['perm']['users'] as $user => $perm) { - $stmt = $db->prepare('INSERT INTO ' . $table . '_users (share_id, user_uid, perm) VALUES (?, ?, ?)', null, MDB2_PREPARE_MANIP); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($this->data['share_id'], $user, $perm)); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } - $stmt->free(); + $db->insert('INSERT INTO ' . $table . '_users (share_id, user_uid, perm) VALUES (?, ?, ?)', array($this->data['share_id'], $user, $perm)); } } // Update the share's group permissions - $stmt = $db->prepare('DELETE FROM ' . $table . '_groups WHERE share_id = ?', null, MDB2_PREPARE_MANIP); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($this->data['share_id'])); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } - $stmt->free(); - + $db->delete('DELETE FROM ' . $table . '_groups WHERE share_id = ?', array($this->data['share_id'])); if (!empty($this->data['perm']['groups'])) { $data = array(); foreach ($this->data['perm']['groups'] as $group => $perm) { - $stmt = $db->prepare('INSERT INTO ' . $table . '_groups (share_id, group_uid, perm) VALUES (?, ?, ?)', null, MDB2_PREPARE_MANIP); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($this->data['share_id'], $group, $perm)); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } - $stmt->free(); + $db->insert('INSERT INTO ' . $table . '_groups (share_id, group_uid, perm) VALUES (?, ?, ?)', array($this->data['share_id'], $group, $perm)); } } @@ -248,11 +234,7 @@ class Horde_Share_Object_Sql extends Horde_Share_Object * @param boolean $update Should the share be saved * after this operation? * - * @TODO: Look at storing the Perm object itself, instead of the data - * (Make it easier to inject the perm object instead of instantiating - * it in the library). - * - * @return boolean True if no error occured, PEAR_Error otherwise + * @return boolean */ public function setPermission($perm, $update = true) { diff --git a/framework/Share/lib/Horde/Share/Object/Sql/Hierarchical.php b/framework/Share/lib/Horde/Share/Object/Sql/Hierarchical.php index 1e8e4b14a..3b88e3d43 100644 --- a/framework/Share/lib/Horde/Share/Object/Sql/Hierarchical.php +++ b/framework/Share/lib/Horde/Share/Object/Sql/Hierarchical.php @@ -11,10 +11,10 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql { /** - * Constructor. This is here primarily to make calling the parent - * constructor(s) from any subclasses cleaner. + * Constructor. + * + * @param array $data * - * @param unknown_type $data * @return Horde_Share_Object_sql_hierarchical */ public function __construct($data) @@ -25,44 +25,44 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql parent::__construct($data); } - public function inheritPermissions() - { - throw new Horde_Share_Exception('Not implemented.'); - } - /** * Return a count of the number of children this share has * - * @param integer $perm A Horde_Perms::* constant + * @param string $user The user to use for checking perms + * @param integer $perm A Horde_Perms::* constant * @param boolean $allLevels Count grandchildren or just children * - * @return mixed The number of child shares || PEAR_Error + * @return integer The number of child shares */ - public function countChildren($perm = Horde_Perms::SHOW, $allLevels = true) + public function countChildren($user, $perm = Horde_Perms::SHOW, $allLevels = true) { - return $this->_shareOb->countShares($GLOBALS['registry']->getAuth(), $perm, null, $this, $allLevels); + return $this->_shareOb->countShares($user, $perm, null, $this, $allLevels); } /** * Get all children of this share. * - * @param int $perm Horde_Perms::* constant. If NULL will return + * @param string $user The user to use for checking perms + * @param integer $perm Horde_Perms::* constant. If NULL will return * all shares regardless of permissions. * @param boolean $allLevels Return all levels. * - * @return mixed An array of Horde_Share_Object objects || PEAR_Error + * @return array An array of Horde_Share_Object objects */ - public function getChildren($perm = Horde_Perms::SHOW, $allLevels = true) + public function getChildren($user, $perm = Horde_Perms::SHOW, $allLevels = true) { - return $this->_shareOb->listShares($GLOBALS['registry']->getAuth(), $perm, null, 0, 0, - null, 1, $this, $allLevels, is_null($perm)); - + return $this->_shareOb->listShares( + $user, array('perm' => $perm, + 'direction' => 1, + 'parent' => $this, + 'all_levels' => $allLevels, + 'ignore_perms' => is_null($perm))); } /** * Returns a child's direct parent * - * @return mixed The direct parent Horde_Share_Object or PEAR_Error + * @return Horde_Share_Object The direct parent Horde_Share_Object */ public function getParent() { @@ -78,10 +78,11 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql { $parents = array(); $share = $this->getParent(); - while (is_a($share, 'Horde_Share_Object')) { + while ($share instanceof Horde_Share_Object) { $parents[] = $share; $share = $share->getParent(); } + return array_reverse($parents); } @@ -90,23 +91,21 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql * * @param mixed $parent A Horde_Share object or share id for the parent. * - * @return mixed true || PEAR_Error + * @return boolean */ public function setParent($parent) { if (!is_null($parent) && !is_a($parent, 'Horde_Share_Object')) { $parent = $this->_shareOb->getShareById($parent); - if ($parent instanceof PEAR_Error) { - Horde::logMessage($parent, 'ERR'); - throw new Horde_Share_Exception($parent->getMessage()); - } } /* If we are an existing share, check for any children */ if ($this->getId()) { - $children = $this->_shareOb->listShares( - $GLOBALS['registry']->getAuth(), Horde_Perms::EDIT, null, 0, 0, null, 0, - $this->getId()); + $children = $this->_shareOb->listShares(null, + array('perm' => Horde_Perms::EDIT, + 'parent' => $this, + 'all_levels' => true, + 'ignore_perms' => true)); } else { $children = array(); } @@ -122,11 +121,11 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql $parent_string = null; } $this->data['share_parents'] = $parent_string; - $query = $this->_shareOb->getWriteDb()->prepare('UPDATE ' . $this->_shareOb->getTable() . ' SET share_parents = ? WHERE share_id = ?', null, MDB2_PREPARE_MANIP); - $result = $query->execute(array($this->data['share_parents'], $this->getId())); - $query->free(); - if ($result instanceof PEAR_Error) { - throw new Horde_Share_Exception($result->getMessage()); + $sql = 'UPDATE ' . $this->_shareOb->getTable() . ' SET share_parents = ? WHERE share_id = ?'; + try { + $this->_shareOb->getStorage()->update($sql, array($this->data['share_parents'], $this->getId())); + } catch (Horde_Db_Exception $e) { + throw new Horde_Share_Exception($e->getMessage()); } /* Now we can reset the children's parent */ @@ -141,7 +140,7 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql * Returns the permission of this share. * * @return Horde_Perms_Permission Permission object that represents the - * permissions on this share. + * permissions on this share. */ public function getPermission() { @@ -183,4 +182,3 @@ class Horde_Share_Object_Sql_Hierarchical extends Horde_Share_Object_Sql } } - diff --git a/framework/Share/lib/Horde/Share/Sql.php b/framework/Share/lib/Horde/Share/Sql.php index acccc78d9..614901877 100644 --- a/framework/Share/lib/Horde/Share/Sql.php +++ b/framework/Share/lib/Horde/Share/Sql.php @@ -9,13 +9,14 @@ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. * * @author Duck + * @author Michael J. Rubinsky * @package Horde_Share */ /** * @package Horde_Share */ -class Horde_Share_Sql extends Horde_Share +class Horde_Share_Sql extends Horde_Share implements Serializable { /* Share has user perms */ const SQL_FLAG_USERS = 1; @@ -23,72 +24,110 @@ class Horde_Share_Sql extends Horde_Share /* Share has group perms */ const SQL_FLAG_GROUPS = 2; + /* Serializable version */ + const VERSION = 1; + /** * Handle for the current database connection. - * @TODO: port to Horde_Db * - * @var MDB2 + * @var Horde_Db_Adapter */ protected $_db; /** - * Handle for the current database connection, used for writing. Defaults - * to the same handle as $db if a separate write database is not required. + * Main share table for the current scope. * - * @var MDB2 + * @var string */ - protected $_write_db; + protected $_table; /** - * SQL connection parameters + * The Horde_Share_Object subclass to instantiate objects as + * + * @var string */ - protected $_params = array(); + protected $_shareObject = 'Horde_Share_Object_Sql'; /** - * Main share table for the current scope. * - * @var string + * @see Horde_Share::__construct() */ - protected $_table; + public function __construct($app, $user, Horde_Perms $perms, Horde_Group $groups) + { + parent::__construct($app, $user, $perms, $groups); + $this->_table = $this->_app . '_shares'; + } /** - * The Horde_Share_Object subclass to instantiate objects as + * Serializes the object. Includes all properties except _table (this can + * be determined by _app), and _db (which is injected when unserialized). + * Note that you MUST set the db adapter after unserializing, but calling + * the setDb() method. * - * @var string + * @return string The serialized object. */ - protected $_shareObject = 'Horde_Share_Object_Sql'; + public function serialize() + { + $data = array( + self::VERSION, + $this->_app, + $this->_root, + $this->_cache, + $this->_shareMap, + $this->_listcache, + $this->_shareObject, + $this->_permsObject); + + return serialize($data); + } /** - * Initializes the object. + * Reconstructs object from serialized properties. + * Note: You MUST set the db adapter via setDb() after unserializing this + * object. + * + * @param string $serialized */ - public function __wakeup() + public function unserialize($data) { + $data = @unserialize($data); + if (!is_array($data) || + !isset($data[0]) || + ($data[0] != self::VERSION)) { + throw new Exception('Cache version change'); + } + + $this->_app = $data[1]; + $this->_root = $data[2]; + $this->_cache = $data[3]; + $this->_shareMap = $data[4]; + $this->_listcache = $data[5]; + $this->_shareObject = $data[6]; + $this->_permsObject = $data [7]; + $this->_table = $this->_app . '_shares'; - $this->_connect(); foreach (array_keys($this->_cache) as $name) { - $this->_cache[$name]->setShareOb($this); + $this->initShareObject($this->_cache[$name]); } - - parent::__wakeup(); } /** - * Returns the properties that need to be serialized. + * (re)connect the share object to this share driver. Userful for when + * share objects are unserialized from a cache separate from the share + * driver. * - * @return array List of serializable properties. + * @param Horde_Share_Object $object */ - public function __sleep() + public function initShareObject($object) { - $properties = get_object_vars($this); - unset($properties['_sortList'], - $properties['_db'], - $properties['_write_db']); - return array_keys($properties); + $object->setShareOb($this); } /** * Get storage table + * + * @return string */ public function getTable() { @@ -96,20 +135,18 @@ class Horde_Share_Sql extends Horde_Share } /** - * Refetence to write db + * + * @return Horde_Db_Adapter_Base */ - public function getWriteDb() - { - return $this->_write_db; - } - - public function getReadDb() + public function getStorage() { return $this->_db; } /** * Finds out if the share has user set + * @TODO: fix method prefix or make protected + * @param boolean */ public function _hasUsers($share) { @@ -134,22 +171,15 @@ class Horde_Share_Sql extends Horde_Share protected function _getShareUsers(&$share) { if ($this->_hasUsers($share)) { - $stmt = $this->_db->prepare('SELECT user_uid, perm FROM ' . $this->_table . '_users WHERE share_id = ?'); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($share['share_id'])); - if ( $result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + try { + $rows = $this->_db->selectAll('SELECT user_uid, perm FROM ' . $this->_table . '_users WHERE share_id = ?', array($share['share_id'])); + foreach ($rows as $row) { $share['perm']['users'][$row['user_uid']] = (int)$row['perm']; } + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } - $stmt->free(); - $result->free(); } } @@ -157,28 +187,21 @@ class Horde_Share_Sql extends Horde_Share * Get groups permissions * * @param array $share Share data array + * * @throws Horde_Share_Exception */ protected function _getShareGroups(&$share) { if ($this->_hasGroups($share)) { - // Get groups permissions - $stmt = $this->_db->prepare('SELECT group_uid, perm FROM ' . $this->_table . '_groups WHERE share_id = ?'); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($share['share_id'])); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + try { + $rows = $this->_db->selectAll('SELECT group_uid, perm FROM ' . $this->_table . '_groups WHERE share_id = ?', array($share['share_id'])); + foreach ($rows as $row) { $share['perm']['groups'][$row['group_uid']] = (int)$row['perm']; } + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } - $stmt->free(); - $result->free(); } } @@ -189,33 +212,21 @@ class Horde_Share_Sql extends Horde_Share * @param string $name The name of the share to retrieve. * * @return Horde_Share_Object_sql The requested share. + * @throws Horde_Exception_NotFound + * @throws Horde_Share_Exception */ protected function _getShare($name) { - $stmt = $this->_db->prepare('SELECT * FROM ' . $this->_table . ' WHERE share_name = ?'); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $results = $stmt->execute(array($name)); - if ($results instanceof PEAR_Error) { - Horde::logMessage($results, 'ERR'); - throw new Horde_Share_Exception($results->getMessage()); - } - $data = $results->fetchRow(MDB2_FETCHMODE_ASSOC); - if ($data instanceof PEAR_Error) { - Horde::logMessage($data, 'ERR'); - throw new Horde_Share_Exception($data->getMessage()); - } elseif (empty($data)) { - throw new Horde_Share_Exception(sprintf(Horde_Share_Translation::t("Share \"%s\" does not exist."), $name)); - } - $stmt->free(); - $results->free(); - - // Convert charset - $data = $this->_fromDriverCharset($data); - - // Populate the perms array + try { + $results = $this->_db->selectOne('SELECT * FROM ' . $this->_table . ' WHERE share_name = ?', array($name)); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + if (!$results) { + throw new Horde_Exception_NotFound(); + } + $data = $this->_fromDriverCharset($results); $this->_loadPermissions($data); return new $this->_shareObject($data); @@ -246,39 +257,23 @@ class Horde_Share_Sql extends Horde_Share * Returns a Horde_Share_Object_sql object corresponding to the given * unique ID, with the details retrieved appropriately. * - * @param integer $cid The id of the share to retrieve. + * @param integer $id The id of the share to retrieve. * * @return Horde_Share_Object_sql The requested share. * @throws Horde_Share_Exception */ protected function _getShareById($id) { - $params = array($id); - $stmt = $this->_db->prepare('SELECT * FROM ' . $this->_table . ' WHERE share_id = ?'); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $results = $stmt->execute($params); - if ($results instanceof PEAR_Error) { - Horde::logMessage($results, 'ERR'); - throw new Horde_Share_Exception($results->getMessage()); + try { + $results = $this->_db->selectOne('SELECT * FROM ' . $this->_table . ' WHERE share_id = ?', array($id)); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } - $data = $results->fetchRow(MDB2_FETCHMODE_ASSOC); - if ($data instanceof PEAR_Error) { - Horde::logMessage($data, 'ERR'); - throw new Horde_Share_Exception($data->getMessage()); - } elseif (empty($data)) { - throw new Horde_Share_Exception(sprintf(Horde_Share_Translation::t("Share ID %d does not exist."), $id)); + if (!$results) { + throw new Horde_Exception_NotFound(); } - - $stmt->free(); - $results->free(); - - // Convert charset - $data = $this->_fromDriverCharset($data); - - // Get permissions + $data = $this->_fromDriverCharset($results); $this->_loadPermissions($data); return new $this->_shareObject($data); @@ -289,25 +284,23 @@ class Horde_Share_Sql extends Horde_Share * to the given set of unique IDs, with the details retrieved * appropriately. * - * @param array $cids The array of ids to retrieve. + * @param array $ids The array of ids to retrieve. * * @return array The requested shares. */ protected function _getShares($ids) { $shares = array(); - $query = 'SELECT * FROM ' . $this->_table . ' WHERE share_id IN (' . implode(', ', $ids) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (empty($result)) { - return array(); + try { + $rows = $this->_db->selectAll('SELECT * FROM ' . $this->_table . ' WHERE share_id IN (' . str_repeat('?', count($ids) - 1) . '?) ', $ids); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } $groups = array(); $users = array(); - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + foreach ($rows as $share) { $shares[(int)$share['share_id']] = $this->_fromDriverCharset($share); if ($this->_hasUsers($share)) { $users[] = (int)$share['share_id']; @@ -316,37 +309,30 @@ class Horde_Share_Sql extends Horde_Share $groups[] = (int)$share['share_id']; } } - $result->free(); - // Get users permissions + // Get users' permissions if (!empty($users)) { - $query = 'SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users ' - . ' WHERE share_id IN (' . implode(', ', $users) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; - } - $result->free(); + try { + $rows = $this->_db->selectAll('SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users WHERE share_id IN (' . str_repeat('?', count($users) - 1) . '?) ', $users); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; } } - // Get groups permissions + // Get groups' permissions if (!empty($groups)) { - $query = 'SELECT share_id, group_uid, perm FROM ' . $this->_table . '_groups' - . ' WHERE share_id IN (' . implode(', ', $groups) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; - } - $result->free(); + try { + $rows = $this->_db->selectAll('SELECT share_id, group_uid, perm FROM ' . $this->_table . '_groups WHERE share_id IN (' . str_repeat('?', count($groups) - 1) . '?) ', $groups); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; } } @@ -360,8 +346,7 @@ class Horde_Share_Sql extends Horde_Share } /** - * Lists *all* shares for the current app/share, regardless of - * permissions. + * 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! @@ -375,52 +360,46 @@ class Horde_Share_Sql extends Horde_Share } /** - * Lists *all* shares for the current app/share, regardless of - * permissions. + * Lists *all* shares for the current app/share, regardless of permissions. * * @return array All shares for the current app/share. + * @throws Horde_Share_Exception */ protected function _listAllShares() { $shares = array(); - $query = 'SELECT * FROM ' . $this->_table . ' ORDER BY share_name ASC'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (empty($result)) { - return array(); + + try { + $rows = $this->_db->selectAll('SELECT * FROM ' . $this->_table . ' ORDER BY share_name ASC'); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + foreach ($rows as $share) { $shares[(int)$share['share_id']] = $this->_fromDriverCharset($share); } - $result->free(); - + // Get users permissions - $query = 'SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - return $result; - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; - } - $result->free(); + try { + $rows = $this->_db->selectAll('SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users'); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; } // Get groups permissions - $query = 'SELECT share_id, group_uid, perm FROM ' . $this->_table . '_groups'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; - } - $result->free(); + try { + $rows = $this->_db->selectAll('SELECT share_id, group_uid, perm FROM ' . $this->_table . '_groups'); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; } $sharelist = array(); @@ -436,56 +415,54 @@ class Horde_Share_Sql 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 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. + * @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.
+     *  'attribtues'  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. - * @throws Horde_Share_Exception */ - public function listShares($userid, $perm = Horde_Perms::SHOW, $attributes = null, - $from = 0, $count = 0, $sort_by = null, $direction = 0) + public function listShares($userid, $params = array()) { + //var_dump($params); + $params = array_merge(array('perm' => Horde_Perms::SHOW, + 'attributes' => null, + 'from' => 0, + 'count' => 0, + 'sort_by' => null, + 'direction' => 0), + $params); $shares = array(); - if (is_null($sort_by)) { + if (is_null($params['sort_by'])) { $sortfield = 's.share_name'; - } elseif ($sort_by == 'owner' || $sort_by == 'id') { - $sortfield = 's.share_' . $sort_by; + } elseif ($params['sort_by'] == 'owner' || $params['sort_by'] == 'id') { + $sortfield = 's.share_' . $params['sort_by']; } else { - $sortfield = 's.attribute_' . $sort_by; + $sortfield = 's.attribute_' . $params['sort_by']; } $query = 'SELECT DISTINCT s.* ' - . $this->getShareCriteria($userid, $perm, $attributes) + . $this->getShareCriteria($userid, $params['perm'], $params['attributes']) . ' ORDER BY ' . $sortfield - . (($direction == 0) ? ' ASC' : ' DESC'); - if ($from > 0 || $count > 0) { - $this->_db->setLimit($count, $from); - } - - // Fix field names for sqlite. MDB2 tries to handle this with - // MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES, but it doesn't stick. - if ($this->_db->phptype == 'sqlite') { - $connection = $this->_db->getConnection(); - @sqlite_query('PRAGMA full_column_names=0', $connection); - @sqlite_query('PRAGMA short_column_names=1', $connection); - } + . (($params['direction'] == 0) ? ' ASC' : ' DESC'); + $query = $this->_db->addLimitOffset($query, array('limit' => $params['count'], 'offset' => $params['from'])); Horde::logMessage(sprintf("SQL Query by Horde_Share_sql::listShares: %s", $query), 'DEBUG'); - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (empty($result)) { - return array(); + try { + $rows = $this->_db->selectAll($query); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } - $users = array(); $groups = array(); - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + foreach ($rows as $share) { $shares[(int)$share['share_id']] = $this->_fromDriverCharset($share); if ($this->_hasUsers($share)) { $users[] = (int)$share['share_id']; @@ -494,22 +471,20 @@ class Horde_Share_Sql extends Horde_Share $groups[] = (int)$share['share_id']; } } - $result->free(); // Get users permissions if (!empty($users)) { $query = 'SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users WHERE share_id IN (' . implode(', ', $users) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; - } - $result->free(); + try { + $rows = $this->_db->selectAll($query); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; } } @@ -518,15 +493,14 @@ class Horde_Share_Sql extends Horde_Share $query = 'SELECT share_id, group_uid, perm FROM ' . $this->_table . '_groups WHERE share_id IN (' . implode(', ', $groups) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; - } - $result->free(); + try { + $rows = $this->_db->selectAll($query); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; } } @@ -538,9 +512,10 @@ class Horde_Share_Sql extends Horde_Share } unset($shares); - try { - return Horde::callHook('share_list', array($userid, $perm, $attributes, $sharelist)); - } catch (Horde_Exception_HookNotSet $e) {} + // Run the results through the callback, if configured. + if (!empty($this->_callbacks['list'])) { + return call_user_func_array($this->_callbacks['list'], array($userid, $sharelist, $params)); + } return $sharelist; } @@ -553,32 +528,22 @@ class Horde_Share_Sql extends Horde_Share */ public function listSystemShares() { - // Fix field names for sqlite. MDB2 tries to handle this with - // MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES, but it doesn't stick. - if ($this->_db->phptype == 'sqlite') { - $connection = $this->_db->getConnection(); - @sqlite_query('PRAGMA full_column_names=0', $connection); - @sqlite_query('PRAGMA short_column_names=1', $connection); - } - $query = 'SELECT * FROM ' . $this->_table . ' WHERE share_owner IS NULL'; Horde::logMessage('SQL Query by Horde_Share_sql::listSystemShares: ' . $query, 'DEBUG'); - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage());; - } elseif (empty($result)) { - return array(); + try { + $rows = $this->_db->selectAll($query); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage());; } $sharelist = array(); - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + foreach ($rows as $share) { $data = $this->_fromDriverCharset($share); $this->_getSharePerms($data); $sharelist[$data['share_name']] = new $this->_shareObject($data); $sharelist[$data['share_name']]->setShareOb($this); } - $result->free(); return $sharelist; } @@ -601,13 +566,14 @@ class Horde_Share_Sql extends Horde_Share $query = $this->getShareCriteria($userid, $perm, $attributes); $query = 'SELECT COUNT(DISTINCT s.share_id) ' . $query; Horde::logMessage(sprintf("SQL Query by Horde_Share_sql::_countShares: %s", $query), 'DEBUG'); - return $this->_db->queryOne($query); + + return $this->_db->selectValue($query); } /** * Returns a new share object. * - * @param string $name The share's name. + * @param string $name The share's name. * * @return Horde_Share_Object_sql A new share object. */ @@ -644,20 +610,13 @@ class Horde_Share_Sql extends Horde_Share $tables = array($this->_table, $this->_table . '_users', $this->_table . '_groups'); - foreach ($tables as $table) { - - /* Remove the share entry */ - $stmt = $this->_write_db->prepare('DELETE FROM ' . $table . ' WHERE share_id = ?', null, MDB2_PREPARE_MANIP); - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute($params); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); + foreach ($tables as $table) { + try { + $this->_db->delete('DELETE FROM ' . $table . ' WHERE share_id = ?', $params); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } - $stmt->free(); } return true; @@ -672,31 +631,20 @@ class Horde_Share_Sql extends Horde_Share * @throws Horde_Share_Exception */ protected function _exists($share) - { - $stmt = $this->_db->prepare('SELECT 1 FROM ' . $this->_table - . ' WHERE share_name = ?'); - - if ($stmt instanceof PEAR_Error) { - Horde::logMessage($stmt, 'ERR'); - throw new Horde_Share_Exception($stmt->getMessage()); - } - $result = $stmt->execute(array($share)); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); + { + try { + return (boolean)$this->_db->selectOne('SELECT 1 FROM ' . $this->_table . ' WHERE share_name = ?', array($share)); + } catch (Horde_Db_Exception $e) { + throw new Horde_Share_Exception($e); } - - $exists = (bool)$result->fetchOne(); - $stmt->free(); - $result->free(); - - return $exists; } /** * Returns an array of criteria for querying shares. * @access protected * + * @TODO: Horde_SQL:: stuff should be refactored/removed when it's ported + * to Horde_Db * @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 returned to those who @@ -705,14 +653,14 @@ class Horde_Share_Sql extends Horde_Share * @return string The criteria string for fetching this user's shares. */ public function getShareCriteria($userid, $perm = Horde_Perms::SHOW, - $attributes = null) + $attributes = null) { $query = ' FROM ' . $this->_table . ' s '; $where = ''; if (!empty($userid)) { // (owner == $userid) - $where .= 's.share_owner = ' . $this->_write_db->quote($userid); + $where .= 's.share_owner = ' . $this->_db->quote($userid); // (name == perm_creator and val & $perm) $where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_creator', '&', $perm) . ')'; @@ -722,14 +670,13 @@ class Horde_Share_Sql extends Horde_Share // (name == perm_users and key == $userid and val & $perm) $query .= ' LEFT JOIN ' . $this->_table . '_users u ON u.share_id = s.share_id'; - $where .= ' OR ( u.user_uid = ' . $this->_write_db->quote($userid) + $where .= ' OR ( u.user_uid = ' . $this->_db->quote($userid) . ' AND (' . Horde_SQL::buildClause($this->_db, 'u.perm', '&', $perm) . '))'; // If the user has any group memberships, check for those also. // @TODO: Inject the group driver try { - $group = $GLOBALS['injector']->getInstance('Horde_Group'); - $groups = $group->getGroupMemberships($userid, true); + $groups = $this->_groups->getGroupMemberships($userid, true); if ($groups) { // (name == perm_groups and key in ($groups) and val & $perm) $ids = array_keys($groups); @@ -767,20 +714,6 @@ class Horde_Share_Sql extends Horde_Share } /** - * Resets the current database name so that MDB2 is always selecting the - * database before sending a query. - * - * @TODO: This needs to be public since it's used as a callback in MDB2. - * Remove when refactored to use Horde_Db - */ - public function _selectDB($db, $scope, $message, $is_manip = null) - { - if ($scope == 'query') { - $db->connected_database_name = ''; - } - } - - /** * Set the SQL table name to use for the current scope's share storage. * * @var string $table The table name @@ -789,103 +722,9 @@ class Horde_Share_Sql extends Horde_Share $this->_table = $table; } - /** - * Attempts to open a connection to the sql server. - * - * @return boolean True on success. - * @throws Horde_Share_Exception - */ - protected function _connect() + public function setStorage(Horde_Db_Adapter $db) { - $this->_params = $GLOBALS['conf']['sql']; - if (!isset($this->_params['database'])) { - $this->_params['database'] = ''; - } - if (!isset($this->_params['username'])) { - $_params['username'] = ''; - } - if (!isset($this->_params['hostspec'])) { - $this->_params['hostspec'] = ''; - } - - /* Connect to the sql server using the supplied parameters. */ - $params = $this->_params; - unset($params['charset']); - $this->_write_db = &MDB2::factory($params); - if ($this->_write_db instanceof PEAR_Error) { - throw new Horde_Share_Exception($this->_write_db->getMessage()); - } - - /* Attach debug handler. */ - $this->_write_db->setOption('debug', true); - $this->_write_db->setOption('debug_handler', array($this, '_selectDB')); - $this->_write_db->setOption('seqcol_name', 'id'); - - /* Set DB portability options. */ - switch ($this->_write_db->phptype) { - case 'mssql': - $this->_write_db->setOption('field_case', CASE_LOWER); - $this->_write_db->setOption('portability', MDB2_PORTABILITY_FIX_CASE | MDB2_PORTABILITY_ERRORS | MDB2_PORTABILITY_RTRIM | MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES); - break; - - default: - switch ($this->_write_db->phptype) { - case 'oci8': - $this->_write_db->setOption('emulate_database', false); - break; - - case 'pgsql': - /* The debug handler breaks PostgreSQL. In most cases it - * shouldn't be necessary, but this may mean we simply can't - * support use of multiple Postgres databases right now. See - * http://bugs.horde.org/ticket/7825 */ - $this->_write_db->setOption('debug', false); - break; - } - $this->_write_db->setOption('field_case', CASE_LOWER); - $this->_write_db->setOption('portability', MDB2_PORTABILITY_FIX_CASE | MDB2_PORTABILITY_ERRORS | MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES); - } - - /* Check if we need to set up the read DB connection seperately. */ - if (!empty($this->_params['splitread'])) { - $params = array_merge($params, $this->_params['read']); - unset($params['charset']); - $this->_db = &MDB2::singleton($params); - if ($this->_db instanceof PEAR_Error) { - throw new Horde_Share_Exception($this->_db); - } - - $this->_db->setOption('seqcol_name', 'id'); - /* Set DB portability options. */ - switch ($this->_db->phptype) { - case 'mssql': - $this->_db->setOption('field_case', CASE_LOWER); - $this->_db->setOption('portability', MDB2_PORTABILITY_FIX_CASE | MDB2_PORTABILITY_ERRORS | MDB2_PORTABILITY_RTRIM | MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES); - break; - - default: - switch ($this->_db->phptype) { - case 'oci8': - $this->_db->setOption('emulate_database', false); - break; - - case 'pgsql': - /* The debug handler breaks PostgreSQL. In most cases it - * shouldn't be necessary, but this may mean we simply - * can't support use of multiple Postgres databases right - * now. See http://bugs.horde.org/ticket/7825 */ - $this->_write_db->setOption('debug', false); - break; - } - $this->_db->setOption('field_case', CASE_LOWER); - $this->_db->setOption('portability', MDB2_PORTABILITY_FIX_CASE | MDB2_PORTABILITY_ERRORS | MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES); - } - } else { - /* Default to the same DB handle as the writer for reading too */ - $this->_db = $this->_write_db; - } - - return true; + $this->_db = $db; } /** @@ -896,7 +735,7 @@ class Horde_Share_Sql extends Horde_Share foreach ($data as $key => $value) { if (substr($key, 0, 9) == 'attribute') { $data[$key] = Horde_String::convertCharset( - $data[$key], $this->_params['charset'], 'UTF-8'); + $data[$key], $this->_db->getOption('charset'), 'UTF-8'); } } @@ -917,7 +756,7 @@ class Horde_Share_Sql extends Horde_Share foreach ($data as $key => $value) { if (substr($key, 0, 9) == 'attribute') { $data[$key] = Horde_String::convertCharset( - $data[$key], 'UTF-8', $this->_params['charset']); + $data[$key], 'UTF-8', $this->_db->getOption('charset')); } } diff --git a/framework/Share/lib/Horde/Share/Sql/Hierarchical.php b/framework/Share/lib/Horde/Share/Sql/Hierarchical.php index b06f234b2..eeac16d1e 100644 --- a/framework/Share/lib/Horde/Share/Sql/Hierarchical.php +++ b/framework/Share/lib/Horde/Share/Sql/Hierarchical.php @@ -20,13 +20,13 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql * Override new share creation so we can allow for shares with empty * share_names. * + * @see Horde_Share::newShare */ - public function newShare($name = '') + public function newShare($owner, $name = '') { $share = $this->_newShare(); - $share->setShareOb($this); - //@TODO: inject the Auth object - $share->set('owner', $GLOBALS['registry']->getAuth()); + $this->initShareObject($share); + $share->set('owner', $owner); return $share; } @@ -47,58 +47,61 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql /** * Returns an array of all 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. - * @param integer $from The share to start listing from. - * @param integer $count The number of shares to return. - * @param string $sort_by The field to sort by - * @param integer $direction The sort direction - * @param mixed $parent Either a share_id, Horde_Share_Object or null. - * @param boolean $alllevels List all levels or just the direct children - * of $parent? + * @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.
+     *  'attribtues'    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.
+     *  'parent'        Start at this share in the hierarchy. Either share_id or
+     *                  Horde_Share_Object
+     *  'all_levels'    List all levels or just the direct children of parent?
+     *  'ignore_perms'  Don't check perms.
+     *
* - * @return mixed The shares the user has access to - * @throws Horde_Share_Exception + * @return array The shares the user has access to. */ - public function &listShares($userid, $perm = Horde_Perms::SHOW, $attributes = null, - $from = 0, $count = 0, $sort_by = null, - $direction = 0, $parent = null, - $allLevels = true, $ignorePerms = false) + 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, + 'parent' => null, + 'all_levels' => true, + 'ignore_perms' => false), + $params); $shares = array(); - if (is_null($sort_by)) { + if (is_null($params['sort_by'])) { $sortfield = 's.share_id'; - } elseif ($sort_by == 'owner' || $sort_by == 'id') { - $sortfield = 's.share_' . $sort_by; + } elseif ($params['sort_by'] == 'owner' || $params['sort_by'] == 'id') { + $sortfield = 's.share_' . $params['sort_by']; } else { - $sortfield = 's.attribute_' . $sort_by; + $sortfield = 's.attribute_' . $params['sort_by']; } $query = 'SELECT DISTINCT s.* ' - . $this->getShareCriteria($userid, $perm, $attributes, - $parent, $allLevels, $ignorePerms) + . $this->getShareCriteria($userid, $params['perm'], $params['attributes'], $params['parent'], $params['all_levels'], $params['ignore_perms']) . ' ORDER BY ' . $sortfield - . (($direction == 0) ? ' ASC' : ' DESC'); - if ($from > 0 || $count > 0) { - $this->_db->setLimit($count, $from); - } + . (($params['direction'] == 0) ? ' ASC' : ' DESC'); + $query = $this->_db->addLimitOffset($query, array('limit' => $params['count'], 'offset' => $params['from'])); Horde::logMessage('Query By Horde_Share_sql_hierarchical: ' . $query, 'DEBUG'); - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (empty($result)) { - return array(); + try { + $rows = $this->_db->selectAll($query); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } $users = array(); $groups = array(); - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + foreach ($rows as $share) { $shares[(int)$share['share_id']] = $this->_fromDriverCharset($share); if ($this->_hasUsers($share)) { $users[] = (int)$share['share_id']; @@ -107,39 +110,35 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql $groups[] = (int)$share['share_id']; } } - $result->free(); // Get users permissions if (!empty($users)) { $query = 'SELECT share_id, user_uid, perm FROM ' . $this->_table - . '_users WHERE share_id IN (' . implode(', ', $users) - . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + . '_users WHERE share_id IN (' . str_repeat('?,', count($users) - 1) . '?)'; + + try { + $rows = $this->_db->selectAll($query, $users); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; - } - $result->free(); } } // Get groups permissions if (!empty($groups)) { $query = 'SELECT share_id, group_uid, perm FROM ' . $this->_table - . '_groups WHERE share_id IN (' . implode(', ', $groups) - . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; - } - $result->free(); + . '_groups WHERE share_id IN (' . str_repeat('?,', count($groups) - 1) . '?)'; + try { + $rows = $this->_db->selectAll($query, $groups); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; } } @@ -151,17 +150,20 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql } unset($shares); - try { - return Horde::callHook('share_list', array($userid, $perm, $attributes, $sharelist)); - } catch (Horde_Exception_HookNotSet $e) {} + // Run the results through the callback, if configured. + if (!empty($this->_callbacks['list'])) { + return call_user_func_array($this->_callbacks['list'], array($userid, $sharelist, $params)); + } return $sharelist; } /** * Returns an array of criteria for querying shares. - * @access protected * + * @TODO: check method visisbility, + * remove ignorePerms param, simply set perm to null for this + * * @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 returned to those who @@ -180,15 +182,14 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql { static $criteria = array(); - if ($parent instanceof Horde_Share_Object) { + if (is_object($parent)) { $parent_id = $parent->getId(); } else { $parent_id = $parent; } - $key = hash('sha1', serialize(array($userid, $perm, $parent_id, $allLevels, $attributes, $ignorePerms))); if (isset($criteria[$key])) { - return $criteria[$key]; + //return $criteria[$key]; } $query = ' FROM ' . $this->_table . ' s '; @@ -209,14 +210,13 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql // (name == perm_users and key == $userid and val & $perm) $query .= ' LEFT JOIN ' . $this->_table . '_users u ON u.share_id = s.share_id'; - $where .= ' OR ( u.user_uid = ' . $this->_write_db->quote($userid) + $where .= ' OR ( u.user_uid = ' . $this->_db->quote($userid) . ' AND (' . Horde_SQL::buildClause($this->_db, 'u.perm', '&', $perm) . '))'; // If the user has any group memberships, check for those also. // @TODO: Inject the group driver try { - $group = $GLOBALS['injector']->getInstance('Horde_Group'); - $groups = $group->getGroupMemberships($userid, true); + $groups = $this->_groups->getGroupMemberships($userid, true); if ($groups) { // (name == perm_groups and key in ($groups) and val & $perm) $ids = array_keys($groups); @@ -260,9 +260,6 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql if ($parent != null) { if (!($parent instanceof Horde_Share_Object)) { $parent = $this->getShareById($parent); - if ($parent instanceof PEAR_Error) { - throw new Horde_Share_Exception($parent->getMessage()); - } } // Need to append the parent's share id to the list of parents in @@ -310,22 +307,22 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql $from = 0, $count = 0) { $sql = 'SELECT DISTINCT(s.share_owner) ' - . $this->getShareCriteria($GLOBALS['registry']->getAuth(), $perm, null, - $parent, $allLevels); + . $this->getShareCriteria($this->_user, $perm, null, $parent, $allLevels); if ($count) { - $this->_db->setLimit($count, $from); + $sql = $this->_db->addLimitOffset($sql, array('limit' => $count, 'offset' => $from)); } - $allowners = $this->_db->queryCol($sql); - if ($allowners instanceof PEAR_Error) { - Horde::logMessage($allowners, 'ERR'); - throw new Horde_Share_Exception($allowners->getMessage()); + try { + $allowners = $this->_db->selectValues($sql); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e); } $owners = array(); foreach ($allowners as $owner) { - if ($this->countShares($GLOBALS['registry']->getAuth(), $perm, $owner, $parent, $allLevels)) { + if ($this->countShares($this->_user, $perm, $owner, $parent, $allLevels)) { $owners[] = $owner; } } @@ -348,10 +345,9 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql public function countOwners($perm = Horde_Perms::SHOW, $parent = null, $allLevels = true) { $sql = 'SELECT COUNT(DISTINCT(s.share_owner)) ' - . $this->getShareCriteria($GLOBALS['registry']->getAuth(), $perm, null, $parent, - $allLevels); + . $this->getShareCriteria($this->_user, $perm, null, $parent, $allLevels); - return $this->_db->queryOne($sql); + return $this->_db->selectValue($sql); } /** @@ -384,10 +380,7 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql public function getShareById($cid) { if (!isset($this->_cache[$cid])) { - $share = &$this->_getShareById($cid); - if ($share instanceof PEAR_Error) { - throw new Horde_Share_Exception($share->getMessage()); - } + $share = $this->_getShareById($cid); $share->setShareOb($this); $this->_cache[$cid] = &$share; } @@ -417,11 +410,7 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql } if (count($missing_ids)) { - $shares = &$this->_getShares($missing_ids); - if ($shares instanceof PEAR_Error) { - throw new Horde_Share_Exception($shares->getMessage()); - } - + $shares = $this->_getShares($missing_ids); foreach (array_keys($shares) as $key) { $this->_cache[$key] = &$shares[$key]; $this->_cache[$key]->setShareOb($this); @@ -441,23 +430,15 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql */ public function removeShare(Horde_Share_Object $share) { - try { - Horde::callHook('share_remove', array($share)); - } catch (Horde_Exception_HookNotSet $e) {} - /* Get the list of all $share's children */ - $children = $share->getChildren(null, true); + $children = $share->getChildren(null, null, true); /* Remove share from the caches. */ $id = $share->getId(); $this->_cache = array(); $this->_listCache = array(); - foreach ($children as $child) { $result = $this->_removeShare($child); - if ($result instanceof PEAR_Error) { - throw new Horde_Share_Exception($result->getMessage()); - } } return $this->_removeShare($share); @@ -475,18 +456,17 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql protected function _getShares($ids) { $shares = array(); - $query = 'SELECT * FROM ' . $this->_table . ' WHERE share_id IN (' . implode(', ', $ids) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (empty($result)) { - return array(); + $query = 'SELECT * FROM ' . $this->_table . ' WHERE share_id IN (' . str_repeat('?,', count($ids) - 1) . '?)'; + try { + $rows = $this->_db->selectAll($query, $ids); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } $groups = array(); $users = array(); - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { + foreach ($rows as $share) { $shares[(int)$share['share_id']] = $this->_fromDriverCharset($share); if ($this->_hasUsers($share)) { $users[] = (int)$share['share_id']; @@ -495,38 +475,34 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql $groups[] = (int)$share['share_id']; } } - $result->free(); // Get users permissions if (!empty($users)) { - $query = 'SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users ' - . ' WHERE share_id IN (' . implode(', ', $users) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; - } - $result->free(); + $query = 'SELECT share_id, user_uid, perm FROM ' . $this->_table . '_users WHERE share_id IN (' . str_repeat('?,', count($users) - 1) . '?)'; + try { + $rows = $this->_db->selectAll($query, $users); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); + } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['users'][$share['user_uid']] = (int)$share['perm']; } } // Get groups permissions if (!empty($groups)) { $query = 'SELECT share_id, group_uid, perm FROM ' . $this->_table . '_groups' - . ' WHERE share_id IN (' . implode(', ', $groups) . ')'; - $result = $this->_db->query($query); - if ($result instanceof PEAR_Error) { - Horde::logMessage($result, 'ERR'); - throw new Horde_Share_Exception($result->getMessage()); - } elseif (!empty($result)) { - while ($share = $result->fetchRow(MDB2_FETCHMODE_ASSOC)) { - $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; - } - $result->free(); + . ' WHERE share_id IN (' . str_repeat('?,', count($groups) - 1) . '?)'; + try { + $rows = $this->_db->selectAll($query, $groups); + } catch (Horde_Db_Exception $e) { + Horde::logMessage($e, 'ERR'); + throw new Horde_Share_Exception($e->getMessage()); } + foreach ($rows as $share) { + $shares[$share['share_id']]['perm']['groups'][$share['group_uid']] = (int)$share['perm']; + } } $sharelist = array(); @@ -569,6 +545,7 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql $query = 'SELECT COUNT(DISTINCT s.share_id) ' . $this->getShareCriteria($userid, $perm, $attributes, $parent, $allLevels); - return $this->_db->queryOne($query); + return $this->_db->selectValue($query); } + } diff --git a/framework/Share/package.xml b/framework/Share/package.xml index 3ce92b6d2..5d6ba47c5 100644 --- a/framework/Share/package.xml +++ b/framework/Share/package.xml @@ -11,8 +11,8 @@ resources a user owns or has access to. chuck@horde.org yes - 2010-10-22 - + 2010-10-29 + 0.0.4 0.0.4 @@ -518,7 +518,7 @@ Initial release as a PEAR package beta beta - 2010-10-22 + 2010-10-29 LGPL * Converted to Horde 4 coding standards diff --git a/horde/lib/Api.php b/horde/lib/Api.php index a0e2bcbc0..9aad2f197 100644 --- a/horde/lib/Api.php +++ b/horde/lib/Api.php @@ -477,16 +477,13 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->newShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - - $share->set('owner', $userName); - $share->set('name', $shareTitle); - - if (is_a($result = $shares->addShare($share), 'PEAR_Error')) { - throw new Horde_Exception($result); + try { + $share = $shares->newShare($GLOBALS['registry']->getAuth(), $shareName); + $share->set('owner', $userName); + $share->set('name', $shareTitle); + $shares->addShare($share); + } catch (Horde_Share_Exception $e) { + throw new Horde_Exception($e); } } @@ -506,13 +503,11 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - - if (is_a($result = $shares->removeShare($share), 'PEAR_Error')) { - throw new Horde_Exception($result); + $share = $shares->getShare($shareName); + try { + $shares->removeShare($share); + } catch (Horde_Share_Exception $e) { + throw new Horde_Exception_Prior($e); } } @@ -534,7 +529,9 @@ class Horde_Api extends Horde_Registry_Api $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - $share_list = &$shares->listShares($userName, Horde_Perms::SHOW, $userName); + $share_list = $shares->listShares($userName, + array('perm' => Horde_Perms::SHOW, + 'attributes' => $userName)); $myshares = array(); foreach ($share_list as $share) { $myshares[] = $share->getName(); @@ -562,12 +559,13 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); + $share = $shares->getShare($shareName); + try { + $perm = $share->getPermission(); + } catch (Horde_Share_Exception $e) { + throw new Horde_Exception_Prior($e); } - $perm = &$share->getPermission(); foreach ($permissions as $permission) { $permission = Horde_String::upper($permission); if (defined('Horde_Perms::' . $permission)) { @@ -575,8 +573,10 @@ class Horde_Api extends Horde_Registry_Api } } - if (is_a($result = $share->setPermission($perm), 'PEAR_Error')) { - throw new Horde_Exception($result); + try { + $share->setPermission($perm); + } catch (Horde_Share_Exception $e) { + throw new Horde_Exception_Prior($e); } } @@ -599,11 +599,7 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - + $shares->getShare($shareName); try { $groups = $GLOBALS['injector']->getInstance('Horde_Group'); $groupId = $groups->getGroupId($groupName); @@ -611,7 +607,7 @@ class Horde_Api extends Horde_Registry_Api throw new Horde_Exception($e); } - $perm = &$share->getPermission(); + $perm = $share->getPermission(); foreach ($permissions as $permission) { $permission = Horde_String::upper($permission); if (defined('Horde_Perms::' . $permission)) { @@ -619,8 +615,10 @@ class Horde_Api extends Horde_Registry_Api } } - if (is_a($result = $share->setPermission($perm), 'PEAR_Error')) { - throw new Horde_Exception($result); + try { + $share->setPermission($perm); + } catch (Horde_Share_Exception $e) { + throw new Horde_Exception($e); } } @@ -641,12 +639,10 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - - if (is_a($result = $share->removeUser($userName), 'PEAR_Error')) { + $share = $shares->getShare($shareName); + try { + $share->removeUser($userName); + } catch (Horde_Share_Exception $e) { throw new Horde_Exception($result); } } @@ -668,20 +664,17 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - + $share = $shares->getShare($shareName); try { $groups = $GLOBALS['injector']->getInstance('Horde_Group'); $groupId = $groups->getGroupId($groupName); } catch (Horde_Group_Exception $e) { throw new Horde_Exception($e); } - - if (is_a($result = $share->removeGroup($groupId), 'PEAR_Error')) { - throw new Horde_Exception($result); + try { + $share->removeGroup($groupId); + } catch (Horde_Share_Exception $e) { + throw new Horde_Exception($e); } } @@ -708,12 +701,8 @@ class Horde_Api extends Horde_Registry_Api Horde_Perms::DELETE => 'delete'); $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - - $perm = &$share->getPermission(); + $share = $shares->getShare($shareName); + $perm = $share->getPermission(); $permissions = $perm->getUserPermissions(); if (empty($permissions[$userName])) { return array(); @@ -750,12 +739,8 @@ class Horde_Api extends Horde_Registry_Api Horde_Perms::DELETE => 'delete'); $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - - $perm = &$share->getPermission(); + $share = $shares->getShare($shareName); + $perm = $share->getPermission(); $permissions = $perm->getGroupPermissions(); if (empty($permissions[$groupName])) { return array(); @@ -787,11 +772,7 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - + $share = $shares->getShare($shareName); $perm = 0; foreach ($permissions as $permission) { $permission = Horde_String::upper($permission); @@ -821,11 +802,7 @@ class Horde_Api extends Horde_Registry_Api } $shares = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create($scope); - - if (is_a($share = &$shares->getShare($shareName), 'PEAR_Error')) { - throw new Horde_Exception($share); - } - + $share = $shares->getShare($shareName); $perm = 0; foreach ($permissions as $permission) { $permission = Horde_String::upper($permission); diff --git a/horde/services/shares/edit.php b/horde/services/shares/edit.php index abf0cf1c2..2786aedbf 100644 --- a/horde/services/shares/edit.php +++ b/horde/services/shares/edit.php @@ -39,7 +39,7 @@ case 'edit': $share = $shares->getShareById(Horde_Util::getFormData('cid')); $form = 'edit.inc'; $perm = $share->getPermission(); - } catch (Horde_Share_Exception $e) { + } catch (Horde_Exception_NotFound $e) { if (($category = Horde_Util::getFormData('share')) !== null) { try { $share = $shares->getShare($category); diff --git a/ingo/lib/Application.php b/ingo/lib/Application.php index 03f244869..cc670f203 100644 --- a/ingo/lib/Application.php +++ b/ingo/lib/Application.php @@ -75,7 +75,7 @@ class Ingo_Application extends Horde_Registry_Application if (trim($name) == '') { $name = $GLOBALS['registry']->getAuth('original'); } - $share = $GLOBALS['ingo_shares']->newShare($signature); + $share = $GLOBALS['ingo_shares']->newShare($GLOBALS['registry']->getAuth(), $signature); $share->set('name', $name); $GLOBALS['ingo_shares']->addShare($share); $GLOBALS['all_rulesets'][$signature] = $share; @@ -216,7 +216,8 @@ class Ingo_Application extends Horde_Registry_Application /* Get a list of all shares this user owns and has perms to delete * and remove them. */ try { - $shares = $GLOBALS['ingo_shares']->listShares($user, Horde_Perms::DELETE, $user); + $shares = $GLOBALS['ingo_shares']->listShares($user, array('perm' => Horde_Perms::DELETE, + 'attributes' => $user)); } catch (Horde_Share_Exception $e) { Horde::logMessage($e, 'ERR'); throw new Ingo_Exception($e); diff --git a/ingo/lib/Ingo.php b/ingo/lib/Ingo.php index 0e0d34017..fdad944ec 100644 --- a/ingo/lib/Ingo.php +++ b/ingo/lib/Ingo.php @@ -368,7 +368,10 @@ class Ingo $permission = Horde_Perms::SHOW) { try { - $rulesets = $GLOBALS['ingo_shares']->listShares($GLOBALS['registry']->getAuth(), $permission, $owneronly ? $GLOBALS['registry']->getAuth() : null); + $rulesets = $GLOBALS['ingo_shares']->listShares( + $GLOBALS['registry']->getAuth(), + array('perm' => $permission, + 'attributes' => $owneronly ? $GLOBALS['registry']->getAuth() : null)); } catch (Horde_Share_Exception $e) { Horde::logMessage($e, 'ERR'); return array(); diff --git a/kronolith/lib/Api.php b/kronolith/lib/Api.php index c99d6ab70..7afd9434e 100644 --- a/kronolith/lib/Api.php +++ b/kronolith/lib/Api.php @@ -127,9 +127,10 @@ class Kronolith_Api extends Horde_Registry_Api } elseif (count($parts) == 1) { // This request is for all calendars owned by the requested user - $calendars = $GLOBALS['kronolith_shares']->listShares($GLOBALS['registry']->getAuth(), - Horde_Perms::SHOW, - $parts[0]); + $calendars = $GLOBALS['kronolith_shares']->listShares( + $GLOBALS['registry']->getAuth(), + array('perm' => Horde_Perms::SHOW, + 'attributes' => $parts[0])); $results = array(); foreach ($calendars as $calendarId => $calendar) { $retpath = 'kronolith/' . $parts[0] . '/' . $calendarId; @@ -666,7 +667,7 @@ class Kronolith_Api extends Horde_Registry_Api } } } - + if (count($ids) == 0) { throw new Kronolith_Exception(_("No iCalendar data was found.")); } else if (count($ids) == 1) { @@ -1330,6 +1331,7 @@ class Kronolith_Api extends Horde_Registry_Api if (!empty($event)) { $uid = $calendar . ':' . $event; } + return $GLOBALS['kronolith_shares']->getShare($calendar)->lock($GLOBALS['injector']->getInstance('Horde_Lock'), $uid); } diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index cce5823aa..a597ad0bc 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -966,7 +966,7 @@ class Kronolith_Driver_Sql extends Kronolith_Driver } try { - $shares = $GLOBALS['kronolith_shares']->listShares($user, Horde_Perms::EDIT); + $shares = $GLOBALS['kronolith_shares']->listShares($user, array('perm' => Horde_Perms::EDIT)); } catch (Horde_Share_Exception $e) { Horde::logMessage($shares, 'ERR'); throw new Kronolith_Exception($shares); diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index c4103fd72..8dde0a933 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -1159,7 +1159,7 @@ class Kronolith if (trim($name) == '') { $name = $GLOBALS['registry']->getAuth('original'); } - $share = &$GLOBALS['kronolith_shares']->newShare($GLOBALS['registry']->getAuth()); + $share = $GLOBALS['kronolith_shares']->newShare($GLOBALS['registry']->getAuth(), $GLOBALS['registry']->getAuth()); $share->set('name', sprintf(_("%s's Calendar"), $name)); $GLOBALS['kronolith_shares']->addShare($share); $GLOBALS['all_calendars'][$GLOBALS['registry']->getAuth()] = new Kronolith_Calendar_Internal(array('share' => $share)); @@ -1519,7 +1519,11 @@ class Kronolith } try { - $calendars = $GLOBALS['kronolith_shares']->listShares($GLOBALS['registry']->getAuth(), $permission, $owneronly ? $GLOBALS['registry']->getAuth() : null, 0, 0, 'name'); + $calendars = $GLOBALS['kronolith_shares']->listShares( + $GLOBALS['registry']->getAuth(), + array('perm' => $permission, + 'attributes' => $owneronly ? $GLOBALS['registry']->getAuth() : null, + 'sort_by' => 'name')); } catch (Horde_Share_Exception $e) { Horde::logMessage($e, 'ERR'); return array(); @@ -1631,7 +1635,7 @@ class Kronolith public static function addShare($info) { try { - $calendar = $GLOBALS['kronolith_shares']->newShare(strval(new Horde_Support_Randomid())); + $calendar = $GLOBALS['kronolith_shares']->newShare($GLOBALS['registry']->getAuth(), strval(new Horde_Support_Randomid())); } catch (Horde_Share_Exception $e) { throw new Kronolith_Exception($e); } @@ -1646,7 +1650,7 @@ class Kronolith $tagger->tag($calendar->getName(), $info['tags'], $calendar->get('owner'), 'calendar'); try { - $result = $GLOBALS['kronolith_shares']->addShare($calendar); + $GLOBALS['kronolith_shares']->addShare($calendar); } catch (Horde_Share_Exception $e) { throw new Kronolith_Exception($e); } diff --git a/kronolith/perms.php b/kronolith/perms.php index 7d27aa22e..fd7f5e2a8 100644 --- a/kronolith/perms.php +++ b/kronolith/perms.php @@ -32,7 +32,7 @@ case 'edit': try { $share = $shares->getShareById(Horde_Util::getFormData('cid')); $perm = $share->getPermission(); - } catch (Horde_Share_Exception $e) { + } catch (Horde_Exception_NotFound $e) { if (($category = Horde_Util::getFormData('share')) !== null) { try { $share = $shares->getShare($category); diff --git a/kronolith/scripts/agenda.php b/kronolith/scripts/agenda.php index fe926c7f9..1624dbadc 100755 --- a/kronolith/scripts/agenda.php +++ b/kronolith/scripts/agenda.php @@ -80,18 +80,25 @@ function send_agendas() // If we found an email address, generate the agenda. switch ($agenda_calendars) { case 'owner': - $calendars = $GLOBALS['kronolith_shares']->listShares($user, Horde_Perms::SHOW, $user); + $calendars = $GLOBALS['kronolith_shares']->listShares( + $user, + array('perm' => Horde_Perms::SHOW, + 'attributes' => $user)); break; case 'read': - $calendars = $GLOBALS['kronolith_shares']->listShares($user, Horde_Perms::SHOW, null); + $calendars = $GLOBALS['kronolith_shares']->listShares( + $user, + array('perm' => Horde_Perms::SHOW)); break; case 'show': default: $calendars = array(); $shown_calendars = unserialize($prefs->getValue('display_cals')); - $cals = $GLOBALS['kronolith_shares']->listShares($user, Horde_Perms::SHOW, null); + $cals = $GLOBALS['kronolith_shares']->listShares( + $user, + array('perm' => Horde_Perms::SHOW)); foreach ($cals as $calId => $cal) { if (in_array($calId, $shown_calendars)) { $calendars[$calId] = $cal; diff --git a/kronolith/scripts/import_squirrelmail_calendar.php b/kronolith/scripts/import_squirrelmail_calendar.php index b0ace2583..a75e6a044 100755 --- a/kronolith/scripts/import_squirrelmail_calendar.php +++ b/kronolith/scripts/import_squirrelmail_calendar.php @@ -44,7 +44,7 @@ while ($row = $handle->fetchRow(DB_FETCHMODE_ASSOC)) { $user = $row['owner_name']; Horde_Auth::setAuth($user, array()); $cli->message('Creating calendar ' . $row['name']); - $share = $kronolith_shares->newShare($row['id']); + $share = $kronolith_shares->newShare($GLOBALS['registry']->getAuth(), $row['id']); $share->set('name', $row['name']); $kronolith_shares->addShare($share); diff --git a/mnemo/lib/Forms/CreateNotepad.php b/mnemo/lib/Forms/CreateNotepad.php index 8c48a506a..13ad7e2b3 100644 --- a/mnemo/lib/Forms/CreateNotepad.php +++ b/mnemo/lib/Forms/CreateNotepad.php @@ -40,7 +40,7 @@ class Mnemo_CreateNotepadForm extends Horde_Form { { // Create new share. try { - $notepad = $GLOBALS['mnemo_shares']->newShare(strval(new Horde_Support_Uuid())); + $notepad = $GLOBALS['mnemo_shares']->newShare($GLOBALS['registry']->getAuth(), strval(new Horde_Support_Uuid())); } catch (Horde_Share_Exception $e) { Horde::logMessage($e->getMessage(), 'ERR'); throw new Mnemo_Exception($e); diff --git a/mnemo/lib/Forms/DeleteNotepad.php b/mnemo/lib/Forms/DeleteNotepad.php index 5fe7d4f74..625783211 100644 --- a/mnemo/lib/Forms/DeleteNotepad.php +++ b/mnemo/lib/Forms/DeleteNotepad.php @@ -80,7 +80,7 @@ class Mnemo_DeleteNotepadForm extends Horde_Form { $name = $GLOBALS['registry']->getAuth(); } try { - $notepad = $GLOBALS['mnemo_shares']->newShare($GLOBALS['registry']->getAuth()); + $notepad = $GLOBALS['mnemo_shares']->newShare($GLOBALS['registry']->getAuth(), $GLOBALS['registry']->getAuth()); } catch (Horde_Share_Exception $e) { return; } diff --git a/mnemo/lib/Mnemo.php b/mnemo/lib/Mnemo.php index f37dde8e3..88c9094d4 100644 --- a/mnemo/lib/Mnemo.php +++ b/mnemo/lib/Mnemo.php @@ -171,7 +171,11 @@ class Mnemo { return array(); } try { - $notepads = $GLOBALS['mnemo_shares']->listShares($GLOBALS['registry']->getAuth(), $permission, $owneronly ? $GLOBALS['registry']->getAuth() : null, 0, 0, 'name'); + $notepads = $GLOBALS['mnemo_shares']->listShares( + $GLOBALS['registry']->getAuth(), + array('perm' => $permission, + 'attributes' => $owneronly ? $GLOBALS['registry']->getAuth() : null, + 'sort_by' => 'name')); } catch (Horde_Share_Exception $e) { Horde::logMessage($e->getMessage(), 'ERR'); return array(); @@ -448,7 +452,7 @@ class Mnemo { if (trim($name) == '') { $name = $GLOBALS['registry']->getAuth(); } - $share = $GLOBALS['mnemo_shares']->newShare($GLOBALS['registry']->getAuth()); + $share = $GLOBALS['mnemo_shares']->newShare($GLOBALS['registry']->getAuth(), $GLOBALS['registry']->getAuth()); $share->set('name', sprintf(_("%s's Notepad"), $name)); $GLOBALS['mnemo_shares']->addShare($share); diff --git a/nag/lib/Api.php b/nag/lib/Api.php index 271aa2730..5c9ee865d 100644 --- a/nag/lib/Api.php +++ b/nag/lib/Api.php @@ -243,9 +243,10 @@ class Nag_Api extends Horde_Registry_Api // // This request is for all tasklists owned by the requested user // - $tasklists = $GLOBALS['nag_shares']->listShares($parts[0], - Horde_Perms::SHOW, - $parts[0]); + $tasklists = $GLOBALS['nag_shares']->listShares( + $parts[0], + array('perm' => Horde_Perms::SHOW, + 'attributes' => $parts[0])); // The last check returns all addressbooks for the requested user, // but that does not mean the requesting user has access to them. diff --git a/nag/lib/Nag.php b/nag/lib/Nag.php index 15b700cef..41a784165 100644 --- a/nag/lib/Nag.php +++ b/nag/lib/Nag.php @@ -374,7 +374,10 @@ class Nag return array(); } try { - $tasklists = $GLOBALS['nag_shares']->listShares($GLOBALS['registry']->getAuth(), $permission, $owneronly ? $GLOBALS['registry']->getAuth() : null, 0, 0, 'name'); + $tasklists = $GLOBALS['nag_shares']->listShares( + $GLOBALS['registry']->getAuth(), + array('perm' => $permission, $owneronly ? $GLOBALS['registry']->getAuth() : null, + 'sort_by' => 'name')); } catch (Horde_Share_Exception $e) { Horde::logMessage($e->getMessage(), 'ERR'); return array(); @@ -445,7 +448,7 @@ class Nag public static function addTasklist($info) { try { - $tasklist = $GLOBALS['nag_shares']->newShare(strval(new Horde_Support_Uuid())); + $tasklist = $GLOBALS['nag_shares']->newShare($GLOBALS['registry']->getAuth(), strval(new Horde_Support_Uuid())); $tasklist->set('name', $info['name']); $tasklist->set('color', $info['color']); $tasklist->set('desc', $info['description']); @@ -738,7 +741,7 @@ class Nag if (trim($name) == '') { $name = $GLOBALS['registry']->getAuth('original'); } - $share = $GLOBALS['nag_shares']->newShare($GLOBALS['registry']->getAuth()); + $share = $GLOBALS['nag_shares']->newShare($GLOBALS['registry']->getAuth(), $GLOBALS['registry']->getAuth()); $share->set('name', sprintf(_("%s's Task List"), $name)); $GLOBALS['nag_shares']->addShare($share); diff --git a/nag/mobile.php b/nag/mobile.php index 7db040985..0e4624428 100644 --- a/nag/mobile.php +++ b/nag/mobile.php @@ -80,7 +80,7 @@ if ($tasks->hasTasks()) { if ($task->tasklist == '**EXTERNAL**') { // Just use a new share that this user owns for tasks from // external calls - if the API gives them back, we'll trust it. - $share = $GLOBALS['nag_shares']->newShare('**EXTERNAL**'); + $share = $GLOBALS['nag_shares']->newShare($GLOBALS['registry']->getAuth(), '**EXTERNAL**'); $owner = $task->tasklist_name; } else { try { diff --git a/nag/templates/list.html.php b/nag/templates/list.html.php index 2cc68fb4e..a1b1dd579 100644 --- a/nag/templates/list.html.php +++ b/nag/templates/list.html.php @@ -40,7 +40,7 @@ if ($tasks->hasTasks()) { if ($task->tasklist == '**EXTERNAL**') { // Just use a new share that this user owns for tasks from // external calls - if the API gives them back, we'll trust it. - $share = $GLOBALS['nag_shares']->newShare('**EXTERNAL**'); + $share = $GLOBALS['nag_shares']->newShare($GLOBALS['registry']->getAuth(), '**EXTERNAL**'); $owner = $task->tasklist_name; } else { try { diff --git a/turba/lib/Api.php b/turba/lib/Api.php index 5067ad96f..4dc9e4ba9 100644 --- a/turba/lib/Api.php +++ b/turba/lib/Api.php @@ -261,9 +261,10 @@ class Turba_Api extends Horde_Registry_Api // No backends are configured to provide shares return array(); } - $addressbooks = $GLOBALS['turba_shares']->listShares($parts[0], - Horde_Perms::READ, - $parts[0]); + $addressbooks = $GLOBALS['turba_shares']->listShares( + $parts[0], + array('perm' => Horde_Perms::READ, + 'attributes' => $parts[0])); // The last check returns all addressbooks for the requested user, // but that does not mean the requesting user has access to them. // Filter out those address books for which the requesting user has diff --git a/turba/lib/Application.php b/turba/lib/Application.php index 5a13ac624..443f02338 100644 --- a/turba/lib/Application.php +++ b/turba/lib/Application.php @@ -433,7 +433,10 @@ class Turba_Application extends Horde_Registry_Application return; } - $shares = $GLOBALS['turba_shares']->listShares($user, Horde_Perms::EDIT, $user); + $shares = $GLOBALS['turba_shares']->listShares( + $user, + array('perm' => Horde_Perms::EDIT, + 'attributes' => $user)); /* Look for the deleted user's default share and remove it */ foreach ($shares as $share) { diff --git a/turba/lib/Turba.php b/turba/lib/Turba.php index 4e6b4c48f..2c97541db 100644 --- a/turba/lib/Turba.php +++ b/turba/lib/Turba.php @@ -537,8 +537,9 @@ class Turba { try { $sources = $GLOBALS['turba_shares']->listShares( - $GLOBALS['registry']->getAuth(), $permission, - $owneronly ? $GLOBALS['registry']->getAuth() : null); + $GLOBALS['registry']->getAuth(), + array('perm' => $permission, + 'attributes' => $owneronly ? $GLOBALS['registry']->getAuth() : null)); } catch (Horde_Share_Exception $e) { Horde::logMessage($e, 'ERR'); return array(); @@ -573,7 +574,7 @@ class Turba { /* Generate the new share. */ try { - $share = $GLOBALS['turba_shares']->newShare($share_id); + $share = $GLOBALS['turba_shares']->newShare($GLOBALS['registry']->getAuth(), $share_id); /* Set the display name for this share. */ $share->set('name', $name); diff --git a/turba/scripts/upgrades/public_to_horde_share.php b/turba/scripts/upgrades/public_to_horde_share.php index db0b6c1f0..443f0056a 100755 --- a/turba/scripts/upgrades/public_to_horde_share.php +++ b/turba/scripts/upgrades/public_to_horde_share.php @@ -59,7 +59,7 @@ if (!$sourceKey) { // Create the new share. $owner_uid = strval(new Horde_Support_Randomid()); -$share = &$turba_shares->newShare($sourceKey . ':' . $owner_uid); +$share = &$turba_shares->newShare($GLOBALS['registry']->getAuth(), $sourceKey . ':' . $owner_uid); if ($share instanceof Horde_Share_Exception) { var_dump($share); exit; diff --git a/whups/lib/Query.php b/whups/lib/Query.php index ee632233b..de835c205 100644 --- a/whups/lib/Query.php +++ b/whups/lib/Query.php @@ -891,7 +891,7 @@ class Whups_QueryManager { { try { $share = $this->_shareManager->getShareById($queryId); - } catch (Horde_Share_Exception $e) { + } catch (Horde_Exception_NotFound $e) { throw new Whups_Exception($e); } return $this->_getQuery($share); @@ -908,8 +908,10 @@ class Whups_QueryManager { function getQueryBySlug($slug) { try { - $shares = $this->_shareManager->listShares($GLOBALS['registry']->getAuth(), Horde_Perms::READ, - array('slug' => $slug)); + $shares = $this->_shareManager->listShares( + $GLOBALS['registry']->getAuth(), + array('perm' => Horde_Perms::READ, + 'attribtues' => array('slug' => $slug))); } catch (Horde_Share_Exception $e) { throw new Whups_Exception($e); } @@ -955,7 +957,7 @@ class Whups_QueryManager { { try { $share = $this->_shareManager->getShareById($queryId); - } catch (Horde_Share_Exception $e) { + } catch (Horde_Exception_NotFound $e) { // If the share doesn't exist yet, then it has open perms. return true; } @@ -1002,7 +1004,7 @@ class Whups_QueryManager { // if necessary. try { $share = $this->_shareManager->getShareById($query->id); - } catch (Horde_Share_Exception $e) { + } catch (Horde_Exception_NotFound $e) { // Share has an id but doesn't exist; just throw an // error. throw new Whups_Exception($e); @@ -1015,7 +1017,7 @@ class Whups_QueryManager { } } else { // Create a new share for the query. - $share = $this->_shareManager->newShare((string)new Horde_Support_Uuid()); + $share = $this->_shareManager->newShare($GLOBALS['registry']->getAuth(), (string)new Horde_Support_Uuid()); $share->set('name', $query->name); $share->set('slug', $query->slug); try { @@ -1043,7 +1045,7 @@ class Whups_QueryManager { try { $share = $this->_shareManager->getShareById($query->id); $this->_shareManager->removeShare($share); - } catch (Horde_Share_Exception $e) { + } catch (Exception $e) { throw new Whups_Exception($e); } $result = $GLOBALS['whups_driver']->deleteQuery($query->id); -- 2.11.0