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 the attributes array */
32 function get_recent($username, $data_dir) {
33 $attributes = array();
34 $types = array('search_what', 'search_where', 'search_folder');
35 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
36 for ($x=1;$x<=$recent_count;$x++
) {
38 foreach ($types as $key) {
39 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
45 /* function to get the saved searches and put them in the saved_attributes array */
46 function get_saved($username, $data_dir) {
47 $saved_attributes = array();
48 $types = array('saved_what', 'saved_where', 'saved_folder');
49 foreach ($types as $key) {
51 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
52 if ($saved_attributes[$key][$x] == "") {
53 array_pop($saved_attributes[$key]);
58 return $saved_attributes;
61 /* function to update recent pref arrays */
62 function update_recent($what, $where, $mailbox, $username, $data_dir) {
63 $attributes = array();
64 $types = array('search_what', 'search_where', 'search_folder');
65 $input = array($what, $where, $mailbox);
66 $attributes = get_recent( $username, $data_dir);
69 for ($i=1;$i<=count($attributes['search_what']);$i++
) {
70 if (isset($attributes['search_what'][$i])) {
71 if ($what == $attributes['search_what'][$i] &&
72 $where == $attributes['search_where'][$i] &&
73 $mailbox == $attributes['search_folder'][$i]) {
80 foreach ($types as $key) {
81 array_push ($attributes[$key], $input[$i]);
82 array_shift ($attributes[$key]);
85 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
87 for ($i=1;$i<=$recent_count;$i++
) {
89 foreach ($types as $key) {
90 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
97 /* function to forget a recent search */
98 function forget_recent($forget_index, $username, $data_dir) {
99 $attributes = array();
100 $types = array('search_what', 'search_where', 'search_folder');
101 $attributes = get_recent( $username, $data_dir);
103 foreach ($types as $key) {
104 array_splice($attributes[$key], $forget_index, 1);
105 array_unshift($attributes[$key], '');
108 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
110 for ($i=1;$i<=$recent_count;$i++
) {
112 foreach ($types as $key) {
113 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
119 /* function to delete a saved search */
120 function delete_saved($delete_index, $username, $data_dir) {
121 $types = array('saved_what', 'saved_where', 'saved_folder');
122 $attributes = get_saved($username, $data_dir);
123 foreach ($types as $key) {
124 array_splice($attributes[$key], $delete_index, 1);
128 $saved_count = count($attributes['saved_what']);
129 $last_element = $saved_count +
1;
130 for ($i=1;$i<=$saved_count;$i++
) {
132 foreach ($types as $key) {
133 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
138 foreach($types as $key) {
139 removePref($data_dir, $username, $key.$last_element);
143 /* function to save a search from recent to saved */
144 function save_recent($save_index, $username, $data_dir) {
145 $attributes = array();
146 $types = array('search_what', 'search_where', 'search_folder');
147 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
148 $saved_array = get_saved($username, $data_dir);
149 $save_index = $save_index -1;
150 $saved_count = (count($saved_array['saved_what']) +
1);
151 $attributes = get_recent ($username, $data_dir);
153 foreach ($types as $key) {
154 $slice = array_slice($attributes[$key], $save_index, 1);
155 $name = $saved_types[$n];
156 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
161 /* ------------------------ main ------------------------ */
163 /* reset these arrays on each page load just in case */
164 $attributes = array ();
165 $saved_attributes = array ();
166 $search_all = 'none';
167 $perbox_count = array ();
168 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
170 /* get mailbox names */
171 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
172 $boxes = sqimap_mailbox_list($imapConnection);
174 /* set current mailbox to INBOX if none was selected or if page
175 was called to search all folders. */
176 if ( !isset($mailbox) ||
$mailbox == 'None' ||
$mailbox == '' ) {
177 $mailbox = $boxes[0]['unformatted'];
179 if ($mailbox == 'All Folders') {
183 displayPageHeader($color, $mailbox);
185 /* See how the page was called and fire off correct function */
186 if ((!isset($submit) ||
empty($submit)) && !empty($what)) {
187 $submit = _("Search");
189 if ( !isset( $submit ) ) {
191 } else if ($submit == _("Search") && !empty($what)) {
192 if ($recent_count > 0) {
193 update_recent($what, $where, $mailbox, $username, $data_dir);
196 elseif ($submit == 'forget') {
197 forget_recent($count, $username, $data_dir);
199 elseif ($submit == 'save') {
200 save_recent($count, $username, $data_dir);
202 elseif ($submit == 'delete') {
203 delete_saved($count, $username, $data_dir);
206 do_hook('search_before_form');
209 "<table width=\"100%\">\n".
210 "<TR><td bgcolor=\"$color[0]\">\n".
211 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
215 /* update the recent and saved searches from the pref files */
216 $attributes = get_recent($username, $data_dir);
217 $saved_attributes = get_saved($username, $data_dir);
218 $saved_count = count($saved_attributes['saved_what']);
221 /* Saved Search Table */
222 if ($saved_count > 0) {
224 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
225 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
226 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
227 for ($i=0; $i < $saved_count; ++
$i) {
229 echo "<TR BGCOLOR=\"$color[0]\">";
231 echo "<TR BGCOLOR=\"$color[4]\">";
233 echo "<TD WIDTH=\"35%\">".$saved_attributes['saved_folder'][$i]."</TD>"
234 . "<TD ALIGN=LEFT>".$saved_attributes['saved_what'][$i]."</TD>"
235 . "<TD ALIGN=CENTER>".$saved_attributes['saved_where'][$i]."</TD>"
237 . '<A HREF=search.php'
238 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
239 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
240 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
241 . '>' . _("edit") . '</A>'
243 . '<A HREF=search.php'
244 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
245 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
246 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
247 . '&submit=Search_no_update'
248 . '>' . _("search") . '</A>'
250 . "<A HREF=search.php?count=$i&submit=delete>"
255 echo "</TABLE></TD></TR></TABLE>\n";
258 /* Recent Search Table */
259 if ($recent_count > 0) {
261 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
262 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
263 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
264 for ($i=1; $i <= $recent_count; ++
$i) {
265 if (isset($attributes['search_folder'][$i])) {
266 if ($attributes['search_folder'][$i] == "") {
267 $attributes['search_folder'][$i] = "INBOX";
271 echo "<TR BGCOLOR=\"$color[0]\">";
273 echo "<TR BGCOLOR=\"$color[4]\">";
275 if (isset($attributes['search_what'][$i]) &&
276 !empty($attributes['search_what'][$i])) {
277 echo "<TD WIDTH=35%>".$attributes['search_folder'][$i]."</TD>"
278 . "<TD ALIGN=LEFT>".$attributes['search_what'][$i]."</TD>"
279 . "<TD ALIGN=CENTER>".$attributes['search_where'][$i]."</TD>"
281 . "<A HREF=search.php?count=$i&submit=save>"
285 . '<A HREF=search.php'
286 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
287 . '&what=' . urlencode($attributes['search_what'][$i])
288 . '&where=' . urlencode($attributes['search_where'][$i])
289 . '&submit=Search_no_update'
290 . '>' . _("search") . '</A>'
292 . "<A HREF=search.php?count=$i&submit=forget>"
298 echo '</TABLE></TD></TR></TABLE><BR>';
302 echo '<B>' . _("Current Search") . '</B>'
303 . '<FORM ACTION="search.php" NAME=s>'
304 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
306 . ' <TD><SELECT NAME="mailbox">';
307 for ($i = 0; $i < count($boxes); $i++
) {
308 if (!in_array('noselect', $boxes[$i]['flags'])) {
309 $box = $boxes[$i]['unformatted'];
310 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
311 if( $box2 == 'INBOX' ) {
314 if ($mailbox == $box) {
315 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
318 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
322 echo "<OPTION VALUE=\"All Folders\"";
323 if ($mailbox == "All Folders") {
326 echo ">All folders</OPTION>\n";
329 " <TD ALIGN=\"CENTER\">\n";
330 if ( !isset( $what ) ) {
333 if ( !isset( $where ) ) {
338 $what_disp = str_replace(',', ' ', $what);
339 $what_disp = str_replace('\\\\', '\\', $what_disp);
340 $what_disp = str_replace('\\"', '"', $what_disp);
341 $what_disp = str_replace('"', '"', $what_disp);
342 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
344 "<TD ALIGN=\"RIGHT\">\n".
345 "<SELECT NAME=\"where\">";
346 s_opt( 'BODY', $where, _("Body") );
347 s_opt( 'TEXT', $where, _("Everywhere") );
348 s_opt( 'SUBJECT', $where, _("Subject") );
349 s_opt( 'FROM', $where, _("From") );
350 s_opt( 'CC', $where, _("Cc") );
351 s_opt( 'TO', $where, _("To") );
352 echo " </SELECT>\n" .
354 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
355 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
360 "</TD></TR></TABLE>\n";
363 do_hook('search_after_form');
366 search all folders option still in the works. returns a table for each
367 folder it finds a match in.
371 if ($allow_thread_sort == TRUE) {
372 $old_value = $allow_thread_sort;
373 $allow_thread_sort = FALSE;
376 if ($search_all == 'all') {
378 $boxcount = count($boxes);
379 echo '<BR><CENTER><B>' .
380 _("Search Results") .
381 "</B><CENTER><BR>\n";
382 for ($x=0;$x<$boxcount;$x++
) {
383 if (!in_array('noselect', $boxes[$x]['flags'])) {
384 $mailbox = $boxes[$x]['unformatted'];
386 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
387 sqimap_mailbox_select($imapConnection, $mailbox);
388 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
389 array_push($perbox_count, $count_all);
392 for ($i=0;$i<count($perbox_count);$i++
) {
393 if ($perbox_count[$i] != "") {
394 $count_all = "found";
398 if ($count_all != "found") {
400 _("No Messages found") .
405 /* search one folder option */
407 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
408 echo '<BR><CENTER><B>' .
409 _("Search Results") .
411 sqimap_mailbox_select($imapConnection, $mailbox);
412 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
416 /* must have search terms to search */
417 if ($submit == _("Search") && empty($what)) {
418 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
421 $allow_thread_sort = $old_value;
423 do_hook('search_bottom');
424 sqimap_logout ($imapConnection);
425 echo '</body></html>';