From 503c7650d1e4178c3c7888d95dcae3777a93861e Mon Sep 17 00:00:00 2001 From: pdontthink Date: Thu, 19 Jun 2008 01:07:37 +0000 Subject: [PATCH] Allow lookups by fields other than nickname; LDAP backend needs to have this implemented if poss git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@13186 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/abook_database.php | 54 +++++++++++++++++++++++++++----- functions/abook_ldap_server.php | 25 ++++++++++++--- functions/abook_local_file.php | 23 +++++++++----- functions/addressbook.php | 55 ++++++++++++++++++++++++--------- include/constants.php | 10 ++++++ 5 files changed, 132 insertions(+), 35 deletions(-) diff --git a/functions/abook_database.php b/functions/abook_database.php index 717236cc..8294cf3e 100644 --- a/functions/abook_database.php +++ b/functions/abook_database.php @@ -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); diff --git a/functions/abook_ldap_server.php b/functions/abook_ldap_server.php index 8b596964..d4b472ad 100644 --- a/functions/abook_ldap_server.php +++ b/functions/abook_ldap_server.php @@ -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 */ diff --git a/functions/abook_local_file.php b/functions/abook_local_file.php index 401013b5..f72796b4 100644 --- a/functions/abook_local_file.php +++ b/functions/abook_local_file.php @@ -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], diff --git a/functions/addressbook.php b/functions/addressbook.php index a802c03a..3b2fe54f 100644 --- a/functions/addressbook.php +++ b/functions/addressbook.php @@ -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; } diff --git a/include/constants.php b/include/constants.php index 0cf49dee..c9ac8431 100644 --- a/include/constants.php +++ b/include/constants.php @@ -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 -- 2.25.1