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');
18 global $allow_thread_sort;
20 /* here are some functions, could go in imap_search.php
22 this was here, pretty handy */
23 function s_opt( $val, $sel, $tit ) {
24 echo " <option value=\"$val\"";
28 echo ">$tit</option>\n";
31 /* 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 */
44 function get_saved($pref_name, $username, $data_dir) {
48 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
49 if ($array[$n] == "") {
58 /* function to update recent pref arrays */
59 function update_recent($what, $where, $mailbox, $username, $data_dir) {
60 $what_array = get_recent('search_what', $username, $data_dir);
61 $where_array = get_recent('search_where', $username, $data_dir);
62 $folder_array = get_recent('search_folder', $username, $data_dir);
64 for ($i=0;$i<count($what_array);$i++
) {
65 if ($what == $what_array[$i] &&
66 $where == $where_array[$i] &&
67 $mailbox == $folder_array[$i]) {
73 array_push ($what_array, $what);
74 array_push ($where_array, $where);
75 array_push ($folder_array, $mailbox);
76 array_shift ($what_array);
77 array_shift ($where_array);
78 array_shift ($folder_array);
79 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
81 for ($i=1;$i<=$recent_count;$i++
) {
82 setPref($data_dir, $username, "search_what$i", $what_array[$n]);
83 setPref($data_dir, $username, "search_where$i", $where_array[$n]);
84 setPref($data_dir, $username, "search_folder$i", $folder_array[$n]);
90 /* function to forget a recent search */
91 function forget_recent($forget_index, $username, $data_dir) {
92 $what_array = get_recent('search_what', $username, $data_dir);
93 $where_array = get_recent('search_where', $username, $data_dir);
94 $folder_array = get_recent('search_folder', $username, $data_dir);
95 array_splice($what_array, $forget_index, 1);
96 array_splice($where_array, $forget_index, 1);
97 array_splice($folder_array, $forget_index, 1);
98 array_unshift($what_array, '');
99 array_unshift($where_array, '');
100 array_unshift($folder_array, '');
101 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
103 for ($i=1;$i<=$recent_count;$i++
) {
104 setPref($data_dir, $username, "search_what$i", $what_array[$n]);
105 setPref($data_dir, $username, "search_where$i", $where_array[$n]);
106 setPref($data_dir, $username, "search_folder$i", $folder_array[$n]);
111 /* function to delete a saved search */
112 function delete_saved($delete_index, $username, $data_dir) {
113 $saved_what_array = get_saved('saved_what', $username, $data_dir);
114 $saved_where_array = get_saved('saved_where', $username, $data_dir);
115 $saved_folder_array = get_saved('saved_folder', $username, $data_dir);
116 array_splice($saved_what_array, $delete_index, 1);
117 array_splice($saved_where_array, $delete_index, 1);
118 array_splice($saved_folder_array, $delete_index, 1);
120 $saved_count = count($saved_what_array);
121 $last_element = $saved_count +
1;
122 if ($last_element < 1) {
123 for ($i=1;$i<=$saved_count;$i++
) {
124 setPref($data_dir, $username, "saved_what$i", $saved_what_array[$n]);
125 setPref($data_dir, $username, "saved_where$i", $saved_where_array[$n]);
126 setPref($data_dir, $username, "saved_folder$i", $saved_folder_array[$n]);
130 removePref($data_dir, $username, "saved_what$last_element");
131 removePref($data_dir, $username, "saved_where$last_element");
132 removePref($data_dir, $username, "saved_folder$last_element");
135 /* function to save a search from recent to saved */
136 function save_recent($save_index, $username, $data_dir) {
137 $what_array = get_recent('search_what', $username, $data_dir);
138 $where_array = get_recent('search_where', $username, $data_dir);
139 $folder_array = get_recent('search_folder', $username, $data_dir);
140 $saved_what_once = array_slice($what_array, $save_index, 1);
141 $saved_where_once = array_slice($where_array, $save_index, 1);
142 $saved_folder_once = array_slice($folder_array, $save_index, 1);
143 $saved_array = get_saved('saved_what', $username, $data_dir);
144 $saved_count = (count($saved_array) +
1);
145 setPref($data_dir, $username, "saved_what$saved_count", $saved_what_once[0]);
146 setPref($data_dir, $username, "saved_where$saved_count", $saved_where_once[0]);
147 setPref($data_dir, $username, "saved_folder$saved_count", $saved_folder_once[0]);
150 /* ------------------------ main ------------------------ */
152 /* reset these arrays on each page load just in case */
153 $what_array = array ();
154 $where_array = array ();
155 $folder_array = array ();
156 $saved_what_array = array ();
157 $saved_where_array = array ();
158 $saved_folder_array = array ();
159 $search_all = 'none';
160 $perbox_count = array ();
162 /* get mailbox names */
163 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
164 $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 ( !isset($mailbox) ||
$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) ||
empty($submit)) && !empty($what)) {
179 $submit = _("Search");
181 if ( !isset( $submit ) ) {
183 Jason, leave E_ALL only on error reporting in php.ini
184 Variables must contain a value befor to check them,
185 or else there are compilation slow down as PHP has
187 Remove this comment once read 8-)
190 } else if ($submit == _("Search") && !empty($what)) {
191 update_recent($what, $where, $mailbox, $username, $data_dir);
193 elseif ($submit == 'forget') {
194 forget_recent($count, $username, $data_dir);
196 elseif ($submit == 'save') {
197 save_recent($count, $username, $data_dir);
199 elseif ($submit == 'delete') {
200 delete_saved($count, $username, $data_dir);
203 do_hook('search_before_form');
206 "<table width=\"100%\">\n".
207 "<TR><td bgcolor=\"$color[0]\">\n".
208 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
212 /* update the recent and saved searches from the pref files */
213 $what_array = get_recent('search_what', $username, $data_dir);
214 $where_array = get_recent('search_where', $username, $data_dir);
215 $folder_array = get_recent('search_folder', $username, $data_dir);
216 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
217 $saved_what_array = get_saved('saved_what', $username, $data_dir);
218 $saved_where_array = get_saved('saved_where', $username, $data_dir);
219 $saved_folder_array = get_saved('saved_folder', $username, $data_dir);
220 $saved_count = count($saved_what_array);
223 /* Saved Search Table */
224 if ($saved_count > 0) {
226 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
227 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
228 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
229 for ($i=0; $i < $saved_count; ++
$i) {
231 echo "<TR BGCOLOR=\"$color[0]\">";
233 echo "<TR BGCOLOR=\"$color[4]\">";
235 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
236 . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>"
237 . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>"
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 . '>' . _("edit") . '</A>'
245 . '<A HREF=search.php'
246 . '?mailbox=' . urlencode($saved_folder_array[$i])
247 . '&what=' . urlencode($saved_what_array[$i])
248 . '&where=' . urlencode($saved_where_array[$i])
249 . '&submit=Search_no_update'
250 . '>' . _("search") . '</A>'
252 . "<A HREF=search.php?count=$i&submit=delete>"
257 echo "</TABLE></TD></TR></TABLE>\n";
260 /* Recent Search Table */
261 if ($recent_count > 0) {
263 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
264 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
265 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
266 for ($i=0; $i < $recent_count; ++
$i) {
267 if (!empty($what_array[$i])) {
268 if ($folder_array[$i] == "") {
269 $folder_array[$i] = "INBOX";
272 echo "<TR BGCOLOR=\"$color[0]\">";
274 echo "<TR BGCOLOR=\"$color[4]\">";
276 echo "<TD WIDTH=35%>$folder_array[$i]</TD>"
277 . "<TD ALIGN=LEFT>$what_array[$i]</TD>"
278 . "<TD ALIGN=CENTER>$where_array[$i]</TD>"
280 . "<A HREF=search.php?count=$i&submit=save>"
284 . '<A HREF=search.php'
285 . '?mailbox=' . urlencode($folder_array[$i])
286 . '&what=' . urlencode($what_array[$i])
287 . '&where=' . urlencode($where_array[$i])
288 . '&submit=Search_no_update'
289 . '>' . _("search") . '</A>'
291 . "<A HREF=search.php?count=$i&submit=forget>"
297 echo '</TABLE></TD></TR></TABLE><BR>';
301 echo '<B>' . _("Current Search") . '</B>'
302 . '<FORM ACTION="search.php" NAME=s>'
303 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
305 . ' <TD><SELECT NAME="mailbox">';
306 for ($i = 0; $i < count($boxes); $i++
) {
307 if (!in_array('noselect', $boxes[$i]['flags'])) {
308 $box = $boxes[$i]['unformatted'];
309 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
310 if( $box2 == 'INBOX' ) {
313 if ($mailbox == $box) {
314 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
317 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
321 echo "<OPTION VALUE=\"All Folders\"";
322 if ($mailbox == "All Folders") {
325 echo ">All folders</OPTION>\n";
328 " <TD ALIGN=\"CENTER\">\n";
329 if ( !isset( $what ) ) {
332 $what_disp = str_replace(',', ' ', $what);
333 $what_disp = str_replace('\\\\', '\\', $what_disp);
334 $what_disp = str_replace('\\"', '"', $what_disp);
335 $what_disp = str_replace('"', '"', $what_disp);
336 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
338 "<TD ALIGN=\"RIGHT\">\n".
339 "<SELECT NAME=\"where\">";
340 s_opt( 'BODY', $where, _("Body") );
341 s_opt( 'TEXT', $where, _("Everywhere") );
342 s_opt( 'SUBJECT', $where, _("Subject") );
343 s_opt( 'FROM', $where, _("From") );
344 s_opt( 'CC', $where, _("Cc") );
345 s_opt( 'TO', $where, _("To") );
346 echo " </SELECT>\n" .
348 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
349 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
354 "</TD></TR></TABLE>\n";
357 do_hook('search_after_form');
360 search all folders option still in the works. returns a table for each
361 folder it finds a match in.
365 if ($allow_thread_sort == true) {
366 $old_value = $allow_thread_sort;
367 $allow_thread_sort = false;
370 if ($search_all == 'all') {
372 $boxcount = count($boxes);
373 echo '<BR><CENTER><B>' .
374 _("Search Results") .
375 "</B><CENTER><BR>\n";
376 for ($x=0;$x<$boxcount;$x++
) {
377 if (!in_array('noselect', $boxes[$x]['flags'])) {
378 $mailbox = $boxes[$x]['unformatted'];
380 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
381 sqimap_mailbox_select($imapConnection, $mailbox);
382 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
383 array_push($perbox_count, $count_all);
386 for ($i=0;$i<count($perbox_count);$i++
) {
387 if ($perbox_count[$i] != "") {
392 if ($count_all == "none") {
394 _("No Messages found") .
399 /* search one folder option */
401 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
402 echo '<BR><CENTER><B>' .
403 _("Search Results") .
405 sqimap_mailbox_select($imapConnection, $mailbox);
406 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
410 /* must have search terms to search */
411 if ($submit == _("Search") && empty($what)) {
412 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
415 $allow_thread_sort = $old_value;
417 do_hook('search_bottom');
418 sqimap_logout ($imapConnection);
419 echo '</body></html>';