6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
12 require_once('../src/validate.php');
13 require_once('../functions/imap.php');
14 require_once('../functions/imap_search.php');
15 require_once('../functions/imap_utf7_decode_local.php');
16 require_once('../functions/array.php');
17 require_once('../functions/strings.php');
19 global $allow_thread_sort;
21 /* here are some functions, could go in imap_search.php
23 this was here, pretty handy */
24 function s_opt( $val, $sel, $tit ) {
25 echo " <option value=\"$val\"";
29 echo ">$tit</option>\n";
32 /* function to get the recent searches and put them in the attributes array */
33 function get_recent($username, $data_dir) {
34 $attributes = array();
35 $types = array('search_what', 'search_where', 'search_folder');
36 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
37 for ($x=1;$x<=$recent_count;$x++
) {
39 foreach ($types as $key) {
40 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
46 /* function to get the saved searches and put them in the saved_attributes array */
47 function get_saved($username, $data_dir) {
48 $saved_attributes = array();
49 $types = array('saved_what', 'saved_where', 'saved_folder');
50 foreach ($types as $key) {
52 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
53 if ($saved_attributes[$key][$x] == "") {
54 array_pop($saved_attributes[$key]);
59 return $saved_attributes;
62 /* function to update recent pref arrays */
63 function update_recent($what, $where, $mailbox, $username, $data_dir) {
64 $attributes = array();
65 $types = array('search_what', 'search_where', 'search_folder');
66 $input = array($what, $where, $mailbox);
67 $attributes = get_recent( $username, $data_dir);
70 for ($i=1;$i<=count($attributes['search_what']);$i++
) {
71 if (isset($attributes['search_what'][$i])) {
72 if ($what == $attributes['search_what'][$i] &&
73 $where == $attributes['search_where'][$i] &&
74 $mailbox == $attributes['search_folder'][$i]) {
81 foreach ($types as $key) {
82 array_push ($attributes[$key], $input[$i]);
83 array_shift ($attributes[$key]);
86 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
88 for ($i=1;$i<=$recent_count;$i++
) {
90 foreach ($types as $key) {
91 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
98 /* function to forget a recent search */
99 function forget_recent($forget_index, $username, $data_dir) {
100 $attributes = array();
101 $types = array('search_what', 'search_where', 'search_folder');
102 $attributes = get_recent( $username, $data_dir);
104 foreach ($types as $key) {
105 array_splice($attributes[$key], $forget_index, 1);
106 array_unshift($attributes[$key], '');
109 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
111 for ($i=1;$i<=$recent_count;$i++
) {
113 foreach ($types as $key) {
114 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
120 /* function to delete a saved search */
121 function delete_saved($delete_index, $username, $data_dir) {
122 $types = array('saved_what', 'saved_where', 'saved_folder');
123 $attributes = get_saved($username, $data_dir);
124 foreach ($types as $key) {
125 array_splice($attributes[$key], $delete_index, 1);
129 $saved_count = count($attributes['saved_what']);
130 $last_element = $saved_count +
1;
131 for ($i=1;$i<=$saved_count;$i++
) {
133 foreach ($types as $key) {
134 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
139 foreach($types as $key) {
140 removePref($data_dir, $username, $key.$last_element);
144 /* function to save a search from recent to saved */
145 function save_recent($save_index, $username, $data_dir) {
146 $attributes = array();
147 $types = array('search_what', 'search_where', 'search_folder');
148 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
149 $saved_array = get_saved($username, $data_dir);
150 $save_index = $save_index -1;
151 $saved_count = (count($saved_array['saved_what']) +
1);
152 $attributes = get_recent ($username, $data_dir);
154 foreach ($types as $key) {
155 $slice = array_slice($attributes[$key], $save_index, 1);
156 $name = $saved_types[$n];
157 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
162 function printSearchMessages($msgs,$mailbox, $cnt, $imapConnection, $usecache = false, $newsort = false) {
163 global $sort, $color;
165 $msort = calc_msort($msgs, $sort, $cnt, true);
168 if ( $mailbox == 'INBOX' ) {
169 $showbox = _("INBOX");
171 $showbox = imap_utf7_decode_local($mailbox);
173 echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
175 displayMessageArray($imapConnection, $cnt, 1,
176 $msgs, $msort, $mailbox, $sort, $color,
182 /* ------------------------ main ------------------------ */
184 /* reset these arrays on each page load just in case */
185 $attributes = array ();
186 $saved_attributes = array ();
187 $search_all = 'none';
188 $perbox_count = array ();
189 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
192 /* get mailbox names */
193 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
194 $boxes = sqimap_mailbox_list($imapConnection);
196 /* set current mailbox to INBOX if none was selected or if page
197 was called to search all folders. */
198 if ( !isset($mailbox) ||
$mailbox == 'None' ||
$mailbox == '' ) {
199 $mailbox = $boxes[0]['unformatted'];
201 if ($mailbox == 'All Folders') {
205 if (isset($composenew) && $composenew) {
206 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
207 "&session=$composesession&attachedmessages=true&";
208 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
210 displayPageHeader($color, $mailbox);
212 /* See how the page was called and fire off correct function */
213 if ((!isset($submit) ||
empty($submit)) && !empty($what)) {
214 $submit = _("Search");
216 if ( !isset( $submit ) ) {
218 } else if ($submit == _("Search") && !empty($what)) {
219 if ($recent_count > 0) {
220 update_recent($what, $where, $mailbox, $username, $data_dir);
223 elseif ($submit == 'forget') {
224 forget_recent($count, $username, $data_dir);
226 elseif ($submit == 'save') {
227 save_recent($count, $username, $data_dir);
229 elseif ($submit == 'delete') {
230 delete_saved($count, $username, $data_dir);
233 do_hook('search_before_form');
237 html_tag( 'tr', "\n" .
238 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
240 '', '', 'width="100%"') . "\n";
242 /* update the recent and saved searches from the pref files */
243 $attributes = get_recent($username, $data_dir);
244 $saved_attributes = get_saved($username, $data_dir);
245 $saved_count = count($saved_attributes['saved_what']);
248 /* Saved Search Table */
249 if ($saved_count > 0) {
251 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
253 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
257 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
258 for ($i=0; $i < $saved_count; ++
$i) {
260 echo html_tag( 'tr', '', '', $color[0] );
262 echo html_tag( 'tr', '', '', $color[4] );
264 echo html_tag( 'td', $saved_attributes['saved_folder'][$i], 'left', '', 'width="35%"' )
265 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
266 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
267 . html_tag( 'td', '', 'right' )
268 . '<a href=search.php'
269 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
270 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
271 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
272 . '>' . _("edit") . '</a>'
274 . '<a href=search.php'
275 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
276 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
277 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
278 . '&submit=Search_no_update'
279 . '>' . _("search") . '</a>'
281 . "<a href=search.php?count=$i&submit=delete>"
286 echo "</table></td></tr></table>\n";
289 if ($recent_count > 0) {
291 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
293 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
297 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
298 for ($i=1; $i <= $recent_count; ++
$i) {
299 if (isset($attributes['search_folder'][$i])) {
300 if ($attributes['search_folder'][$i] == "") {
301 $attributes['search_folder'][$i] = "INBOX";
305 echo html_tag( 'tr', '', '', $color[0] );
307 echo html_tag( 'tr', '', '', $color[0] );
309 if (isset($attributes['search_what'][$i]) &&
310 !empty($attributes['search_what'][$i])) {
311 echo html_tag( 'td', $attributes['search_folder'][$i], 'left', '', 'width="35%"' )
312 . html_tag( 'td', $attributes['search_what'][$i], 'left' )
313 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
314 . html_tag( 'td', '', 'right' )
315 . "<a href=search.php?count=$i&submit=save>"
319 . '<a href=search.php'
320 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
321 . '&what=' . urlencode($attributes['search_what'][$i])
322 . '&where=' . urlencode($attributes['search_where'][$i])
323 . '&submit=Search_no_update'
324 . '>' . _("search") . '</a>'
326 . "<a href=search.php?count=$i&submit=forget>"
332 echo '</table></td></tr></table><br>';
336 if (isset($newsort)) {
338 session_register('sort');
341 /*********************************************************************
342 * Check to see if we can use cache or not. Currently the only time *
343 * when you will not use it is when a link on the left hand frame is *
344 * used. Also check to make sure we actually have the array in the *
345 * registered session data. :) *
346 *********************************************************************/
347 if (! isset($use_mailbox_cache)) {
348 $use_mailbox_cache = 0;
351 /* There is a problem with registered vars in 4.1 */
353 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
354 $use_mailbox_cache = FALSE;
359 echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
360 . '<form action="search.php" name="s">'
361 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
363 . html_tag( 'td', '', 'left' )
364 . '<select name="mailbox">';
365 for ($i = 0; $i < count($boxes); $i++
) {
366 if (!in_array('noselect', $boxes[$i]['flags'])) {
367 $box = $boxes[$i]['unformatted'];
368 $box2 = str_replace(' ', ' ',
369 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
370 if( $box2 == 'INBOX' ) {
373 echo ' <option value="' . $box . '"';
374 if ($mailbox == $box) { echo ' selected'; }
375 echo '>' . $box2 . '</option>' . "\n";
378 echo '<option value="All Folders"';
379 if ($mailbox == 'All Folders') {
382 echo ">All folders</option>\n";
385 if ( !isset( $what ) ) {
388 if ( !isset( $where ) ) {
393 $what_disp = str_replace(',', ' ', $what);
394 $what_disp = str_replace('\\\\', '\\', $what_disp);
395 $what_disp = str_replace('\\"', '"', $what_disp);
396 $what_disp = str_replace('"', '"', $what_disp);
397 echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' )
398 . html_tag( 'td', '', 'right' )
399 . "<select name=\"where\">";
400 s_opt( 'BODY', $where, _("Body") );
401 s_opt( 'TEXT', $where, _("Everywhere") );
402 s_opt( 'SUBJECT', $where, _("Subject") );
403 s_opt( 'FROM', $where, _("From") );
404 s_opt( 'CC', $where, _("Cc") );
405 s_opt( 'TO', $where, _("To") );
406 echo " </select>\n" .
408 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
412 "</td></tr></table>\n";
415 do_hook('search_after_form');
418 search all folders option still in the works. returns a table for each
419 folder it finds a match in.
423 if ($allow_thread_sort == TRUE) {
424 $old_value = $allow_thread_sort;
425 $allow_thread_sort = FALSE;
428 if ($search_all == 'all') {
430 $boxcount = count($boxes);
431 echo '<BR><CENTER><B>' .
432 _("Search Results") .
433 "</B><CENTER><BR>\n";
434 for ($x=0;$x<$boxcount;$x++
) {
435 if (!in_array('noselect', $boxes[$x]['flags'])) {
436 $mailbox = $boxes[$x]['unformatted'];
438 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
439 sqimap_mailbox_select($imapConnection, $mailbox);
440 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
441 $count_all = count($msgs);
442 printSearchMessages($msgs, $mailbox, $count_all, $imapConnection);
443 array_push($perbox_count, $count_all);
446 for ($i=0;$i<count($perbox_count);$i++
) {
447 if ($perbox_count[$i]) {
453 echo '<br><center>' . _("No Messages Found") . '</center>';
457 /* search one folder option */
459 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
461 . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n";
462 sqimap_mailbox_select($imapConnection, $mailbox);
463 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
465 printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection);
467 echo '<br><center>' . _("No Messages Found") . '</center>';
472 /* must have search terms to search */
473 if ($submit == _("Search") && empty($what)) {
475 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
478 $allow_thread_sort = $old_value;
481 do_hook('search_bottom');
482 sqimap_logout ($imapConnection);
483 echo '</body></html>';