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