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