moving forms to separate subpackage. It is easier to dig through functions
[squirrelmail.git] / functions / imap_asearch.php
CommitLineData
cd33ec11 1<?php
2
3/**
4 * imap_search.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
cd33ec11 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP asearch routines
40fbe929 10 *
11 * $Id$
d6c32258 12 * @package squirrelmail
00b05f03 13 * @see search.php
40fbe929 14 * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt
15 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
cd33ec11 16 *
40fbe929 17 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
cd33ec11 18 */
19
d6c32258 20/** This functionality requires the IMAP and date functions */
ff6f916c 21require_once(SM_PATH . 'functions/imap_general.php');
cd33ec11 22require_once(SM_PATH . 'functions/date.php');
23
00b05f03 24/** Set to TRUE to dump the imap dialogue
25 * @global bool $imap_asearch_debug_dump
26 */
cd33ec11 27$imap_asearch_debug_dump = FALSE;
28
40fbe929 29/** Imap SEARCH keys
00b05f03 30 * @global array $imap_asearch_opcodes
31 */
cd33ec11 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
00b05f03 71/** Imap SEARCH month names encoding
72 * @global array $imap_asearch_months
73 */
cd33ec11 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
00b05f03 89/** Error message titles according to imap server returned code
90 * @global array $imap_error_titles
91 */
ff6f916c 92$imap_error_titles = array(
93 'OK' => '',
94 'NO' => _("ERROR : Could not complete request."),
95 'BAD' => _("ERROR : Bad or malformed request."),
40fbe929 96 'BYE' => _("ERROR : Imap server closed the connection."),
97 '' => _("ERROR : Connection dropped by imap-server.")
ff6f916c 98);
99
00b05f03 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
40fbe929 107 * @param string $message an optional error message
108 * @param string $link an optional link to try again
00b05f03 109 */
110//@global array color sm colors array
40fbe929 111function sqimap_asearch_error_box($response, $query, $message, $link = '')
ff6f916c 112{
113 global $imap_error_titles;
114
abd74f7d 115 if (!array_key_exists($response, $imap_error_titles))
ff6f916c 116 $title = _("ERROR : Unknown imap response.");
117 else
118 $title = $imap_error_titles[$response];
40fbe929 119 if ($link == '')
120 $message_title = _("Reason Given: ");
121 else
122 $message_title = _("Possible reason : ");
b28bec15 123 if (function_exists('sqimap_error_box'))
40fbe929 124 sqimap_error_box($title, $query, $message_title, $message, $link);
b28bec15 125 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
797784f9 126 global $color;
b28bec15 127 require_once(SM_PATH . 'functions/display_messages.php');
128 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
129 if ($query != '')
130 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
131 if ($message_title != '')
132 $string .= $message_title;
133 if ($message != '')
134 $string .= htmlspecialchars($message);
40fbe929 135 if ($link != '')
136 $string .= $link;
b28bec15 137 $string .= "</font><br>\n";
138 error_box($string,$color);
139 }
ff6f916c 140}
141
48af4b64 142/**
143 * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
d2f031ed 144 * @param mixed $var any variable (reference)
00b05f03 145 * @return mixed zls ('') if $var is not defined, otherwise $var
48af4b64 146 */
cd33ec11 147function asearch_nz(&$var)
148{
149 if (isset($var))
150 return $var;
151 return '';
152}
153
48af4b64 154/**
155 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
156 * except it doesn't handle hex constructs
00b05f03 157 * @param string $string string to unhtmlentity()
158 * @return string decoded string
48af4b64 159 */
cd33ec11 160function asearch_unhtmlentities($string) {
161 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
162 for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
163 $trans_tbl['&#' . $i . ';'] = chr($i);
164 return strtr($string, $trans_tbl);
165/* I think the one above is quicker, though it should be benchmarked
166 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
167 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
168*/
169}
170
00b05f03 171/**
172 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
173 * @global imap_asearch_debug_dump
174 * @param string $var_name
175 * @param string $var_var
176 */
ff6f916c 177function s_debug_dump($var_name, $var_var)
cd33ec11 178{
179 global $imap_asearch_debug_dump;
f9fb0d38 180 if ($imap_asearch_debug_dump) {
b28bec15 181 if (function_exists('sm_print_r')) //Only exists since 1.4.2
f9fb0d38 182 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
183 else {
184 echo '<pre>';
185 echo htmlentities($var_name);
186 print_r($var_var);
187 echo '</pre>';
188 }
189 }
cd33ec11 190}
191
00b05f03 192/** Encode a string to quoted or literal as defined in rfc 3501
193 *
194