6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
12 /* Path for SquirrelMail required files. */
13 define('SM_PATH','../');
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');
23 global $allow_thread_sort;
25 /* get globals we may need */
27 $key = $_COOKIE['key'];
28 $username = $_SESSION['username'];
29 $onetimepad = $_SESSION['onetimepad'];
30 $delimiter = $_SESSION['delimiter'];
32 if (isset($_GET['mailbox'])) {
33 $mailbox = strip_tags($_GET['mailbox']);
35 if (isset($_GET['submit'])) {
36 $submit = strip_tags($_GET['submit']);
38 if (isset($_GET['what'])) {
39 $what = $_GET['what'];
41 if (isset($_GET['where'])) {
42 $where = strip_tags($_GET['where']);
44 if (isset($_GET['checkall'])) {
45 $checkall = strip_tags($_GET['checkall']);
47 if (isset($_GET['count'])) {
48 $count = strip_tags($_GET['count']);
50 /* end of get globals */
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\"";
59 echo ">$tit</option>\n";
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++
) {
69 foreach ($types as $key) {
70 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
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) {
82 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
83 if ($saved_attributes[$key][$x] == "") {
84 array_pop($saved_attributes[$key]);
89 return $saved_attributes;
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);
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]) {
111 foreach ($types as $key) {
112 array_push ($attributes[$key], $input[$i]);
113 array_shift ($attributes[$key]);
116 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
118 for ($i=1;$i<=$recent_count;$i++
) {
120 foreach ($types as $key) {
121 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
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);
134 foreach ($types as $key) {
135 array_splice($attributes[$key], $forget_index - 1, 1);
136 array_unshift($attributes[$key], '');
139 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
141 for ($i=1;$i<=$recent_count;$i++
) {
143 foreach ($types as $key) {
144 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
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);
159 $saved_count = count($attributes['saved_what']);
160 $last_element = $saved_count +
1;
161 for ($i=1;$i<=$saved_count;$i++
) {
163 foreach ($types as $key) {
164 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
169 foreach($types as $key) {
170 removePref($data_dir, $username, $key.$last_element);
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);
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]);
192 function printSearchMessages($msgs,$mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false) {
193 global $sort, $color;
195 $msort = calc_msort($msgs, $sort);
197 if ( $mailbox == 'INBOX' ) {
198 $showbox = _("INBOX");
200 $showbox = imap_utf7_decode_local($mailbox);
202 echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
205 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
206 $toggle_all = get_selectall_link(1, $sort);
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);
213 printHeader($mailbox, 6, $color, false);
215 displayMessageArray($imapConnection, $cnt, 1,
216 $msort, $mailbox, $sort, $color, $cnt, $where, $what);
218 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
219 echo '</td></tr></table>';
224 /* ------------------------ main ------------------------ */
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);
233 /* get globals we may need */
235 $key = $_COOKIE['key'];
236 $username = $_SESSION['username'];
237 $onetimepad = $_SESSION['onetimepad'];
238 $delimiter = $_SESSION['delimiter'];
240 if (isset($_GET['mailbox'])) {
241 $mailbox = strip_tags($_GET['mailbox']);
243 if (isset($_GET['submit'])) {
244 $submit = strip_tags($_GET['submit']);
246 if (isset($_GET['what'])) {
247 $what = $_GET['what'];
249 if (isset($_GET['where'])) {
250 $where = strip_tags($_GET['where']);
252 if (isset($_GET['checkall'])) {
253 $checkall = strip_tags($_GET['checkall']);
255 if (isset($_GET['count'])) {
256 $count = strip_tags($_GET['count']);
259 /* get mailbox names */
260 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
261 $boxes = sqimap_mailbox_list($imapConnection);
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'];
268 if ($mailbox == 'All Folders') {
272 if (isset($composenew) && $composenew) {
273 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
274 "&session=$composesession&attachedmessages=true&";
275 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
277 displayPageHeader($color, $mailbox);
279 /* See how the page was called and fire off correct function */
280 if ((!isset($submit) ||
empty($submit)) && !empty($what)) {
281 $submit = _("Search");
283 if ( !isset( $submit ) ) {
285 } else if ($submit == _("Search") && !empty($what)) {
286 if ($recent_count > 0) {
287 update_recent($what, $where, $mailbox, $username, $data_dir);
290 elseif ($submit == 'forget') {
291 forget_recent($count, $username, $data_dir);
293 elseif ($submit == 'save') {
294 save_recent($count, $username, $data_dir);
296 elseif ($submit == 'delete') {
297 delete_saved($count, $username, $data_dir);
300 do_hook('search_before_form');
302 echo html_tag( 'table',
303 html_tag( 'tr', "\n" .
304 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
306 '', '', 'width="100%"') . "\n";
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']);
314 /* Saved Search Table */
315 if ($saved_count > 0) {
317 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
319 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
323 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
324 for ($i=0; $i < $saved_count; ++
$i) {
326 echo html_tag( 'tr', '', '', $color[0] );
328 echo html_tag( 'tr', '', '', $color[4] );
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 . '&what=' . htmlentities($saved_attributes['saved_what'][$i])
337 . '&where=' . htmlentities($saved_attributes['saved_where'][$i])
338 . '>' . _("edit") . '</a>'
340 . '<a href=search.php'
341 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
342 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
343 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
344 . '&submit=Search_no_update'
345 . '>' . _("search") . '</a>'
347 . "<a href=search.php?count=$i&submit=delete>"
352 echo "</table></td></tr></table>\n";
355 if ($recent_count > 0) {
357 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
359 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
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";
371 echo html_tag( 'tr', '', '', $color[0] );
373 echo html_tag( 'tr', '', '', $color[0] );
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&submit=save>"
385 . '<a href=search.php'
386 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
387 . '&what=' . urlencode($attributes['search_what'][$i])
388 . '&where=' . urlencode($attributes['search_where'][$i])
389 . '&submit=Search_no_update'
390 . '>' . _("search") . '</a>'
392 . "<a href=search.php?count=$i&submit=forget>"
398 echo '</table></td></tr></table><br>';
402 if (isset($newsort)) {
404 sqsession_register($sort, 'sort');
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;
417 /* There is a problem with registered vars in 4.1 */
419 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
420 $use_mailbox_cache = FALSE;
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"' )
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(' ', ' ',
435 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
436 if( $box2 == 'INBOX' ) {
439 echo ' <option value="' . $box . '"';
440 if ($mailbox == $box) { echo ' selected'; }
441 echo '>' . $box2 . '</option>' . "\n";
444 echo '<option value="All Folders"';
445 if ($mailbox == 'All Folders') {
448 echo ">All folders</option>\n";
451 if ( !isset( $what ) ) {
454 if ( !isset( $where ) ) {
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('"', '"', $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" .
474 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
478 "</td></tr></table>\n";
481 do_hook('search_after_form');
484 search all folders option still in the works. returns a table for each
485 folder it finds a match in.
489 if ($allow_thread_sort == TRUE) {
490 $old_value = $allow_thread_sort;
491 $allow_thread_sort = FALSE;
494 if ($search_all == 'all') {
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'];
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);
513 for ($i=0;$i<count($perbox_count);$i++
) {
514 if ($perbox_count[$i]) {
520 echo '<br><center>' . _("No Messages Found") . '</center>';
524 /* search one folder option */
526 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
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);
532 printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection,
533 $where, $what, false, false);
535 echo '<br><center>' . _("No Messages Found") . '</center>';
540 /* must have search terms to search */
541 if ($submit == _("Search") && empty($what)) {
543 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
546 $allow_thread_sort = $old_value;
549 do_hook('search_bottom');
550 sqimap_logout ($imapConnection);
551 echo '</body></html>';