common functions are documented only in one backend
[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
2c300e0b 17 * @link http://www.ietf.org/rfc/rfc3501.txt
40fbe929 18 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
cd33ec11 19 */
20
0e218c3b 21/** This functionality requires the IMAP and date functions
22 */
ff6f916c 23require_once(SM_PATH . 'functions/imap_general.php');
cd33ec11 24require_once(SM_PATH . 'functions/date.php');
25
00b05f03 26/** Set to TRUE to dump the imap dialogue
27 * @global bool $imap_asearch_debug_dump
28 */
cd33ec11 29$imap_asearch_debug_dump = FALSE;
30
40fbe929 31/** Imap SEARCH keys
00b05f03 32 * @global array $imap_asearch_opcodes
33 */
cd33ec11 34$imap_asearch_opcodes = array(
2c300e0b 35/* <sequence-set> => 'asequence', */ // Special handling, @see sqimap_asearch_build_criteria()
cd33ec11 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',
2c300e0b 46 'HEADER' => 'afield', // Special syntax for this one, @see sqimap_asearch_build_criteria()
cd33ec11 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
00b05f03 73/** Imap SEARCH month names encoding
74 * @global array $imap_asearch_months
75 */
cd33ec11 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
00b05f03 91/** Error message titles according to imap server returned code
92 * @global array $imap_error_titles
93 */
ff6f916c 94$imap_error_titles = array(
95 'OK' => '',
96 'NO' => _("ERROR : Could not complete request."),
97 'BAD' => _("ERROR : Bad or malformed request."),
40fbe929 98 'BYE' => _("ERROR : Imap server closed the connection."),
99 '' => _("ERROR : Connection dropped by imap-server.")
ff6f916c 100);
101
00b05f03 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
40fbe929 109 * @param string $message an optional error message
110 * @param string $link an optional link to try again
00b05f03 111 */
112//@global array color sm colors array
40fbe929 113function sqimap_asearch_error_box($response, $query, $message, $link = '')
ff6f916c 114{
115 global $imap_error_titles;
116
abd74f7d 117 if (!array_key_exists($response, $imap_error_titles))
ff6f916c 118 $title = _("ERROR : Unknown imap response.");
119 else
120 $title = $imap_error_titles[$response];
40fbe929 121 if ($link == '')
122 $message_title = _("Reason Given: ");
123 else
124 $message_title = _("Possible reason : ");
b28bec15 125 if (function_exists('sqimap_error_box'))
40fbe929 126 sqimap_error_box($title, $query, $message_title, $message, $link);
b28bec15 127 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
797784f9 128 global $color;
b28bec15 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);
40fbe929 137 if ($link != '')
138 $string .= $link;
b28bec15 139 $string .= "</font><br>\n";
140 error_box($string,$color);
141 }
ff6f916c 142}
143
48af4b64 144/**
2c300e0b 145 * This is a convenient way to avoid spreading if (isset(... all over the code
d2f031ed 146 * @param mixed $var any variable (reference)
0e218c3b 147 * @param mixed $def default value to return if unset (default is zls (''), pass 0 or array() when appropriate)
2c300e0b 148 * @return mixed $def if $var is unset, otherwise $var
48af4b64 149 */
2c300e0b 150function asearch_nz(&$var, $def = '')
cd33ec11 151{
152 if (isset($var))
153 return $var;
2c300e0b 154 return $def;
cd33ec11 155}
156
48af4b64 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
00b05f03 160 * @param string $string string to unhtmlentity()
161 * @return string decoded string
48af4b64 162 */
cd33ec11 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
00b05f03 174/**
175 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
0e218c3b 176 * @global bool imap_asearch_debug_dump
00b05f03 177 * @param string $var_name
178 * @param string $var_var
179 */
ff6f916c 180function s_debug_dump($var_name, $var_var)
cd33ec11 181{
182 global $imap_asearch_debug_dump;
f9fb0d38 183 if ($imap_asearch_debug_dump) {
b28bec15 184 if (function_exists('sm_print_r')) //Only exists since 1.4.2
f9fb0d38 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 }
cd33ec11 193}
194
00b05f03 195/** Encode a string to quoted or literal as defined in rfc 3501
196 *
197