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