Set the base path before every session_start in case we loose track of the
[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 *
eb19bc67 11 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
12 *
13 * @version $Id$
d6c32258 14 * @package squirrelmail
eb19bc67 15 * @subpackage imap
00b05f03 16 * @see search.php
40fbe929 17 * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt
18 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
cd33ec11 19 */
20
d6c32258 21/** This functionality requires the IMAP and date functions */
ff6f916c 22require_once(SM_PATH . 'functions/imap_general.php');
cd33ec11 23require_once(SM_PATH . 'functions/date.php');
24
00b05f03 25/** Set to TRUE to dump the imap dialogue
26 * @global bool $imap_asearch_debug_dump
27 */
cd33ec11 28$imap_asearch_debug_dump = FALSE;
29
40fbe929 30/** Imap SEARCH keys
00b05f03 31 * @global array $imap_asearch_opcodes
32 */
cd33ec11 33$imap_asearch_opcodes = array(
34/* <message set> => 'asequence', */
35/*'ALL' is binary operator */
36 'ANSWERED' => '',
37 'BCC' => 'astring',
38 'BEFORE' => 'adate',
39 'BODY' => 'astring',
40 'CC' => 'astring',
41 'DELETED' => '',
42 'DRAFT' => '',
43 'FLAGGED' => '',
44 'FROM' => 'astring',
45 'HEADER' => 'afield', /* Special syntax for this one, see below */
46 'KEYWORD' => 'akeyword',
47 'LARGER' => 'anum',
48 'NEW' => '',
49/*'NOT' is unary operator */
50 'OLD' => '',
51 'ON' => 'adate',
52/*'OR' is binary operator */
53 'RECENT' => '',
54 'SEEN' => '',
55 'SENTBEFORE' => 'adate',
56 'SENTON' => 'adate',
57 'SENTSINCE' => 'adate',
58 'SINCE' => 'adate',
59 'SMALLER' => 'anum',
60 'SUBJECT' => 'astring',
61 'TEXT' => 'astring',
62 'TO' => 'astring',
63 'UID' => 'asequence',
64 'UNANSWERED' => '',
65 'UNDELETED' => '',
66 'UNDRAFT' => '',
67 'UNFLAGGED' => '',
68 'UNKEYWORD' => 'akeyword',
69 'UNSEEN' => ''
70);
71
00b05f03 72/** Imap SEARCH month names encoding
73 * @global array $imap_asearch_months
74 */
cd33ec11 75$imap_asearch_months = array(
76 '01' => 'jan',
77 '02' => 'feb',
78 '03' => 'mar',
79 '04' => 'apr',
80 '05' => 'may',
81 '06' => 'jun',
82 '07' => 'jul',
83 '08' => 'aug',
84 '09' => 'sep',
85 '10' => 'oct',
86 '11' => 'nov',
87 '12' => 'dec'
88);
89
00b05f03 90/** Error message titles according to imap server returned code
91 * @global array $imap_error_titles
92 */
ff6f916c 93$imap_error_titles = array(
94 'OK' => '',
95 'NO' => _("ERROR : Could not complete request."),
96 'BAD' => _("ERROR : Bad or malformed request."),
40fbe929 97 'BYE' => _("ERROR : Imap server closed the connection."),
98 '' => _("ERROR : Connection dropped by imap-server.")
ff6f916c 99);
100
00b05f03 101/**
102 * Function to display an error related to an IMAP-query.
103 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
104 * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code
105 * @global array imap_error_titles
106 * @param string $response the imap server response code
107 * @param string $query the failed query
40fbe929 108 * @param string $message an optional error message
109 * @param string $link an optional link to try again
00b05f03 110 */
111//@global array color sm colors array
40fbe929 112function sqimap_asearch_error_box($response, $query, $message, $link = '')
ff6f916c 113{
114 global $imap_error_titles;
115
abd74f7d 116 if (!array_key_exists($response, $imap_error_titles))
ff6f916c 117 $title = _("ERROR : Unknown imap response.");
118 else
119 $title = $imap_error_titles[$response];
40fbe929 120 if ($link == '')
121 $message_title = _("Reason Given: ");
122 else
123 $message_title = _("Possible reason : ");
b28bec15 124 if (function_exists('sqimap_error_box'))
40fbe929 125 sqimap_error_box($title, $query, $message_title, $message, $link);
b28bec15 126 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
797784f9 127 global $color;
b28bec15 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);
40fbe929 136 if ($link != '')
137 $string .= $link;
b28bec15 138 $string .= "</font><br>\n";
139 error_box($string,$color);
140 }
ff6f916c 141}
142
48af4b64 143/**
144 * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
d2f031ed 145 * @param mixed $var any variable (reference)
00b05f03 146 * @return mixed zls ('') if $var is not defined, otherwise $var
48af4b64 147 */
cd33ec11 148function asearch_nz(&$var)
149{
150 if (isset($var))
151 return $var;
152 return '';
153}
154
48af4b64 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
00b05f03 158 * @param string $string string to unhtmlentity()
159 * @return string decoded string
48af4b64 160 */
cd33ec11 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
00b05f03 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 */
ff6f916c 178function s_debug_dump($var_name, $var_var)
cd33ec11 179{
180 global $imap_asearch_debug_dump;
f9fb0d38 181 if ($imap_asearch_debug_dump) {
b28bec15 182 if (function_exists('sm_print_r')) //Only exists since 1.4.2
f9fb0d38 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 }
cd33ec11 191}
192
00b05f03 193/** Encode a string to quoted or literal as defined in rfc 3501
194 *
195