8ace7760cb04d2182659311e9745bfa33d35e1db
[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/array.php');
16 require_once('../functions/strings.php');
17
18 global $allow_thread_sort;
19
20 /* here are some functions, could go in imap_search.php
21
22 this was here, pretty handy */
23 function s_opt( $val, $sel, $tit ) {
24 echo " <option value=\"$val\"";
25 if ( $sel == $val ) {
26 echo ' selected';
27 }
28 echo ">$tit</option>\n";
29 }
30
31 /* function to get the recent searches and put them in the attributes array */
32 function get_recent($username, $data_dir) {
33 $attributes = array();
34 $types = array('search_what', 'search_where', 'search_folder');
35 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
36 for ($x=1;$x<=$recent_count;$x++) {
37 reset($types);
38 foreach ($types as $key) {
39 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
40 }
41 }
42 return $attributes;
43 }
44
45 /* function to get the saved searches and put them in the saved_attributes array */
46 function get_saved($username, $data_dir) {
47 $saved_attributes = array();
48 $types = array('saved_what', 'saved_where', 'saved_folder');
49 foreach ($types as $key) {
50 for ($x=1;;$x++) {
51 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
52 if ($saved_attributes[$key][$x] == "") {
53 array_pop($saved_attributes[$key]);
54 break;
55 }
56 }
57 }
58 return $saved_attributes;
59 }
60
61 /* function to update recent pref arrays */
62 function update_recent($what, $where, $mailbox, $username, $data_dir) {
63 $attributes = array();
64 $types = array('search_what', 'search_where', 'search_folder');
65 $input = array($what, $where, $mailbox);
66 $attributes = get_recent( $username, $data_dir);
67 reset($types);
68 $dupe = 'no';
69 for ($i=1;$i<=count($attributes['search_what']);$i++) {
70 if (isset($attributes['search_what'][$i])) {
71 if ($what == $attributes['search_what'][$i] &&
72 $where == $attributes['search_where'][$i] &&
73 $mailbox == $attributes['search_folder'][$i]) {
74 $dupe = 'yes';
75 }
76 }
77 }
78 if ($dupe == 'no') {
79 $i = 0;
80 foreach ($types as $key) {
81 array_push ($attributes[$key], $input[$i]);
82 array_shift ($attributes[$key]);
83 $i++;
84 }
85 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
86 $n=0;
87 for ($i=1;$i<=$recent_count;$i++) {
88 reset($types);
89 foreach ($types as $key) {
90 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
91 }
92 $n++;
93 }
94 }
95 }
96
97 /* function to forget a recent search */
98 function forget_recent($forget_index, $username, $data_dir) {
99 $attributes = array();
100 $types = array('search_what', 'search_where', 'search_folder');
101 $attributes = get_recent( $username, $data_dir);
102 reset($types);
103 foreach ($types as $key) {
104 array_splice($attributes[$key], $forget_index, 1);
105 array_unshift($attributes[$key], '');
106 }
107 reset($types);
108 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
109 $n=0;
110 for ($i=1;$i<=$recent_count;$i++) {
111 reset($types);
112 foreach ($types as $key) {
113 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
114 }
115 $n++;
116 }
117 }
118
119 /* function to delete a saved search */
120 function delete_saved($delete_index, $username, $data_dir) {
121 $types = array('saved_what', 'saved_where', 'saved_folder');
122 $attributes = get_saved($username, $data_dir);
123 foreach ($types as $key) {
124 array_splice($attributes[$key], $delete_index, 1);
125 }
126 reset($types);
127 $n=0;
128 $saved_count = count($attributes['saved_what']);
129 $last_element = $saved_count + 1;
130 for ($i=1;$i<=$saved_count;$i++) {
131 reset($types);
132 foreach ($types as $key) {
133 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
134 }
135 $n++;
136 }
137 reset($types);
138 foreach($types as $key) {
139 removePref($data_dir, $username, $key.$last_element);
140 }
141 }
142
143 /* function to save a search from recent to saved */
144 function save_recent($save_index, $username, $data_dir) {
145 $attributes = array();
146 $types = array('search_what', 'search_where', 'search_folder');
147 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
148 $saved_array = get_saved($username, $data_dir);
149 $save_index = $save_index -1;
150 $saved_count = (count($saved_array['saved_what']) + 1);
151 $attributes = get_recent ($username, $data_dir);
152 $n = 0;
153 foreach ($types as $key) {
154 $slice = array_slice($attributes[$key], $save_index, 1);
155 $name = $saved_types[$n];
156 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
157 $n++;
158 }
159 }
160
161 /* ------------------------ main ------------------------ */
162
163 /* reset these arrays on each page load just in case */
164 $attributes = array ();
165 $saved_attributes = array ();
166 $search_all = 'none';
167 $perbox_count = array ();
168 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
169
170 /* get mailbox names */
171 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
172 $boxes = sqimap_mailbox_list($imapConnection);
173
174 /* set current mailbox to INBOX if none was selected or if page
175 was called to search all folders. */
176 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
177 $mailbox = $boxes[0]['unformatted'];
178 }
179 if ($mailbox == 'All Folders') {
180 $search_all = 'all';
181 }
182
183 if (isset($composenew) && $composenew) {
184 displayPageHeader($color, $mailbox, 'comp_in_new();');
185 } else {
186 displayPageHeader($color, $mailbox);
187 }
188 /* See how the page was called and fire off correct function */
189 if ((!isset($submit) || empty($submit)) && !empty($what)) {
190 $submit = _("Search");
191 }
192 if ( !isset( $submit ) ) {
193 $submit = '';
194 } else if ($submit == _("Search") && !empty($what)) {
195 if ($recent_count > 0) {
196 update_recent($what, $where, $mailbox, $username, $data_dir);
197 }
198 }
199 elseif ($submit == 'forget') {
200 forget_recent($count, $username, $data_dir);
201 }
202 elseif ($submit == 'save') {
203 save_recent($count, $username, $data_dir);
204 }
205 elseif ($submit == 'delete') {
206 delete_saved($count, $username, $data_dir);
207 }
208
209 do_hook('search_before_form');
210
211 echo "<BR>\n".
212 "<table width=\"100%\">\n".
213 "<TR><td bgcolor=\"$color[0]\">\n".
214 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
215 "</TD></TR>\n".
216 "</TABLE>\n";
217
218 /* update the recent and saved searches from the pref files */
219 $attributes = get_recent($username, $data_dir);
220 $saved_attributes = get_saved($username, $data_dir);
221 $saved_count = count($saved_attributes['saved_what']);
222 $count_all = 0;
223
224 /* Saved Search Table */
225 if ($saved_count > 0) {
226 echo "<BR>\n"
227 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
228 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
229 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
230 for ($i=0; $i < $saved_count; ++$i) {
231 if ($i % 2) {
232 echo "<TR BGCOLOR=\"$color[0]\">";
233 } else {
234 echo "<TR BGCOLOR=\"$color[4]\">";
235 }
236 echo "<TD WIDTH=\"35%\">".$saved_attributes['saved_folder'][$i]."</TD>"
237 . "<TD ALIGN=LEFT>".$saved_attributes['saved_what'][$i]."</TD>"
238 . "<TD ALIGN=CENTER>".$saved_attributes['saved_where'][$i]."</TD>"
239 . '<TD ALIGN=RIGHT>'
240 . '<A HREF=search.php'
241 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
242 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
243 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
244 . '>' . _("edit") . '</A>'
245 . '&nbsp;|&nbsp;'
246 . '<A HREF=search.php'
247 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
248 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
249 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
250 . '&amp;submit=Search_no_update'
251 . '>' . _("search") . '</A>'
252 . '&nbsp;|&nbsp;'
253 . "<A HREF=search.php?count=$i&amp;submit=delete>"
254 . _("delete")
255 . '</A>'
256 . '</TD></TR>';
257 }
258 echo "</TABLE></TD></TR></TABLE>\n";
259 }
260
261 /* Recent Search Table */
262 if ($recent_count > 0) {
263 echo "<BR>\n"
264 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
265 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
266 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
267 for ($i=1; $i <= $recent_count; ++$i) {
268 if (isset($attributes['search_folder'][$i])) {
269 if ($attributes['search_folder'][$i] == "") {
270 $attributes['search_folder'][$i] = "INBOX";
271 }
272 }
273 if ($i % 2) {
274 echo "<TR BGCOLOR=\"$color[0]\">";
275 } else {
276 echo "<TR BGCOLOR=\"$color[4]\">";
277 }
278 if (isset($attributes['search_what'][$i]) &&
279 !empty($attributes['search_what'][$i])) {
280 echo "<TD WIDTH=35%>".$attributes['search_folder'][$i]."</TD>"
281 . "<TD ALIGN=LEFT>".$attributes['search_what'][$i]."</TD>"
282 . "<TD ALIGN=CENTER>".$attributes['search_where'][$i]."</TD>"
283 . '<TD ALIGN=RIGHT>'
284 . "<A HREF=search.php?count=$i&amp;submit=save>"
285 . _("save")
286 . '</A>'
287 . '&nbsp;|&nbsp;'
288 . '<A HREF=search.php'
289 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
290 . '&amp;what=' . urlencode($attributes['search_what'][$i])
291 . '&amp;where=' . urlencode($attributes['search_where'][$i])
292 . '&amp;submit=Search_no_update'
293 . '>' . _("search") . '</A>'
294 . '&nbsp;|&nbsp;'
295 . "<A HREF=search.php?count=$i&amp;submit=forget>"
296 . _("forget")
297 . '</A>'
298 . '</TD></TR>';
299 }
300 }
301 echo '</TABLE></TD></TR></TABLE><BR>';
302 }
303
304 /* Search Form */
305 echo '<B>' . _("Current Search") . '</B>'
306 . '<FORM ACTION="search.php" NAME=s>'
307 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
308 . ' <TR>'
309 . ' <TD><SELECT NAME="mailbox">';
310 for ($i = 0; $i < count($boxes); $i++) {
311 if (!in_array('noselect', $boxes[$i]['flags'])) {
312 $box = $boxes[$i]['unformatted'];
313 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
314 if( $box2 == 'INBOX' ) {
315 $box2 = _("INBOX");
316 }
317 if ($mailbox == $box) {
318 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
319 }
320 else {
321 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
322 }
323 }
324 }
325 echo "<OPTION VALUE=\"All Folders\"";
326 if ($mailbox == "All Folders") {
327 echo "SELECTED";
328 }
329 echo ">All folders</OPTION>\n";
330 echo ' </SELECT>'.
331 " </TD>\n".
332 " <TD ALIGN=\"CENTER\">\n";
333 if ( !isset( $what ) ) {
334 $what = '';
335 }
336 if ( !isset( $where ) ) {
337 $where = '';
338 }
339
340
341 $what_disp = str_replace(',', ' ', $what);
342 $what_disp = str_replace('\\\\', '\\', $what_disp);
343 $what_disp = str_replace('\\"', '"', $what_disp);
344 $what_disp = str_replace('"', '&quot;', $what_disp);
345 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
346 " </TD>\n".
347 "<TD ALIGN=\"RIGHT\">\n".
348 "<SELECT NAME=\"where\">";
349 s_opt( 'BODY', $where, _("Body") );
350 s_opt( 'TEXT', $where, _("Everywhere") );
351 s_opt( 'SUBJECT', $where, _("Subject") );
352 s_opt( 'FROM', $where, _("From") );
353 s_opt( 'CC', $where, _("Cc") );
354 s_opt( 'TO', $where, _("To") );
355 echo " </SELECT>\n" .
356 " </TD>\n".
357 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
358 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
359 " </TD>\n".
360 " </TR>\n".
361 "</FORM>\n".
362 " </TABLE>\n".
363 "</TD></TR></TABLE>\n";
364
365
366 do_hook('search_after_form');
367
368 /*
369 search all folders option still in the works. returns a table for each
370 folder it finds a match in.
371 */
372
373 $old_value = 0;
374 if ($allow_thread_sort == TRUE) {
375 $old_value = $allow_thread_sort;
376 $allow_thread_sort = FALSE;
377 }
378
379 if ($search_all == 'all') {
380 $mailbox == '';
381 $boxcount = count($boxes);
382 echo '<BR><CENTER><B>' .
383 _("Search Results") .
384 "</B><CENTER><BR>\n";
385 for ($x=0;$x<$boxcount;$x++) {
386 if (!in_array('noselect', $boxes[$x]['flags'])) {
387 $mailbox = $boxes[$x]['unformatted'];
388 }
389 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
390 sqimap_mailbox_select($imapConnection, $mailbox);
391 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
392 array_push($perbox_count, $count_all);
393 }
394 }
395 for ($i=0;$i<count($perbox_count);$i++) {
396 if ($perbox_count[$i] != "") {
397 $count_all = "found";
398 break;
399 }
400 }
401 if ($count_all != "found") {
402 echo '<br><b>' .
403 _("No Messages found") .
404 '</b><br>';
405 }
406 }
407
408 /* search one folder option */
409 else {
410 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
411 echo '<BR><CENTER><B>' .
412 _("Search Results") .
413 "</B></CENTER>\n";
414 sqimap_mailbox_select($imapConnection, $mailbox);
415 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
416 }
417 }
418
419 /* must have search terms to search */
420 if ($submit == _("Search") && empty($what)) {
421 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
422 }
423
424 $allow_thread_sort = $old_value;
425
426 do_hook('search_bottom');
427 sqimap_logout ($imapConnection);
428 echo '</body></html>';
429
430 ?>