UI mods
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
11
12 /* Path for SquirrelMail required files. */
13 define('SM_PATH','../');
14
15 /* SquirrelMail required files. */
16 require_once(SM_PATH . 'include/validate.php');
17 require_once(SM_PATH . 'functions/imap.php');
18 require_once(SM_PATH . 'functions/imap_search.php');
19 require_once(SM_PATH . 'functions/imap_mailbox.php');
20 require_once(SM_PATH . 'functions/array.php');
21 require_once(SM_PATH . 'functions/strings.php');
22
23 global $allow_thread_sort;
24
25 /* get globals we may need */
26
27 $key = $_COOKIE['key'];
28 $username = $_SESSION['username'];
29 $onetimepad = $_SESSION['onetimepad'];
30 $delimiter = $_SESSION['delimiter'];
31
32 if (isset($_GET['mailbox'])) {
33 $mailbox = strip_tags($_GET['mailbox']);
34 }
35 if (isset($_GET['submit'])) {
36 $submit = strip_tags($_GET['submit']);
37 }
38 if (isset($_GET['what'])) {
39 $what = $_GET['what'];
40 }
41 if (isset($_GET['where'])) {
42 $where = strip_tags($_GET['where']);
43 }
44 if (isset($_GET['checkall'])) {
45 $checkall = strip_tags($_GET['checkall']);
46 }
47 if (isset($_GET['count'])) {
48 $count = strip_tags($_GET['count']);
49 }
50 /* end of get globals */
51
52 /* here are some functions, could go in imap_search.php
53 this was here, pretty handy */
54 function s_opt( $val, $sel, $tit ) {
55 echo " <option value=\"$val\"";
56 if ( $sel == $val ) {
57 echo ' selected';
58 }
59 echo ">$tit</option>\n";
60 }
61
62 /* function to get the recent searches and put them in the attributes array */
63 function get_recent($username, $data_dir) {
64 $attributes = array();
65 $types = array('search_what', 'search_where', 'search_folder');
66 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
67 for ($x=1;$x<=$recent_count;$x++) {
68 reset($types);
69 foreach ($types as $key) {
70 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
71 }
72 }
73 return $attributes;
74 }
75
76 /* function to get the saved searches and put them in the saved_attributes array */
77 function get_saved($username, $data_dir) {
78 $saved_attributes = array();
79 $types = array('saved_what', 'saved_where', 'saved_folder');
80 foreach ($types as $key) {
81 for ($x=1;;$x++) {
82 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
83 if ($saved_attributes[$key][$x] == "") {
84 array_pop($saved_attributes[$key]);
85 break;
86 }
87 }
88 }
89 return $saved_attributes;
90 }
91
92 /* function to update recent pref arrays */
93 function update_recent($what, $where, $mailbox, $username, $data_dir) {
94 $attributes = array();
95 $types = array('search_what', 'search_where', 'search_folder');
96 $input = array($what, $where, $mailbox);
97 $attributes = get_recent( $username, $data_dir);
98 reset($types);
99 $dupe = 'no';
100 for ($i=1;$i<=count($attributes['search_what']);$i++) {
101 if (isset($attributes['search_what'][$i])) {
102 if ($what == $attributes['search_what'][$i] &&
103 $where == $attributes['search_where'][$i] &&
104 $mailbox == $attributes['search_folder'][$i]) {
105 $dupe = 'yes';
106 }
107 }
108 }
109 if ($dupe == 'no') {
110 $i = 0;
111 foreach ($types as $key) {
112 array_push ($attributes[$key], $input[$i]);
113 array_shift ($attributes[$key]);
114 $i++;
115 }
116 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
117 $n=0;
118 for ($i=1;$i<=$recent_count;$i++) {
119 reset($types);
120 foreach ($types as $key) {
121 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
122 }
123 $n++;
124 }
125 }
126 }
127
128 /* function to forget a recent search */
129 function forget_recent($forget_index, $username, $data_dir) {
130 $attributes = array();
131 $types = array('search_what', 'search_where', 'search_folder');
132 $attributes = get_recent( $username, $data_dir);
133 reset($types);
134 foreach ($types as $key) {
135 array_splice($attributes[$key], $forget_index - 1, 1);
136 array_unshift($attributes[$key], '');
137 }
138 reset($types);
139 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
140 $n=0;
141 for ($i=1;$i<=$recent_count;$i++) {
142 reset($types);
143 foreach ($types as $key) {
144 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
145 }
146 $n++;
147 }
148 }
149
150 /* function to delete a saved search */
151 function delete_saved($delete_index, $username, $data_dir) {
152 $types = array('saved_what', 'saved_where', 'saved_folder');
153 $attributes = get_saved($username, $data_dir);
154 foreach ($types as $key) {
155 array_splice($attributes[$key], $delete_index, 1);
156 }
157 reset($types);
158 $n=0;
159 $saved_count = count($attributes['saved_what']);
160 $last_element = $saved_count + 1;
161 for ($i=1;$i<=$saved_count;$i++) {
162 reset($types);
163 foreach ($types as $key) {
164 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
165 }
166 $n++;
167 }
168 reset($types);
169 foreach($types as $key) {
170 removePref($data_dir, $username, $key.$last_element);
171 }
172 }
173
174 /* function to save a search from recent to saved */
175 function save_recent($save_index, $username, $data_dir) {
176 $attributes = array();
177 $types = array('search_what', 'search_where', 'search_folder');
178 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
179 $saved_array = get_saved($username, $data_dir);
180 $save_index = $save_index -1;
181 $saved_count = (count($saved_array['saved_what']) + 1);
182 $attributes = get_recent ($username, $data_dir);
183 $n = 0;
184 foreach ($types as $key) {
185 $slice = array_slice($attributes[$key], $save_index, 1);
186 $name = $saved_types[$n];
187 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
188 $n++;
189 }
190 }
191
192 function printSearchMessages($msgs,$mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false) {
193 global $sort, $color;
194
195 $msort = calc_msort($msgs, $sort);
196 if ($cnt > 0) {
197 if ( $mailbox == 'INBOX' ) {
198 $showbox = _("INBOX");
199 } else {
200 $showbox = imap_utf7_decode_local($mailbox);
201 }
202 echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
203
204
205 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
206 $toggle_all = get_selectall_link(1, $sort);
207
208 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
209 echo '<tr><td>';
210
211 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
212 $msg_cnt_str, $toggle_all, 1);
213
214 echo '</td></tr>';
215 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
216 echo '<tr><td>';
217 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
218 echo ' <tr><td>';
219 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
220 echo '<tr><td>';
221
222 printHeader($mailbox, 6, $color, false);
223
224 displayMessageArray($imapConnection, $cnt, 1,
225 $msort, $mailbox, $sort, $color, $cnt, $where, $what);
226
227 echo '</td></tr></table></td></tr></table>';
228 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
229 echo '</td></tr></table>';
230 }
231 }
232
233 /* ------------------------ main ------------------------ */
234
235 /* reset these arrays on each page load just in case */
236 $attributes = array ();
237 $saved_attributes = array ();
238 $search_all = 'none';
239 $perbox_count = array ();
240 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
241
242 /* get globals we may need */
243
244 $key = $_COOKIE['key'];
245 $username = $_SESSION['username'];
246 $onetimepad = $_SESSION['onetimepad'];
247 $delimiter = $_SESSION['delimiter'];
248
249 if (isset($_GET['mailbox'])) {
250 $mailbox = strip_tags($_GET['mailbox']);
251 }
252 if (isset($_GET['submit'])) {
253 $submit = strip_tags($_GET['submit']);
254 }
255 if (isset($_GET['what'])) {
256 $what = $_GET['what'];
257 }
258 if (isset($_GET['where'])) {
259 $where = strip_tags($_GET['where']);
260 }
261 if (isset($_GET['checkall'])) {
262 $checkall = strip_tags($_GET['checkall']);
263 }
264 if (isset($_GET['count'])) {
265 $count = strip_tags($_GET['count']);
266 }
267
268 /* get mailbox names */
269 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
270 $boxes = sqimap_mailbox_list($imapConnection);
271
272 /* set current mailbox to INBOX if none was selected or if page
273 was called to search all folders. */
274 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
275 $mailbox = $boxes[0]['unformatted'];
276 }
277 if ($mailbox == 'All Folders') {
278 $search_all = 'all';
279 }
280
281 if (isset($composenew) && $composenew) {
282 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
283 "&amp;session=$composesession&amp;attachedmessages=true&amp";
284 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
285 } else {
286 displayPageHeader($color, $mailbox);
287 }
288 /* See how the page was called and fire off correct function */
289 if ((!isset($submit) || empty($submit)) && !empty($what)) {
290 $submit = _("Search");
291 }
292 if ( !isset( $submit ) ) {
293 $submit = '';
294 } else if ($submit == _("Search") && !empty($what)) {
295 if ($recent_count > 0) {
296 update_recent($what, $where, $mailbox, $username, $data_dir);
297 }
298 }
299 elseif ($submit == 'forget') {
300 forget_recent($count, $username, $data_dir);
301 }
302 elseif ($submit == 'save') {
303 save_recent($count, $username, $data_dir);
304 }
305 elseif ($submit == 'delete') {
306 delete_saved($count, $username, $data_dir);
307 }
308
309 do_hook('search_before_form');
310
311 echo html_tag( 'table',
312 html_tag( 'tr', "\n" .
313 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
314 ) ,
315 '', '', 'width="100%"') . "\n";
316
317 /* update the recent and saved searches from the pref files */
318 $attributes = get_recent($username, $data_dir);
319 $saved_attributes = get_saved($username, $data_dir);
320 $saved_count = count($saved_attributes['saved_what']);
321 $count_all = 0;
322
323 /* Saved Search Table */
324 if ($saved_count > 0) {
325 echo "<br>\n"
326 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
327 . html_tag( 'tr',
328 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
329 )
330 . html_tag( 'tr' )
331 . html_tag( 'td' )
332 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
333 for ($i=0; $i < $saved_count; ++$i) {
334 if ($i % 2) {
335 echo html_tag( 'tr', '', '', $color[0] );
336 } else {
337 echo html_tag( 'tr', '', '', $color[4] );
338 }
339 echo html_tag( 'td', $saved_attributes['saved_folder'][$i], 'left', '', 'width="35%"' )
340 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
341 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
342 . html_tag( 'td', '', 'right' )
343 . '<a href=search.php'
344 . '?mailbox=' . htmlentities($saved_attributes['saved_folder'][$i])
345 . '&amp;what=' . htmlentities($saved_attributes['saved_what'][$i])
346 . '&amp;where=' . htmlentities($saved_attributes['saved_where'][$i])
347 . '>' . _("edit") . '</a>'
348 . '&nbsp;|&nbsp;'
349 . '<a href=search.php'
350 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
351 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
352 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
353 . '&amp;submit=Search_no_update'
354 . '>' . _("search") . '</a>'
355 . '&nbsp;|&nbsp;'
356 . "<a href=search.php?count=$i&amp;submit=delete>"
357 . _("delete")
358 . '</a>'
359 . '</td></tr>';
360 }
361 echo "</table></td></tr></table>\n";
362 }
363
364 if ($recent_count > 0) {
365 echo "<br>\n"
366 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
367 . html_tag( 'tr',
368 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
369 )
370 . html_tag( 'tr' )
371 . html_tag( 'td' )
372 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
373 for ($i=1; $i <= $recent_count; ++$i) {
374 if (isset($attributes['search_folder'][$i])) {
375 if ($attributes['search_folder'][$i] == "") {
376 $attributes['search_folder'][$i] = "INBOX";
377 }
378 }
379 if ($i % 2) {
380 echo html_tag( 'tr', '', '', $color[0] );
381 } else {
382 echo html_tag( 'tr', '', '', $color[0] );
383 }
384 if (isset($attributes['search_what'][$i]) &&
385 !empty($attributes['search_what'][$i])) {
386 echo html_tag( 'td', $attributes['search_folder'][$i], 'left', '', 'width="35%"' )
387 . html_tag( 'td', $attributes['search_what'][$i], 'left' )
388 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
389 . html_tag( 'td', '', 'right' )
390 . "<a href=search.php?count=$i&amp;submit=save>"
391 . _("save")
392 . '</a>'
393 . '&nbsp;|&nbsp;'
394 . '<a href=search.php'
395 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
396 . '&amp;what=' . urlencode($attributes['search_what'][$i])
397 . '&amp;where=' . urlencode($attributes['search_where'][$i])
398 . '&amp;submit=Search_no_update'
399 . '>' . _("search") . '</a>'
400 . '&nbsp;|&nbsp;'
401 . "<a href=search.php?count=$i&amp;submit=forget>"
402 . _("forget")
403 . '</a>'
404 . '</td></tr>';
405 }
406 }
407 echo '</table></td></tr></table><br>';
408 }
409
410
411 if (isset($newsort)) {
412 $sort = $newsort;
413 sqsession_register($sort, 'sort');
414 }
415
416 /*********************************************************************
417 * Check to see if we can use cache or not. Currently the only time *
418 * when you will not use it is when a link on the left hand frame is *
419 * used. Also check to make sure we actually have the array in the *
420 * registered session data. :) *
421 *********************************************************************/
422 if (! isset($use_mailbox_cache)) {
423 $use_mailbox_cache = 0;
424 }
425
426 /* There is a problem with registered vars in 4.1 */
427 /*
428 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
429 $use_mailbox_cache = FALSE;
430 }
431 */
432
433 /* Search Form */
434 echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
435 . '<form action="search.php" name="s">'
436 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
437 . html_tag( 'tr' )
438 . html_tag( 'td', '', 'left' )
439 . '<select name="mailbox">';
440 for ($i = 0; $i < count($boxes); $i++) {
441 if (!in_array('noselect', $boxes[$i]['flags'])) {
442 $box = $boxes[$i]['unformatted'];
443 $box2 = str_replace(' ', '&nbsp;',
444 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
445 if( $box2 == 'INBOX' ) {
446 $box2 = _("INBOX");
447 }
448 echo ' <option value="' . $box . '"';
449 if ($mailbox == $box) { echo ' selected'; }
450 echo '>' . $box2 . '</option>' . "\n";
451 }
452 }
453 echo '<option value="All Folders"';
454 if ($mailbox == 'All Folders') {
455 echo ' selected';
456 }
457 echo ">All folders</option>\n";
458 echo ' </select>'.
459 " </td>\n";
460 if ( !isset( $what ) ) {
461 $what = '';
462 }
463 if ( !isset( $where ) ) {
464 $where = 'FROM';
465 }
466
467
468 $what_disp = str_replace(',', ' ', $what);
469 $what_disp = str_replace('\\\\', '\\', $what_disp);
470 $what_disp = str_replace('\\"', '"', $what_disp);
471 $what_disp = str_replace('"', '&quot;', $what_disp);
472 echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' )
473 . html_tag( 'td', '', 'right' )
474 . "<select name=\"where\">";
475 s_opt( 'BODY', $where, _("Body") );
476 s_opt( 'TEXT', $where, _("Everywhere") );
477 s_opt( 'SUBJECT', $where, _("Subject") );
478 s_opt( 'FROM', $where, _("From") );
479 s_opt( 'CC', $where, _("Cc") );
480 s_opt( 'TO', $where, _("To") );
481 echo " </select>\n" .
482 " </td>\n".
483 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
484 " </tr>\n".
485 "</form>\n".
486 " </table>\n".
487 "</td></tr></table>\n";
488
489
490 do_hook('search_after_form');
491
492 /*
493 search all folders option still in the works. returns a table for each
494 folder it finds a match in.
495 */
496
497 $old_value = 0;
498 if ($allow_thread_sort == TRUE) {
499 $old_value = $allow_thread_sort;
500 $allow_thread_sort = FALSE;
501 }
502
503 if ($search_all == 'all') {
504 $mailbox == '';
505 $boxcount = count($boxes);
506 echo '<BR><CENTER><B>' .
507 _("Search Results") .
508 "</B><CENTER><BR>\n";
509 for ($x=0;$x<$boxcount;$x++) {
510 if (!in_array('noselect', $boxes[$x]['flags'])) {
511 $mailbox = $boxes[$x]['unformatted'];
512 }
513 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
514 sqimap_mailbox_select($imapConnection, $mailbox);
515 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
516 $count_all = count($msgs);
517 printSearchMessages($msgs, $mailbox, $count_all, $imapConnection,
518 $where, $what, false, false);
519 array_push($perbox_count, $count_all);
520 }
521 }
522 for ($i=0;$i<count($perbox_count);$i++) {
523 if ($perbox_count[$i]) {
524 $count_all = true;
525 break;
526 }
527 }
528 if (!$count_all) {
529 echo '<br><center>' . _("No Messages Found") . '</center>';
530 }
531 }
532
533 /* search one folder option */
534 else {
535 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
536 echo '<br>'
537 . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n";
538 sqimap_mailbox_select($imapConnection, $mailbox);
539 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
540 if (count($msgs)) {
541 printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection,
542 $where, $what, false, false);
543 } else {
544 echo '<br><center>' . _("No Messages Found") . '</center>';
545 }
546 }
547 }
548
549 /* must have search terms to search */
550 if ($submit == _("Search") && empty($what)) {
551 echo '<br>'
552 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
553 }
554
555 $allow_thread_sort = $old_value;
556
557
558 do_hook('search_bottom');
559 sqimap_logout ($imapConnection);
560 echo '</body></html>';
561
562 ?>