r2l by yoav
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
11
12 require_once('../src/validate.php');
13 require_once('../functions/imap.php');
14 require_once('../functions/imap_search.php');
15 require_once('../functions/imap_utf7_decode_local.php');
16 require_once('../functions/array.php');
17 require_once('../functions/strings.php');
18 require_once('../functions/html.php');
19
20 global $allow_thread_sort;
21
22 /* here are some functions, could go in imap_search.php
23
24 this was here, pretty handy */
25 function s_opt( $val, $sel, $tit ) {
26 echo " <option value=\"$val\"";
27 if ( $sel == $val ) {
28 echo ' selected';
29 }
30 echo ">$tit</option>\n";
31 }
32
33 /* function to get the recent searches and put them in the attributes array */
34 function get_recent($username, $data_dir) {
35 $attributes = array();
36 $types = array('search_what', 'search_where', 'search_folder');
37 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
38 for ($x=1;$x<=$recent_count;$x++) {
39 reset($types);
40 foreach ($types as $key) {
41 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
42 }
43 }
44 return $attributes;
45 }
46
47 /* function to get the saved searches and put them in the saved_attributes array */
48 function get_saved($username, $data_dir) {
49 $saved_attributes = array();
50 $types = array('saved_what', 'saved_where', 'saved_folder');
51 foreach ($types as $key) {
52 for ($x=1;;$x++) {
53 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
54 if ($saved_attributes[$key][$x] == "") {
55 array_pop($saved_attributes[$key]);
56 break;
57 }
58 }
59 }
60 return $saved_attributes;
61 }
62
63 /* function to update recent pref arrays */
64 function update_recent($what, $where, $mailbox, $username, $data_dir) {
65 $attributes = array();
66 $types = array('search_what', 'search_where', 'search_folder');
67 $input = array($what, $where, $mailbox);
68 $attributes = get_recent( $username, $data_dir);
69 reset($types);
70 $dupe = 'no';
71 for ($i=1;$i<=count($attributes['search_what']);$i++) {
72 if (isset($attributes['search_what'][$i])) {
73 if ($what == $attributes['search_what'][$i] &&
74 $where == $attributes['search_where'][$i] &&
75 $mailbox == $attributes['search_folder'][$i]) {
76 $dupe = 'yes';
77 }
78 }
79 }
80 if ($dupe == 'no') {
81 $i = 0;
82 foreach ($types as $key) {
83 array_push ($attributes[$key], $input[$i]);
84 array_shift ($attributes[$key]);
85 $i++;
86 }
87 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
88 $n=0;
89 for ($i=1;$i<=$recent_count;$i++) {
90 reset($types);
91 foreach ($types as $key) {
92 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
93 }
94 $n++;
95 }
96 }
97 }
98
99 /* function to forget a recent search */
100 function forget_recent($forget_index, $username, $data_dir) {
101 $attributes = array();
102 $types = array('search_what', 'search_where', 'search_folder');
103 $attributes = get_recent( $username, $data_dir);
104 reset($types);
105 foreach ($types as $key) {
106 array_splice($attributes[$key], $forget_index, 1);
107 array_unshift($attributes[$key], '');
108 }
109 reset($types);
110 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
111 $n=0;
112 for ($i=1;$i<=$recent_count;$i++) {
113 reset($types);
114 foreach ($types as $key) {
115 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
116 }
117 $n++;
118 }
119 }
120
121 /* function to delete a saved search */
122 function delete_saved($delete_index, $username, $data_dir) {
123 $types = array('saved_what', 'saved_where', 'saved_folder');
124 $attributes = get_saved($username, $data_dir);
125 foreach ($types as $key) {
126 array_splice($attributes[$key], $delete_index, 1);
127 }
128 reset($types);
129 $n=0;
130 $saved_count = count($attributes['saved_what']);
131 $last_element = $saved_count + 1;
132 for ($i=1;$i<=$saved_count;$i++) {
133 reset($types);
134 foreach ($types as $key) {
135 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
136 }
137 $n++;
138 }
139 reset($types);
140 foreach($types as $key) {
141 removePref($data_dir, $username, $key.$last_element);
142 }
143 }
144
145 /* function to save a search from recent to saved */
146 function save_recent($save_index, $username, $data_dir) {
147 $attributes = array();
148 $types = array('search_what', 'search_where', 'search_folder');
149 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
150 $saved_array = get_saved($username, $data_dir);
151 $save_index = $save_index -1;
152 $saved_count = (count($saved_array['saved_what']) + 1);
153 $attributes = get_recent ($username, $data_dir);
154 $n = 0;
155 foreach ($types as $key) {
156 $slice = array_slice($attributes[$key], $save_index, 1);
157 $name = $saved_types[$n];
158 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
159 $n++;
160 }
161 }
162
163 /* ------------------------ main ------------------------ */
164
165 /* reset these arrays on each page load just in case */
166 $attributes = array ();
167 $saved_attributes = array ();
168 $search_all = 'none';
169 $perbox_count = array ();
170 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
171
172 /* get mailbox names */
173 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
174 $boxes = sqimap_mailbox_list($imapConnection);
175
176 /* set current mailbox to INBOX if none was selected or if page
177 was called to search all folders. */
178 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
179 $mailbox = $boxes[0]['unformatted'];
180 }
181 if ($mailbox == 'All Folders') {
182 $search_all = 'all';
183 }
184
185 if (isset($composenew) && $composenew) {
186 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
187 "&amp;session=$composesession&amp;attachedmessages=true&amp";
188 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
189 } else {
190 displayPageHeader($color, $mailbox);
191 }
192 /* See how the page was called and fire off correct function */
193 if ((!isset($submit) || empty($submit)) && !empty($what)) {
194 $submit = _("Search");
195 }
196 if ( !isset( $submit ) ) {
197 $submit = '';
198 } else if ($submit == _("Search") && !empty($what)) {
199 if ($recent_count > 0) {
200 update_recent($what, $where, $mailbox, $username, $data_dir);
201 }
202 }
203 elseif ($submit == 'forget') {
204 forget_recent($count, $username, $data_dir);
205 }
206 elseif ($submit == 'save') {
207 save_recent($count, $username, $data_dir);
208 }
209 elseif ($submit == 'delete') {
210 delete_saved($count, $username, $data_dir);
211 }
212
213 do_hook('search_before_form');
214
215 echo "<br>\n".
216 html_tag( 'table',
217 html_tag( 'tr', "\n" .
218 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
219 ) ,
220 '', '', 'width="100%"') . "\n";
221
222 /* update the recent and saved searches from the pref files */
223 $attributes = get_recent($username, $data_dir);
224 $saved_attributes = get_saved($username, $data_dir);
225 $saved_count = count($saved_attributes['saved_what']);
226 $count_all = 0;
227
228 /* Saved Search Table */
229 if ($saved_count > 0) {
230 echo "<br>\n"
231 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
232 . html_tag( 'tr',
233 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
234 )
235 . html_tag( 'tr' )
236 . html_tag( 'td' )
237 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
238 for ($i=0; $i < $saved_count; ++$i) {
239 if ($i % 2) {
240 echo html_tag( 'tr', '', '', $color[0] );
241 } else {
242 echo html_tag( 'tr', '', '', $color[4] );
243 }
244 echo html_tag( 'td', $saved_attributes['saved_folder'][$i], 'left', '', 'width="35%"' )
245 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
246 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
247 . html_tag( 'td', '', 'right' )
248 . '<a href=search.php'
249 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
250 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
251 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
252 . '>' . _("edit") . '</a>'
253 . '&nbsp;|&nbsp;'
254 . '<a href=search.php'
255 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
256 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
257 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
258 . '&amp;submit=Search_no_update'
259 . '>' . _("search") . '</a>'
260 . '&nbsp;|&nbsp;'
261 . "<a href=search.php?count=$i&amp;submit=delete>"
262 . _("delete")
263 . '</a>'
264 . '</td></tr>';
265 }
266 echo "</table></td></tr></table>\n";
267 }
268
269 /* Recent Search Table */
270 if ($recent_count > 0) {
271 echo "<br>\n"
272 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
273 . html_tag( 'tr',
274 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
275 )
276 . html_tag( 'tr' )
277 . html_tag( 'td' )
278 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
279 for ($i=1; $i <= $recent_count; ++$i) {
280 if (isset($attributes['search_folder'][$i])) {
281 if ($attributes['search_folder'][$i] == "") {
282 $attributes['search_folder'][$i] = "INBOX";
283 }
284 }
285 if ($i % 2) {
286 echo html_tag( 'tr', '', '', $color[0] );
287 } else {
288 echo html_tag( 'tr', '', '', $color[0] );
289 }
290 if (isset($attributes['search_what'][$i]) &&
291 !empty($attributes['search_what'][$i])) {
292 echo html_tag( 'td', $attributes['search_folder'][$i], 'left', '', 'width="35%"' )
293 . html_tag( 'td', $attributes['search_what'][$i], 'left' )
294 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
295 . html_tag( 'td', '', 'right' )
296 . "<a href=search.php?count=$i&amp;submit=save>"
297 . _("save")
298 . '</a>'
299 . '&nbsp;|&nbsp;'
300 . '<a href=search.php'
301 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
302 . '&amp;what=' . urlencode($attributes['search_what'][$i])
303 . '&amp;where=' . urlencode($attributes['search_where'][$i])
304 . '&amp;submit=Search_no_update'
305 . '>' . _("search") . '</a>'
306 . '&nbsp;|&nbsp;'
307 . "<a href=search.php?count=$i&amp;submit=forget>"
308 . _("forget")
309 . '</a>'
310 . '</td></tr>';
311 }
312 }
313 echo '</table></td></tr></table><br>';
314 }
315
316 /* Search Form */
317 echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
318 . '<form action="search.php" name="s">'
319 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
320 . html_tag( 'tr' )
321 . html_tag( 'td', '', 'left' )
322 . '<select name="mailbox">';
323 for ($i = 0; $i < count($boxes); $i++) {
324 if (!in_array('noselect', $boxes[$i]['flags'])) {
325 $box = $boxes[$i]['unformatted'];
326 $box2 = str_replace(' ', '&nbsp;',
327 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
328 if( $box2 == 'INBOX' ) {
329 $box2 = _("INBOX");
330 }
331 echo ' <option value="' . $box . '"';
332 if ($mailbox == $box) { echo ' selected'; }
333 echo '>' . $box2 . '</option>' . "\n";
334 }
335 }
336 echo '<option value="All Folders"';
337 if ($mailbox == 'All Folders') {
338 echo ' selected';
339 }
340 echo ">All folders</option>\n";
341 echo ' </select>'.
342 " </td>\n";
343 if ( !isset( $what ) ) {
344 $what = '';
345 }
346 if ( !isset( $where ) ) {
347 $where = '';
348 }
349
350
351 $what_disp = str_replace(',', ' ', $what);
352 $what_disp = str_replace('\\\\', '\\', $what_disp);
353 $what_disp = str_replace('\\"', '"', $what_disp);
354 $what_disp = str_replace('"', '&quot;', $what_disp);
355 echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' )
356 . html_tag( 'td', '', 'right' )
357 . "<select name=\"where\">";
358 s_opt( 'BODY', $where, _("Body") );
359 s_opt( 'TEXT', $where, _("Everywhere") );
360 s_opt( 'SUBJECT', $where, _("Subject") );
361 s_opt( 'FROM', $where, _("From") );
362 s_opt( 'CC', $where, _("Cc") );
363 s_opt( 'TO', $where, _("To") );
364 echo " </select>\n" .
365 " </td>\n".
366 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
367 " </tr>\n".
368 "</form>\n".
369 " </table>\n".
370 "</td></tr></table>\n";
371
372
373 do_hook('search_after_form');
374
375 /*
376 search all folders option still in the works. returns a table for each
377 folder it finds a match in.
378 */
379
380 $old_value = 0;
381 if ($allow_thread_sort == TRUE) {
382 $old_value = $allow_thread_sort;
383 $allow_thread_sort = FALSE;
384 }
385
386 if ($search_all == 'all') {
387 $mailbox == '';
388 $boxcount = count($boxes);
389 echo '<br><center><b>' .
390 _("Search Results") .
391 "</b><center><br>\n";
392 for ($x=0;$x<$boxcount;$x++) {
393 if (!in_array('noselect', $boxes[$x]['flags'])) {
394 $mailbox = $boxes[$x]['unformatted'];
395 }
396 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
397 sqimap_mailbox_select($imapConnection, $mailbox);
398 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
399 array_push($perbox_count, $count_all);
400 }
401 }
402 for ($i=0;$i<count($perbox_count);$i++) {
403 if ($perbox_count[$i] != "") {
404 $count_all = "found";
405 break;
406 }
407 }
408 if ($count_all != "found") {
409 echo '<br><b>' .
410 _("No Messages found") .
411 '</b><br>';
412 }
413 }
414
415 /* search one folder option */
416 else {
417 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
418 echo '<br>'
419 . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n";
420 sqimap_mailbox_select($imapConnection, $mailbox);
421 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
422 }
423 }
424
425 /* must have search terms to search */
426 if ($submit == _("Search") && empty($what)) {
427 echo '<br>'
428 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
429 }
430
431 $allow_thread_sort = $old_value;
432
433 do_hook('search_bottom');
434 sqimap_logout ($imapConnection);
435 echo '</body></html>';
436
437 ?>