--- sql.orig.php	2010-04-29 12:58:43.000000000 +0200
+++ sql.php	2010-05-31 14:35:36.000000000 +0200
@@ -449,10 +449,12 @@
             $sortfield = 's.attribute_' . $sort_by;
         }
 
+		// Ibon. bcziglii. 20100524
+		// In order to get working the changes in _getShareCriteria we have to comment out the ORDER BY part of the query
         $query = 'SELECT DISTINCT s.* '
-            . $this->_getShareCriteria($userid, $perm, $attributes)
-            . ' ORDER BY ' . $sortfield
-            . (($direction == 0) ? ' ASC' : ' DESC');
+            . $this->_getShareCriteria($userid, $perm, $attributes);
+        //    . ' ORDER BY ' . $sortfield
+        //    . (($direction == 0) ? ' ASC' : ' DESC');
         if ($from > 0 || $count > 0) {
             $this->_db->setLimit($count, $from);
         }
@@ -664,25 +666,70 @@
         $query = ' FROM ' . $this->_table . ' s ';
         $where = '';
 
+		/*
+		/ Ibon. bcziglii. 20100524. This function is generating the problematic slow queries
+		/ The following patch tries to optimize the way we do the query. 
+		/ We can do it because in our horde instalation we don't allow sharing with all the users (authenticated or not)
+		/ perm_creator, perm_default and perm_guest will always be zero in our database
+		/ We are changing the original LEFT JOIN with a UNION
+		/ At the moment we ONLY change the query if the user is not part of any group and if we don't have $attributes
+		/
+		/ Original query:
+			SELECT DISTINCT s.*  
+			FROM nag_shares s  
+			LEFT JOIN nag_shares_users AS u ON u.share_id = s.share_id 
+			WHERE s.share_owner = 'username' 
+			OR (s.perm_creator & 2) 
+			OR (s.perm_default & 2) 
+			OR ( u.user_uid = 'username' AND (u.perm & 2)) 
+			ORDER BY s.attribute_name ASC;
+		/
+		/ Inproved query (written by David Fernandez -sysadmin at the UPV/EHU-
+		/ based on the patch sent by John Madden to the kronolith mailing list
+			SELECT DISTINCT s.*
+			FROM nag_shares s
+			WHERE s.share_owner = 'username'
+			UNION
+			SELECT s2.*
+			FROM nag_shares s2 RIGHT JOIN nag_shares_users u ON s2.share_id = u.share_id
+			WHERE ( u.user_uid = 'username' AND (u.perm & 2))
+		*/
+
+		// Ibon. bcziglii. We are moving the following 2 lines because we check the $attributes var before the original code does
+        $attributes = $this->_toDriverKeys($attributes);
+        $attributes = $this->_toDriverCharset($attributes);
+
         if (!empty($userid)) {
             // (owner == $userid)
             $where .= 's.share_owner = ' . $this->_write_db->quote($userid);
 
             // (name == perm_creator and val & $perm)
-            $where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_creator', '&', $perm) . ')';
+			// Ibon - we don't need it
+            //$where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_creator', '&', $perm) . ')';
 
             // (name == perm_creator and val & $perm)
-            $where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_default',  '&', $perm) . ')';
-
-            // (name == perm_users and key == $userid and val & $perm)
-            $query .= ' LEFT JOIN ' . $this->_table . '_users AS u ON u.share_id = s.share_id';
-            $where .= ' OR ( u.user_uid = ' .  $this->_write_db->quote($userid)
-                . ' AND (' . Horde_SQL::buildClause($this->_db, 'u.perm', '&', $perm) . '))';
+			// Ibon - we don't need it
+            //$where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_default',  '&', $perm) . ')';
 
             // If the user has any group memberships, check for those also.
             require_once 'Horde/Group.php';
             $group = &Group::singleton();
             $groups = $group->getGroupMemberships($userid, true);
+
+			// Ibon - if the user is not part of any group and if we don't have $attributes - we create a better query:
+			if ( !$groups && !is_array($attributes) && empty($attributes) ) {
+				$where .= " UNION SELECT s2.* 
+								FROM ".$this->_table." s2 RIGHT JOIN ".$this->_table."_users u ON s2.share_id = u.share_id
+								WHERE ( u.user_uid = ".$this->_write_db->quote($userid)." AND (u.perm & $perm))";
+			} else {
+				// (name == perm_users and key == $userid and val & $perm)
+				// Ibon - original query for the rest of cases
+				$query .= ' LEFT JOIN ' . $this->_table . '_users AS u ON u.share_id = s.share_id';
+				$where .= ' OR ( u.user_uid = ' .  $this->_write_db->quote($userid)
+					. ' AND (' . Horde_SQL::buildClause($this->_db, 'u.perm', '&', $perm) . '))';			
+			}
+			
+
             if (!is_a($groups, 'PEAR_Error') && $groups) {
                 // (name == perm_groups and key in ($groups) and val & $perm)
                 $ids = array_keys($groups);
@@ -700,9 +747,6 @@
             $where = '(' . Horde_SQL::buildClause($this->_db, 's.perm_guest', '&', $perm) . ')';
         }
 
-        $attributes = $this->_toDriverKeys($attributes);
-        $attributes = $this->_toDriverCharset($attributes);
-
         if (is_array($attributes)) {
             // Build attribute/key filter.
             $where = ' (' . $where . ') ';