| 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/array.php'); |
| 16 | require_once('../functions/strings.php'); |
| 17 | |
| 18 | |
| 19 | // here are some functions, could go in imap_search.php |
| 20 | // this was here, pretty handy |
| 21 | |
| 22 | function s_opt( $val, $sel, $tit ) { |
| 23 | echo " <option value=\"$val\""; |
| 24 | if ( $sel == $val ) { |
| 25 | echo ' selected'; |
| 26 | } |
| 27 | echo ">$tit</option>\n"; |
| 28 | } |
| 29 | |
| 30 | // function to get the recent searches and put them in arrays |
| 31 | |
| 32 | function get_recent($pref_name, $username, $data_dir) { |
| 33 | $array = array (); |
| 34 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 35 | $n = 0; |
| 36 | for ($x=1;$x<=$recent_count;$x++) { |
| 37 | $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", ""); |
| 38 | $n++; |
| 39 | } |
| 40 | return $array; |
| 41 | } |
| 42 | |
| 43 | // function to get the saved searches and put them in arrays |
| 44 | |
| 45 | function get_saved($pref_name, $username, $data_dir) { |
| 46 | $array = array (); |
| 47 | $n = 0; |
| 48 | for ($x=1;;$x++) { |
| 49 | $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", ""); |
| 50 | if ($array[$n] == "") { |
| 51 | array_pop($array); |
| 52 | return $array; |
| 53 | } |
| 54 | $n++; |
| 55 | } |
| 56 | return $array; |
| 57 | } |
| 58 | |
| 59 | // function to update pref file with recent searches |
| 60 | |
| 61 | function update_recent($array, $recent_value, $pref_name, $username, $data_dir) { |
| 62 | $array = get_recent($pref_name, $username, $data_dir); |
| 63 | array_push ($array, $recent_value); |
| 64 | array_shift ($array); |
| 65 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 66 | $n=0; |
| 67 | for ($i=1;$i<=$recent_count;$i++) { |
| 68 | setPref($data_dir, $username, "$pref_name" . "$i", $array[$n]); |
| 69 | $n++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // function to "forget" a recent search |
| 74 | |
| 75 | function forget_recent($forget_index, $username, $data_dir) { |
| 76 | $what_array = get_recent("search_what", $username, $data_dir); |
| 77 | $where_array = get_recent("search_where", $username, $data_dir); |
| 78 | $folder_array = get_recent("search_folder", $username, $data_dir); |
| 79 | array_splice($what_array, $forget_index, 1); |
| 80 | array_splice($where_array, $forget_index, 1); |
| 81 | array_splice($folder_array, $forget_index, 1); |
| 82 | array_unshift($what_array, ""); |
| 83 | array_unshift($where_array, ""); |
| 84 | array_unshift($folder_array, ""); |
| 85 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 86 | $n=0; |
| 87 | for ($i=1;$i<=$recent_count;$i++) { |
| 88 | setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]); |
| 89 | setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]); |
| 90 | setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]); |
| 91 | $n++; |
| 92 | } |
| 93 | |
| 94 | // function to delete a saved search |
| 95 | } |
| 96 | function delete_saved($delete_index, $username, $data_dir) { |
| 97 | $saved_what_array = get_saved("saved_what", $username, $data_dir); |
| 98 | $saved_where_array = get_saved("saved_where", $username, $data_dir); |
| 99 | $saved_folder_array = get_saved("saved_folder", $username, $data_dir); |
| 100 | array_splice($saved_what_array, $delete_index, 1); |
| 101 | array_splice($saved_where_array, $delete_index, 1); |
| 102 | array_splice($saved_folder_array, $delete_index, 1); |
| 103 | $n=0; |
| 104 | $saved_count = count($saved_what_array); |
| 105 | $last_element = $saved_count + 1; |
| 106 | if ($last_element < 1) { |
| 107 | for ($i=1;$i<=$saved_count;$i++) { |
| 108 | setPref($data_dir, $username, "saved_what" . "$i", $saved_what_array[$n]); |
| 109 | setPref($data_dir, $username, "saved_where" . "$i", $saved_where_array[$n]); |
| 110 | setPref($data_dir, $username, "saved_folder" . "$i", $saved_folder_array[$n]); |
| 111 | $n++; |
| 112 | } |
| 113 | } |
| 114 | removePref($data_dir, $username, "saved_what" . "$last_element"); |
| 115 | removePref($data_dir, $username, "saved_where" . "$last_element"); |
| 116 | removePref($data_dir, $username, "saved_folder" . "$last_element"); |
| 117 | } |
| 118 | |
| 119 | // function to save a search from recent to saved |
| 120 | |
| 121 | function save_recent($save_index, $username, $data_dir) { |
| 122 | $what_array = get_recent("search_what", $username, $data_dir); |
| 123 | $where_array = get_recent("search_where", $username, $data_dir); |
| 124 | $folder_array = get_recent("search_folder", $username, $data_dir); |
| 125 | $saved_what_once = array_slice($what_array, $save_index, 1); |
| 126 | $saved_where_once = array_slice($where_array, $save_index, 1); |
| 127 | $saved_folder_once = array_slice($folder_array, $save_index, 1); |
| 128 | $saved_array = get_saved("saved_what", $username, $data_dir); |
| 129 | $saved_count = (count($saved_array) + 1); |
| 130 | setPref($data_dir, $username, "saved_what" . "$saved_count", $saved_what_once[0]); |
| 131 | setPref($data_dir, $username, "saved_where" . "$saved_count", $saved_where_once[0]); |
| 132 | setPref($data_dir, $username, "saved_folder" . "$saved_count", $saved_folder_once[0]); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | /* ------------------------ main ------------------------ */ |
| 138 | |
| 139 | // reset these arrays on each page load just in case |
| 140 | |
| 141 | $what_array = array (); |
| 142 | $where_array = array (); |
| 143 | $folder_array = array (); |
| 144 | $saved_what_array = array (); |
| 145 | $saved_where_array = array (); |
| 146 | $saved_folder_array = array (); |
| 147 | $search_all = "none"; |
| 148 | |
| 149 | // get mailbox names |
| 150 | |
| 151 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
| 152 | $boxes = sqimap_mailbox_list($imapConnection); |
| 153 | |
| 154 | |
| 155 | // set current mailbox to INBOX if none was selected or if page |
| 156 | // was called to search all folders. |
| 157 | |
| 158 | if ($mailbox == 'None' || $mailbox == "" ) { |
| 159 | $mailbox = $boxes[0]['unformatted']; |
| 160 | } |
| 161 | if ($mailbox == "All Folders") { |
| 162 | $search_all = "all"; |
| 163 | } |
| 164 | |
| 165 | // page headers |
| 166 | |
| 167 | displayPageHeader($color, $mailbox); |
| 168 | |
| 169 | // if the page is called from a search link or button update recent values |
| 170 | // in pref files here |
| 171 | |
| 172 | if ($submit == "Search" && !empty($what)) { |
| 173 | update_recent($what_array, $what, "search_what", $username, $data_dir); |
| 174 | update_recent($where_array, $where, "search_where", $username, $data_dir); |
| 175 | update_recent($folder_array, $mailbox, "search_folder", $username, $data_dir); |
| 176 | } |
| 177 | // if the page is called from a "forget recent" link remove search from pref file |
| 178 | elseif ($submit == "forget") { |
| 179 | forget_recent($count, $username, $data_dir); |
| 180 | } |
| 181 | // if the page is called from a "save recent" link add search to saved searches |
| 182 | elseif ($submit == "save") { |
| 183 | save_recent($count, $username, $data_dir); |
| 184 | } |
| 185 | elseif ($submit == "delete") { |
| 186 | delete_saved($count, $username, $data_dir); |
| 187 | } |
| 188 | // if the page is called from a "delete saved" link delete saved search |
| 189 | do_hook('search_before_form'); |
| 190 | |
| 191 | echo "<BR>\n". |
| 192 | " <table width=\"100%\">\n". |
| 193 | " <TR><td bgcolor=\"$color[0]\">\n". |
| 194 | " <CENTER><B>"._("Search")."</B></CENTER>\n". |
| 195 | " </TD></TR>\n". |
| 196 | " </TABLE>\n"; |
| 197 | # ' <TR><td align=center>'; |
| 198 | |
| 199 | // update the recent and saved searches from the pref files |
| 200 | |
| 201 | $what_array = get_recent("search_what", $username, $data_dir); |
| 202 | $where_array = get_recent("search_where", $username, $data_dir); |
| 203 | $folder_array = get_recent("search_folder", $username, $data_dir); |
| 204 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
| 205 | $saved_what_array = get_saved("saved_what", $username, $data_dir); |
| 206 | $saved_where_array = get_saved("saved_where", $username, $data_dir); |
| 207 | $saved_folder_array = get_saved("saved_folder", $username, $data_dir); |
| 208 | $saved_count = count($saved_what_array); |
| 209 | |
| 210 | /* Saved Search Table */ |
| 211 | if ($saved_count > 0) { |
| 212 | echo "<BR>\n" |
| 213 | . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>" |
| 214 | . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>' |
| 215 | . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>'; |
| 216 | for ($i=0; $i < $saved_count; ++$i) { |
| 217 | if ($i % 2) { |
| 218 | echo "<TR BGCOLOR=\"$color[0]\">"; |
| 219 | } else { |
| 220 | echo "<TR BGCOLOR=\"$color[4]\">"; |
| 221 | } |
| 222 | echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>" |
| 223 | . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>" |
| 224 | . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>" |
| 225 | . '<TD ALIGN=RIGHT>' |
| 226 | . '<A HREF=search.php' |
| 227 | . '?mailbox=' . urlencode($saved_folder_array[$i]) |
| 228 | . '&what=' . urlencode($saved_what_array[$i]) |
| 229 | . '&where=' . urlencode($saved_where_array[$i]) |
| 230 | . '>' . _("edit") . '</A>' |
| 231 | . ' | ' |
| 232 | . '<A HREF=search.php' |
| 233 | . '?mailbox=' . urlencode($saved_folder_array[$i]) |
| 234 | . '&what=' . urlencode($saved_what_array[$i]) |
| 235 | . '&where=' . urlencode($saved_where_array[$i]) |
| 236 | . '&submit=Search_no_update' |
| 237 | . '>' . _("search") . '</A>' |
| 238 | . ' | ' |
| 239 | . "<A HREF=search.php?count=$i&submit=delete>" |
| 240 | . _("delete") |
| 241 | . '</A>' |
| 242 | . '</TD></TR>'; |
| 243 | } |
| 244 | echo "</TABLE></TD></TR></TABLE>\n"; |
| 245 | } |
| 246 | |
| 247 | /* Recent Search Table */ |
| 248 | if ($recent_count > 0) { |
| 249 | echo "<BR>\n" |
| 250 | . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n" |
| 251 | . '<TR><TD ALIGN=CENTER><B>Recent Searches</B></TD></TR><TR><TD>' |
| 252 | . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>'; |
| 253 | for ($i=0; $i < $recent_count; ++$i) { |
| 254 | if (!empty($what_array[$i])) { |
| 255 | if ($folder_array[$i] == "") { |
| 256 | $folder_array[$i] = "INBOX"; |
| 257 | } |
| 258 | if ($i % 2) { |
| 259 | echo "<TR BGCOLOR=\"$color[0]\">"; |
| 260 | } else { |
| 261 | echo "<TR BGCOLOR=\"$color[4]\">"; |
| 262 | } |
| 263 | echo "<TD WIDTH=35%>$folder_array[$i]</TD>" |
| 264 | . "<TD ALIGN=LEFT>$what_array[$i]</TD>" |
| 265 | . "<TD ALIGN=CENTER>$where_array[$i]</TD>" |
| 266 | . '<TD ALIGN=RIGHT>' |
| 267 | . "<A HREF=search.php?count=$i&submit=save>" |
| 268 | . _("save") |
| 269 | . '</A>' |
| 270 | . ' | ' |
| 271 | . '<A HREF=search.php' |
| 272 | . '?mailbox=' . urlencode($folder_array[$i]) |
| 273 | . '&what=' . urlencode($what_array[$i]) |
| 274 | . '&where=' . urlencode($where_array[$i]) |
| 275 | . '&submit=Search_no_update' |
| 276 | . '>' . _("search") . '</A>' |
| 277 | . ' | ' |
| 278 | . "<A HREF=search.php?count=$i&submit=forget>" |
| 279 | . _("forget") |
| 280 | . '</A>' |
| 281 | . '</TD></TR>'; |
| 282 | } |
| 283 | } |
| 284 | echo '</TABLE></TD></TR></TABLE><BR>'; |
| 285 | } |
| 286 | |
| 287 | /* Search Form */ |
| 288 | echo '<B>Current Search</B>' |
| 289 | . '<FORM ACTION="search.php" NAME=s>' |
| 290 | . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>' |
| 291 | . ' <TR>' |
| 292 | . ' <TD><SELECT NAME="mailbox">'; |
| 293 | for ($i = 0; $i < count($boxes); $i++) { |
| 294 | if (!in_array('noselect', $boxes[$i]['flags'])) { |
| 295 | $box = $boxes[$i]['unformatted']; |
| 296 | $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']); |
| 297 | if ($mailbox == $box) { |
| 298 | echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n"; |
| 299 | } |
| 300 | else { |
| 301 | echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n"; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | echo "<OPTION VALUE=\"All Folders\""; |
| 306 | if ($mailbox == "All Folders") { |
| 307 | echo "SELECTED"; |
| 308 | } |
| 309 | echo ">All folders</OPTION>\n"; |
| 310 | echo ' </SELECT>'. |
| 311 | " </TD>\n". |
| 312 | " <TD ALIGN=\"CENTER\">\n"; |
| 313 | $what_disp = str_replace(',', ' ', $what); |
| 314 | $what_disp = str_replace('\\\\', '\\', $what_disp); |
| 315 | $what_disp = str_replace('\\"', '"', $what_disp); |
| 316 | $what_disp = str_replace('"', '"', $what_disp); |
| 317 | echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n". |
| 318 | " </TD>\n". |
| 319 | "<TD ALIGN=\"RIGHT\">\n". |
| 320 | "<SELECT NAME=\"where\">"; |
| 321 | s_opt( 'BODY', $where, _("Body") ); |
| 322 | s_opt( 'TEXT', $where, _("Everywhere") ); |
| 323 | s_opt( 'SUBJECT', $where, _("Subject") ); |
| 324 | s_opt( 'FROM', $where, _("From") ); |
| 325 | s_opt( 'CC', $where, _("Cc") ); |
| 326 | s_opt( 'TO', $where, _("To") ); |
| 327 | echo " </SELECT>\n" . |
| 328 | " </TD>\n". |
| 329 | " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n". |
| 330 | " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n". |
| 331 | " </TD>\n". |
| 332 | " </TR>\n". |
| 333 | "</FORM>\n". |
| 334 | " </TABLE>\n". |
| 335 | "</TD></TR></TABLE>\n"; |
| 336 | |
| 337 | |
| 338 | do_hook("search_after_form"); |
| 339 | |
| 340 | // search all folders option still in the works. returns a table for each |
| 341 | // folder it finds a match in. The toggle all link does not work |
| 342 | |
| 343 | |
| 344 | if ($search_all == "all") { |
| 345 | $mailbox == ""; |
| 346 | $boxcount = count($boxes); |
| 347 | echo "<BR><CENTER><B>Search Results</B><CENTER><BR>\n"; |
| 348 | for ($x=0;$x<$boxcount;$x++) { |
| 349 | if (!in_array('noselect', $boxes[$x]['flags'])) { |
| 350 | $mailbox = $boxes[$x]['unformatted']; |
| 351 | } |
| 352 | echo "<BR><CENTER><B>Folder: $mailbox</CENTER></B>"; |
| 353 | if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) { |
| 354 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 355 | sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // search one folder option |
| 361 | |
| 362 | else { |
| 363 | if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) { |
| 364 | echo "<BR><CENTER><B>Search Results</B></CENTER>\n"; |
| 365 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 366 | sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // must have search terms to search |
| 371 | |
| 372 | if ($submit == "Search" && empty($what)) { |
| 373 | echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n"; |
| 374 | } |
| 375 | |
| 376 | do_hook("search_bottom"); |
| 377 | |
| 378 | // all done |
| 379 | |
| 380 | sqimap_logout ($imapConnection); |
| 381 | echo '</body></html>'; |
| 382 | |
| 383 | ?> |