Skip to content

Commit

Permalink
Add IMP_Contacts#searchEmail()
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Jul 28, 2014
1 parent a928f6c commit 0df158d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
8 changes: 2 additions & 6 deletions imp/lib/Basic/Contacts.php
Expand Up @@ -55,13 +55,9 @@ protected function _init()
}

if ($this->vars->searched || $prefs->getValue('display_contact')) {
$search_params = $injector->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$a_list = iterator_to_array($registry->call('contacts/search', array($this->vars->get('search', ''), array(
'fields' => $search_params['fields'],
'returnFields' => array('email', 'name'),
'rfc822Return' => true,
$a_list = iterator_to_array($injector->getInstance('IMP_Contacts')->searchEmail($this->vars->get('search', ''), array(
'sources' => array($this->vars->source)
))));
)));
} else {
$a_list = array();
}
Expand Down
47 changes: 47 additions & 0 deletions imp/lib/Contacts.php
Expand Up @@ -55,6 +55,53 @@ public function addAddress($addr, $name)
return $escapeName;
}

/**
* Search the addressbook for email addresses.
*
* @param string $str The search string.
* @param array $opts Additional options:
* - email_exact: (boolean) Require exact match in e-mail?
* - levenshtein: (boolean) Do levenshtein sorting of results?
* - sources: (array) Use this list of sources instead of default.
*
* @return Horde_Mail_Rfc822_List Results.
*/
public function searchEmail($str, array $opts = array())
{
global $registry;

if (!$registry->hasMethod('contacts/search')) {
return new Horde_Mail_Rfc822_List();
}

$spref = $this->getAddressbookSearchParams();

try {
$search = $registry->call('contacts/search', array($str, array(
'customStrict' => empty($opts['email_exact']) ? array() : array('email'),
'fields' => $spref['fields'],
'returnFields' => array('email', 'name'),
'rfc822Return' => true,
'sources' => empty($opts['sources']) ? $spref['sources'] : $opts['sources']
)));
} catch (Horde_Exception $e) {
Horde::log($e, 'ERR');
return new Horde_Mail_Rfc822_List();
}

if (empty($opts['levenshtein'])) {
return $search;
}

$sort_list = array();
foreach ($search->base_addresses as $val) {
$sort_list[strval($val)] = @levenshtein($str, $val);
}
asort($sort_list, SORT_NUMERIC);

return new Horde_Mail_Rfc822_List(array_keys($sort_list));
}

/**
* Determines parameters needed to do an address search
*
Expand Down
36 changes: 12 additions & 24 deletions imp/lib/Images.php
Expand Up @@ -60,7 +60,7 @@ public function showInlineImage(IMP_Contents $contents)
*/
protected function _showInlineImage(IMP_Contents $contents)
{
global $injector, $prefs, $registry;
global $injector, $prefs;

if ($this->alwaysShow || !$prefs->getValue('image_replacement')) {
return true;
Expand All @@ -71,32 +71,20 @@ protected function _showInlineImage(IMP_Contents $contents)
return false;
}

if ($registry->hasMethod('contacts/search')) {
$sparams = $injector->getInstance('IMP_Contacts')->getAddressbookSearchParams();
$res = $injector->getInstance('IMP_Contacts')->searchEmail($from->bare->addresses, array(
'email_exact' => true
));

try {
$res = $registry->call('contacts/search', array($from->bare_addresses, array(
'customStrict' => array('email'),
'fields' => array_fill_keys($sparams['sources'], array('email')),
'returnFields' => array('email'),
'rfc822Return' => true,
'sources' => $sparams['sources']
)));
if (count($res)) {
/* Don't allow personal addresses by default - this is the only
* e-mail address a Spam sender for sure knows you will recognize
* so it is too much of a loophole. */
$res->setIteratorFilter(0, $injector->getInstance('IMP_Identity')->getAllFromAddresses());

// Don't allow personal addresses by default - this is the
// only e-mail address a Spam sender for sure knows you will
// recognize so it is too much of a loophole.
$res->setIteratorFilter(0, $injector->getInstance('IMP_Identity')->getAllFromAddresses());

foreach ($from as $val) {
if ($res->contains($val)) {
return true;
}
foreach ($from as $val) {
if ($res->contains($val)) {
return true;
}
} catch (Horde_Exception $e) {
// Ignore errors from the search - default to not showing
// images.
Horde::log($e, 'INFO');
}
}

Expand Down

0 comments on commit 0df158d

Please sign in to comment.