Fix from Seth Randall. The range was 1 to large.
[squirrelmail.git] / functions / imap_asearch.php
... / ...
CommitLineData
1<?php
2
3/**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP asearch routines
10 *
11 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
12 *
13 * @version $Id$
14 * @package squirrelmail
15 * @subpackage imap
16 * @see search.php
17 * @link http://www.ietf.org/rfc/rfc3501.txt
18 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
19 */
20
21/** This functionality requires the IMAP and date functions
22 */
23require_once(SM_PATH . 'functions/imap_general.php');
24require_once(SM_PATH . 'functions/date.php');
25
26/** Set to TRUE to dump the imap dialogue
27 * @global bool $imap_asearch_debug_dump
28 */
29$imap_asearch_debug_dump = FALSE;
30
31/** Imap SEARCH keys
32 * @global array $imap_asearch_opcodes
33 */
34$imap_asearch_opcodes = array(
35/* <sequence-set> => 'asequence', */ // Special handling, @see sqimap_asearch_build_criteria()
36/*'ALL' is binary operator */
37 'ANSWERED' => '',
38 'BCC' => 'astring',
39 'BEFORE' => 'adate',
40 'BODY' => 'astring',
41 'CC' => 'astring',
42 'DELETED' => '',
43 'DRAFT' => '',
44 'FLAGGED' => '',
45 'FROM' => 'astring',
46 'HEADER' => 'afield', // Special syntax for this one, @see sqimap_asearch_build_criteria()
47 'KEYWORD' => 'akeyword',
48 'LARGER' => 'anum',
49 'NEW' => '',
50/*'NOT' is unary operator */
51 'OLD' => '',
52 'ON' => 'adate',
53/*'OR' is binary operator */
54 'RECENT' => '',
55 'SEEN' => '',
56 'SENTBEFORE' => 'adate',
57 'SENTON' => 'adate',
58 'SENTSINCE' => 'adate',
59 'SINCE' => 'adate',
60 'SMALLER' => 'anum',
61 'SUBJECT' => 'astring',
62 'TEXT' => 'astring',
63 'TO' => 'astring',
64 'UID' => 'asequence',
65 'UNANSWERED' => '',
66 'UNDELETED' => '',
67 'UNDRAFT' => '',
68 'UNFLAGGED' => '',
69 'UNKEYWORD' => 'akeyword',
70 'UNSEEN' => ''
71);
72
73/** Imap SEARCH month names encoding
74 * @global array $imap_asearch_months
75 */
76$imap_asearch_months = array(
77 '01' => 'jan',
78 '02' => 'feb',
79 '03' => 'mar',
80 '04' => 'apr',
81 '05' => 'may',
82 '06' => 'jun',
83 '07' => 'jul',
84 '08' => 'aug',
85 '09' => 'sep',
86 '10' => 'oct',
87 '11' => 'nov',
88 '12' => 'dec'
89);
90
91/** Error message titles according to imap server returned code
92 * @global array $imap_error_titles
93 */
94$imap_error_titles = array(
95 'OK' => '',
96 'NO' => _("ERROR : Could not complete request."),
97 'BAD' => _("ERROR : Bad or malformed request."),
98 'BYE' => _("ERROR : Imap server closed the connection."),
99 '' => _("ERROR : Connection dropped by imap-server.")
100);
101
102/**
103 * Function to display an error related to an IMAP-query.
104 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
105 * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code
106 * @global array imap_error_titles
107 * @param string $response the imap server response code
108 * @param string $query the failed query
109 * @param string $message an optional error message
110 * @param string $link an optional link to try again
111 */
112//@global array color sm colors array
113function sqimap_asearch_error_box($response, $query, $message, $link = '')
114{
115 global $imap_error_titles;
116
117 if (!array_key_exists($response, $imap_error_titles))
118 $title = _("ERROR : Unknown imap response.");
119 else
120 $title = $imap_error_titles[$response];
121 if ($link == '')
122 $message_title = _("Reason Given: ");
123 else
124 $message_title = _("Possible reason : ");
125 if (function_exists('sqimap_error_box'))
126 sqimap_error_box($title, $query, $message_title, $message, $link);
127 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
128 global $color;
129 require_once(SM_PATH . 'functions/display_messages.php');
130 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
131 if ($query != '')
132 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
133 if ($message_title != '')
134 $string .= $message_title;
135 if ($message != '')
136 $string .= htmlspecialchars($message);
137 if ($link != '')
138 $string .= $link;
139 $string .= "</font><br>\n";
140 error_box($string,$color);
141 }
142}
143
144/**
145 * This is a convenient way to avoid spreading if (isset(... all over the code
146 * @param mixed $var any variable (reference)
147 * @param mixed $def default value to return if unset (default is zls (''), pass 0 or array() when appropriate)
148 * @return mixed $def if $var is unset, otherwise $var
149 */
150function asearch_nz(&$var, $def = '')
151{
152 if (isset($var))
153 return $var;
154 return $def;
155}
156
157/**
158 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
159 * except it doesn't handle hex constructs
160 * @param string $string string to unhtmlentity()
161 * @return string decoded string
162 */
163function asearch_unhtmlentities($string) {
164 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
165 for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
166 $trans_tbl['&#' . $i . ';'] = chr($i);
167 return strtr($string, $trans_tbl);
168/* I think the one above is quicker, though it should be benchmarked
169 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
170 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
171*/
172}
173
174/**
175 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
176 * @global bool imap_asearch_debug_dump
177 * @param string $var_name
178 * @param string $var_var
179 */
180function s_debug_dump($var_name, $var_var)
181{
182 global $imap_asearch_debug_dump;
183 if ($imap_asearch_debug_dump) {
184 if (function_exists('sm_print_r')) //Only exists since 1.4.2
185 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
186 else {
187 echo '<pre>';
188 echo htmlentities($var_name);
189 print_r($var_var);
190 echo '</pre>';
191 }
192 }
193}
194
195/** Encode a string to quoted or literal as defined in rfc 3501
196 *
197