Small change that fixes the HTML "select all"
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
a5ee5ac2 4 * search.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
2d367c68 11
35586184 12require_once('../src/validate.php');
13require_once('../functions/imap.php');
14require_once('../functions/imap_search.php');
15require_once('../functions/array.php');
46d38f78 16require_once('../functions/strings.php');
c61bb006 17
56e0b3b7 18
19// here are some functions, could go in imap_search.php
20// this was here, pretty handy
21
29eb5486 22function s_opt( $val, $sel, $tit ) {
23 echo " <option value=\"$val\"";
0d672ac0 24 if ( $sel == $val ) {
bd9bbfef 25 echo ' selected';
99d2a184 26 }
29eb5486 27 echo ">$tit</option>\n";
28}
99d2a184 29
56e0b3b7 30// function to get the recent searches and put them in arrays
31
32function get_recent($pref_name, $username, $data_dir) {
33 $array = array ();
34 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
35 $n = 0;
36 for ($x=1;$x<=$recent_count;$x++) {
37 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
38 $n++;
39 }
40 return $array;
41}
42
43// function to get the saved searches and put them in arrays
44
45function get_saved($pref_name, $username, $data_dir) {
46 $array = array ();
47 $n = 0;
48 for ($x=1;;$x++) {
49 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
50 if ($array[$n] == "") {
51 array_pop($array);
52 return $array;
53 }
54 $n++;
55 }
56 return $array;
57}
58
59// function to update pref file with recent searches
60
61function 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);
64 array_shift ($array);
65 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
66 $n=0;
67 for ($i=1;$i<=$recent_count;$i++) {
68 setPref($data_dir, $username, "$pref_name" . "$i", $array[$n]);
69 $n++;
70 }
71}
72
73// function to "forget" a recent search
74
75function 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);
83a92ae8 82 array_unshift($what_array, "");
83 array_unshift($where_array, "");
84 array_unshift($folder_array, "");
56e0b3b7 85 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
86 $n=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]);
91 $n++;
92 }
6c8388a9 93
94// function to delete a saved search
56e0b3b7 95}
96function 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);
103 $n=0;
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]);
111 $n++;
112 }
113 }
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");
117}
6c8388a9 118
119// function to save a search from recent to saved
120
56e0b3b7 121function 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]);
133}
134
135
136
0d672ac0 137/* ------------------------ main ------------------------ */
138
56e0b3b7 139// reset these arrays on each page load just in case
140
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";
148
149// get mailbox names
150
29eb5486 151$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
c1cb7ba4 152$boxes = sqimap_mailbox_list($imapConnection);
153
56e0b3b7 154
155// set current mailbox to INBOX if none was selected or if page
156// was called to search all folders.
157
158if ($mailbox == 'None' || $mailbox == "" ) {
159 $mailbox = $boxes[0]['unformatted'];
160}
6c8388a9 161if ($mailbox == "All Folders") {
56e0b3b7 162 $search_all = "all";
c1cb7ba4 163}
164
56e0b3b7 165// page headers
166
c1cb7ba4 167displayPageHeader($color, $mailbox);
c61bb006 168
56e0b3b7 169// if the page is called from a search link or button update recent values
170// in pref files here
0d672ac0 171
88cb1b4d 172if ( !isset( $submit ) ) {
173 $submit = '';
174} else if ($submit == 'Search' && !empty($what)) {
56e0b3b7 175 update_recent($what_array, $what, "search_what", $username, $data_dir);
176 update_recent($where_array, $where, "search_where", $username, $data_dir);
177 update_recent($folder_array, $mailbox, "search_folder", $username, $data_dir);
178}
179// if the page is called from a "forget recent" link remove search from pref file
88cb1b4d 180elseif ($submit == 'forget') {
56e0b3b7 181 forget_recent($count, $username, $data_dir);
182}
183// if the page is called from a "save recent" link add search to saved searches
88cb1b4d 184elseif ($submit == 'save') {
56e0b3b7 185 save_recent($count, $username, $data_dir);
186}
88cb1b4d 187elseif ($submit == 'delete') {
56e0b3b7 188 delete_saved($count, $username, $data_dir);
189}
190// if the page is called from a "delete saved" link delete saved search
29eb5486 191do_hook('search_before_form');
56e0b3b7 192
193echo "<BR>\n".
88cb1b4d 194 "<table width=\"100%\">\n".
195 "<TR><td bgcolor=\"$color[0]\">\n".
196 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
197 "</TD></TR>\n".
198 "</TABLE>\n";
c61bb006 199
88cb1b4d 200// update the recent and saved searches from the pref files
a7d0eaf6 201
56e0b3b7 202$what_array = get_recent("search_what", $username, $data_dir);
203$where_array = get_recent("search_where", $username, $data_dir);
204$folder_array = get_recent("search_folder", $username, $data_dir);
205$recent_count = getPref($data_dir, $username, 'search_memory', 0);
88cb1b4d 206$saved_what_array = get_saved("saved_what", $username, $data_dir);
56e0b3b7 207$saved_where_array = get_saved("saved_where", $username, $data_dir);
208$saved_folder_array = get_saved("saved_folder", $username, $data_dir);
209$saved_count = count($saved_what_array);
91954f9e 210$count_all = 0;
a7d0eaf6 211
184ef883 212/* Saved Search Table */
56e0b3b7 213if ($saved_count > 0) {
184ef883 214 echo "<BR>\n"
88cb1b4d 215 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
216 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
217 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
184ef883 218 for ($i=0; $i < $saved_count; ++$i) {
88cb1b4d 219 if ($i % 2) {
220 echo "<TR BGCOLOR=\"$color[0]\">";
221 } else {
184ef883 222 echo "<TR BGCOLOR=\"$color[4]\">";
223 }
224 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
88cb1b4d 225 . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>"
226 . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>"
227 . '<TD ALIGN=RIGHT>'
228 . '<A HREF=search.php'
229 . '?mailbox=' . urlencode($saved_folder_array[$i])
230 . '&what=' . urlencode($saved_what_array[$i])
231 . '&where=' . urlencode($saved_where_array[$i])
232 . '>' . _("edit") . '</A>'
233 . '&nbsp;|&nbsp;'
234 . '<A HREF=search.php'
235 . '?mailbox=' . urlencode($saved_folder_array[$i])
236 . '&what=' . urlencode($saved_what_array[$i])
237 . '&where=' . urlencode($saved_where_array[$i])
238 . '&submit=Search_no_update'
239 . '>' . _("search") . '</A>'
240 . '&nbsp;|&nbsp;'
241 . "<A HREF=search.php?count=$i&submit=delete>"
242 . _("delete")
243 . '</A>'
244 . '</TD></TR>';
56e0b3b7 245 }
184ef883 246 echo "</TABLE></TD></TR></TABLE>\n";
a7d0eaf6 247}
248
184ef883 249/* Recent Search Table */
56e0b3b7 250if ($recent_count > 0) {
184ef883 251 echo "<BR>\n"
252 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
253 . '<TR><TD ALIGN=CENTER><B>Recent Searches</B></TD></TR><TR><TD>'
254 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
255 for ($i=0; $i < $recent_count; ++$i) {
56e0b3b7 256 if (!empty($what_array[$i])) {
184ef883 257 if ($folder_array[$i] == "") {
258 $folder_array[$i] = "INBOX";
259 }
260 if ($i % 2) {
261 echo "<TR BGCOLOR=\"$color[0]\">";
262 } else {
263 echo "<TR BGCOLOR=\"$color[4]\">";
264 }
265 echo "<TD WIDTH=35%>$folder_array[$i]</TD>"
266 . "<TD ALIGN=LEFT>$what_array[$i]</TD>"
267 . "<TD ALIGN=CENTER>$where_array[$i]</TD>"
268 . '<TD ALIGN=RIGHT>'
269 . "<A HREF=search.php?count=$i&submit=save>"
270 . _("save")
271 . '</A>'
272 . '&nbsp;|&nbsp;'
273 . '<A HREF=search.php'
274 . '?mailbox=' . urlencode($folder_array[$i])
275 . '&what=' . urlencode($what_array[$i])
276 . '&where=' . urlencode($where_array[$i])
277 . '&submit=Search_no_update'
278 . '>' . _("search") . '</A>'
279 . '&nbsp;|&nbsp;'
280 . "<A HREF=search.php?count=$i&submit=forget>"
281 . _("forget")
282 . '</A>'
283 . '</TD></TR>';
0d672ac0 284 }
29eb5486 285 }
184ef883 286 echo '</TABLE></TD></TR></TABLE><BR>';
56e0b3b7 287}
184ef883 288
289/* Search Form */
290echo '<B>Current Search</B>'
f1c7f8bb 291 . '<FORM ACTION="search.php" NAME=s>'
184ef883 292 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
293 . ' <TR>'
294 . ' <TD><SELECT NAME="mailbox">';
56e0b3b7 295for ($i = 0; $i < count($boxes); $i++) {
296 if (!in_array('noselect', $boxes[$i]['flags'])) {
297 $box = $boxes[$i]['unformatted'];
298 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
299 if ($mailbox == $box) {
300 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
0d672ac0 301 }
56e0b3b7 302 else {
303 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
88cb1b4d 304 }
56e0b3b7 305 }
306}
6c8388a9 307 echo "<OPTION VALUE=\"All Folders\"";
308 if ($mailbox == "All Folders") {
56e0b3b7 309 echo "SELECTED";
0d672ac0 310 }
56e0b3b7 311 echo ">All folders</OPTION>\n";
312echo ' </SELECT>'.
313 " </TD>\n".
314 " <TD ALIGN=\"CENTER\">\n";
88cb1b4d 315if ( !isset( $what ) ) {
316 $what = '';
317}
56e0b3b7 318$what_disp = str_replace(',', ' ', $what);
319$what_disp = str_replace('\\\\', '\\', $what_disp);
320$what_disp = str_replace('\\"', '"', $what_disp);
321$what_disp = str_replace('"', '&quot;', $what_disp);
322echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
323 " </TD>\n".
324 "<TD ALIGN=\"RIGHT\">\n".
325 "<SELECT NAME=\"where\">";
326s_opt( 'BODY', $where, _("Body") );
327s_opt( 'TEXT', $where, _("Everywhere") );
328s_opt( 'SUBJECT', $where, _("Subject") );
329s_opt( 'FROM', $where, _("From") );
330s_opt( 'CC', $where, _("Cc") );
331s_opt( 'TO', $where, _("To") );
332echo " </SELECT>\n" .
333 " </TD>\n".
334 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
335 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n".
336 " </TD>\n".
337 " </TR>\n".
338 "</FORM>\n".
339 " </TABLE>\n".
340 "</TD></TR></TABLE>\n";
341
342
88cb1b4d 343do_hook('search_after_form');
56e0b3b7 344
88cb1b4d 345/*
346 search all folders option still in the works. returns a table for each
347 folder it finds a match in. The toggle all link does not work
348*/
56e0b3b7 349
88cb1b4d 350if ($search_all == 'all') {
351 $mailbox == '';
56e0b3b7 352 $boxcount = count($boxes);
88cb1b4d 353 echo '<BR><CENTER><B>' .
354 _("Search Results") .
355 "</B><CENTER><BR>\n";
56e0b3b7 356 for ($x=0;$x<$boxcount;$x++) {
357 if (!in_array('noselect', $boxes[$x]['flags'])) {
88cb1b4d 358 $mailbox = $boxes[$x]['unformatted'];
359 }
56e0b3b7 360 if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) {
361 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 362 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
363 }
364 }
365 if ($count_all == 0) {
366 echo '<br><b>' .
367 _("No Messages found") .
368 '</b><br>';
56e0b3b7 369 }
0d672ac0 370}
0d672ac0 371
88cb1b4d 372// search one folder option
56e0b3b7 373
374else {
88cb1b4d 375 if (($submit == 'Search' || $submit == 'Search_no_update') && !empty($what)) {
376 echo '<BR><CENTER><B>' .
377 _("Search Results") .
378 "</B></CENTER>\n";
56e0b3b7 379 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 380 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
56e0b3b7 381 }
29eb5486 382}
56e0b3b7 383
88cb1b4d 384// must have search terms to search
56e0b3b7 385
88cb1b4d 386if ($submit == 'Search' && empty($what)) {
56e0b3b7 387 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
388}
389
29eb5486 390do_hook("search_bottom");
99d2a184 391
56e0b3b7 392// all done
393
394sqimap_logout ($imapConnection);
29eb5486 395echo '</body></html>';
99d2a184 396
91954f9e 397?>