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