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