Basic mailto: support.
[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 sqgetGlobalVar('username', $username, SQ_SESSION);
27 sqgetGlobalVar('key', $key, SQ_COOKIE);
28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
29 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
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 mailbox names */
242 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
243 $boxes = sqimap_mailbox_list($imapConnection);
244
245 /* set current mailbox to INBOX if none was selected or if page
246 was called to search all folders. */
247 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
248 $mailbox = $boxes[0]['unformatted'];
249 }
250 if ($mailbox == 'All Folders') {
251 $search_all = 'all';
252 }
253
254 if (isset($composenew) && $composenew) {
255 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
256 "&amp;session=$composesession&amp;attachedmessages=true&amp";
257 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
258 } else {
259 displayPageHeader($color, $mailbox);
260 }
261 /* See how the page was called and fire off correct function */
262 if ((!isset($submit) || empty($submit)) && !empty($what)) {
263 $submit = _("Search");
264 }
265 if ( !isset( $submit ) ) {
266 $submit = '';
267 } else if ($submit == _("Search") && !empty($what)) {
268 if ($recent_count > 0) {
269 update_recent($what, $where, $mailbox, $username, $data_dir);
270 }
271 }
272 elseif ($submit == 'forget') {
273 forget_recent($count, $username, $data_dir);
274 }
275 elseif ($submit == 'save') {
276 save_recent($count, $username, $data_dir);
277 }
278 elseif ($submit == 'delete') {
279 delete_saved($count, $username, $data_dir);
280 }
281
282 do_hook('search_before_form');
283
284 echo html_tag( 'table',
285 html_tag( 'tr', "\n" .
286 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
287 ) ,
288 '', '', 'width="100%"') . "\n";
289
290 /* update the recent and saved searches from the pref files */
291 $attributes = get_recent($username, $data_dir);
292 $saved_attributes = get_saved($username, $data_dir);
293 $saved_count = count($saved_attributes['saved_what']);
294 $count_all = 0;
295
296 /* Saved Search Table */
297 if ($saved_count > 0) {
298 echo "<br>\n"
299 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
300 . html_tag( 'tr',
301 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
302 )
303 . html_tag( 'tr' )
304 . html_tag( 'td' )
305 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
306 for ($i=0; $i < $saved_count; ++$i) {
307 if ($i % 2) {
308 echo html_tag( 'tr', '', '', $color[0] );
309 } else {
310 echo html_tag( 'tr', '', '', $color[4] );
311 }
312 echo html_tag( 'td', imap_utf7_decode_local($saved_attributes['saved_folder'][$i]), 'left', '', 'width="35%"' )
313 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
314 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
315 . html_tag( 'td', '', 'right' )
316 . '<a href=search.php'
317 . '?mailbox=' . htmlspecialchars($saved_attributes['saved_folder'][$i])
318 . '&amp;what=' . htmlspecialchars($saved_attributes['saved_what'][$i])
319 . '&amp;where=' . htmlspecialchars($saved_attributes['saved_where'][$i])
320 . '>' . _("edit") . '</a>'
321 . '&nbsp;|&nbsp;'
322 . '<a href=search.php'
323 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
324 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
325 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
326 . '&amp;submit=Search_no_update'
327 . '>' . _("search") . '</a>'
328 . '&nbsp;|&nbsp;'
329 . "<a href=search.php?count=$i&amp;submit=delete>"
330 . _("delete")
331 . '</a>'
332 . '</td></tr>';
333 }
334 echo "</table></td></tr></table>\n";
335 }
336
337 if ($recent_count > 0) {
338 echo "<br>\n"
339 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
340 . html_tag( 'tr',
341 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
342 )
343 . html_tag( 'tr' )
344 . html_tag( 'td' )
345 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
346 for ($i=1; $i <= $recent_count; ++$i) {
347 if (isset($attributes['search_folder'][$i])) {
348 if ($attributes['search_folder'][$i] == "") {
349 $attributes['search_folder'][$i] = "INBOX";
350 }
351 }
352 if ($i % 2) {
353 echo html_tag( 'tr', '', '', $color[0] );
354 } else {
355 echo html_tag( 'tr', '', '', $color[0] );
356 }
357 if (isset($attributes['search_what'][$i]) &&
358 !empty($attributes['search_what'][$i])) {
359 echo html_tag( 'td', imap_utf7_decode_local($attributes['search_folder'][$i]), 'left', '', 'width="35%"' )
360 . html_tag( 'td', htmlspecialchars($attributes['search_what'][$i]), 'left' )
361 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
362 . html_tag( 'td', '', 'right' )
363 . "<a href=search.php?count=$i&amp;submit=save>"
364 . _("save")
365 . '</a>'
366 . '&nbsp;|&nbsp;'
367 . '<a href=search.php'
368 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
369 . '&amp;what=' . urlencode($attributes['search_what'][$i])
370 . '&amp;where=' . urlencode($attributes['search_where'][$i])
371 . '&amp;submit=Search_no_update'
372 . '>' . _("search") . '</a>'
373 . '&nbsp;|&nbsp;'
374 . "<a href=search.php?count=$i&amp;submit=forget>"
375 . _("forget")
376 . '</a>'
377 . '</td></tr>';
378 }
379 }
380 echo '</table></td></tr></table><br>';
381 }
382
383
384 if (isset($newsort)) {
385 $sort = $newsort;
386 sqsession_register($sort, 'sort');
387 }
388
389 /*********************************************************************
390 * Check to see if we can use cache or not. Currently the only time *
391 * when you will not use it is when a link on the left hand frame is *
392 * used. Also check to make sure we actually have the array in the *
393 * registered session data. :) *
394 *********************************************************************/
395 if (! isset($use_mailbox_cache)) {
396 $use_mailbox_cache = 0;
397 }
398
399 /* There is a problem with registered vars in 4.1 */
400 /*
401 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
402 $use_mailbox_cache = FALSE;
403 }
404 */
405
406 /* Search Form */
407 echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
408 . '<form action="search.php" name="s">'
409 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
410 . html_tag( 'tr' )
411 . html_tag( 'td', '', 'left' )
412 . '<select name="mailbox">'
413 . '<option value="All Folders"';
414 if ($mailbox == 'All Folders') {
415 echo ' selected';
416 }
417 echo '>[ ' . _("All Folders") . " ]</option>\n";
418
419 $show_selected = array(strtolower($mailbox));
420 echo sqimap_mailbox_option_list($imapConnection, $show_selected, 0, $boxes);
421
422 echo ' </select>'.
423 " </td>\n";
424 if ( !isset( $what ) ) {
425 $what = '';
426 }
427 if ( !isset( $where ) ) {
428 $where = 'FROM';
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 ?>