6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
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');
19 // here are some functions, could go in imap_search.php
20 // this was here, pretty handy
22 function s_opt( $val, $sel, $tit ) {
23 echo " <option value=\"$val\"";
27 echo ">$tit</option>\n";
30 // function to get the recent searches and put them in arrays
32 function get_recent($pref_name, $username, $data_dir) {
34 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
36 for ($x=1;$x<=$recent_count;$x++
) {
37 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
43 // function to get the saved searches and put them in arrays
45 function get_saved($pref_name, $username, $data_dir) {
49 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
50 if ($array[$n] == "") {
59 // function to update pref file with recent searches
61 function 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);
65 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
67 for ($i=1;$i<=$recent_count;$i++
) {
68 setPref($data_dir, $username, "$pref_name" . "$i", $array[$n]);
73 // function to "forget" a recent search
75 function 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);
82 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
84 for ($i=1;$i<=$recent_count;$i++
) {
85 setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]);
86 setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]);
87 setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]);
91 // function to delete a saved search
93 function delete_saved($delete_index, $username, $data_dir) {
94 $saved_what_array = get_saved("saved_what", $username, $data_dir);
95 $saved_where_array = get_saved("saved_where", $username, $data_dir);
96 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
97 array_splice($saved_what_array, $delete_index, 1);
98 array_splice($saved_where_array, $delete_index, 1);
99 array_splice($saved_folder_array, $delete_index, 1);
101 $saved_count = count($saved_what_array);
102 $last_element = $saved_count +
1;
103 if ($last_element < 1) {
104 for ($i=1;$i<=$saved_count;$i++
) {
105 setPref($data_dir, $username, "saved_what" . "$i", $saved_what_array[$n]);
106 setPref($data_dir, $username, "saved_where" . "$i", $saved_where_array[$n]);
107 setPref($data_dir, $username, "saved_folder" . "$i", $saved_folder_array[$n]);
111 removePref($data_dir, $username, "saved_what" . "$last_element");
112 removePref($data_dir, $username, "saved_where" . "$last_element");
113 removePref($data_dir, $username, "saved_folder" . "$last_element");
116 // function to save a search from recent to saved
118 function save_recent($save_index, $username, $data_dir) {
119 $what_array = get_recent("search_what", $username, $data_dir);
120 $where_array = get_recent("search_where", $username, $data_dir);
121 $folder_array = get_recent("search_folder", $username, $data_dir);
122 $saved_what_once = array_slice($what_array, $save_index, 1);
123 $saved_where_once = array_slice($where_array, $save_index, 1);
124 $saved_folder_once = array_slice($folder_array, $save_index, 1);
125 $saved_array = get_saved("saved_what", $username, $data_dir);
126 $saved_count = (count($saved_array) +
1);
127 setPref($data_dir, $username, "saved_what" . "$saved_count", $saved_what_once[0]);
128 setPref($data_dir, $username, "saved_where" . "$saved_count", $saved_where_once[0]);
129 setPref($data_dir, $username, "saved_folder" . "$saved_count", $saved_folder_once[0]);
134 /* ------------------------ main ------------------------ */
136 // reset these arrays on each page load just in case
138 $what_array = array ();
139 $where_array = array ();
140 $folder_array = array ();
141 $saved_what_array = array ();
142 $saved_where_array = array ();
143 $saved_folder_array = array ();
144 $search_all = "none";
148 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
149 $boxes = sqimap_mailbox_list($imapConnection);
152 // set current mailbox to INBOX if none was selected or if page
153 // was called to search all folders.
155 if ($mailbox == 'None' ||
$mailbox == "" ) {
156 $mailbox = $boxes[0]['unformatted'];
158 if ($mailbox == "All Folders") {
164 displayPageHeader($color, $mailbox);
166 // if the page is called from a search link or button update recent values
167 // in pref files here
169 if ($submit == "Search" && !empty($what)) {
170 update_recent($what_array, $what, "search_what", $username, $data_dir);
171 update_recent($where_array, $where, "search_where", $username, $data_dir);
172 update_recent($folder_array, $mailbox, "search_folder", $username, $data_dir);
174 // if the page is called from a "forget recent" link remove search from pref file
175 elseif ($submit == "forget") {
176 forget_recent($count, $username, $data_dir);
178 // if the page is called from a "save recent" link add search to saved searches
179 elseif ($submit == "save") {
180 save_recent($count, $username, $data_dir);
182 elseif ($submit == "delete") {
183 delete_saved($count, $username, $data_dir);
185 // if the page is called from a "delete saved" link delete saved search
186 do_hook('search_before_form');
189 " <table width=\"100%\" align=center cellpadding=2 cellspacing=0 border=0>\n".
190 " <TR><td bgcolor=\"$color[0]\">\n".
191 " <CENTER><B>"._("Search")."</B></CENTER>\n".
193 ' <TR><td align=center>';
195 // update the recent and saved searches from the pref files
197 $what_array = get_recent("search_what", $username, $data_dir);
198 $where_array = get_recent("search_where", $username, $data_dir);
199 $folder_array = get_recent("search_folder", $username, $data_dir);
200 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
201 $saved_what_array = get_saved("saved_what", $username, $data_dir);
202 $saved_where_array = get_saved("saved_where", $username, $data_dir);
203 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
204 $saved_count = count($saved_what_array);
206 // saved search table
207 if ($saved_count > 0) {
208 echo "<BR><B>Saved Searches</B>\n".
209 "<TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0 BGCOLOR=\"$color[0]\">\n";
210 for ($i=0;$i<$saved_count;$i++
) {
211 echo "<TR><TD WIDTH=35%>$saved_folder_array[$i]</TD>\n".
212 "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>\n".
213 "<TD ALIGN=RIGHT>$saved_where_array[$i] \n".
214 "<A HREF=search.php?".
215 "mailbox=", urlencode($saved_folder_array[$i]), "&".
216 "what=", urlencode($saved_what_array[$i]), "&".
217 "where=", urlencode($saved_where_array[$i]), ">".
219 " | <A HREF=search.php?".
220 "mailbox=", urlencode($saved_folder_array[$i]), "&".
221 "what=", urlencode($saved_what_array[$i]), "&".
222 "where=", urlencode($saved_where_array[$i]), "&".
223 "submit=Search_no_update>search</A>\n".
224 " | <A HREF=search.php?count=$i&submit=delete>delete</A>".
230 // recent search table
232 if ($recent_count > 0) {
233 echo "<BR><B>Recent Searches</B>\n".
234 "<TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0 BGCOLOR=\"$color[0]\">\n";
235 for ($i=0;$i<$recent_count;$i++
) {
236 if (!empty($what_array[$i])) {
237 if ($folder_array[$i] == "") {
238 $folder_array[$i] = "INBOX";
240 echo "<TR><TD WIDTH=35%>$folder_array[$i]</TD>\n".
241 "<TD ALIGN=LEFT>$what_array[$i]</TD>\n".
242 "<TD ALIGN=RIGHT>$where_array[$i] ".
243 "<A HREF=search.php?".
244 "count=$i&submit=save".
246 " | <A HREF=search.php?".
247 "mailbox=", urlencode($folder_array[$i]), "&".
248 "what=", urlencode($what_array[$i]), "&".
249 "where=", urlencode($where_array[$i]), "&".
250 "submit=Search_no_update>search</A>\n".
251 " | <A HREF=search.php?count=$i&submit=forget>forget</A>".
252 " </TD></TR>\n";
259 echo "<B>Current Search</B>";
260 echo " <TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0>\n";
261 echo "<FORM ACTION=\"search.php\" NAME=s>\n".
263 ' <TD><SELECT NAME="mailbox">';
264 for ($i = 0; $i < count($boxes); $i++
) {
265 if (!in_array('noselect', $boxes[$i]['flags'])) {
266 $box = $boxes[$i]['unformatted'];
267 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
268 if ($mailbox == $box) {
269 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
272 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
276 echo "<OPTION VALUE=\"All Folders\"";
277 if ($mailbox == "All Folders") {
280 echo ">All folders</OPTION>\n";
283 " <TD ALIGN=\"CENTER\">\n";
284 $what_disp = str_replace(',', ' ', $what);
285 $what_disp = str_replace('\\\\', '\\', $what_disp);
286 $what_disp = str_replace('\\"', '"', $what_disp);
287 $what_disp = str_replace('"', '"', $what_disp);
288 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
290 "<TD ALIGN=\"RIGHT\">\n".
291 "<SELECT NAME=\"where\">";
292 s_opt( 'BODY', $where, _("Body") );
293 s_opt( 'TEXT', $where, _("Everywhere") );
294 s_opt( 'SUBJECT', $where, _("Subject") );
295 s_opt( 'FROM', $where, _("From") );
296 s_opt( 'CC', $where, _("Cc") );
297 s_opt( 'TO', $where, _("To") );
298 echo " </SELECT>\n" .
300 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
301 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n".
306 "</TD></TR></TABLE>\n";
309 do_hook("search_after_form");
311 // search all folders option still in the works. returns a table for each
312 // folder it finds a match in. The toggle all link does not work
315 if ($search_all == "all") {
317 $boxcount = count($boxes);
318 echo "<BR><CENTER><B>Search Results</B><CENTER><BR>\n";
319 for ($x=0;$x<$boxcount;$x++
) {
320 if (!in_array('noselect', $boxes[$x]['flags'])) {
321 $mailbox = $boxes[$x]['unformatted'];
323 echo "<BR><CENTER><B>Folder: $mailbox</CENTER></B>";
324 if (($submit == "Search" ||
$submit == "Search_no_update") && !empty($what)) {
325 sqimap_mailbox_select($imapConnection, $mailbox);
326 sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos);
331 // search one folder option
334 if (($submit == "Search" ||
$submit == "Search_no_update") && !empty($what)) {
335 echo "<BR><CENTER><B>Search Results</B></CENTER>\n";
336 sqimap_mailbox_select($imapConnection, $mailbox);
337 sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos);
341 // must have search terms to search
343 if ($submit == "Search" && empty($what)) {
344 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
347 do_hook("search_bottom");
351 sqimap_logout ($imapConnection);
352 echo '</body></html>';