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