Search a mailbox.
public search ( mixed $mailbox, Horde_Imap_Client_Search_Query $query = null, array $options = [] ) : array | ||
$mailbox | mixed | The mailbox to search. Either a Horde_Imap_Client_Mailbox object or a string (UTF-8). |
$query | Horde_Imap_Client_Search_Query | The search query. Defaults to an ALL search. |
$options | array | Additional options:
- nocache: (boolean) Don't cache the results. DEFAULT: false (results cached, if possible) - partial: (mixed) The range of results to return (message sequence numbers) Only a single range is supported (represented by the minimum and maximum values contained in the range given). DEFAULT: All messages are returned. - results: (array) The data to return. Consists of zero or more of the following flags: - Horde_Imap_Client::SEARCH_RESULTS_COUNT - Horde_Imap_Client::SEARCH_RESULTS_MATCH (DEFAULT) - Horde_Imap_Client::SEARCH_RESULTS_MAX - Horde_Imap_Client::SEARCH_RESULTS_MIN - Horde_Imap_Client::SEARCH_RESULTS_SAVE - Horde_Imap_Client::SEARCH_RESULTS_RELEVANCY - sequence: (boolean) If true, returns an array of sequence numbers. DEFAULT: Returns an array of UIDs - sort: (array) Sort the returned list of messages. Multiple sort criteria can be specified. Any sort criteria can be sorted in reverse order (instead of the default ascending order) by adding a Horde_Imap_Client::SORT_REVERSE element to the array directly before adding the sort element. The following sort criteria are available: - Horde_Imap_Client::SORT_ARRIVAL - Horde_Imap_Client::SORT_CC - Horde_Imap_Client::SORT_DATE - Horde_Imap_Client::SORT_DISPLAYFROM On servers that don't support SORT=DISPLAY, this criteria will fallback to doing client-side sorting. - Horde_Imap_Client::SORT_DISPLAYFROM_FALLBACK On servers that don't support SORT=DISPLAY, this criteria will fallback to Horde_Imap_Client::SORT_FROM [since 2.4.0]. - Horde_Imap_Client::SORT_DISPLAYTO On servers that don't support SORT=DISPLAY, this criteria will fallback to doing client-side sorting. - Horde_Imap_Client::SORT_DISPLAYTO_FALLBACK On servers that don't support SORT=DISPLAY, this criteria will fallback to Horde_Imap_Client::SORT_TO [since 2.4.0]. - Horde_Imap_Client::SORT_FROM - Horde_Imap_Client::SORT_SEQUENCE - Horde_Imap_Client::SORT_SIZE - Horde_Imap_Client::SORT_SUBJECT - Horde_Imap_Client::SORT_TO [On servers that support SEARCH=FUZZY, this criteria is also available:] - Horde_Imap_Client::SORT_RELEVANCY |
return | array | An array with the following keys:
- count: (integer) The number of messages that match the search criteria. Always returned. - match: (Horde_Imap_Client_Ids) The IDs that match $criteria, sorted if the 'sort' modifier was set. Returned if Horde_Imap_Client::SEARCH_RESULTS_MATCH is set. - max: (integer) The UID (default) or message sequence number (if 'sequence' is true) of the highest message that satisifies $criteria. Returns null if no matches found. Returned if Horde_Imap_Client::SEARCH_RESULTS_MAX is set. - min: (integer) The UID (default) or message sequence number (if 'sequence' is true) of the lowest message that satisifies $criteria. Returns null if no matches found. Returned if Horde_Imap_Client::SEARCH_RESULTS_MIN is set. - modseq: (integer) The highest mod-sequence for all messages being returned. Returned if 'sort' is false, the search query includes a MODSEQ command, and the server supports the CONDSTORE IMAP extension. - relevancy: (array) The list of relevancy scores. Returned if Horde_Imap_Client::SEARCH_RESULTS_RELEVANCY is set and the server supports FUZZY search matching. - save: (boolean) Whether the search results were saved. Returned if Horde_Imap_Client::SEARCH_RESULTS_SAVE is set. |
/** * Query the validity of a charset. * * @param string $charset The charset to query. * @param boolean $cached If true, only query cached values. * * @return boolean True if the charset is valid for searching. */ public function query($charset, $cached = false) { $charset = Horde_String::upper($charset); if (isset($this->_charsets[$charset])) { return $this->_charsets[$charset]; } elseif ($cached) { return null; } if (!$this->_baseob) { throw new RuntimeException('Base object needs to be defined to query for charset.'); } /* Use a dummy search query and search for BADCHARSET response. */ $query = new Horde_Imap_Client_Search_Query(); $query->charset($charset, false); $query->ids($this->_baseob->getIdsOb(1, true)); $query->text('a'); try { $this->_baseob->search('INBOX', $query, array('nocache' => true, 'sequence' => true)); $this->_charsets[$charset] = true; } catch (Horde_Imap_Client_Exception $e) { $this->_charsets[$charset] = $e->getCode() !== Horde_Imap_Client_Exception::BADCHARSET; } $this->notify(); return $this->_charsets[$charset]; }
/** * Constructor. * * @param Horde_Imap_Client_Base $base_ob Base driver object. * @param mixed $mailbox Mailbox to sync. * @param array $sync Token sync data. * @param array $curr Current sync data. * @param integer $criteria Mask of criteria to return. * @param Horde_Imap_Client_Ids $ids List of known UIDs. * * @throws Horde_Imap_Client_Exception * @throws Horde_Imap_Client_Exception_Sync */ public function __construct(Horde_Imap_Client_Base $base_ob, $mailbox, $sync, $curr, $criteria, $ids) { foreach (self::$map as $key => $val) { if (isset($sync[$key])) { $this->{$val} = $sync[$key]; } } /* Check uidvalidity. */ if (!$this->uidvalidity || $curr['V'] != $this->uidvalidity) { throw new Horde_Imap_Client_Exception_Sync('UIDs in cached mailbox have changed.', Horde_Imap_Client_Exception_Sync::UIDVALIDITY_CHANGED); } $this->mailbox = $mailbox; /* This was a UIDVALIDITY check only. */ if (!$criteria) { return; } $sync_all = $criteria & Horde_Imap_Client::SYNC_ALL; /* New messages. */ if ($sync_all || $criteria & Horde_Imap_Client::SYNC_NEWMSGS || $criteria & Horde_Imap_Client::SYNC_NEWMSGSUIDS) { $this->newmsgs = empty($this->uidnext) ? !empty($curr['U']) : !empty($curr['U']) && $curr['U'] > $this->uidnext; if ($this->newmsgs && ($sync_all || $criteria & Horde_Imap_Client::SYNC_NEWMSGSUIDS)) { $new_ids = empty($this->uidnext) ? Horde_Imap_Client_Ids::ALL : $this->uidnext . ':' . $curr['U']; $squery = new Horde_Imap_Client_Search_Query(); $squery->ids(new Horde_Imap_Client_Ids($new_ids)); $sres = $base_ob->search($mailbox, $squery); $this->_newmsgsuids = $sres['match']; } } /* Do single status call to get all necessary data. */ if ($this->highestmodseq && ($sync_all || $criteria & Horde_Imap_Client::SYNC_FLAGS || $criteria & Horde_Imap_Client::SYNC_FLAGSUIDS || $criteria & Horde_Imap_Client::SYNC_VANISHED || $criteria & Horde_Imap_Client::SYNC_VANISHEDUIDS)) { $status_sync = $base_ob->status($mailbox, Horde_Imap_Client::STATUS_SYNCMODSEQ | Horde_Imap_Client::STATUS_SYNCFLAGUIDS | Horde_Imap_Client::STATUS_SYNCVANISHED); if (!is_null($ids)) { $ids = $base_ob->resolveIds($mailbox, $ids); } } /* Flag changes. */ if ($sync_all || $criteria & Horde_Imap_Client::SYNC_FLAGS) { $this->flags = $this->highestmodseq ? $this->highestmodseq != $curr['H'] : true; } if ($sync_all || $criteria & Horde_Imap_Client::SYNC_FLAGSUIDS) { if ($this->highestmodseq) { if ($this->highestmodseq == $status_sync['syncmodseq']) { $this->_flagsuids = is_null($ids) ? $status_sync['syncflaguids'] : $base_ob->getIdsOb(array_intersect($ids->ids, $status_sync['syncflaguids']->ids)); } else { $squery = new Horde_Imap_Client_Search_Query(); $squery->modseq($this->highestmodseq + 1); $sres = $base_ob->search($mailbox, $squery, array('ids' => $ids)); $this->_flagsuids = $sres['match']; } } else { /* Without MODSEQ, need to mark all FLAGS as changed. */ $this->_flagsuids = $base_ob->resolveIds($mailbox, is_null($ids) ? $base_ob->getIdsOb(Horde_Imap_Client_Ids::ALL) : $ids); } } /* Vanished messages. */ if ($sync_all || $criteria & Horde_Imap_Client::SYNC_VANISHED || $criteria & Horde_Imap_Client::SYNC_VANISHEDUIDS) { if ($this->highestmodseq && $this->highestmodseq == $status_sync['syncmodseq']) { $vanished = is_null($ids) ? $status_sync['syncvanished'] : $base_ob->getIdsOb(array_intersect($ids->ids, $status_sync['syncvanished']->ids)); } else { $vanished = $base_ob->vanished($mailbox, $this->highestmodseq ? $this->highestmodseq : 1, array('ids' => $ids)); } $this->vanished = (bool) count($vanished); $this->_vanisheduids = $vanished; } }