[Pkg-owncloud-commits] [php-sabredav] 03/80: findByUri in PDO.php, and anyof/allof support in searchPrincipals

David Prévot taffit at moszumanska.debian.org
Thu Jan 7 02:56:13 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit e4deb2c87d89cf194ee5fd27518d30e1e345652a
Author: vmire <vincent_257 at mireau.com>
Date:   Tue Jul 14 23:08:27 2015 +0200

    findByUri in PDO.php, and anyof/allof support in searchPrincipals
---
 lib/DAVACL/PrincipalBackend/AbstractBackend.php |  7 +++
 lib/DAVACL/PrincipalBackend/PDO.php             | 65 ++++++++++++++++++++++---
 2 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/lib/DAVACL/PrincipalBackend/AbstractBackend.php b/lib/DAVACL/PrincipalBackend/AbstractBackend.php
index 5a28a28..10dd97b 100644
--- a/lib/DAVACL/PrincipalBackend/AbstractBackend.php
+++ b/lib/DAVACL/PrincipalBackend/AbstractBackend.php
@@ -44,6 +44,13 @@ abstract class AbstractBackend implements BackendInterface {
             ['{http://sabredav.org/ns}email-address' => substr($uri, 7)]
         );
 
+		//searchPrincipals did the search with wildcards (%email%)
+		//here, we only want exact matches
+		for($i=0;$i<count($result);$i++){
+			$uri = $result[$i];
+			error_log("Principal URI: ".$uri);
+		}
+
         if ($result) {
             return $result[0];
         }
diff --git a/lib/DAVACL/PrincipalBackend/PDO.php b/lib/DAVACL/PrincipalBackend/PDO.php
index e16e6c3..2ff773f 100644
--- a/lib/DAVACL/PrincipalBackend/PDO.php
+++ b/lib/DAVACL/PrincipalBackend/PDO.php
@@ -240,26 +240,26 @@ class PDO extends AbstractBackend implements CreatePrincipalSupport {
      * @return array
      */
     function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
+		if(count($searchProperties) == 0) return [];	//No criteria
 
-        $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE 1=1 ';
+        $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE ';
         $values = [];
         foreach ($searchProperties as $property => $value) {
-
+			$column;
             switch ($property) {
-
                 case '{DAV:}displayname' :
-                    $query .= ' AND displayname LIKE ?';
-                    $values[] = '%' . $value . '%';
+					$column = "displayname";
                     break;
                 case '{http://sabredav.org/ns}email-address' :
-                    $query .= ' AND lower(email) = lower(?)';
-                    $values[] = $value;
+					$column = "email";
                     break;
                 default :
                     // Unsupported property
                     return [];
-
             }
+			if(count($values) > 0) $query .= (strcmp($test,"anyof")==0 ? " OR ":" AND ");
+            $query .= 'lower('.$column.') LIKE lower(?)';
+            $values[] = '%' . $value . '%';
 
         }
         $stmt = $this->pdo->prepare($query);
@@ -280,6 +280,55 @@ class PDO extends AbstractBackend implements CreatePrincipalSupport {
 
     }
 
+	/**
+     * Finds a principal by its URI.
+     *
+     * This method may receive any type of uri, but mailto: addresses will be
+     * the most common.
+     *
+     * Implementation of this API is optional. It is currently used by the
+     * CalDAV system to find principals based on their email addresses. If this
+     * API is not implemented, some features may not work correctly.
+     *
+     * This method must return a relative principal path, or null, if the
+     * principal was not found or you refuse to find it.
+     *
+     * @param string $uri
+     * @param string $principalPrefix
+     * @return string
+     */
+	function findByUri($uri, $principalPrefix) {
+		$idx = strpos($uri,":");
+		if($idx===FALSE){
+			return null;
+		}
+		
+		$scheme = substr($uri,0,$idx);
+		$value = substr($uri,$idx+1);
+
+		$uri = null;
+		switch($scheme){
+			case "mailto":
+				$query = 'SELECT uri FROM ' . $this->tableName . ' WHERE lower(email)=lower(?)';
+				$stmt = $this->pdo->prepare($query);
+		        $stmt->execute([ $value ]);
+	        
+	        	while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+	           		// Checking if the principal is in the prefix
+	           		list($rowPrefix) = URLUtil::splitPath($row['uri']);
+					if($rowPrefix !== $principalPrefix) continue;
+					
+	            	$uri = $row['uri'];
+					break; //Stop on first match
+				}
+				break;
+			default:
+				//unsupported uri scheme
+				return null;
+		}
+		return $uri;
+    }
+
     /**
      * Returns the list of members for a group-principal
      *

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list