6 * Copyright (c) 1999-2003 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/strings.php');
22 global $allow_thread_sort;
24 /* get globals we may need */
26 sqgetGlobalVar('username', $username, SQ_SESSION
);
27 sqgetGlobalVar('key', $key, SQ_COOKIE
);
28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
29 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
31 if (isset($_GET['mailbox'])) {
32 $mailbox = strip_tags($_GET['mailbox']);
34 if (isset($_GET['submit'])) {
35 $submit = strip_tags($_GET['submit']);
37 if (isset($_GET['what'])) {
38 $what = $_GET['what'];
40 if (isset($_GET['where'])) {
41 $where = strip_tags($_GET['where']);
43 if (isset($_GET['checkall'])) {
44 $checkall = strip_tags($_GET['checkall']);
46 if (isset($_GET['count'])) {
47 $count = strip_tags($_GET['count']);
49 /* end of get globals */
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\"";
58 echo ">$tit</option>\n";
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++
) {
68 foreach ($types as $key) {
69 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
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) {
81 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
82 if ($saved_attributes[$key][$x] == "") {
83 array_pop($saved_attributes[$key]);
88 return $saved_attributes;
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);
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]) {
110 foreach ($types as $key) {
111 array_push ($attributes[$key], $input[$i]);
112 array_shift ($attributes[$key]);
115 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
117 for ($i=1;$i<=$recent_count;$i++
) {
119 foreach ($types as $key) {
120 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
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);
133 foreach ($types as $key) {
134 array_splice($attributes[$key], $forget_index - 1, 1);
135 array_unshift($attributes[$key], '');
138 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
140 for ($i=1;$i<=$recent_count;$i++
) {
142 foreach ($types as $key) {
143 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
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);
158 $saved_count = count($attributes['saved_what']);
159 $last_element = $saved_count +
1;
160 for ($i=1;$i<=$saved_count;$i++
) {
162 foreach ($types as $key) {
163 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
168 foreach($types as $key) {
169 removePref($data_dir, $username, $key.$last_element);
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);
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]);
191 function printSearchMessages($msgs,$mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false) {
192 global $sort, $color;
195 $msort = calc_msort($msgs, $sort);
196 if ( $mailbox == 'INBOX' ) {
197 $showbox = _("INBOX");
199 $showbox = imap_utf7_decode_local($mailbox);
201 echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
204 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
205 $toggle_all = get_selectall_link(1, $sort);
207 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
210 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
211 $msg_cnt_str, $toggle_all, 1);
214 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
216 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
218 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
221 printHeader($mailbox, 6, $color, false);
223 displayMessageArray($imapConnection, $cnt, 1,
224 $msort, $mailbox, $sort, $color, $cnt, $where, $what);
226 echo '</td></tr></table></td></tr></table>';
227 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
228 echo '</td></tr></table>';
232 /* ------------------------ main ------------------------ */
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);
241 /* get mailbox names */
242 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
243 $boxes = sqimap_mailbox_list($imapConnection);
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'];
250 if ($mailbox == 'All Folders') {
254 if (isset($composenew) && $composenew) {
255 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
256 "&session=$composesession&attachedmessages=true&";
257 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
259 displayPageHeader($color, $mailbox);
261 /* See how the page was called and fire off correct function */
262 if ((!isset($submit) ||
empty($submit)) && !empty($what)) {
263 $submit = _("Search");
265 if ( !isset( $submit ) ) {
267 } else if ($submit == _("Search") && !empty($what)) {
268 if ($recent_count > 0) {
269 update_recent($what, $where, $mailbox, $username, $data_dir);
272 elseif ($submit == 'forget') {
273 forget_recent($count, $username, $data_dir);
275 elseif ($submit == 'save') {
276 save_recent($count, $username, $data_dir);
278 elseif ($submit == 'delete') {
279 delete_saved($count, $username, $data_dir);
282 do_hook('search_before_form');
284 echo html_tag( 'table',
285 html_tag( 'tr', "\n" .
286 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
288 '', '', 'width="100%"') . "\n";
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']);
296 /* Saved Search Table */
297 if ($saved_count > 0) {
299 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
301 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
305 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
306 for ($i=0; $i < $saved_count; ++
$i) {
308 echo html_tag( 'tr', '', '', $color[0] );
310 echo html_tag( 'tr', '', '', $color[4] );
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 . '&what=' . htmlspecialchars($saved_attributes['saved_what'][$i])
319 . '&where=' . htmlspecialchars($saved_attributes['saved_where'][$i])
320 . '>' . _("edit") . '</a>'
322 . '<a href=search.php'
323 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
324 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
325 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
326 . '&submit=Search_no_update'
327 . '>' . _("search") . '</a>'
329 . "<a href=search.php?count=$i&submit=delete>"
334 echo "</table></td></tr></table>\n";
337 if ($recent_count > 0) {
339 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
341 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
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";
353 echo html_tag( 'tr', '', '', $color[0] );
355 echo html_tag( 'tr', '', '', $color[0] );
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&submit=save>"
367 . '<a href=search.php'
368 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
369 . '&what=' . urlencode($attributes['search_what'][$i])
370 . '&where=' . urlencode($attributes['search_where'][$i])
371 . '&submit=Search_no_update'
372 . '>' . _("search") . '</a>'
374 . "<a href=search.php?count=$i&submit=forget>"
380 echo '</table></td></tr></table><br>';
384 if (isset($newsort)) {
386 sqsession_register($sort, 'sort');
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;
399 /* There is a problem with registered vars in 4.1 */
401 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
402 $use_mailbox_cache = FALSE;
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"' )
411 . html_tag( 'td', '', 'left' )
412 . '<select name="mailbox">'
413 . '<option value="All Folders"';
414 if ($mailbox == 'All Folders') {
417 echo '>[ ' . _("All Folders") . " ]</option>\n";
419 $show_selected = array(strtolower($mailbox));
420 echo sqimap_mailbox_option_list($imapConnection, $show_selected, 0, $boxes);
424 if ( !isset( $what ) ) {
427 if ( !isset( $where ) ) {
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('"', '"', $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" .
447 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
451 "</td></tr></table>\n";
454 do_hook('search_after_form');
457 search all folders option still in the works. returns a table for each
458 folder it finds a match in.
462 if ($allow_thread_sort == TRUE) {
463 $old_value = $allow_thread_sort;
464 $allow_thread_sort = FALSE;
467 if ($search_all == 'all') {
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'];
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);
486 for ($i=0;$i<count($perbox_count);$i++
) {
487 if ($perbox_count[$i]) {
493 echo '<br><center>' . _("No Messages Found") . '</center>';
497 /* search one folder option */
499 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
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);
505 printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection,
506 $where, $what, false, false);
508 echo '<br><center>' . _("No Messages Found") . '</center>';
513 /* must have search terms to search */
514 if ($submit == _("Search") && empty($what)) {
516 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
519 $allow_thread_sort = $old_value;
522 do_hook('search_bottom');
523 sqimap_logout ($imapConnection);
524 echo '</body></html>';