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