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 array_unshift($what_array, "");
83 array_unshift($where_array, "");
84 array_unshift($folder_array, "");
85 $recent_count = getPref($data_dir, $username, 'search_memory', 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]);
94 // function to delete a saved search
96 function 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);
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]);
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");
119 // function to save a search from recent to saved
121 function 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]);
137 /* ------------------------ main ------------------------ */
139 // reset these arrays on each page load just in case
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";
151 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
152 $boxes = sqimap_mailbox_list($imapConnection);
155 // set current mailbox to INBOX if none was selected or if page
156 // was called to search all folders.
158 if ($mailbox == 'None' ||
$mailbox == "" ) {
159 $mailbox = $boxes[0]['unformatted'];
161 if ($mailbox == "All Folders") {
167 displayPageHeader($color, $mailbox);
169 // if the page is called from a search link or button update recent values
170 // in pref files here
172 if ($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);
177 // if the page is called from a "forget recent" link remove search from pref file
178 elseif ($submit == "forget") {
179 forget_recent($count, $username, $data_dir);
181 // if the page is called from a "save recent" link add search to saved searches
182 elseif ($submit == "save") {
183 save_recent($count, $username, $data_dir);
185 elseif ($submit == "delete") {
186 delete_saved($count, $username, $data_dir);
188 // if the page is called from a "delete saved" link delete saved search
189 do_hook('search_before_form');
192 " <table width=\"100%\">\n".
193 " <TR><td bgcolor=\"$color[0]\">\n".
194 " <CENTER><B>"._("Search")."</B></CENTER>\n".
197 # ' <TR><td align=center>';
199 // update the recent and saved searches from the pref files
201 $what_array = get_recent("search_what", $username, $data_dir);
202 $where_array = get_recent("search_where", $username, $data_dir);
203 $folder_array = get_recent("search_folder", $username, $data_dir);
204 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
205 $saved_what_array = get_saved("saved_what", $username, $data_dir);
206 $saved_where_array = get_saved("saved_where", $username, $data_dir);
207 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
208 $saved_count = count($saved_what_array);
211 /* Saved Search Table */
212 if ($saved_count > 0) {
214 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
215 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
216 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
217 for ($i=0; $i < $saved_count; ++
$i) {
219 echo "<TR BGCOLOR=\"$color[0]\">";
221 echo "<TR BGCOLOR=\"$color[4]\">";
223 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
224 . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>"
225 . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>"
227 . '<A HREF=search.php'
228 . '?mailbox=' . urlencode($saved_folder_array[$i])
229 . '&what=' . urlencode($saved_what_array[$i])
230 . '&where=' . urlencode($saved_where_array[$i])
231 . '>' . _("edit") . '</A>'
233 . '<A HREF=search.php'
234 . '?mailbox=' . urlencode($saved_folder_array[$i])
235 . '&what=' . urlencode($saved_what_array[$i])
236 . '&where=' . urlencode($saved_where_array[$i])
237 . '&submit=Search_no_update'
238 . '>' . _("search") . '</A>'
240 . "<A HREF=search.php?count=$i&submit=delete>"
245 echo "</TABLE></TD></TR></TABLE>\n";
248 /* Recent Search Table */
249 if ($recent_count > 0) {
251 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
252 . '<TR><TD ALIGN=CENTER><B>Recent Searches</B></TD></TR><TR><TD>'
253 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
254 for ($i=0; $i < $recent_count; ++
$i) {
255 if (!empty($what_array[$i])) {
256 if ($folder_array[$i] == "") {
257 $folder_array[$i] = "INBOX";
260 echo "<TR BGCOLOR=\"$color[0]\">";
262 echo "<TR BGCOLOR=\"$color[4]\">";
264 echo "<TD WIDTH=35%>$folder_array[$i]</TD>"
265 . "<TD ALIGN=LEFT>$what_array[$i]</TD>"
266 . "<TD ALIGN=CENTER>$where_array[$i]</TD>"
268 . "<A HREF=search.php?count=$i&submit=save>"
272 . '<A HREF=search.php'
273 . '?mailbox=' . urlencode($folder_array[$i])
274 . '&what=' . urlencode($what_array[$i])
275 . '&where=' . urlencode($where_array[$i])
276 . '&submit=Search_no_update'
277 . '>' . _("search") . '</A>'
279 . "<A HREF=search.php?count=$i&submit=forget>"
285 echo '</TABLE></TD></TR></TABLE><BR>';
289 echo '<B>Current Search</B>'
290 . '<FORM ACTION="search.php" NAME=s>'
291 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
293 . ' <TD><SELECT NAME="mailbox">';
294 for ($i = 0; $i < count($boxes); $i++
) {
295 if (!in_array('noselect', $boxes[$i]['flags'])) {
296 $box = $boxes[$i]['unformatted'];
297 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
298 if ($mailbox == $box) {
299 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
302 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
306 echo "<OPTION VALUE=\"All Folders\"";
307 if ($mailbox == "All Folders") {
310 echo ">All folders</OPTION>\n";
313 " <TD ALIGN=\"CENTER\">\n";
314 $what_disp = str_replace(',', ' ', $what);
315 $what_disp = str_replace('\\\\', '\\', $what_disp);
316 $what_disp = str_replace('\\"', '"', $what_disp);
317 $what_disp = str_replace('"', '"', $what_disp);
318 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
320 "<TD ALIGN=\"RIGHT\">\n".
321 "<SELECT NAME=\"where\">";
322 s_opt( 'BODY', $where, _("Body") );
323 s_opt( 'TEXT', $where, _("Everywhere") );
324 s_opt( 'SUBJECT', $where, _("Subject") );
325 s_opt( 'FROM', $where, _("From") );
326 s_opt( 'CC', $where, _("Cc") );
327 s_opt( 'TO', $where, _("To") );
328 echo " </SELECT>\n" .
330 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
331 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n".
336 "</TD></TR></TABLE>\n";
339 do_hook("search_after_form");
341 // search all folders option still in the works. returns a table for each
342 // folder it finds a match in. The toggle all link does not work
345 if ($search_all == "all") {
347 $boxcount = count($boxes);
348 echo "<BR><CENTER><B>Search Results</B><CENTER><BR>\n";
349 for ($x=0;$x<$boxcount;$x++
) {
350 if (!in_array('noselect', $boxes[$x]['flags'])) {
351 $mailbox = $boxes[$x]['unformatted'];
353 if (($submit == "Search" ||
$submit == "Search_no_update") && !empty($what)) {
354 sqimap_mailbox_select($imapConnection, $mailbox);
355 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos, $search_all, $count_all);
358 if ($count_all == 0) {
359 echo "<br><b>No Messages found</b><br>";
363 // search one folder option
366 if (($submit == "Search" ||
$submit == "Search_no_update") && !empty($what)) {
367 echo "<BR><CENTER><B>Search Results</B></CENTER>\n";
368 sqimap_mailbox_select($imapConnection, $mailbox);
369 sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos, $search_all, $count_all);
373 // must have search terms to search
375 if ($submit == "Search" && empty($what)) {
376 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
379 do_hook("search_bottom");
383 sqimap_logout ($imapConnection);
384 echo '</body></html>';