php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #21168 imap_search() does not support the OR keyword
Submitted: 2002-12-23 19:00 UTC Modified: 2017-10-23 00:57 UTC
Votes:8
Avg. Score:4.1 ± 1.4
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:3 (42.9%)
From: slusarz at colorado dot edu Assigned: kalle (profile)
Status: Closed Package: IMAP related
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2002-12-23 19:00 UTC] slusarz at colorado dot edu
I sent this to Dan in August and haven't seen anything done about it yet, so I will put it here.  Note that the c-client DOES support the OR keyword (see the MARC article listed below) - it appears that PHP is not configuring the search correctly to use the keyword though.

-----

Quoting Dan Kalowsky <dank@deadmime.org>:

| Hi there,
| 
| Jon Pairse has given me your email regarding a commit you did on the IMP
| project for HORDE.  Not being very familiar with IMP, any chance you can
| give me a sample piece of PHP code for what doesn't work with the
| imap_search() function in PHP?
| 
| I'll see if I can get that fixed for 4.3.0

For imap_search() code, go to:
http://cvs.horde.org/co.php/imp/lib/Filter.php?login=2&r=1.20

The code is in the _matchFilter() method.  It generates a search query that 
looks like the following:

UNDELETED OR FIELD1 "FOOBAR1" OR FIELD2 "FOOBAR2" FIELD3 "FOOBAR3"

Running this query directly on my imap installation (courier-imap 1.5), it 
correctly does an OR search on FIELD1, FIELD2, FIELD3.  This search returns 
no results in PHP on the same mailbox.

My lame attempt to figure out the problem can be found here:
http://marc.theaimsgroup.com/?l=imp&m=102832554619977&w=2

-----

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-16 16:35 UTC] iliaa@php.net
This is really not a php problem to solve, PHP uses mail_criteria(), an imap function used to determine the nature of the search. Unfortunately, this function only supports IMAP2 functionality and does not support IMAP4 directives, which is what prevents you from using OR keyword.
Until the author of c-client decided to introduce mail_criteria() equivalent that supports IMAP4 don't expect to see OR keyword support in PHP.
 [2003-06-16 16:46 UTC] slusarz at colorado dot edu
The workaround we developed, using PHP code, can be found at:
http://cvs.horde.org/cvs.php/horde/lib/IMAP/Search.php
 [2003-06-16 17:18 UTC] chagenbu@php.net
Yes, it is a PHP problem. c-client has provided the necessary interfaces for a long time. Excuse the large documentation dump, but:

			   Mailbox Searching

void mail_search (MAILSTREAM *stream,char *criteria);
void mail_search_full (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,
		       long flags);
	stream	stream to search
	charset	MIME character set to use when searching strings
	pgm	search program
	flags	option flags

     This function causes a mailbox search, using the given MIME
charset (NIL means the default, US-ASCII) and the given search program.
A search program is a structure that holds the following data:

SEARCHSET *msgno;	a set of message sequence numbers
SEARCHSET *uid;		a set of unique identifiers
SEARCHOR *or;		OR result of two search programs
SEARCHPGMLIST *not;	AND result of list of NOT'ed search programs
SEARCHHEADER *header;	message headers
STRINGLIST *bcc;	string(s) appear in bcc list
STRINGLIST *body;	string(s) appear in message body text
STRINGLIST *cc;		string(s) appear in cc list
STRINGLIST *from;	string(s) appear in from
STRINGLIST *keyword;	user flag string(s) set
STRINGLIST *unkeyword;	user flag strings() not set
STRINGLIST *subject;	string(s) appear in subject
STRINGLIST *text;	string(s) appear in message header or body
STRINGLIST *to;		string(s) appear in to list
unsigned long larger;	larger than this many octets
unsigned long smaller;	smaller than this many octes
	The following dates are in form:
		((year - BASEYEAR) << 9) + (month << 5) + day
unsigned short sentbefore;
			sent before this date
unsigned short senton;	sent on this date
unsigned short sentsince;
			sent since this date
unsigned short before;	received before this date
unsigned short on;	received on this date
unsigned short since;	received since this date
unsigned int answered : 1;
			message answered
unsigned int unanswered : 1;
			message not answered
unsigned int deleted : 1;
			message deleted
unsigned int undeleted : 1;
			message not deleted
unsigned int draft : 1;	message is a draft
unsigned int undraft : 1;
			message is not a draft
unsigned int flagged : 1;
			message flagged as urgent
unsigned int unflagged : 1;
			message not flagged as urgent
unsigned int recent : 1;
			message recent since last parse of mailbox
unsigned int old : 1;	message not recent since last parse of mailbox
unsigned int seen : 1;	message read
unsigned int unseen : 1;
			message not read

     The following auxillary structures are used by search programs:
	SEARCHHEADER:	header line searching
char *line;		header line field name
char *text;		text header line
SEARCHHEADER *next;	next SEARCHHEADER in list (AND'ed)

	SEARCHSET:	message number set
unsigned long first;	first number in set
unsigned long last;	if non-zero, last number in set
SEARCHSET *next;	next SEARCHSET in list (AND'ed)

	SEARCHOR:	two search programs, OR'ed together
SEARCHPGM *first;	first program
SEARCHPGM *second;	second program
SEARCHOR *next;		next SEARCHOR in list

	SEARCHPGMLIST:	list of search programs
SEARCHPGM *pgm;		search program (AND'd with others in list)
SEARCHPGMLIST *next;	next SEARCHPGM in list

     mail_search(), the older interface, accepts a search criteria
argument as a character string in IMAP2 (RFC-1176) format.  Do not try
to use any IMAP4 search criteria with this interface.

So, we just need to use the right calls. It's a pain that c-client doesn't let you just pass in the right search string, though.
 [2003-06-16 17:34 UTC] pollita@php.net
The troble is that extending imap_search to support the OR keyword (using the current c-client) would require ugly lexing of the search parameter and is outside the scope of what PHP should be doing to interface to the imap library.

That said, it doesn't seem unreasonable to introduce a completely separate interface which allows the user to generate their own search programs.  Something like:

$ret = imap_search_ex($stream, imap_search_or(imap_search_criteria("From = 'joe@example.com'"),imap_search_and(imap_search_criteria("From = 'bob@nowhere.org'"),imap_search_criteria("To = 'joe@example.com'"))));

which would be the equivalent of:

From = 'joe@example.com' OR (From = 'bob@nowhere.org' AND To = 'joe@example.com')

This is not a minor addition either, but is more within PHP's scope...
 [2003-06-17 11:10 UTC] sniper@php.net
reclassified (not bug, a feature request)

 [2010-02-01 14:42 UTC] dominik at dokdok dot com
This is related to http://bugs.php.net/bug.php?id=15238
 [2011-04-08 21:55 UTC] jani@php.net
-Summary: [IMAP] imap_search() does not support the OR keyword +Summary: imap_search() does not support the OR keyword -Package: Feature/Change Request +Package: IMAP related -Operating System: unknown +Operating System: * -PHP Version: 4.3.3-dev, 5.0.0-dev +PHP Version: *
 [2017-10-23 00:57 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2017-10-23 00:57 UTC] kalle@php.net
Gonna close this as we should rely on c-client to have the functionality like noted over a decade ago
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 25 00:01:33 2024 UTC