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