Allow lookups by fields other than nickname; LDAP backend needs to have this implemen...
authorpdontthink <pdontthink@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Thu, 19 Jun 2008 01:07:37 +0000 (01:07 +0000)
committerpdontthink <pdontthink@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Thu, 19 Jun 2008 01:07:37 +0000 (01:07 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@13186 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/abook_database.php
functions/abook_ldap_server.php
functions/abook_local_file.php
functions/addressbook.php
include/constants.php

index 717236ccfa1bea4cb305072f9764e62cd384db3b..8294cf3eaa141c18fe1f0adeff578be6c586c5d2 100644 (file)
@@ -194,6 +194,34 @@ class abook_database extends addressbook_backend {
         $this->dbh = false;
     }
 
+    /**
+     * Determine internal database field name given one of
+     * the SquirrelMail SM_ABOOK_FIELD_* constants
+     *
+     * @param integer $field The SM_ABOOK_FIELD_* contant to look up
+     *
+     * @return string The desired field name, or the string "ERROR"
+     *                if the $field is not understood (the caller
+     *                is responsible for handing errors)
+     *
+     */
+    function get_field_name($field) {
+        switch ($field) {
+            case SM_ABOOK_FIELD_NICKNAME:
+                return 'nickname';
+            case SM_ABOOK_FIELD_FIRSTNAME:
+                return 'firstname';
+            case SM_ABOOK_FIELD_LASTNAME:
+                return 'lastname';
+            case SM_ABOOK_FIELD_EMAIL:
+                return 'email';
+            case SM_ABOOK_FIELD_LABEL:
+                return 'label';
+            default:
+                return 'ERROR';
+        }
+    }
+
     /* ========================== Public ======================== */
 
     /**
@@ -260,23 +288,33 @@ class abook_database extends addressbook_backend {
     }
 
     /**
-     * Lookup alias
-     * @param string $alias alias
-     * @return array search results
+     * Lookup an address by the indicated field.
+     *
+     * @param string  $value The value to look up
+     * @param integer $field The field to look in, should be one
+     *                       of the SM_ABOOK_FIELD_* constants
+     *                       defined in include/constants.php
+     *                       (OPTIONAL; defaults to nickname field)
+     *
+     * @return array Array with lookup results when the value
+     *               was found, an empty array if the value was
+     *               not found.
+     *
      */
-    function lookup($alias) {
-        if (empty($alias)) {
+    function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
+        if (empty($value)) {
             return array();
         }
 
-        $alias = strtolower($alias);
+        $value = strtolower($value);
 
         if (!$this->open()) {
             return false;
         }
 
-        $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
-                         $this->table, $this->owner, $this->dbh->quoteString($alias));
+        $query = sprintf("SELECT * FROM %s WHERE owner = '%s' AND LOWER(%s) = '%s'",
+                         $this->table, $this->owner, $this->get_field_name($field), 
+                         $this->dbh->quoteString($value));
 
         $res = $this->dbh->query($query);
 
index 8b596964932e214c659f505982c9d2fd7818e2b3..d4b472adbc755a6d686da70a8c6f9ba7590c115f 100644 (file)
@@ -722,14 +722,29 @@ class abook_ldap_server extends addressbook_backend {
     }
 
     /**
-     * Lookup an alias
-     * @param string $alias alias
-     * @return array search results
+     * Lookup an address by the indicated field.
+     *
+     * @param string  $value The value to look up
+     * @param integer $field The field to look in, should be one
+     *                       of the SM_ABOOK_FIELD_* constants
+     *                       defined in include/constants.php
+     *                       (OPTIONAL; defaults to nickname field)
+     *
+     * @return array Array with lookup results when the value
+     *               was found, an empty array if the value was
+     *               not found.
+     *
      * @since 1.5.2
+     *
      */
-    function lookup($alias) {
+    function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
+
+//FIXME: implement lookup by other fields
+        if ($field != SM_ABOOK_FIELD_NICKNAME)
+            return $this->set_error('LDAP lookup of fields other than nickname/alias not yet implemented');
+
         /* Generate the dn and try to retrieve that single entry */
-        $cn = $this->quotevalue($alias);
+        $cn = $this->quotevalue($value);
         $dn = 'cn=' . $cn . ',' . $this->basedn;
 
         /* Do the search */
index 401013b526a33f6ce85b6bc44225f15b7de24b53..f72796b496dfb2edb5ea7e12d9ad0a5eda463152 100644 (file)
@@ -317,16 +317,25 @@ class abook_local_file extends addressbook_backend {
     }
 
     /**
-     * Lookup alias
-     * @param string $alias alias
-     * @return array search results
+     * Lookup an address by the indicated field.
+     *
+     * @param string  $value The value to look up
+     * @param integer $field The field to look in, should be one
+     *                       of the SM_ABOOK_FIELD_* constants
+     *                       defined in include/constants.php
+     *                       (OPTIONAL; defaults to nickname field)
+     *
+     * @return array Array with lookup results when the value
+     *               was found, an empty array if the value was
+     *               not found.
+     *
      */
-    function lookup($alias) {
-        if(empty($alias)) {
+    function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
+        if(empty($value)) {
             return array();
         }
 
-        $alias = strtolower($alias);
+        $value = strtolower($value);
 
         $this->open();
         @rewind($this->filehandle);
@@ -341,7 +350,7 @@ class abook_local_file extends addressbook_backend {
                 $oTemplate->display('footer.tpl');
                 die();
             } else {
-                if(strtolower($row[0]) == $alias) {
+                if(strtolower($row[$field]) == $value) {
                    return array('nickname'  => $row[0],
                       'name'      => $this->fullname($row[1], $row[2]),
                       'firstname' => $row[1],
index a802c03adcb13b3a06d1b0d7729c06613e6bdb9e..3b2fe54f4a51931ec0d7793777b36aa9aa662496 100644 (file)
@@ -594,13 +594,25 @@ class AddressBook {
 
 
     /**
-     * Lookup an address by alias.
+     * Lookup an address by the indicated field.
+     *
      * Only possible in local backends.
-     * @param string $alias
-     * @param integer backend number
-     * @return array lookup results. False, if not found.
+     *
+     * @param string  $value The value to look up
+     * @param integer $bnum  The number of the backend to
+     *                       look within (OPTIONAL; defaults 
+     *                       to look in all local backends)
+     * @param integer $field The field to look in, should be one
+     *                       of the SM_ABOOK_FIELD_* constants
+     *                       defined in include/constants.php
+     *                       (OPTIONAL; defaults to nickname field)
+     *
+     * @return mixed Array with lookup results when the value
+     *               was found, an empty array if the value was
+     *               not found, or false if an error occured.
+     *
      */
-    function lookup($alias, $bnum = -1) {
+    function lookup($value, $bnum = -1, $field = SM_ABOOK_FIELD_NICKNAME) {
 
         $ret = array();
 
@@ -609,7 +621,7 @@ class AddressBook {
                 $this->error = _("Unknown address book backend");
                 return false;
             }
-            $res = $this->backends[$bnum]->lookup($alias);
+            $res = $this->backends[$bnum]->lookup($value, $field);
             if (is_array($res)) {
                return $res;
             } else {
@@ -622,13 +634,18 @@ class AddressBook {
         for ($i = 0 ; $i < sizeof($sel) ; $i++) {
             $backend = &$sel[$i];
             $backend->error = '';
-            $res = $backend->lookup($alias);
+            $res = $backend->lookup($value, $field);
+
+            // return an address if one is found
+            // (empty array means lookup concluded
+            // but no result found - in this case,
+            // proceed to next backend)
+            //
             if (is_array($res)) {
-               if(!empty($res))
-              return $res;
+                if (!empty($res)) return $res;
             } else {
-               $this->error = $backend->error;
-               return false;
+                $this->error = $backend->error;
+                return false;
             }
         }
 
@@ -906,11 +923,19 @@ class addressbook_backend {
     }
 
     /**
-     * Find entry in backend by alias
-     * @param string $alias name used for id
-     * @return bool
+     * Find entry in backend by the indicated field
+     *
+     * @param string  $value The value to look up
+     * @param integer $field The field to look in, should be one
+     *                       of the SM_ABOOK_FIELD_* constants
+     *                       defined in include/constants.php
+     *
+     * @return mixed Array with lookup results when the value
+     *               was found, an empty array if the value was
+     *               not found, or false if an error occured.
+     *
      */
-    function lookup($alias) {
+    function lookup($value, $field) {
         $this->set_error('lookup is not implemented');
         return false;
     }
index 0cf49dee7549724dba3ca44cb9635b22faa6d38d..c9ac84312e948a2e4fac65c04ed25dc738c1db49 100644 (file)
@@ -182,6 +182,16 @@ define('SQM_COL_TO', 9);
 define('SQM_COL_CC', 10);
 define('SQM_COL_BCC', 11);
 
+/**
+ * Address book field list
+ * @since 1.4.16 and 1.5.2
+ */
+define('SM_ABOOK_FIELD_NICKNAME', 0);
+define('SM_ABOOK_FIELD_FIRSTNAME', 1);
+define('SM_ABOOK_FIELD_LASTNAME', 2);
+define('SM_ABOOK_FIELD_EMAIL', 3);
+define('SM_ABOOK_FIELD_LABEL', 4);
+
 /**
  * Generic variable type constants
  * @since 1.5.2