Fixed problem with "forget" link on a recent search
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
a5ee5ac2 4 * search.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
2d367c68 11
35586184 12require_once('../src/validate.php');
13require_once('../functions/imap.php');
14require_once('../functions/imap_search.php');
15require_once('../functions/array.php');
46d38f78 16require_once('../functions/strings.php');
c61bb006 17
56e0b3b7 18
19// here are some functions, could go in imap_search.php
20// this was here, pretty handy
21
29eb5486 22function s_opt( $val, $sel, $tit ) {
23 echo " <option value=\"$val\"";
0d672ac0 24 if ( $sel == $val ) {
bd9bbfef 25 echo ' selected';
99d2a184 26 }
29eb5486 27 echo ">$tit</option>\n";
28}
99d2a184 29
56e0b3b7 30// function to get the recent searches and put them in arrays
31
32function get_recent($pref_name, $username, $data_dir) {
33 $array = array ();
34 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
35 $n = 0;
36 for ($x=1;$x<=$recent_count;$x++) {
37 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
38 $n++;
39 }
40 return $array;
41}
42
43// function to get the saved searches and put them in arrays
44
45function get_saved($pref_name, $username, $data_dir) {
46 $array = array ();
47 $n = 0;
48 for ($x=1;;$x++) {
49 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
50 if ($array[$n] == "") {
51 array_pop($array);
52 return $array;
53 }
54 $n++;
55 }
56 return $array;
57}
58
59// function to update pref file with recent searches
60
61function update_recent($array, $recent_value, $pref_name, $username, $data_dir) {
62 $array = get_recent($pref_name, $username, $data_dir);
63 array_push ($array, $recent_value);
64 array_shift ($array);
65 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
66 $n=0;
67 for ($i=1;$i<=$recent_count;$i++) {
68 setPref($data_dir, $username, "$pref_name" . "$i", $array[$n]);
69 $n++;
70 }
71}
72
73// function to "forget" a recent search
74
75function forget_recent($forget_index, $username, $data_dir) {
76 $what_array = get_recent("search_what", $username, $data_dir);
77 $where_array = get_recent("search_where", $username, $data_dir);
78 $folder_array = get_recent("search_folder", $username, $data_dir);
79 array_splice($what_array, $forget_index, 1);
80 array_splice($where_array, $forget_index, 1);
81 array_splice($folder_array, $forget_index, 1);
83a92ae8 82 array_unshift($what_array, "");
83 array_unshift($where_array, "");
84 array_unshift($folder_array, "");
56e0b3b7 85 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
86 $n=0;
87 for ($i=1;$i<=$recent_count;$i++) {
88 setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]);
89 setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]);
90 setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]);
91 $n++;
92 }
6c8388a9 93
94// function to delete a saved search
56e0b3b7 95}
96function delete_saved($delete_index, $username, $data_dir) {
97 $saved_what_array = get_saved("saved_what", $username, $data_dir);
98 $saved_where_array = get_saved("saved_where", $username, $data_dir);
99 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
100 array_splice($saved_what_array, $delete_index, 1);
101 array_splice($saved_where_array, $delete_index, 1);
102 array_splice($saved_folder_array, $delete_index, 1);
103 $n=0;
104 $saved_count = count($saved_what_array);
105 $last_element = $saved_count + 1;
106 if ($last_element < 1) {
107 for ($i=1;$i<=$saved_count;$i++) {
108 setPref($data_dir, $username, "saved_what" . "$i", $saved_what_array[$n]);
109 setPref($data_dir, $username, "saved_where" . "$i", $saved_where_array[$n]);
110 setPref($data_dir, $username, "saved_folder" . "$i", $saved_folder_array[$n]);
111 $n++;
112 }
113 }
114 removePref($data_dir, $username, "saved_what" . "$last_element");
115 removePref($data_dir, $username, "saved_where" . "$last_element");
116 removePref($data_dir, $username, "saved_folder" . "$last_element");
117}
6c8388a9 118
119// function to save a search from recent to saved
120
56e0b3b7 121function save_recent($save_index, $username, $data_dir) {
122 $what_array = get_recent("search_what", $username, $data_dir);
123 $where_array = get_recent("search_where", $username, $data_dir);
124 $folder_array = get_recent("search_folder", $username, $data_dir);
125 $saved_what_once = array_slice($what_array, $save_index, 1);
126 $saved_where_once = array_slice($where_array, $save_index, 1);
127 $saved_folder_once = array_slice($folder_array, $save_index, 1);
128 $saved_array = get_saved("saved_what", $username, $data_dir);
129 $saved_count = (count($saved_array) + 1);
130 setPref($data_dir, $username, "saved_what" . "$saved_count", $saved_what_once[0]);
131 setPref($data_dir, $username, "saved_where" . "$saved_count", $saved_where_once[0]);
132 setPref($data_dir, $username, "saved_folder" . "$saved_count", $saved_folder_once[0]);
133}
134
135
136
0d672ac0 137/* ------------------------ main ------------------------ */
138
56e0b3b7 139// reset these arrays on each page load just in case
140
141$what_array = array ();
142$where_array = array ();
143$folder_array = array ();
144$saved_what_array = array ();
145$saved_where_array = array ();
146$saved_folder_array = array ();
147$search_all = "none";
148
149// get mailbox names
150
29eb5486 151$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
c1cb7ba4 152$boxes = sqimap_mailbox_list($imapConnection);
153
56e0b3b7 154
155// set current mailbox to INBOX if none was selected or if page
156// was called to search all folders.
157
158if ($mailbox == 'None' || $mailbox == "" ) {
159 $mailbox = $boxes[0]['unformatted'];
160}
6c8388a9 161if ($mailbox == "All Folders") {
56e0b3b7 162 $search_all = "all";
c1cb7ba4 163}
164
56e0b3b7 165// page headers
166
c1cb7ba4 167displayPageHeader($color, $mailbox);
c61bb006 168
56e0b3b7 169// if the page is called from a search link or button update recent values
170// in pref files here
0d672ac0 171
56e0b3b7 172if ($submit == "Search" && !empty($what)) {
173 update_recent($what_array, $what, "search_what", $username, $data_dir);
174 update_recent($where_array, $where, "search_where", $username, $data_dir);
175 update_recent($folder_array, $mailbox, "search_folder", $username, $data_dir);
176}
177// if the page is called from a "forget recent" link remove search from pref file
178elseif ($submit == "forget") {
179 forget_recent($count, $username, $data_dir);
180}
181// if the page is called from a "save recent" link add search to saved searches
182elseif ($submit == "save") {
183 save_recent($count, $username, $data_dir);
184}
185elseif ($submit == "delete") {
186 delete_saved($count, $username, $data_dir);
187}
188// if the page is called from a "delete saved" link delete saved search
29eb5486 189do_hook('search_before_form');
56e0b3b7 190
191echo "<BR>\n".
a7d0eaf6 192 " <table width=\"100%\" align=center cellpadding=2 cellspacing=0 border=0>\n".
56e0b3b7 193 " <TR><td bgcolor=\"$color[0]\">\n".
194 " <CENTER><B>"._("Search")."</B></CENTER>\n".
195 " </TD></TR>\n".
196 ' <TR><td align=center>';
c61bb006 197
56e0b3b7 198// update the recent and saved searches from the pref files
a7d0eaf6 199
56e0b3b7 200$what_array = get_recent("search_what", $username, $data_dir);
201$where_array = get_recent("search_where", $username, $data_dir);
202$folder_array = get_recent("search_folder", $username, $data_dir);
203$recent_count = getPref($data_dir, $username, 'search_memory', 0);
204$saved_what_array = get_saved("saved_what", $username, $data_dir);
205$saved_where_array = get_saved("saved_where", $username, $data_dir);
206$saved_folder_array = get_saved("saved_folder", $username, $data_dir);
207$saved_count = count($saved_what_array);
a7d0eaf6 208
56e0b3b7 209// saved search table
210if ($saved_count > 0) {
211 echo "<BR><B>Saved Searches</B>\n".
212 "<TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0 BGCOLOR=\"$color[0]\">\n";
213 for ($i=0;$i<$saved_count;$i++) {
214 echo "<TR><TD WIDTH=35%>$saved_folder_array[$i]</TD>\n".
215 "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>\n".
216 "<TD ALIGN=RIGHT>$saved_where_array[$i]&nbsp;&nbsp;&nbsp;\n".
217 "<A HREF=search.php?".
218 "mailbox=", urlencode($saved_folder_array[$i]), "&".
219 "what=", urlencode($saved_what_array[$i]), "&".
220 "where=", urlencode($saved_where_array[$i]), ">".
221 "edit</A>\n".
222 "&nbsp;|&nbsp;<A HREF=search.php?".
223 "mailbox=", urlencode($saved_folder_array[$i]), "&".
224 "what=", urlencode($saved_what_array[$i]), "&".
225 "where=", urlencode($saved_where_array[$i]), "&".
226 "submit=Search_no_update>search</A>\n".
227 "&nbsp;|&nbsp;<A HREF=search.php?count=$i&submit=delete>delete</A>".
228 "&nbsp;&nbsp;";
229 }
230echo "</TABLE>\n";
a7d0eaf6 231}
232
56e0b3b7 233// recent search table
c61bb006 234
56e0b3b7 235if ($recent_count > 0) {
236 echo "<BR><B>Recent Searches</B>\n".
237 "<TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0 BGCOLOR=\"$color[0]\">\n";
238 for ($i=0;$i<$recent_count;$i++) {
239 if (!empty($what_array[$i])) {
240 if ($folder_array[$i] == "") {
241 $folder_array[$i] = "INBOX";
242 }
243 echo "<TR><TD WIDTH=35%>$folder_array[$i]</TD>\n".
244 "<TD ALIGN=LEFT>$what_array[$i]</TD>\n".
245 "<TD ALIGN=RIGHT>$where_array[$i]&nbsp;&nbsp;&nbsp;".
246 "<A HREF=search.php?".
247 "count=$i&submit=save".
248 ">save</A>\n".
249 "&nbsp;|&nbsp;<A HREF=search.php?".
250 "mailbox=", urlencode($folder_array[$i]), "&".
251 "what=", urlencode($what_array[$i]), "&".
252 "where=", urlencode($where_array[$i]), "&".
253 "submit=Search_no_update>search</A>\n".
254 "&nbsp;|&nbsp;<A HREF=search.php?count=$i&submit=forget>forget</A>".
255 "&nbsp;&nbsp;</TD></TR>\n";
0d672ac0 256 }
29eb5486 257 }
56e0b3b7 258 echo "</TABLE>\n".
259 "<BR>\n";
260}
261// search form
262echo "<B>Current Search</B>";
263echo " <TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0>\n";
264echo "<FORM ACTION=\"search.php\" NAME=s>\n".
265 " <TR >\n".
266 ' <TD><SELECT NAME="mailbox">';
267for ($i = 0; $i < count($boxes); $i++) {
268 if (!in_array('noselect', $boxes[$i]['flags'])) {
269 $box = $boxes[$i]['unformatted'];
270 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
271 if ($mailbox == $box) {
272 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
0d672ac0 273 }
56e0b3b7 274 else {
275 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
276 }
277 }
278}
6c8388a9 279 echo "<OPTION VALUE=\"All Folders\"";
280 if ($mailbox == "All Folders") {
56e0b3b7 281 echo "SELECTED";
0d672ac0 282 }
56e0b3b7 283 echo ">All folders</OPTION>\n";
284echo ' </SELECT>'.
285 " </TD>\n".
286 " <TD ALIGN=\"CENTER\">\n";
287$what_disp = str_replace(',', ' ', $what);
288$what_disp = str_replace('\\\\', '\\', $what_disp);
289$what_disp = str_replace('\\"', '"', $what_disp);
290$what_disp = str_replace('"', '&quot;', $what_disp);
291echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
292 " </TD>\n".
293 "<TD ALIGN=\"RIGHT\">\n".
294 "<SELECT NAME=\"where\">";
295s_opt( 'BODY', $where, _("Body") );
296s_opt( 'TEXT', $where, _("Everywhere") );
297s_opt( 'SUBJECT', $where, _("Subject") );
298s_opt( 'FROM', $where, _("From") );
299s_opt( 'CC', $where, _("Cc") );
300s_opt( 'TO', $where, _("To") );
301echo " </SELECT>\n" .
302 " </TD>\n".
303 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
304 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n".
305 " </TD>\n".
306 " </TR>\n".
307 "</FORM>\n".
308 " </TABLE>\n".
309 "</TD></TR></TABLE>\n";
310
311
29eb5486 312do_hook("search_after_form");
56e0b3b7 313
314// search all folders option still in the works. returns a table for each
315// folder it finds a match in. The toggle all link does not work
316
317
318if ($search_all == "all") {
319 $mailbox == "";
320 $boxcount = count($boxes);
321 echo "<BR><CENTER><B>Search Results</B><CENTER><BR>\n";
322 for ($x=0;$x<$boxcount;$x++) {
323 if (!in_array('noselect', $boxes[$x]['flags'])) {
324 $mailbox = $boxes[$x]['unformatted'];
325 }
326 echo "<BR><CENTER><B>Folder: $mailbox</CENTER></B>";
327 if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) {
328 sqimap_mailbox_select($imapConnection, $mailbox);
329 sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos);
330 }
331 }
0d672ac0 332}
0d672ac0 333
56e0b3b7 334// search one folder option
335
336else {
337 if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) {
338 echo "<BR><CENTER><B>Search Results</B></CENTER>\n";
339 sqimap_mailbox_select($imapConnection, $mailbox);
340 sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos);
341 }
29eb5486 342}
56e0b3b7 343
344// must have search terms to search
345
346if ($submit == "Search" && empty($what)) {
347 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
348}
349
29eb5486 350do_hook("search_bottom");
99d2a184 351
56e0b3b7 352// all done
353
354sqimap_logout ($imapConnection);
29eb5486 355echo '</body></html>';
99d2a184 356
43fdb2a4 357?>