Skip to content

Commit

Permalink
[mms] Fix search in Facebook driver.
Browse files Browse the repository at this point in the history
Search query can contain nested 'ANDs' and 'ORs', so that needs to be
handled.

Additionally, cache key was completely broken since implode() won't work
on the multi-level $criteria array.
  • Loading branch information
slusarz committed Mar 25, 2014
1 parent 078255c commit 4bc08db
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
1 change: 1 addition & 0 deletions turba/docs/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
v4.2.0-git
----------

[mms] Fix search in Facebook driver.
[jan] Add script to import contacts from Open-Xchange.
[jan] Don't remove missing contacts from distributions lists in Kolab backends
(Bug #12861).
Expand Down
90 changes: 50 additions & 40 deletions turba/lib/Driver/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,63 +82,73 @@ public function hasPermission($perm)
*/
protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
{
$results = array();
$key = implode('|', array(
'turba_fb_search',
$GLOBALS['registry']->getAuth(),
md5(json_encode($criteria) . '_' .
implode('.' , $fields) . '_' .
implode('.', $blobFields))
));

$key = 'turba_fb_search|' . $GLOBALS['registry']->getAuth() . '|' . md5(implode('.', $criteria) . '_' . implode('.' , $fields) . '_' . implode('.', $blobFields));
if ($values = $this->_cache->get($key, 3600)) {
$values = json_decode($values, true);
return $count_only ? count($values) : $values;
}

$results = array();
foreach ($this->_getAddressBook($fields) as $key => $contact) {
// No search criteria, return full list.
if (!count($criteria)) {
// If no search criteria, return full list.
if (!count($criteria) ||
$this->_criteriaFound($criteria, $contact)) {
$results[$key] = $contact;
continue;
}
foreach ($criteria as $op => $vals) {
if ($op == 'AND') {
if (!count($vals)) {
$found = false;
} else {
$found = true;
foreach ($vals as $val) {
if (isset($contact[$val['field']])) {
switch ($val['op']) {
case 'LIKE':
if (stristr($contact[$val['field']], $val['test']) === false) {
$found = false;
break 2;
}
}
}
}
}
$this->_cache->set($key, json_encode($results));

return $count_only ? count($results) : $results;
}

/**
*/
protected function _criteriaFound($criteria, $contact, $and = false)
{
foreach ($criteria as $key => $val) {
switch (strval($key)) {
case 'AND':
return count($val)
? $this->_criteriaFound($val, $contact, true)
: false;

case 'OR':
return $this->_criteriaFound($val, $contact, false);

default:
if (!isset($val['field'])) {
if ($this->_criteriaFound($val, $contact)) {
if (!$and) {
return true;
}
} elseif ($and) {
return false;
}
} elseif ($op == 'OR') {
$found = false;
foreach ($vals as $val) {
if (isset($contact[$val['field']])) {
switch ($val['op']) {
case 'LIKE':
if (empty($val['test']) ||
stristr($contact[$val['field']], $val['test']) !== false) {
$found = true;
break 2;
}
} elseif (isset($contact[$val['field']])) {
switch ($val['op']) {
case 'LIKE':
if (stristr($contact[$val['field']], $val['test']) === false) {
if ($and) {
return false;
}
} elseif (!$and) {
return true;
}
break;
}
} else {
$found = false;
}
}
if ($found) {
$results[$key] = $contact;
break;
}
}
$this->_cache->set($key, json_encode($results));

return $count_only ? count($results) : $results;
return $and;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions turba/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
</stability>
<license uri="http://www.horde.org/licenses/apache">ASL</license>
<notes>
* [mms] Fix search in Facebook driver.
* [jan] Add script to import contacts from Open-Xchange.
* [jan] Don&apos;t remove missing contacts from distributions lists in Kolab backends (Bug #12861).
* [jan] Support binary fields (photos) in Kolab backends.
Expand Down Expand Up @@ -1823,6 +1824,7 @@
<date>2014-03-14</date>
<license uri="http://www.horde.org/licenses/apache">ASL</license>
<notes>
* [mms] Fix search in Facebook driver.
* [jan] Add script to import contacts from Open-Xchange.
* [jan] Don&apos;t remove missing contacts from distributions lists in Kolab backends (Bug #12861).
* [jan] Support binary fields (photos) in Kolab backends.
Expand Down

0 comments on commit 4bc08db

Please sign in to comment.