Skip to content

Commit 2b4c463

Browse files
committed
Search Query functionality added
1 parent 487ddd2 commit 2b4c463

File tree

6 files changed

+786
-78
lines changed

6 files changed

+786
-78
lines changed

src/IMAP/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function setConfig(array $config) {
155155
* @return resource|boolean
156156
*/
157157
public function getConnection() {
158+
$this->checkConnection();
158159
return $this->connection;
159160
}
160161

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/*
3+
* File: InvalidWhereQueryCriteriaException.php
4+
* Category: Exception
5+
* Author: M. Goldenbaum
6+
* Created: 21.07.18 19:04
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Webklex\IMAP\Exceptions;
14+
15+
use \Exception;
16+
17+
/**
18+
* Class InvalidWhereQueryCriteriaException
19+
*
20+
* @package Webklex\IMAP\Exceptions
21+
*/
22+
class InvalidWhereQueryCriteriaException extends Exception {
23+
24+
}

src/IMAP/Folder.php

Lines changed: 41 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
1616
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
17+
use Webklex\IMAP\Query\WhereQuery;
1718
use Webklex\IMAP\Support\FolderCollection;
1819
use Webklex\IMAP\Support\MessageCollection;
1920

@@ -123,6 +124,19 @@ public function __construct(Client $client, $folder) {
123124
$this->parseAttributes($folder->attributes);
124125
}
125126

127+
/**
128+
* Get a new search query instance
129+
* @param string $charset
130+
*
131+
* @return WhereQuery
132+
*/
133+
public function query($charset = 'UTF-8'){
134+
$this->getClient()->checkConnection();
135+
$this->getClient()->openFolder($this);
136+
137+
return new WhereQuery($this->getClient(), $charset);
138+
}
139+
126140
/**
127141
* Determine if folder has children.
128142
*
@@ -153,6 +167,7 @@ public function setChildren($children = []) {
153167
* @param integer|null $fetch_options
154168
* @param boolean $fetch_body
155169
* @param boolean $fetch_attachment
170+
* @param boolean $fetch_flags
156171
*
157172
* @return Message|null
158173
*/
@@ -171,14 +186,21 @@ public function getMessage($uid, $msglist = null, $fetch_options = null, $fetch_
171186
* @param int|null $fetch_options
172187
* @param boolean $fetch_body
173188
* @param boolean $fetch_attachment
189+
* @param boolean $fetch_flags
190+
* @param int|null $limit
191+
* @param int $page
192+
* @param string $charset
174193
*
175194
* @return MessageCollection
176195
* @throws Exceptions\ConnectionFailedException
177196
* @throws GetMessagesFailedException
178197
* @throws MessageSearchValidationException
179198
*/
180-
public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false, $limit = null, $page = 1) {
181-
return $this->searchMessages([[$criteria]], $fetch_options, $fetch_body, $fetch_attachment, $limit, $page);
199+
public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false, $limit = null, $page = 1, $charset = "UTF-8") {
200+
201+
return $this->query($charset)->where($criteria)->setFetchOptions($fetch_options)->setFetchBody($fetch_body)
202+
->setFetchAttachment($fetch_attachment)->setFetchFlags($fetch_flags)
203+
->limit($limit, $page)->get();
182204
}
183205

184206
/**
@@ -188,6 +210,10 @@ public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_bod
188210
* @param int|null $fetch_options
189211
* @param boolean $fetch_body
190212
* @param boolean $fetch_attachment
213+
* @param boolean $fetch_flags
214+
* @param int|null $limit
215+
* @param int $page
216+
* @param string $charset
191217
*
192218
* @return MessageCollection
193219
* @throws Exceptions\ConnectionFailedException
@@ -197,8 +223,8 @@ public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_bod
197223
* @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead
198224
* @see Folder::getMessages()
199225
*/
200-
public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) {
201-
return $this->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
226+
public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false, $limit = null, $page = 1, $charset = "UTF-8") {
227+
return $this->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags, $limit, $page, $charset);
202228
}
203229

