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
21 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 */
31 function get_recent($pref_name, $username, $data_dir) {
33 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
35 for ($x=1;$x<=$recent_count;$x++
) {
36 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
42 /* function to get the saved searches and put them in arrays */
43 function get_saved($pref_name, $username, $data_dir) {
47 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
48 if ($array[$n] == "") {
57 /* function to update recent pref arrays */
58 function update_recent($what, $where, $mailbox, $username, $data_dir) {
59 $what_array = get_recent("search_what", $username, $data_dir);
60 $where_array = get_recent("search_where", $username, $data_dir);
61 $folder_array = get_recent("search_folder", $username, $data_dir);
63 for ($i=0;$i<count($what_array);$i++
) {
64 if ($what == $what_array[$i] &&
65 $where == $where_array[$i] &&
66 $mailbox == $folder_array[$i]) {
72 array_push ($what_array, $what);
73 array_push ($where_array, $where);
74 array_push ($folder_array, $mailbox);
75 array_shift ($what_array);
76 array_shift ($where_array);
77 array_shift ($folder_array);
78 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
80 for ($i=1;$i<=$recent_count;$i++
) {
81 setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]);
82 setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]);
83 setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]);
89 /* function to forget a recent search */
90 function forget_recent($forget_index, $username, $data_dir) {
91 $what_array = get_recent("search_what", $username, $data_dir);
92 $where_array = get_recent("search_where", $username, $data_dir);
93 $folder_array = get_recent("search_folder", $username, $data_dir);
94 array_splice($what_array, $forget_index, 1);
95 array_splice($where_array, $forget_index, 1);
96 array_splice($folder_array, $forget_index, 1);
97 array_unshift($what_array, "");
98 array_unshift($where_array, "");
99 array_unshift($folder_array, "");
100 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
102 for ($i=1;$i<=$recent_count;$i++
) {
103 setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]);
104 setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]);
105 setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]);
110 /* function to delete a saved search */
111 function delete_saved($delete_index, $username, $data_dir) {
112 $saved_what_array = get_saved("saved_what", $username, $data_dir);
113 $saved_where_array = get_saved("saved_where", $username, $data_dir);
114 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
115 array_splice($saved_what_array, $delete_index, 1);
116 array_splice($saved_where_array, $delete_index, 1);
117 array_splice($saved_folder_array, $delete_index, 1);
119 $saved_count = count($saved_what_array);
120 $last_element = $saved_count +
1;
121 if ($last_element < 1) {
122 for ($i=1;$i<=$saved_count;$i++
) {
123 setPref($data_dir, $username, "saved_what" . "$i", $saved_what_array[$n]);
124 setPref($data_dir, $username, "saved_where" . "$i", $saved_where_array[$n]);
125 setPref($data_dir, $username, "saved_folder" . "$i", $saved_folder_array[$n]);
129 removePref($data_dir, $username, "saved_what" . "$last_element");
130 removePref($data_dir, $username, "saved_where" . "$last_element");
131 removePref($data_dir, $username, "saved_folder" . "$last_element");
134 /* function to save a search from recent to saved */
135 function save_recent($save_index, $username, $data_dir) {
136 $what_array = get_recent("search_what", $username, $data_dir);
137 $where_array = get_recent("search_where", $username, $data_dir);
138 $folder_array = get_recent("search_folder", $username, $data_dir);
139 $saved_what_once = array_slice($what_array, $save_index, 1);
140 $saved_where_once = array_slice($where_array, $save_index, 1);
141 $saved_folder_once = array_slice($folder_array, $save_index, 1);
142 $saved_array = get_saved("saved_what", $username, $data_dir);
143 $saved_count = (count($saved_array) +
1);
144 setPref($data_dir, $username, "saved_what" . "$saved_count", $saved_what_once[0]);
145 setPref($data_dir, $username, "saved_where" . "$saved_count", $saved_where_once[0]);
146 setPref($data_dir, $username, "saved_folder" . "$saved_count", $saved_folder_once[0]);
149 /* ------------------------ main ------------------------ */
151 /* reset these arrays on each page load just in case */
152 $what_array = array ();
153 $where_array = array ();
154 $folder_array = array ();
155 $saved_what_array = array ();
156 $saved_where_array = array ();
157 $saved_folder_array = array ();
158 $search_all = "none";
159 $perbox_count = array ();
161 /* get mailbox names */
162 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
163 $boxes = sqimap_mailbox_list($imapConnection);
166 /* set current mailbox to INBOX if none was selected or if page
167 was called to search all folders. */
168 if ($mailbox == 'None' ||
$mailbox == '' ) {
169 $mailbox = $boxes[0]['unformatted'];
171 if ($mailbox == 'All Folders') {
175 displayPageHeader($color, $mailbox);
177 /* See how the page was called and fire off correct function */
178 //if ( !isset( $submit ) ) {
180 //} else if ($submit == 'Search' && !empty($what)) {
181 if ((!isset($submit) ||
empty($submit)) && !empty($what)) {
184 if ($submit == 'Search' && !empty($what)) {
185 update_recent($what, $where, $mailbox, $username, $data_dir);
187 elseif ($submit == 'forget') {
188 forget_recent($count, $username, $data_dir);
190 elseif ($submit == 'save') {
191 save_recent($count, $username, $data_dir);
193 elseif ($submit == 'delete') {
194 delete_saved($count, $username, $data_dir);
197 do_hook('search_before_form');
200 "<table width=\"100%\">\n".
201 "<TR><td bgcolor=\"$color[0]\">\n".
202 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
206 /* update the recent and saved searches from the pref files */
207 $what_array = get_recent("search_what", $username, $data_dir);
208 $where_array = get_recent("search_where", $username, $data_dir);
209 $folder_array = get_recent("search_folder", $username, $data_dir);
210 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
211 $saved_what_array = get_saved("saved_what", $username, $data_dir);
212 $saved_where_array = get_saved("saved_where", $username, $data_dir);
213 $saved_folder_array = get_saved("saved_folder", $username, $data_dir);
214 $saved_count = count($saved_what_array);
217 /* Saved Search Table */
218 if ($saved_count > 0) {
220 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
221 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
222 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
223 for ($i=0; $i < $saved_count; ++
$i) {
225 echo "<TR BGCOLOR=\"$color[0]\">";
227 echo "<TR BGCOLOR=\"$color[4]\">";
229 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
230 . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>"
231 . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>"
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 . '>' . _("edit") . '</A>'
239 . '<A HREF=search.php'
240 . '?mailbox=' . urlencode($saved_folder_array[$i])
241 . '&what=' . urlencode($saved_what_array[$i])
242 . '&where=' . urlencode($saved_where_array[$i])
243 . '&submit=Search_no_update'
244 . '>' . _("search") . '</A>'
246 . "<A HREF=search.php?count=$i&submit=delete>"
251 echo "</TABLE></TD></TR></TABLE>\n";
254 /* Recent Search Table */
255 if ($recent_count > 0) {
257 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
258 . '<TR><TD ALIGN=CENTER><B>Recent Searches</B></TD></TR><TR><TD>'
259 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
260 for ($i=0; $i < $recent_count; ++
$i) {
261 if (!empty($what_array[$i])) {
262 if ($folder_array[$i] == "") {
263 $folder_array[$i] = "INBOX";
266 echo "<TR BGCOLOR=\"$color[0]\">";
268 echo "<TR BGCOLOR=\"$color[4]\">";
270 echo "<TD WIDTH=35%>$folder_array[$i]</TD>"
271 . "<TD ALIGN=LEFT>$what_array[$i]</TD>"
272 . "<TD ALIGN=CENTER>$where_array[$i]</TD>"
274 . "<A HREF=search.php?count=$i&submit=save>"
278 . '<A HREF=search.php'
279 . '?mailbox=' . urlencode($folder_array[$i])
280 . '&what=' . urlencode($what_array[$i])
281 . '&where=' . urlencode($where_array[$i])
282 . '&submit=Search_no_update'
283 . '>' . _("search") . '</A>'
285 . "<A HREF=search.php?count=$i&submit=forget>"
291 echo '</TABLE></TD></TR></TABLE><BR>';
295 echo '<B>' . _("Current Search") . '</B>'
296 . '<FORM ACTION="search.php" NAME=s>'
297 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
299 . ' <TD><SELECT NAME="mailbox">';
300 for ($i = 0; $i < count($boxes); $i++
) {
301 if (!in_array('noselect', $boxes[$i]['flags'])) {
302 $box = $boxes[$i]['unformatted'];
303 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
304 if( $box2 == 'INBOX' ) {
307 if ($mailbox == $box) {
308 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
311 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
315 echo "<OPTION VALUE=\"All Folders\"";
316 if ($mailbox == "All Folders") {
319 echo ">All folders</OPTION>\n";
322 " <TD ALIGN=\"CENTER\">\n";
323 if ( !isset( $what ) ) {
326 $what_disp = str_replace(',', ' ', $what);
327 $what_disp = str_replace('\\\\', '\\', $what_disp);
328 $what_disp = str_replace('\\"', '"', $what_disp);
329 $what_disp = str_replace('"', '"', $what_disp);
330 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
332 "<TD ALIGN=\"RIGHT\">\n".
333 "<SELECT NAME=\"where\">";
334 s_opt( 'BODY', $where, _("Body") );
335 s_opt( 'TEXT', $where, _("Everywhere") );
336 s_opt( 'SUBJECT', $where, _("Subject") );
337 s_opt( 'FROM', $where, _("From") );
338 s_opt( 'CC', $where, _("Cc") );
339 s_opt( 'TO', $where, _("To") );
340 echo " </SELECT>\n" .
342 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
343 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n".
348 "</TD></TR></TABLE>\n";
351 do_hook('search_after_form');
354 search all folders option still in the works. returns a table for each
355 folder it finds a match in.
358 if ($search_all == 'all') {
360 $boxcount = count($boxes);
361 echo '<BR><CENTER><B>' .
362 _("Search Results") .
363 "</B><CENTER><BR>\n";
364 for ($x=0;$x<$boxcount;$x++
) {
365 if (!in_array('noselect', $boxes[$x]['flags'])) {
366 $mailbox = $boxes[$x]['unformatted'];
368 if (($submit == "Search" ||
$submit == "Search_no_update") && !empty($what)) {
369 sqimap_mailbox_select($imapConnection, $mailbox);
370 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
371 array_push($perbox_count, $count_all);
374 for ($i=0;$i<count($perbox_count);$i++
) {
375 if ($perbox_count[$i] != "") {
380 if ($count_all == "none") {
382 _("No Messages found") .
387 /* search one folder option */
389 if (($submit == 'Search' ||
$submit == 'Search_no_update') && !empty($what)) {
390 echo '<BR><CENTER><B>' .
391 _("Search Results") .
393 sqimap_mailbox_select($imapConnection, $mailbox);
394 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
398 /* must have search terms to search */
399 if ($submit == 'Search' && empty($what)) {
400 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
403 do_hook("search_bottom");
404 sqimap_logout ($imapConnection);
405 echo '</body></html>';