More rg=0
[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 bgcolor="' . $color[0] . '" border="0" width="100%" cellpadding="1" cellspacing="0"><tr><td>';
209 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
210 $msg_cnt_str, $toggle_all, 1);
211
212
213 printHeader($mailbox, 6, $color, false);
214
215 displayMessageArray($imapConnection, $cnt, 1,
216 $msort, $mailbox, $sort, $color, $cnt, $where, $what);
217
218 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
219 echo '</td></tr></table>';
220
221 }
222 }
223
224 /* ------------------------ main ------------------------ */
225
226 /* reset these arrays on each page load just in case */
227 $attributes = array ();
228 $saved_attributes = array ();
229 $search_all = 'none';
230 $perbox_count = array ();
231 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
232
233 /* get globals we may need */
234
235 $key = $_COOKIE['key'];
236 $username = $_SESSION['username'];
237 $onetimepad = $_SESSION['onetimepad'];
238 $delimiter = $_SESSION['delimiter'];
239
240 if (isset($_GET['mailbox'])) {
241 $mailbox = strip_tags($_GET['mailbox']);
242 }
243 if (isset($_GET['submit'])) {
244 $submit = strip_tags($_GET['submit']);
245 }
246 if (isset($_GET['what'])) {
247 $what = $_GET['what'];
248 }
249 if (isset($_GET['where'])) {
250 $where = strip_tags($_GET['where']);
251 }
252 if (isset($_GET['checkall'])) {
253 $checkall = strip_tags($_GET['checkall']);
254 }
255 if (isset($_GET['count'])) {
256 $count = strip_tags($_GET['count']);
257 }
258
259 /* get mailbox names */
260 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
261 $boxes = sqimap_mailbox_list($imapConnection);
262
263 /* set current mailbox to INBOX if none was selected or if page
264 was called to search all folders. */
265 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
266 $mailbox = $boxes[0]['unformatted'];
267 }
268 if ($mailbox == 'All Folders') {
269 $search_all = 'all';
270 }
271
272 if (isset($composenew) && $composenew) {
273 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
274 "&amp;session=$composesession&amp;attachedmessages=true&amp";
275 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
276 } else {
277 displayPageHeader($color, $mailbox);
278 }
279 /* See how the page was called and fire off correct function */
280 if ((!isset($submit) || empty($submit)) && !empty($what)) {
281 $submit = _("Search");
282 }
283 if ( !isset( $submit ) ) {
284 $submit = '';
285 } else if ($submit == _("Search") && !empty($what)) {
286 if ($recent_count > 0) {
287 update_recent($what, $where, $mailbox, $username, $data_dir);
288 }
289 }
290 elseif ($submit == 'forget') {
291 forget_recent($count, $username, $data_dir);
292 }
293 elseif ($submit == 'save') {
294 save_recent($count, $username, $data_dir);
295 }
296 elseif ($submit == 'delete') {
297 delete_saved($count, $username, $data_dir);
298 }
299
300 do_hook('search_before_form');
301
302 echo html_tag( 'table',
303 html_tag( 'tr', "\n" .
304 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
305 ) ,
306 '', '', 'width="100%"') . "\n";
307
308 /* update the recent and saved searches from the pref files */
309 $attributes = get_recent($username, $data_dir);
310 $saved_attributes = get_saved($username, $data_dir);
311 $saved_count = count($saved_attributes['saved_what']);
312 $count_all = 0;
313
314 /* Saved Search Table */
315 if ($saved_count > 0) {
316 echo "<br>\n"
317 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
318 . html_tag( 'tr',
319 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
320 )
321 . html_tag( 'tr' )
322 . html_tag( 'td' )
323 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
324 for ($i=0; $i < $saved_count; ++$i) {
325 if ($i % 2) {
326 echo html_tag( 'tr', '', '', $color[0] );
327 } else {
328 echo html_tag( 'tr', '', '', $color[4] );
329 }
330 echo html_tag( 'td', $saved_attributes['saved_folder'][$i], 'left', '', 'width="35%"' )
331 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
332 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
333 . html_tag( 'td', '', 'right' )
334 . '<a href=search.php'
335 . '?mailbox=' . htmlentities($saved_attributes['saved_folder'][$i])
336 . '&amp;what=' . htmlentities($saved_attributes['saved_what'][$i])
337 . '&amp;where=' . htmlentities($saved_attributes['saved_where'][$i])
338 . '>' . _("edit") . '</a>'
339 . '&nbsp;|&nbsp;'
340 . '<a href=search.php'
341 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
342 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
343 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
344 . '&amp;submit=Search_no_update'
345 . '>' . _("search") . '</a>'
346 . '&nbsp;|&nbsp;'
347 . "<a href=search.php?count=$i&amp;submit=delete>"
348 . _("delete")
349 . '</a>'
350 . '</td></tr>';
351 }
352 echo "</table></td></tr></table>\n";
353 }
354
355 if ($recent_count > 0) {
356 echo "<br>\n"
357 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
358 . html_tag( 'tr',
359 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
360 )
361 . html_tag( 'tr' )
362 . html_tag( 'td' )
363 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
364 for ($i=1; $i <= $recent_count; ++$i) {
365 if (isset($attributes['search_folder'][$i])) {
366 if ($attributes['search_folder'][$i] == "") {
367 $attributes['search_folder'][$i] = "INBOX";
368 }
369 }
370 if ($i % 2) {
371 echo html_tag( 'tr', '', '', $color[0] );
372 } else {
373 echo html_tag( 'tr', '', '', $color[0] );
374 }
375 if (isset($attributes['search_what'][$i]) &&
376 !empty($attributes['search_what'][$i])) {
377 echo html_tag( 'td', $attributes['search_folder'][$i], 'left', '', 'width="35%"' )
378 . html_tag( 'td', $attributes['search_what'][$i], 'left' )
379 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
380 . html_tag( 'td', '', 'right' )
381 . "<a href=search.php?count=$i&amp;submit=save>"
382 . _("save")
383 . '</a>'
384 . '&nbsp;|&nbsp;'
385 . '<a href=search.php'
386 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
387 . '&amp;what=' . urlencode($attributes['search_what'][$i])
388 . '&amp;where=' . urlencode($attributes['search_where'][$i])
389 . '&amp;submit=Search_no_update'
390 . '>' . _("search") . '</a>'
391 . '&nbsp;|&nbsp;'
392 . "<a href=search.php?count=$i&amp;submit=forget>"
393 . _("forget")
394 . '</a>'
395 . '</td></tr>';
396 }
397 }
398 echo '</table></td></tr></table><br>';
399 }
400
401
402 if (isset($newsort)) {
403 $sort = $newsort;
404 session_register('sort');
405 }
406
407 /*********************************************************************
408 * Check to see if we can use cache or not. Currently the only time *
409 * when you will not use it is when a link on the left hand frame is *
410 * used. Also check to make sure we actually have the array in the *
411 * registered session data. :) *
412 *********************************************************************/
413 if (! isset($use_mailbox_cache)) {
414 $use_mailbox_cache = 0;
415 }
416
417 /* There is a problem with registered vars in 4.1 */
418 /*
419 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
420 $use_mailbox_cache = FALSE;
421 }
422 */
423
424 /* Search Form */
425 echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
426 . '<form action="search.php" name="s">'
427 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
428 . html_tag( 'tr' )
429 . html_tag( 'td', '', 'left' )
430 . '<select name="mailbox">';
431 for ($i = 0; $i < count($boxes); $i++) {
432 if (!in_array('noselect', $boxes[$i]['flags'])) {
433 $box = $boxes[$i]['unformatted'];
434 $box2 = str_replace(' ', '&nbsp;',
435 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
436 if( $box2 == 'INBOX' ) {
437 $box2 = _("INBOX");
438 }
439 echo ' <option value="' . $box . '"';
440 if ($mailbox == $box) { echo ' selected'; }
441 echo '>' . $box2 . '</option>' . "\n";
442 }
443 }
444 echo '<option value="All Folders"';
445 if ($mailbox == 'All Folders') {
446 echo ' selected';
447 }
448 echo ">All folders</option>\n";
449 echo ' </select>'.
450 " </td>\n";
451 if ( !isset( $what ) ) {
452 $what = '';
453 }
454 if ( !isset( $where ) ) {
455 $where = '';
456 }
457
458
459 $what_disp = str_replace(',', ' ', $what);
460 $what_disp = str_replace('\\\\', '\\', $what_disp);
461 $what_disp = str_replace('\\"', '"', $what_disp);
462 $what_disp = str_replace('"', '&quot;', $what_disp);
463 echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' )
464 . html_tag( 'td', '', 'right' )
465 . "<select name=\"where\">";
466 s_opt( 'BODY', $where, _("Body") );
467 s_opt( 'TEXT', $where, _("Everywhere") );
468 s_opt( 'SUBJECT', $where, _("Subject") );
469 s_opt( 'FROM', $where, _("From") );
470 s_opt( 'CC', $where, _("Cc") );
471 s_opt( 'TO', $where, _("To") );
472 echo " </select>\n" .
473 " </td>\n".
474 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
475 " </tr>\n".
476 "</form>\n".
477 " </table>\n".
478 "</td></tr></table>\n";
479
480
481 do_hook('search_after_form');
482
483 /*
484 search all folders option still in the works. returns a table for each
485 folder it finds a match in.
486 */
487
488 $old_value = 0;
489 if ($allow_thread_sort == TRUE) {
490 $old_value = $allow_thread_sort;
491 $allow_thread_sort = FALSE;
492 }
493
494 if ($search_all == 'all') {
495 $mailbox == '';
496 $boxcount = count($boxes);
497 echo '<BR><CENTER><B>' .
498 _("Search Results") .
499 "</B><CENTER><BR>\n";
500 for ($x=0;$x<$boxcount;$x++) {
501 if (!in_array('noselect', $boxes[$x]['flags'])) {
502 $mailbox = $boxes[$x]['unformatted'];
503 }
504 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
505 sqimap_mailbox_select($imapConnection, $mailbox);
506 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
507 $count_all = count($msgs);
508 printSearchMessages($msgs, $mailbox, $count_all, $imapConnection,
509 $where, $what, false, false);
510 array_push($perbox_count, $count_all);
511 }
512 }
513 for ($i=0;$i<count($perbox_count);$i++) {
514 if ($perbox_count[$i]) {
515 $count_all = true;
516 break;
517 }
518 }
519 if (!$count_all) {
520 echo '<br><center>' . _("No Messages Found") . '</center>';
521 }
522 }
523
524 /* search one folder option */
525 else {
526 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
527 echo '<br>'
528 . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n";
529 sqimap_mailbox_select($imapConnection, $mailbox);
530 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
531 if (count($msgs)) {
532 printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection,
533 $where, $what, false, false);
534 } else {
535 echo '<br><center>' . _("No Messages Found") . '</center>';
536 }
537 }
538 }
539
540 /* must have search terms to search */
541 if ($submit == _("Search") && empty($what)) {
542 echo '<br>'
543 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
544 }
545
546 $allow_thread_sort = $old_value;
547
548
549 do_hook('search_bottom');
550 sqimap_logout ($imapConnection);
551 echo '</body></html>';
552
553 ?>