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