| 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 | /* Path for SquirrelMail required files. */ |
| 13 | define('SM_PATH','../'); |
| 14 | |
| 15 | /* SquirrelMail required files. */ |
| 16 | require_once(SM_PATH . 'include/validate.php'); |
| 17 | require_once(SM_PATH . 'functions/imap.php'); |
| 18 | require_once(SM_PATH . 'functions/imap_search.php'); |
| 19 | require_once(SM_PATH . 'functions/imap_mailbox.php'); |
| 20 | require_once(SM_PATH . 'functions/array.php'); |
| 21 | require_once(SM_PATH . 'functions/strings.php'); |
| 22 | |
| 23 | global $allow_thread_sort; |
| 24 | |
| 25 | /* here are some functions, could go in imap_search.php |
| 26 | |
| 27 | this was here, pretty handy */ |
| 28 | function s_opt( $val, $sel, $tit ) { |
| 29 | echo " <option value=\"$val\""; |
| 30 | if ( $sel == $val ) { |
| 31 | echo ' selected'; |
| 32 | } |
| 33 | echo ">$tit</option>\n"; |
| 34 | } |
| 35 | |
| 36 | /* function to get the recent searches and put them in the attributes array */ |
| 37 | function get_recent($username, $data_dir) { |
| 38 | $attributes = array(); |
| 39 | $types = array('search_what', 'search_where', 'search_folder'); |
| 40 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 41 | for ($x=1;$x<=$recent_count;$x++) { |
| 42 | reset($types); |
| 43 | foreach ($types as $key) { |
| 44 | $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, ""); |
| 45 | } |
| 46 | } |
| 47 | return $attributes; |
| 48 | } |
| 49 | |
| 50 | /* function to get the saved searches and put them in the saved_attributes array */ |
| 51 | function 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 | } |
| 62 | } |
| 63 | return $saved_attributes; |
| 64 | } |
| 65 | |
| 66 | /* function to update recent pref arrays */ |
| 67 | function update_recent($what, $where, $mailbox, $username, $data_dir) { |
| 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); |
| 73 | $dupe = 'no'; |
| 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 | } |
| 81 | } |
| 82 | } |
| 83 | if ($dupe == 'no') { |
| 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 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* function to forget a recent search */ |
| 103 | function forget_recent($forget_index, $username, $data_dir) { |
| 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) { |
| 109 | array_splice($attributes[$key], $forget_index - 1, 1); |
| 110 | array_unshift($attributes[$key], ''); |
| 111 | } |
| 112 | reset($types); |
| 113 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 114 | $n=0; |
| 115 | for ($i=1;$i<=$recent_count;$i++) { |
| 116 | reset($types); |
| 117 | foreach ($types as $key) { |
| 118 | setPref($data_dir, $username, $key.$i, $attributes[$key][$n]); |
| 119 | } |
| 120 | $n++; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* function to delete a saved search */ |
| 125 | function delete_saved($delete_index, $username, $data_dir) { |
| 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); |
| 132 | $n=0; |
| 133 | $saved_count = count($attributes['saved_what']); |
| 134 | $last_element = $saved_count + 1; |
| 135 | for ($i=1;$i<=$saved_count;$i++) { |
| 136 | reset($types); |
| 137 | foreach ($types as $key) { |
| 138 | setPref($data_dir, $username, $key.$i, $attributes[$key][$n]); |
| 139 | } |
| 140 | $n++; |
| 141 | } |
| 142 | reset($types); |
| 143 | foreach($types as $key) { |
| 144 | removePref($data_dir, $username, $key.$last_element); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* function to save a search from recent to saved */ |
| 149 | function save_recent($save_index, $username, $data_dir) { |
| 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 | } |
| 164 | } |
| 165 | |
| 166 | function printSearchMessages($msgs,$mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false) { |
| 167 | global $sort, $color; |
| 168 | |
| 169 | $msort = calc_msort($msgs, $sort); |
| 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 | |
| 178 | |
| 179 | $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt); |
| 180 | $toggle_all = get_selectall_link(1, $sort); |
| 181 | |
| 182 | echo '<table bgcolor="' . $color[0] . '" border="0" width="100%" cellpadding="1" cellspacing="0"><tr><td>'; |
| 183 | mail_message_listing_beginning($imapConnection, $mailbox, $sort, |
| 184 | $msg_cnt_str, $toggle_all, 1); |
| 185 | |
| 186 | |
| 187 | printHeader($mailbox, 6, $color, false); |
| 188 | |
| 189 | displayMessageArray($imapConnection, $cnt, 1, |
| 190 | $msort, $mailbox, $sort, $color, $cnt, $where, $what); |
| 191 | |
| 192 | mail_message_listing_end($cnt, '', $msg_cnt_str, $color); |
| 193 | echo '</td></tr></table>'; |
| 194 | |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* ------------------------ main ------------------------ */ |
| 199 | |
| 200 | /* reset these arrays on each page load just in case */ |
| 201 | $attributes = array (); |
| 202 | $saved_attributes = array (); |
| 203 | $search_all = 'none'; |
| 204 | $perbox_count = array (); |
| 205 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 206 | |
| 207 | |
| 208 | /* get mailbox names */ |
| 209 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
| 210 | $boxes = sqimap_mailbox_list($imapConnection); |
| 211 | |
| 212 | /* set current mailbox to INBOX if none was selected or if page |
| 213 | was called to search all folders. */ |
| 214 | if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) { |
| 215 | $mailbox = $boxes[0]['unformatted']; |
| 216 | } |
| 217 | if ($mailbox == 'All Folders') { |
| 218 | $search_all = 'all'; |
| 219 | } |
| 220 | |
| 221 | if (isset($composenew) && $composenew) { |
| 222 | $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox). |
| 223 | "&session=$composesession&attachedmessages=true&"; |
| 224 | displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false); |
| 225 | } else { |
| 226 | displayPageHeader($color, $mailbox); |
| 227 | } |
| 228 | /* See how the page was called and fire off correct function */ |
| 229 | if ((!isset($submit) || empty($submit)) && !empty($what)) { |
| 230 | $submit = _("Search"); |
| 231 | } |
| 232 | if ( !isset( $submit ) ) { |
| 233 | $submit = ''; |
| 234 | } else if ($submit == _("Search") && !empty($what)) { |
| 235 | if ($recent_count > 0) { |
| 236 | update_recent($what, $where, $mailbox, $username, $data_dir); |
| 237 | } |
| 238 | } |
| 239 | elseif ($submit == 'forget') { |
| 240 | forget_recent($count, $username, $data_dir); |
| 241 | } |
| 242 | elseif ($submit == 'save') { |
| 243 | save_recent($count, $username, $data_dir); |
| 244 | } |
| 245 | elseif ($submit == 'delete') { |
| 246 | delete_saved($count, $username, $data_dir); |
| 247 | } |
| 248 | |
| 249 | do_hook('search_before_form'); |
| 250 | |
| 251 | echo html_tag( 'table', |
| 252 | html_tag( 'tr', "\n" . |
| 253 | html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] ) |
| 254 | ) , |
| 255 | '', '', 'width="100%"') . "\n"; |
| 256 | |
| 257 | /* update the recent and saved searches from the pref files */ |
| 258 | $attributes = get_recent($username, $data_dir); |
| 259 | $saved_attributes = get_saved($username, $data_dir); |
| 260 | $saved_count = count($saved_attributes['saved_what']); |
| 261 | $count_all = 0; |
| 262 | |
| 263 | /* Saved Search Table */ |
| 264 | if ($saved_count > 0) { |
| 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"' ); |
| 273 | for ($i=0; $i < $saved_count; ++$i) { |
| 274 | if ($i % 2) { |
| 275 | echo html_tag( 'tr', '', '', $color[0] ); |
| 276 | } else { |
| 277 | echo html_tag( 'tr', '', '', $color[4] ); |
| 278 | } |
| 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' |
| 284 | . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i]) |
| 285 | . '&what=' . urlencode($saved_attributes['saved_what'][$i]) |
| 286 | . '&where=' . urlencode($saved_attributes['saved_where'][$i]) |
| 287 | . '>' . _("edit") . '</a>' |
| 288 | . ' | ' |
| 289 | . '<a href=search.php' |
| 290 | . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i]) |
| 291 | . '&what=' . urlencode($saved_attributes['saved_what'][$i]) |
| 292 | . '&where=' . urlencode($saved_attributes['saved_where'][$i]) |
| 293 | . '&submit=Search_no_update' |
| 294 | . '>' . _("search") . '</a>' |
| 295 | . ' | ' |
| 296 | . "<a href=search.php?count=$i&submit=delete>" |
| 297 | . _("delete") |
| 298 | . '</a>' |
| 299 | . '</td></tr>'; |
| 300 | } |
| 301 | echo "</table></td></tr></table>\n"; |
| 302 | } |
| 303 | |
| 304 | if ($recent_count > 0) { |
| 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"' ); |
| 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 | } |
| 318 | } |
| 319 | if ($i % 2) { |
| 320 | echo html_tag( 'tr', '', '', $color[0] ); |
| 321 | } else { |
| 322 | echo html_tag( 'tr', '', '', $color[0] ); |
| 323 | } |
| 324 | if (isset($attributes['search_what'][$i]) && |
| 325 | !empty($attributes['search_what'][$i])) { |
| 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&submit=save>" |
| 331 | . _("save") |
| 332 | . '</a>' |
| 333 | . ' | ' |
| 334 | . '<a href=search.php' |
| 335 | . '?mailbox=' . urlencode($attributes['search_folder'][$i]) |
| 336 | . '&what=' . urlencode($attributes['search_what'][$i]) |
| 337 | . '&where=' . urlencode($attributes['search_where'][$i]) |
| 338 | . '&submit=Search_no_update' |
| 339 | . '>' . _("search") . '</a>' |
| 340 | . ' | ' |
| 341 | . "<a href=search.php?count=$i&submit=forget>" |
| 342 | . _("forget") |
| 343 | . '</a>' |
| 344 | . '</td></tr>'; |
| 345 | } |
| 346 | } |
| 347 | echo '</table></td></tr></table><br>'; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | if (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 | *********************************************************************/ |
| 362 | if (! isset($use_mailbox_cache)) { |
| 363 | $use_mailbox_cache = 0; |
| 364 | } |
| 365 | |
| 366 | /* There is a problem with registered vars in 4.1 */ |
| 367 | /* |
| 368 | if( substr( phpversion(), 0, 3 ) == '4.1' ) { |
| 369 | $use_mailbox_cache = FALSE; |
| 370 | } |
| 371 | */ |
| 372 | |
| 373 | /* Search Form */ |
| 374 | echo 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">'; |
| 380 | for ($i = 0; $i < count($boxes); $i++) { |
| 381 | if (!in_array('noselect', $boxes[$i]['flags'])) { |
| 382 | $box = $boxes[$i]['unformatted']; |
| 383 | $box2 = str_replace(' ', ' ', |
| 384 | imap_utf7_decode_local($boxes[$i]['unformatted-disp'])); |
| 385 | if( $box2 == 'INBOX' ) { |
| 386 | $box2 = _("INBOX"); |
| 387 | } |
| 388 | echo ' <option value="' . $box . '"'; |
| 389 | if ($mailbox == $box) { echo ' selected'; } |
| 390 | echo '>' . $box2 . '</option>' . "\n"; |
| 391 | } |
| 392 | } |
| 393 | echo '<option value="All Folders"'; |
| 394 | if ($mailbox == 'All Folders') { |
| 395 | echo ' selected'; |
| 396 | } |
| 397 | echo ">All folders</option>\n"; |
| 398 | echo ' </select>'. |
| 399 | " </td>\n"; |
| 400 | if ( !isset( $what ) ) { |
| 401 | $what = ''; |
| 402 | } |
| 403 | if ( !isset( $where ) ) { |
| 404 | $where = ''; |
| 405 | } |
| 406 | |
| 407 | |
| 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('"', '"', $what_disp); |
| 412 | echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' ) |
| 413 | . html_tag( 'td', '', 'right' ) |
| 414 | . "<select name=\"where\">"; |
| 415 | s_opt( 'BODY', $where, _("Body") ); |
| 416 | s_opt( 'TEXT', $where, _("Everywhere") ); |
| 417 | s_opt( 'SUBJECT', $where, _("Subject") ); |
| 418 | s_opt( 'FROM', $where, _("From") ); |
| 419 | s_opt( 'CC', $where, _("Cc") ); |
| 420 | s_opt( 'TO', $where, _("To") ); |
| 421 | echo " </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"; |
| 428 | |
| 429 | |
| 430 | do_hook('search_after_form'); |
| 431 | |
| 432 | /* |
| 433 | search all folders option still in the works. returns a table for each |
| 434 | folder it finds a match in. |
| 435 | */ |
| 436 | |
| 437 | $old_value = 0; |
| 438 | if ($allow_thread_sort == TRUE) { |
| 439 | $old_value = $allow_thread_sort; |
| 440 | $allow_thread_sort = FALSE; |
| 441 | } |
| 442 | |
| 443 | if ($search_all == 'all') { |
| 444 | $mailbox == ''; |
| 445 | $boxcount = count($boxes); |
| 446 | echo '<BR><CENTER><B>' . |
| 447 | _("Search Results") . |
| 448 | "</B><CENTER><BR>\n"; |
| 449 | for ($x=0;$x<$boxcount;$x++) { |
| 450 | if (!in_array('noselect', $boxes[$x]['flags'])) { |
| 451 | $mailbox = $boxes[$x]['unformatted']; |
| 452 | } |
| 453 | if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) { |
| 454 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 455 | $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all); |
| 456 | $count_all = count($msgs); |
| 457 | printSearchMessages($msgs, $mailbox, $count_all, $imapConnection, |
| 458 | $where, $what, false, false); |
| 459 | array_push($perbox_count, $count_all); |
| 460 | } |
| 461 | } |
| 462 | for ($i=0;$i<count($perbox_count);$i++) { |
| 463 | if ($perbox_count[$i]) { |
| 464 | $count_all = true; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | if (!$count_all) { |
| 469 | echo '<br><center>' . _("No Messages Found") . '</center>'; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* search one folder option */ |
| 474 | else { |
| 475 | if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) { |
| 476 | echo '<br>' |
| 477 | . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n"; |
| 478 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 479 | $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all); |
| 480 | if (count($msgs)) { |
| 481 | printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection, |
| 482 | $where, $what, false, false); |
| 483 | } else { |
| 484 | echo '<br><center>' . _("No Messages Found") . '</center>'; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* must have search terms to search */ |
| 490 | if ($submit == _("Search") && empty($what)) { |
| 491 | echo '<br>' |
| 492 | . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n"; |
| 493 | } |
| 494 | |
| 495 | $allow_thread_sort = $old_value; |
| 496 | |
| 497 | |
| 498 | do_hook('search_bottom'); |
| 499 | sqimap_logout ($imapConnection); |
| 500 | echo '</body></html>'; |
| 501 | |
| 502 | ?> |