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