fixed comp_in_new handling
[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_utf7_decode_local.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 /* ------------------------ main ------------------------ */
163
164 /* reset these arrays on each page load just in case */
165 $attributes = array ();
166 $saved_attributes = array ();
167 $search_all = 'none';
168 $perbox_count = array ();
169 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
170
171 /* get mailbox names */
172 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
173 $boxes = sqimap_mailbox_list($imapConnection);
174
175 /* set current mailbox to INBOX if none was selected or if page
176 was called to search all folders. */
177 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
178 $mailbox = $boxes[0]['unformatted'];
179 }
180 if ($mailbox == 'All Folders') {
181 $search_all = 'all';
182 }
183
184 if (isset($composenew) && $composenew) {
185 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
186 "&amp;session=$composesession&amp;attachedmessages=true&amp";
187 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
188 } else {
189 displayPageHeader($color, $mailbox);
190 }
191 /* See how the page was called and fire off correct function */
192 if ((!isset($submit) || empty($submit)) && !empty($what)) {
193 $submit = _("Search");
194 }
195 if ( !isset( $submit ) ) {
196 $submit = '';
197 } else if ($submit == _("Search") && !empty($what)) {
198 if ($recent_count > 0) {
199 update_recent($what, $where, $mailbox, $username, $data_dir);
200 }
201 }
202 elseif ($submit == 'forget') {
203 forget_recent($count, $username, $data_dir);
204 }
205 elseif ($submit == 'save') {
206 save_recent($count, $username, $data_dir);
207 }
208 elseif ($submit == 'delete') {
209 delete_saved($count, $username, $data_dir);
210 }
211
212 do_hook('search_before_form');
213
214 echo "<BR>\n".
215 "<table width=\"100%\">\n".
216 "<TR><td bgcolor=\"$color[0]\">\n".
217 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
218 "</TD></TR>\n".
219 "</TABLE>\n";
220
221 /* update the recent and saved searches from the pref files */
222 $attributes = get_recent($username, $data_dir);
223 $saved_attributes = get_saved($username, $data_dir);
224 $saved_count = count($saved_attributes['saved_what']);
225 $count_all = 0;
226
227 /* Saved Search Table */
228 if ($saved_count > 0) {
229 echo "<BR>\n"
230 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
231 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
232 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
233 for ($i=0; $i < $saved_count; ++$i) {
234 if ($i % 2) {
235 echo "<TR BGCOLOR=\"$color[0]\">";
236 } else {
237 echo "<TR BGCOLOR=\"$color[4]\">";
238 }
239 echo "<TD WIDTH=\"35%\">".$saved_attributes['saved_folder'][$i]."</TD>"
240 . "<TD ALIGN=LEFT>".$saved_attributes['saved_what'][$i]."</TD>"
241 . "<TD ALIGN=CENTER>".$saved_attributes['saved_where'][$i]."</TD>"
242 . '<TD ALIGN=RIGHT>'
243 . '<A HREF=search.php'
244 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
245 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
246 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
247 . '>' . _("edit") . '</A>'
248 . '&nbsp;|&nbsp;'
249 . '<A HREF=search.php'
250 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
251 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
252 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
253 . '&amp;submit=Search_no_update'
254 . '>' . _("search") . '</A>'
255 . '&nbsp;|&nbsp;'
256 . "<A HREF=search.php?count=$i&amp;submit=delete>"
257 . _("delete")
258 . '</A>'
259 . '</TD></TR>';
260 }
261 echo "</TABLE></TD></TR></TABLE>\n";
262 }
263
264 /* Recent Search Table */
265 if ($recent_count > 0) {
266 echo "<BR>\n"
267 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
268 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
269 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
270 for ($i=1; $i <= $recent_count; ++$i) {
271 if (isset($attributes['search_folder'][$i])) {
272 if ($attributes['search_folder'][$i] == "") {
273 $attributes['search_folder'][$i] = "INBOX";
274 }
275 }
276 if ($i % 2) {
277 echo "<TR BGCOLOR=\"$color[0]\">";
278 } else {
279 echo "<TR BGCOLOR=\"$color[4]\">";
280 }
281 if (isset($attributes['search_what'][$i]) &&
282 !empty($attributes['search_what'][$i])) {
283 echo "<TD WIDTH=35%>".$attributes['search_folder'][$i]."</TD>"
284 . "<TD ALIGN=LEFT>".$attributes['search_what'][$i]."</TD>"
285 . "<TD ALIGN=CENTER>".$attributes['search_where'][$i]."</TD>"
286 . '<TD ALIGN=RIGHT>'
287 . "<A HREF=search.php?count=$i&amp;submit=save>"
288 . _("save")
289 . '</A>'
290 . '&nbsp;|&nbsp;'
291 . '<A HREF=search.php'
292 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
293 . '&amp;what=' . urlencode($attributes['search_what'][$i])
294 . '&amp;where=' . urlencode($attributes['search_where'][$i])
295 . '&amp;submit=Search_no_update'
296 . '>' . _("search") . '</A>'
297 . '&nbsp;|&nbsp;'
298 . "<A HREF=search.php?count=$i&amp;submit=forget>"
299 . _("forget")
300 . '</A>'
301 . '</TD></TR>';
302 }
303 }
304 echo '</TABLE></TD></TR></TABLE><BR>';
305 }
306
307 /* Search Form */
308 echo '<B>' . _("Current Search") . '</B>'
309 . '<FORM ACTION="search.php" NAME=s>'
310 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
311 . ' <TR>'
312 . ' <TD><SELECT NAME="mailbox">';
313 for ($i = 0; $i < count($boxes); $i++) {
314 if (!in_array('noselect', $boxes[$i]['flags'])) {
315 $box = $boxes[$i]['unformatted'];
316 $box2 = imap_utf7_decode_local(str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']));
317 if( $box2 == 'INBOX' ) {
318 $box2 = _("INBOX");
319 }
320 if ($mailbox == $box) {
321 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
322 }
323 else {
324 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
325 }
326 }
327 }
328 echo "<OPTION VALUE=\"All Folders\"";
329 if ($mailbox == "All Folders") {
330 echo "SELECTED";
331 }
332 echo ">All folders</OPTION>\n";
333 echo ' </SELECT>'.
334 " </TD>\n".
335 " <TD ALIGN=\"CENTER\">\n";
336 if ( !isset( $what ) ) {
337 $what = '';
338 }
339 if ( !isset( $where ) ) {
340 $where = '';
341 }
342
343
344 $what_disp = str_replace(',', ' ', $what);
345 $what_disp = str_replace('\\\\', '\\', $what_disp);
346 $what_disp = str_replace('\\"', '"', $what_disp);
347 $what_disp = str_replace('"', '&quot;', $what_disp);
348 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
349 " </TD>\n".
350 "<TD ALIGN=\"RIGHT\">\n".
351 "<SELECT NAME=\"where\">";
352 s_opt( 'BODY', $where, _("Body") );
353 s_opt( 'TEXT', $where, _("Everywhere") );
354 s_opt( 'SUBJECT', $where, _("Subject") );
355 s_opt( 'FROM', $where, _("From") );
356 s_opt( 'CC', $where, _("Cc") );
357 s_opt( 'TO', $where, _("To") );
358 echo " </SELECT>\n" .
359 " </TD>\n".
360 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
361 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
362 " </TD>\n".
363 " </TR>\n".
364 "</FORM>\n".
365 " </TABLE>\n".
366 "</TD></TR></TABLE>\n";
367
368
369 do_hook('search_after_form');
370
371 /*
372 search all folders option still in the works. returns a table for each
373 folder it finds a match in.
374 */
375
376 $old_value = 0;
377 if ($allow_thread_sort == TRUE) {
378 $old_value = $allow_thread_sort;
379 $allow_thread_sort = FALSE;
380 }
381
382 if ($search_all == 'all') {
383 $mailbox == '';
384 $boxcount = count($boxes);
385 echo '<BR><CENTER><B>' .
386 _("Search Results") .
387 "</B><CENTER><BR>\n";
388 for ($x=0;$x<$boxcount;$x++) {
389 if (!in_array('noselect', $boxes[$x]['flags'])) {
390 $mailbox = $boxes[$x]['unformatted'];
391 }
392 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
393 sqimap_mailbox_select($imapConnection, $mailbox);
394 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
395 array_push($perbox_count, $count_all);
396 }
397 }
398 for ($i=0;$i<count($perbox_count);$i++) {
399 if ($perbox_count[$i] != "") {
400 $count_all = "found";
401 break;
402 }
403 }
404 if ($count_all != "found") {
405 echo '<br><b>' .
406 _("No Messages found") .
407 '</b><br>';
408 }
409 }
410
411 /* search one folder option */
412 else {
413 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
414 echo '<BR><CENTER><B>' .
415 _("Search Results") .
416 "</B></CENTER>\n";
417 sqimap_mailbox_select($imapConnection, $mailbox);
418 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
419 }
420 }
421
422 /* must have search terms to search */
423 if ($submit == _("Search") && empty($what)) {
424 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
425 }
426
427 $allow_thread_sort = $old_value;
428
429 do_hook('search_bottom');
430 sqimap_logout ($imapConnection);
431 echo '</body></html>';
432
433 ?>