Fixed bug with Cyrus and SendMDN. SquirrelMail wasn't allowed to set Seen flag twice.
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
a5ee5ac2 4 * search.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
2d367c68 11
35586184 12require_once('../src/validate.php');
13require_once('../functions/imap.php');
14require_once('../functions/imap_search.php');
15require_once('../functions/array.php');
46d38f78 16require_once('../functions/strings.php');
c61bb006 17
56e0b3b7 18
23a9084b 19/* here are some functions, could go in imap_search.php
56e0b3b7 20
23a9084b 21 this was here, pretty handy */
29eb5486 22function s_opt( $val, $sel, $tit ) {
23 echo " <option value=\"$val\"";
0d672ac0 24 if ( $sel == $val ) {
bd9bbfef 25 echo ' selected';
99d2a184 26 }
29eb5486 27 echo ">$tit</option>\n";
28}
99d2a184 29
23a9084b 30/* function to get the recent searches and put them in arrays */
56e0b3b7 31function get_recent($pref_name, $username, $data_dir) {
32 $array = array ();
33 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
34 $n = 0;
35 for ($x=1;$x<=$recent_count;$x++) {
23a9084b 36 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
37 $n++;
56e0b3b7 38 }
39 return $array;
40}
41
23a9084b 42/* function to get the saved searches and put them in arrays */
56e0b3b7 43function get_saved($pref_name, $username, $data_dir) {
44 $array = array ();
45 $n = 0;
46 for ($x=1;;$x++) {
47 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
23a9084b 48 if ($array[$n] == "") {
49 array_pop($array);
50 return $array;
51 }
52 $n++;
56e0b3b7 53 }
54 return $array;
55}
56
23a9084b 57/* function to update recent pref arrays */
58function update_recent($what, $where, $mailbox, $username, $data_dir) {
59 $what_array = get_recent("search_what", $username, $data_dir);
60 $where_array = get_recent("search_where", $username, $data_dir);
61 $folder_array = get_recent("search_folder", $username, $data_dir);
62 $dupe = "no";
63 for ($i=0;$i<count($what_array);$i++) {
64 if ($what == $what_array[$i] &&
65 $where == $where_array[$i] &&
66 $mailbox == $folder_array[$i]) {
67
68 $dupe = "yes";
69 }
70 }
71 if ($dupe == "no") {
72 array_push ($what_array, $what);
73 array_push ($where_array, $where);
74 array_push ($folder_array, $mailbox);
75 array_shift ($what_array);
76 array_shift ($where_array);
77 array_shift ($folder_array);
56e0b3b7 78 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
79 $n=0;
80 for ($i=1;$i<=$recent_count;$i++) {
23a9084b 81 setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]);
82 setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]);
83 setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]);
56e0b3b7 84 $n++;
23a9084b 85 }
56e0b3b7 86 }
87}
88
23a9084b 89/* function to forget a recent search */
56e0b3b7 90function forget_recent($forget_index, $username, $data_dir) {
91 $what_array = get_recent("search_what", $username, $data_dir);
92 $where_array = get_recent("search_where", $username, $data_dir);
93 $folder_array = get_recent("search_folder", $username, $data_dir);
94 array_splice($what_array, $forget_index, 1);
95 array_splice($where_array, $forget_index, 1);
96 array_splice($folder_array, $forget_index, 1);
23a9084b 97 array_unshift($what_array, "");
98 array_unshift($where_array, "");
99 array_unshift($folder_array, "");
56e0b3b7 100 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
101 $n=0;
102 for ($i=1;$i<=$recent_count;$i++) {
103 setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]);
23a9084b 104 setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]);
105 setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]);
106 $n++;
56e0b3b7 107 }
108}
23a9084b 109
110/* function to delete a saved search */
56e0b3b7 111function delete_saved($delete_index, $username, $data_dir) {
112 $saved_what_array = get_saved("saved_what", $username, $data_dir);
113 $saved_where_array = get_saved("saved_where", $username, $data_dir);
114 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
115 array_splice($saved_what_array, $delete_index, 1);
116 array_splice($saved_where_array, $delete_index, 1);
117 array_splice($saved_folder_array, $delete_index, 1);
118 $n=0;
119 $saved_count = count($saved_what_array);
120 $last_element = $saved_count + 1;
121 if ($last_element < 1) {
122 for ($i=1;$i<=$saved_count;$i++) {
123 setPref($data_dir, $username, "saved_what" . "$i", $saved_what_array[$n]);
23a9084b 124 setPref($data_dir, $username, "saved_where" . "$i", $saved_where_array[$n]);
125 setPref($data_dir, $username, "saved_folder" . "$i", $saved_folder_array[$n]);
126 $n++;
56e0b3b7 127 }
128 }
129 removePref($data_dir, $username, "saved_what" . "$last_element");
130 removePref($data_dir, $username, "saved_where" . "$last_element");
131 removePref($data_dir, $username, "saved_folder" . "$last_element");
132}
6c8388a9 133
23a9084b 134/* function to save a search from recent to saved */
56e0b3b7 135function save_recent($save_index, $username, $data_dir) {
136 $what_array = get_recent("search_what", $username, $data_dir);
137 $where_array = get_recent("search_where", $username, $data_dir);
138 $folder_array = get_recent("search_folder", $username, $data_dir);
139 $saved_what_once = array_slice($what_array, $save_index, 1);
140 $saved_where_once = array_slice($where_array, $save_index, 1);
141 $saved_folder_once = array_slice($folder_array, $save_index, 1);
142 $saved_array = get_saved("saved_what", $username, $data_dir);
143 $saved_count = (count($saved_array) + 1);
144 setPref($data_dir, $username, "saved_what" . "$saved_count", $saved_what_once[0]);
145 setPref($data_dir, $username, "saved_where" . "$saved_count", $saved_where_once[0]);
146 setPref($data_dir, $username, "saved_folder" . "$saved_count", $saved_folder_once[0]);
147}
148
0d672ac0 149/* ------------------------ main ------------------------ */
150
23a9084b 151/* reset these arrays on each page load just in case */
56e0b3b7 152$what_array = array ();
153$where_array = array ();
154$folder_array = array ();
155$saved_what_array = array ();
156$saved_where_array = array ();
157$saved_folder_array = array ();
158$search_all = "none";
23a9084b 159$perbox_count = array ();
56e0b3b7 160
23a9084b 161/* get mailbox names */
29eb5486 162$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
c1cb7ba4 163$boxes = sqimap_mailbox_list($imapConnection);
164
56e0b3b7 165
23a9084b 166/* set current mailbox to INBOX if none was selected or if page
167 was called to search all folders. */
3b7d68e6 168if ($mailbox == 'None' || $mailbox == '' ) {
56e0b3b7 169 $mailbox = $boxes[0]['unformatted'];
170}
3b7d68e6 171if ($mailbox == 'All Folders') {
56e0b3b7 172 $search_all = "all";
c1cb7ba4 173}
174
175displayPageHeader($color, $mailbox);
c61bb006 176
23a9084b 177/* See how the page was called and fire off correct function */
d4144adf 178//if ( !isset( $submit ) ) {
179// $submit = '';
180//} else if ($submit == 'Search' && !empty($what)) {
181if ((!isset($submit) || empty($submit)) && !empty($what)) {
182 $submit = 'Search';
183}
184if ($submit == 'Search' && !empty($what)) {
23a9084b 185 update_recent($what, $where, $mailbox, $username, $data_dir);
56e0b3b7 186}
88cb1b4d 187elseif ($submit == 'forget') {
56e0b3b7 188 forget_recent($count, $username, $data_dir);
189}
88cb1b4d 190elseif ($submit == 'save') {
56e0b3b7 191 save_recent($count, $username, $data_dir);
192}
88cb1b4d 193elseif ($submit == 'delete') {
56e0b3b7 194 delete_saved($count, $username, $data_dir);
195}
23a9084b 196
29eb5486 197do_hook('search_before_form');
56e0b3b7 198
199echo "<BR>\n".
88cb1b4d 200 "<table width=\"100%\">\n".
201 "<TR><td bgcolor=\"$color[0]\">\n".
202 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
203 "</TD></TR>\n".
204 "</TABLE>\n";
c61bb006 205
23a9084b 206/* update the recent and saved searches from the pref files */
56e0b3b7 207$what_array = get_recent("search_what", $username, $data_dir);
208$where_array = get_recent("search_where", $username, $data_dir);
209$folder_array = get_recent("search_folder", $username, $data_dir);
210$recent_count = getPref($data_dir, $username, 'search_memory', 0);
88cb1b4d 211$saved_what_array = get_saved("saved_what", $username, $data_dir);
56e0b3b7 212$saved_where_array = get_saved("saved_where", $username, $data_dir);
213$saved_folder_array = get_saved("saved_folder", $username, $data_dir);
214$saved_count = count($saved_what_array);
91954f9e 215$count_all = 0;
a7d0eaf6 216
184ef883 217/* Saved Search Table */
56e0b3b7 218if ($saved_count > 0) {
184ef883 219 echo "<BR>\n"
88cb1b4d 220 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
221 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
222 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
184ef883 223 for ($i=0; $i < $saved_count; ++$i) {
88cb1b4d 224 if ($i % 2) {
225 echo "<TR BGCOLOR=\"$color[0]\">";
226 } else {
184ef883 227 echo "<TR BGCOLOR=\"$color[4]\">";
228 }
229 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
88cb1b4d 230 . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>"
231 . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>"
232 . '<TD ALIGN=RIGHT>'
233 . '<A HREF=search.php'
234 . '?mailbox=' . urlencode($saved_folder_array[$i])
235 . '&what=' . urlencode($saved_what_array[$i])
236 . '&where=' . urlencode($saved_where_array[$i])
237 . '>' . _("edit") . '</A>'
238 . '&nbsp;|&nbsp;'
239 . '<A HREF=search.php'
240 . '?mailbox=' . urlencode($saved_folder_array[$i])
241 . '&what=' . urlencode($saved_what_array[$i])
242 . '&where=' . urlencode($saved_where_array[$i])
243 . '&submit=Search_no_update'
244 . '>' . _("search") . '</A>'
245 . '&nbsp;|&nbsp;'
246 . "<A HREF=search.php?count=$i&submit=delete>"
247 . _("delete")
248 . '</A>'
249 . '</TD></TR>';
56e0b3b7 250 }
184ef883 251 echo "</TABLE></TD></TR></TABLE>\n";
a7d0eaf6 252}
253
184ef883 254/* Recent Search Table */
56e0b3b7 255if ($recent_count > 0) {
184ef883 256 echo "<BR>\n"
257 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
258 . '<TR><TD ALIGN=CENTER><B>Recent Searches</B></TD></TR><TR><TD>'
259 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
260 for ($i=0; $i < $recent_count; ++$i) {
56e0b3b7 261 if (!empty($what_array[$i])) {
184ef883 262 if ($folder_array[$i] == "") {
263 $folder_array[$i] = "INBOX";
264 }
265 if ($i % 2) {
266 echo "<TR BGCOLOR=\"$color[0]\">";
267 } else {
268 echo "<TR BGCOLOR=\"$color[4]\">";
269 }
270 echo "<TD WIDTH=35%>$folder_array[$i]</TD>"
271 . "<TD ALIGN=LEFT>$what_array[$i]</TD>"
272 . "<TD ALIGN=CENTER>$where_array[$i]</TD>"
273 . '<TD ALIGN=RIGHT>'
274 . "<A HREF=search.php?count=$i&submit=save>"
275 . _("save")
276 . '</A>'
277 . '&nbsp;|&nbsp;'
278 . '<A HREF=search.php'
279 . '?mailbox=' . urlencode($folder_array[$i])
280 . '&what=' . urlencode($what_array[$i])
281 . '&where=' . urlencode($where_array[$i])
282 . '&submit=Search_no_update'
283 . '>' . _("search") . '</A>'
284 . '&nbsp;|&nbsp;'
285 . "<A HREF=search.php?count=$i&submit=forget>"
286 . _("forget")
287 . '</A>'
288 . '</TD></TR>';
0d672ac0 289 }
29eb5486 290 }
184ef883 291 echo '</TABLE></TD></TR></TABLE><BR>';
56e0b3b7 292}
184ef883 293
294/* Search Form */
3b7d68e6 295echo '<B>' . _("Current Search") . '</B>'
f1c7f8bb 296 . '<FORM ACTION="search.php" NAME=s>'
184ef883 297 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
298 . ' <TR>'
299 . ' <TD><SELECT NAME="mailbox">';
56e0b3b7 300for ($i = 0; $i < count($boxes); $i++) {
301 if (!in_array('noselect', $boxes[$i]['flags'])) {
302 $box = $boxes[$i]['unformatted'];
303 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
3b7d68e6 304 if( $box2 == 'INBOX' ) {
305 $box2 = _("INBOX");
306 }
56e0b3b7 307 if ($mailbox == $box) {
308 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
0d672ac0 309 }
56e0b3b7 310 else {
311 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
88cb1b4d 312 }
56e0b3b7 313 }
314}
6c8388a9 315 echo "<OPTION VALUE=\"All Folders\"";
316 if ($mailbox == "All Folders") {
56e0b3b7 317 echo "SELECTED";
0d672ac0 318 }
56e0b3b7 319 echo ">All folders</OPTION>\n";
320echo ' </SELECT>'.
321 " </TD>\n".
322 " <TD ALIGN=\"CENTER\">\n";
88cb1b4d 323if ( !isset( $what ) ) {
324 $what = '';
325}
56e0b3b7 326$what_disp = str_replace(',', ' ', $what);
327$what_disp = str_replace('\\\\', '\\', $what_disp);
328$what_disp = str_replace('\\"', '"', $what_disp);
329$what_disp = str_replace('"', '&quot;', $what_disp);
330echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
331 " </TD>\n".
332 "<TD ALIGN=\"RIGHT\">\n".
333 "<SELECT NAME=\"where\">";
334s_opt( 'BODY', $where, _("Body") );
335s_opt( 'TEXT', $where, _("Everywhere") );
336s_opt( 'SUBJECT', $where, _("Subject") );
337s_opt( 'FROM', $where, _("From") );
338s_opt( 'CC', $where, _("Cc") );
339s_opt( 'TO', $where, _("To") );
340echo " </SELECT>\n" .
341 " </TD>\n".
342 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
343 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n".
344 " </TD>\n".
345 " </TR>\n".
346 "</FORM>\n".
347 " </TABLE>\n".
348 "</TD></TR></TABLE>\n";
349
350
88cb1b4d 351do_hook('search_after_form');
56e0b3b7 352
88cb1b4d 353/*
354 search all folders option still in the works. returns a table for each
23a9084b 355 folder it finds a match in.
88cb1b4d 356*/
56e0b3b7 357
88cb1b4d 358if ($search_all == 'all') {
359 $mailbox == '';
56e0b3b7 360 $boxcount = count($boxes);
88cb1b4d 361 echo '<BR><CENTER><B>' .
362 _("Search Results") .
363 "</B><CENTER><BR>\n";
56e0b3b7 364 for ($x=0;$x<$boxcount;$x++) {
365 if (!in_array('noselect', $boxes[$x]['flags'])) {
88cb1b4d 366 $mailbox = $boxes[$x]['unformatted'];
367 }
56e0b3b7 368 if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) {
369 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 370 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
23a9084b 371 array_push($perbox_count, $count_all);
372 }
373 }
374 for ($i=0;$i<count($perbox_count);$i++) {
375 if ($perbox_count[$i] != "") {
376 break;
88cb1b4d 377 }
23a9084b 378 $count_all = "none";
88cb1b4d 379 }
23a9084b 380 if ($count_all == "none") {
88cb1b4d 381 echo '<br><b>' .
382 _("No Messages found") .
383 '</b><br>';
56e0b3b7 384 }
0d672ac0 385}
0d672ac0 386
23a9084b 387/* search one folder option */
56e0b3b7 388else {
88cb1b4d 389 if (($submit == 'Search' || $submit == 'Search_no_update') && !empty($what)) {
390 echo '<BR><CENTER><B>' .
391 _("Search Results") .
392 "</B></CENTER>\n";
56e0b3b7 393 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 394 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
56e0b3b7 395 }
29eb5486 396}
56e0b3b7 397
23a9084b 398/* must have search terms to search */
88cb1b4d 399if ($submit == 'Search' && empty($what)) {
56e0b3b7 400 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
401}
402
29eb5486 403do_hook("search_bottom");
56e0b3b7 404sqimap_logout ($imapConnection);
29eb5486 405echo '</body></html>';
99d2a184 406
91954f9e 407?>