204230
/**
@@ -218,8 +244,11 @@ public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $
218244
* ---------------------------------------------------------------------------------------
219245
* @param int|null $fetch_options
220246
* @param boolean $fetch_body
221-
* @param string $charset
222247
* @param boolean $fetch_attachment
248+
* @param boolean $fetch_flags
249+
* @param int|null $limit
250+
* @param int $page
251+
* @param string $charset
223252
*
224253
* @return MessageCollection
225254
*
@@ -261,83 +290,17 @@ public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $
261290
* ; M:-12; N:+1; Y:+12
262291
* / ( ("+" / "-") 4DIGIT ) ; Local differential
263292
* ; hours+min. (HHMM)
293+
*
294+
* @deprecated 1.2.1:2.0.0 No longer needed. Use Folder::query() instead
295+
* @see Folder::query()
264296
*/
265-
public function searchMessages(array $where, $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false, $charset = "UTF-8", $limit = null, $page = 1) {
266-
297+
public function searchMessages(array $where, $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false, $limit = null, $page = 1, $charset = "UTF-8") {
267298
$this->getClient()->checkConnection();
268299

269-
if ($this->validateWhereStatements($where) === false) {
270-
throw new MessageSearchValidationException('Invalid imap search criteria provided');
271-
}
272-
273-
try {
274-
$this->getClient()->openFolder($this);
275-
$messages = MessageCollection::make([]);
276-
277-
$query = '';
278-
foreach ($where as $statement) {
279-
if (count($statement) == 1) {
280-
$query .= $statement[0];
281-
} else {
282-
$value = $statement[1];
283-
if ($value instanceof \Carbon\Carbon) {
284-
$value = $value->format('d M y');
285-
}
286-
$query .= $statement[0].' "'.$value.'"';
287-
}
288-
$query .= ' ';
289-
}
290-
291-
$query = trim($query);
292-
293-
$availableMessages = array_reverse(imap_search($this->getClient()->getConnection(), $query, SE_UID, $charset));
294-
295-
$numMessages = imap_num_msg($this->getClient()->getConnection());
296-
if($limit == null || $limit > $numMessages){
297-
$maxMessages = $numMessages;
298-
}else{
299-
$maxMessages = $limit;
300-
}
301-
if ($availableMessages !== false) {
302-
for ($msglist = ($page * $maxMessages) - $limit; $msglist < $page * $maxMessages; $msglist++){
303-
$msgno = $availableMessages[$msglist];
304-
$message = new Message($msgno, $msglist, $this->getClient(), $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
305-
$messages->put($message->getMessageId(), $message);
306-
}
307-
}
308-
309-
return $messages;
310-
} catch (\Exception $e) {
311-
$message = $e->getMessage();
312-
313-
throw new GetMessagesFailedException($message);
314-
}
315-
}
316-
317-
/**
318-
* Validate a given statement array
319-
*
320-
* @param array $statements
321-
*
322-
* @return bool
323-
*
324-
* @doc http://php.net/manual/en/function.imap-search.php
325-
* https://tools.ietf.org/html/rfc1064
326-
* https://tools.ietf.org/html/rfc822
327-
*/
328-
protected function validateWhereStatements($statements) {
329-
foreach ($statements as $statement) {
330-
$criteria = $statement[0];
331-
if (in_array($criteria, [
332-
'OR', 'AND',
333-
'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD',
334-
'NEW', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO',
335-
'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN']) === false) {
336-
return false;
337-
}
338-
}
300+
return $this->query($charset)->where($where)->setFetchOptions($fetch_options)->setFetchBody($fetch_body)
301+
->setFetchAttachment($fetch_attachment)->setFetchFlags($fetch_flags)
302+
->limit($limit, $page)->get();
339303

340-
return empty($statements) === false;
341304
}
342305

343306
/**

0 commit comments

Comments
 (0)