"Kent B. Hansen" <kent@tpv.dk> & r2l by Yoav
[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');
361c7059 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
548a552a 184if (isset($composenew) && $composenew) {
d7f8e6e6 185 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
186 "&amp;session=$composesession&amp;attachedmessages=true&amp";
187 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
548a552a 188} else {
189 displayPageHeader($color, $mailbox);
190}
23a9084b 191/* See how the page was called and fire off correct function */
d4144adf 192if ((!isset($submit) || empty($submit)) && !empty($what)) {
70c4fd84 193 $submit = _("Search");
d4144adf 194}
70c4fd84 195if ( !isset( $submit ) ) {
70c4fd84 196 $submit = '';
197} else if ($submit == _("Search") && !empty($what)) {
aa45f943 198 if ($recent_count > 0) {
199 update_recent($what, $where, $mailbox, $username, $data_dir);
200 }
56e0b3b7 201}
88cb1b4d 202elseif ($submit == 'forget') {
56e0b3b7 203 forget_recent($count, $username, $data_dir);
204}
88cb1b4d 205elseif ($submit == 'save') {
56e0b3b7 206 save_recent($count, $username, $data_dir);
207}
88cb1b4d 208elseif ($submit == 'delete') {
56e0b3b7 209 delete_saved($count, $username, $data_dir);
210}
23a9084b 211
29eb5486 212do_hook('search_before_form');
56e0b3b7 213
214echo "<BR>\n".
88cb1b4d 215 "<table width=\"100%\">\n".
216 "<TR><td bgcolor=\"$color[0]\">\n".
217 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
218 "</TD></TR>\n".
219 "</TABLE>\n";
c61bb006 220
23a9084b 221/* update the recent and saved searches from the pref files */
bebf762c 222$attributes = get_recent($username, $data_dir);
bebf762c 223$saved_attributes = get_saved($username, $data_dir);
224$saved_count = count($saved_attributes['saved_what']);
91954f9e 225$count_all = 0;
a7d0eaf6 226
184ef883 227/* Saved Search Table */
56e0b3b7 228if ($saved_count > 0) {
184ef883 229 echo "<BR>\n"
88cb1b4d 230 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
231 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
232 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
184ef883 233 for ($i=0; $i < $saved_count; ++$i) {
88cb1b4d 234 if ($i % 2) {
235 echo "<TR BGCOLOR=\"$color[0]\">";
236 } else {
184ef883 237 echo "<TR BGCOLOR=\"$color[4]\">";
238 }
bebf762c 239 echo "<TD WIDTH=\"35%\">".$saved_attributes['saved_folder'][$i]."</TD>"
240 . "<TD ALIGN=LEFT>".$saved_attributes['saved_what'][$i]."</TD>"
241 . "<TD ALIGN=CENTER>".$saved_attributes['saved_where'][$i]."</TD>"
88cb1b4d 242 . '<TD ALIGN=RIGHT>'
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])
88cb1b4d 247 . '>' . _("edit") . '</A>'
248 . '&nbsp;|&nbsp;'
249 . '<A HREF=search.php'
bebf762c 250 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
251 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
252 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
5e9e90fd 253 . '&amp;submit=Search_no_update'
88cb1b4d 254 . '>' . _("search") . '</A>'
255 . '&nbsp;|&nbsp;'
5e9e90fd 256 . "<A HREF=search.php?count=$i&amp;submit=delete>"
88cb1b4d 257 . _("delete")
258 . '</A>'
259 . '</TD></TR>';
56e0b3b7 260 }
184ef883 261 echo "</TABLE></TD></TR></TABLE>\n";
a7d0eaf6 262}
263
184ef883 264/* Recent Search Table */
56e0b3b7 265if ($recent_count > 0) {
184ef883 266 echo "<BR>\n"
267 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
962c725d 268 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
184ef883 269 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
bebf762c 270 for ($i=1; $i <= $recent_count; ++$i) {
271 if (isset($attributes['search_folder'][$i])) {
272 if ($attributes['search_folder'][$i] == "") {
273 $attributes['search_folder'][$i] = "INBOX";
274 }
184ef883 275 }
276 if ($i % 2) {
277 echo "<TR BGCOLOR=\"$color[0]\">";
278 } else {
279 echo "<TR BGCOLOR=\"$color[4]\">";
280 }
bebf762c 281 if (isset($attributes['search_what'][$i]) &&
282 !empty($attributes['search_what'][$i])) {
283 echo "<TD WIDTH=35%>".$attributes['search_folder'][$i]."</TD>"
284 . "<TD ALIGN=LEFT>".$attributes['search_what'][$i]."</TD>"
285 . "<TD ALIGN=CENTER>".$attributes['search_where'][$i]."</TD>"
184ef883 286 . '<TD ALIGN=RIGHT>'
5e9e90fd 287 . "<A HREF=search.php?count=$i&amp;submit=save>"
184ef883 288 . _("save")
289 . '</A>'
290 . '&nbsp;|&nbsp;'
291 . '<A HREF=search.php'
bebf762c 292 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
293 . '&amp;what=' . urlencode($attributes['search_what'][$i])
294 . '&amp;where=' . urlencode($attributes['search_where'][$i])
5e9e90fd 295 . '&amp;submit=Search_no_update'
184ef883 296 . '>' . _("search") . '</A>'
297 . '&nbsp;|&nbsp;'
5e9e90fd 298 . "<A HREF=search.php?count=$i&amp;submit=forget>"
184ef883 299 . _("forget")
300 . '</A>'
301 . '</TD></TR>';
0d672ac0 302 }
bebf762c 303 }
184ef883 304 echo '</TABLE></TD></TR></TABLE><BR>';
56e0b3b7 305}
184ef883 306
307/* Search Form */
3b7d68e6 308echo '<B>' . _("Current Search") . '</B>'
f1c7f8bb 309 . '<FORM ACTION="search.php" NAME=s>'
184ef883 310 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
311 . ' <TR>'
312 . ' <TD><SELECT NAME="mailbox">';
56e0b3b7 313for ($i = 0; $i < count($boxes); $i++) {
314 if (!in_array('noselect', $boxes[$i]['flags'])) {
315 $box = $boxes[$i]['unformatted'];
46bc57c1 316 $box2 = str_replace(' ', '&nbsp;',
317 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
3b7d68e6 318 if( $box2 == 'INBOX' ) {
319 $box2 = _("INBOX");
70c4fd84 320 }
56e0b3b7 321 if ($mailbox == $box) {
322 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
0d672ac0 323 }
56e0b3b7 324 else {
325 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
88cb1b4d 326 }
56e0b3b7 327 }
328}
6c8388a9 329 echo "<OPTION VALUE=\"All Folders\"";
330 if ($mailbox == "All Folders") {
56e0b3b7 331 echo "SELECTED";
0d672ac0 332 }
56e0b3b7 333 echo ">All folders</OPTION>\n";
334echo ' </SELECT>'.
335 " </TD>\n".
336 " <TD ALIGN=\"CENTER\">\n";
88cb1b4d 337if ( !isset( $what ) ) {
338 $what = '';
339}
e970aa41 340if ( !isset( $where ) ) {
341 $where = '';
342}
343
344
56e0b3b7 345$what_disp = str_replace(',', ' ', $what);
346$what_disp = str_replace('\\\\', '\\', $what_disp);
347$what_disp = str_replace('\\"', '"', $what_disp);
348$what_disp = str_replace('"', '&quot;', $what_disp);
349echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
350 " </TD>\n".
351 "<TD ALIGN=\"RIGHT\">\n".
352 "<SELECT NAME=\"where\">";
353s_opt( 'BODY', $where, _("Body") );
354s_opt( 'TEXT', $where, _("Everywhere") );
355s_opt( 'SUBJECT', $where, _("Subject") );
356s_opt( 'FROM', $where, _("From") );
357s_opt( 'CC', $where, _("Cc") );
358s_opt( 'TO', $where, _("To") );
359echo " </SELECT>\n" .
360 " </TD>\n".
361 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
70c4fd84 362 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
56e0b3b7 363 " </TD>\n".
364 " </TR>\n".
365 "</FORM>\n".
366 " </TABLE>\n".
367 "</TD></TR></TABLE>\n";
368
369
88cb1b4d 370do_hook('search_after_form');
56e0b3b7 371
88cb1b4d 372/*
373 search all folders option still in the works. returns a table for each
70c4fd84 374 folder it finds a match in.
88cb1b4d 375*/
56e0b3b7 376
e92eab8b 377$old_value = 0;
794d59c0 378if ($allow_thread_sort == TRUE) {
e92eab8b 379 $old_value = $allow_thread_sort;
794d59c0 380 $allow_thread_sort = FALSE;
e92eab8b 381}
382
88cb1b4d 383if ($search_all == 'all') {
384 $mailbox == '';
56e0b3b7 385 $boxcount = count($boxes);
88cb1b4d 386 echo '<BR><CENTER><B>' .
387 _("Search Results") .
388 "</B><CENTER><BR>\n";
56e0b3b7 389 for ($x=0;$x<$boxcount;$x++) {
390 if (!in_array('noselect', $boxes[$x]['flags'])) {
88cb1b4d 391 $mailbox = $boxes[$x]['unformatted'];
392 }
70c4fd84 393 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
56e0b3b7 394 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 395 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
23a9084b 396 array_push($perbox_count, $count_all);
397 }
398 }
399 for ($i=0;$i<count($perbox_count);$i++) {
400 if ($perbox_count[$i] != "") {
e13e66be 401 $count_all = "found";
402 break;
88cb1b4d 403 }
404 }
e13e66be 405 if ($count_all != "found") {
88cb1b4d 406 echo '<br><b>' .
407 _("No Messages found") .
408 '</b><br>';
56e0b3b7 409 }
0d672ac0 410}
0d672ac0 411
23a9084b 412/* search one folder option */
56e0b3b7 413else {
70c4fd84 414 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
88cb1b4d 415 echo '<BR><CENTER><B>' .
416 _("Search Results") .
417 "</B></CENTER>\n";
56e0b3b7 418 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 419 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
56e0b3b7 420 }
29eb5486 421}
56e0b3b7 422
23a9084b 423/* must have search terms to search */
70c4fd84 424if ($submit == _("Search") && empty($what)) {
56e0b3b7 425 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
426}
427
e92eab8b 428$allow_thread_sort = $old_value;
429
70c4fd84 430do_hook('search_bottom');
56e0b3b7 431sqimap_logout ($imapConnection);
29eb5486 432echo '</body></html>';
99d2a184 433
91954f9e 434?>