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