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