/**
* Perform a search of the Global Address Book.
*
* @param array $query A query array. @see self::getSearchResults()
*
* @return array The results array. @see self::getSearchResults()
*/
protected function _searchGal(array $query)
{
ob_start();
$return = array('rows' => array(), 'status' => Horde_ActiveSync_Request_Search::STORE_STATUS_SUCCESS, 'total' => 0);
try {
$results = $this->_connector->contacts_search($query['query'], array('pictures' => !empty($query[Horde_ActiveSync_Request_Search::SEARCH_PICTURE])));
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->err($e);
$this->_endBuffer();
return $return;
}
// Honor range, and don't bother if no results
$results = array_pop($results);
$count = count($results);
if (!$count) {
$this->_endBuffer();
return $return;
}
$return['total'] = $count;
$this->_logger->info(sprintf("[%s] Horde_Core_ActiveSync_Driver::_searchGal() found %d matches.", $this->_pid, $count));
if (!empty($query['range'])) {
preg_match('/(.*)\\-(.*)/', $query['range'], $matches);
$return_count = $matches[2] - $matches[1];
$rows = array_slice($results, $matches[1], $return_count + 1, true);
}
$picture_count = 0;
foreach ($rows as $row) {
// Explicitly disallow returning contact groups since EAS clients
// only expect a SINGLE email address to be returned. Returning
// multiple email addresses, or the group syntax will cause most
// clients to silently throw out all but the first email address in
// the list, or will completely fail to send the message altogether.
if (empty($row['__type']) || $row['__type'] != 'Object') {
continue;
}
$entry = array(Horde_ActiveSync::GAL_ALIAS => !empty($row['alias']) ? $row['alias'] : '', Horde_ActiveSync::GAL_DISPLAYNAME => $row['name'], Horde_ActiveSync::GAL_EMAILADDRESS => !empty($row['email']) ? $row['email'] : '', Horde_ActiveSync::GAL_FIRSTNAME => $row['firstname'], Horde_ActiveSync::GAL_LASTNAME => $row['lastname'], Horde_ActiveSync::GAL_COMPANY => !empty($row['company']) ? $row['company'] : '', Horde_ActiveSync::GAL_HOMEPHONE => !empty($row['homePhone']) ? $row['homePhone'] : '', Horde_ActiveSync::GAL_PHONE => !empty($row['workPhone']) ? $row['workPhone'] : '', Horde_ActiveSync::GAL_MOBILEPHONE => !empty($row['cellPhone']) ? $row['cellPhone'] : '', Horde_ActiveSync::GAL_TITLE => !empty($row['title']) ? $row['title'] : '', Horde_ActiveSync::GAL_OFFICE => !empty($row['office']) ? $row['office'] : '');
if (!empty($query[Horde_ActiveSync_Request_Search::SEARCH_PICTURE])) {
$picture = Horde_ActiveSync::messageFactory('GalPicture');
if (empty($row['photo'])) {
$picture->status = Horde_ActiveSync_Status::NO_PICTURE;
} elseif (!empty($query[Horde_ActiveSync_Request_Search::SEARCH_MAXPICTURES]) && $picture_count > $query[Horde_ActiveSync_Request_Search::SEARCH_MAXPICTURES]) {
$picture->status = Horde_ActiveSync_Status::PICTURE_LIMIT_REACHED;
} elseif (!empty($query[Horde_ActiveSync_Request_Search::SEARCH_MAXSIZE]) && strlen($row['photo']) > $query[Horde_ActiveSync_Request_Search::SEARCH_MAXSIZE]) {
$picture->status = Horde_ActiveSync_Status::PICTURE_TOO_LARGE;
} else {
$picture->data = base64_encode($row['photo']['load']['data']);
$picture->status = Horde_ActiveSync_Status::PICTURE_SUCCESS;
++$picture_count;
}
$entry[Horde_ActiveSync::GAL_PICTURE] = $picture;
}
$return['rows'][] = $entry;
}
$this->_endBuffer();
return $return;
}