$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 ======================== */
/**
}
/**
- * 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);
}
/**
- * 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 */
}
/**
- * 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);
$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],
/**
- * 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();
$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 {
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;
}
}
}
/**
- * 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;
}
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