6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
12 require_once('../src/validate.php');
13 require_once('../functions/imap.php');
14 require_once('../functions/imap_search.php');
15 require_once('../functions/imap_utf7_decode_local.php');
16 require_once('../functions/array.php');
17 require_once('../functions/strings.php');
18 require_once('../functions/html.php');
20 global $allow_thread_sort;
22 /* here are some functions, could go in imap_search.php
24 this was here, pretty handy */
25 function s_opt( $val, $sel, $tit ) {
26 echo " <option value=\"$val\"";
30 echo ">$tit</option>\n";
33 /* function to get the recent searches and put them in the attributes array */
34 function get_recent($username, $data_dir) {
35 $attributes = array();
36 $types = array('search_what', 'search_where', 'search_folder');
37 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
38 for ($x=1;$x<=$recent_count;$x++
) {
40 foreach ($types as $key) {
41 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
47 /* function to get the saved searches and put them in the saved_attributes array */
48 function get_saved($username, $data_dir) {
49 $saved_attributes = array();
50 $types = array('saved_what', 'saved_where', 'saved_folder');
51 foreach ($types as $key) {
53 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
54 if ($saved_attributes[$key][$x] == "") {
55 array_pop($saved_attributes[$key]);
60 return $saved_attributes;
63 /* function to update recent pref arrays */
64 function update_recent($what, $where, $mailbox, $username, $data_dir) {
65 $attributes = array();
66 $types = array('search_what', 'search_where', 'search_folder');
67 $input = array($what, $where, $mailbox);
68 $attributes = get_recent( $username, $data_dir);
71 for ($i=1;$i<=count($attributes['search_what']);$i++
) {
72 if (isset($attributes['search_what'][$i])) {
73 if ($what == $attributes['search_what'][$i] &&
74 $where == $attributes['search_where'][$i] &&
75 $mailbox == $attributes['search_folder'][$i]) {
82 foreach ($types as $key) {
83 array_push ($attributes[$key], $input[$i]);
84 array_shift ($attributes[$key]);
87 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
89 for ($i=1;$i<=$recent_count;$i++
) {
91 foreach ($types as $key) {
92 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
99 /* function to forget a recent search */
100 function forget_recent($forget_index, $username, $data_dir) {
101 $attributes = array();
102 $types = array('search_what', 'search_where', 'search_folder');
103 $attributes = get_recent( $username, $data_dir);
105 foreach ($types as $key) {
106 array_splice($attributes[$key], $forget_index, 1);
107 array_unshift($attributes[$key], '');
110 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
112 for ($i=1;$i<=$recent_count;$i++
) {
114 foreach ($types as $key) {
115 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
121 /* function to delete a saved search */
122 function delete_saved($delete_index, $username, $data_dir) {
123 $types = array('saved_what', 'saved_where', 'saved_folder');
124 $attributes = get_saved($username, $data_dir);
125 foreach ($types as $key) {
126 array_splice($attributes[$key], $delete_index, 1);
130 $saved_count = count($attributes['saved_what']);
131 $last_element = $saved_count +
1;
132 for ($i=1;$i<=$saved_count;$i++
) {
134 foreach ($types as $key) {
135 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
140 foreach($types as $key) {
141 removePref($data_dir, $username, $key.$last_element);
145 /* function to save a search from recent to saved */
146 function save_recent($save_index, $username, $data_dir) {
147 $attributes = array();
148 $types = array('search_what', 'search_where', 'search_folder');
149 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
150 $saved_array = get_saved($username, $data_dir);
151 $save_index = $save_index -1;
152 $saved_count = (count($saved_array['saved_what']) +
1);
153 $attributes = get_recent ($username, $data_dir);
155 foreach ($types as $key) {
156 $slice = array_slice($attributes[$key], $save_index, 1);
157 $name = $saved_types[$n];
158 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
163 /* ------------------------ main ------------------------ */
165 /* reset these arrays on each page load just in case */
166 $attributes = array ();
167 $saved_attributes = array ();
168 $search_all = 'none';
169 $perbox_count = array ();
170 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
172 /* get mailbox names */
173 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
174 $boxes = sqimap_mailbox_list($imapConnection);
176 /* set current mailbox to INBOX if none was selected or if page
177 was called to search all folders. */
178 if ( !isset($mailbox) ||
$mailbox == 'None' ||
$mailbox == '' ) {
179 $mailbox = $boxes[0]['unformatted'];
181 if ($mailbox == 'All Folders') {
185 if (isset($composenew) && $composenew) {
186 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
187 "&session=$composesession&attachedmessages=true&";
188 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
190 displayPageHeader($color, $mailbox);
192 /* See how the page was called and fire off correct function */
193 if ((!isset($submit) ||
empty($submit)) && !empty($what)) {
194 $submit = _("Search");
196 if ( !isset( $submit ) ) {
198 } else if ($submit == _("Search") && !empty($what)) {
199 if ($recent_count > 0) {
200 update_recent($what, $where, $mailbox, $username, $data_dir);
203 elseif ($submit == 'forget') {
204 forget_recent($count, $username, $data_dir);
206 elseif ($submit == 'save') {
207 save_recent($count, $username, $data_dir);
209 elseif ($submit == 'delete') {
210 delete_saved($count, $username, $data_dir);
213 do_hook('search_before_form');
217 html_tag( 'tr', "\n" .
218 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
220 '', '', 'width="100%"') . "\n";
222 /* update the recent and saved searches from the pref files */
223 $attributes = get_recent($username, $data_dir);
224 $saved_attributes = get_saved($username, $data_dir);
225 $saved_count = count($saved_attributes['saved_what']);
228 /* Saved Search Table */
229 if ($saved_count > 0) {
231 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
233 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
237 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
238 for ($i=0; $i < $saved_count; ++
$i) {
240 echo html_tag( 'tr', '', '', $color[0] );
242 echo html_tag( 'tr', '', '', $color[4] );
244 echo html_tag( 'td', $saved_attributes['saved_folder'][$i], 'left', '', 'width="35%"' )
245 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
246 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
247 . html_tag( 'td', '', 'right' )
248 . '<a href=search.php'
249 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
250 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
251 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
252 . '>' . _("edit") . '</a>'
254 . '<a href=search.php'
255 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
256 . '&what=' . urlencode($saved_attributes['saved_what'][$i])
257 . '&where=' . urlencode($saved_attributes['saved_where'][$i])
258 . '&submit=Search_no_update'
259 . '>' . _("search") . '</a>'
261 . "<a href=search.php?count=$i&submit=delete>"
266 echo "</table></td></tr></table>\n";
269 /* Recent Search Table */
270 if ($recent_count > 0) {
272 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
274 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
278 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
279 for ($i=1; $i <= $recent_count; ++
$i) {
280 if (isset($attributes['search_folder'][$i])) {
281 if ($attributes['search_folder'][$i] == "") {
282 $attributes['search_folder'][$i] = "INBOX";
286 echo html_tag( 'tr', '', '', $color[0] );
288 echo html_tag( 'tr', '', '', $color[0] );
290 if (isset($attributes['search_what'][$i]) &&
291 !empty($attributes['search_what'][$i])) {
292 echo html_tag( 'td', $attributes['search_folder'][$i], 'left', '', 'width="35%"' )
293 . html_tag( 'td', $attributes['search_what'][$i], 'left' )
294 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
295 . html_tag( 'td', '', 'right' )
296 . "<a href=search.php?count=$i&submit=save>"
300 . '<a href=search.php'
301 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
302 . '&what=' . urlencode($attributes['search_what'][$i])
303 . '&where=' . urlencode($attributes['search_where'][$i])
304 . '&submit=Search_no_update'
305 . '>' . _("search") . '</a>'
307 . "<a href=search.php?count=$i&submit=forget>"
313 echo '</table></td></tr></table><br>';
317 echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
318 . '<form action="search.php" name="s">'
319 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
321 . html_tag( 'td', '', 'left' )
322 . '<select name="mailbox">';
323 for ($i = 0; $i < count($boxes); $i++
) {
324 if (!in_array('noselect', $boxes[$i]['flags'])) {
325 $box = $boxes[$i]['unformatted'];
326 $box2 = str_replace(' ', ' ',
327 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
328 if( $box2 == 'INBOX' ) {
331 echo ' <option value="' . $box . '"';
332 if ($mailbox == $box) { echo ' selected'; }
333 echo '>' . $box2 . '</option>' . "\n";
336 echo '<option value="All Folders"';
337 if ($mailbox == 'All Folders') {
340 echo ">All folders</option>\n";
343 if ( !isset( $what ) ) {
346 if ( !isset( $where ) ) {
351 $what_disp = str_replace(',', ' ', $what);
352 $what_disp = str_replace('\\\\', '\\', $what_disp);
353 $what_disp = str_replace('\\"', '"', $what_disp);
354 $what_disp = str_replace('"', '"', $what_disp);
355 echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' )
356 . html_tag( 'td', '', 'right' )
357 . "<select name=\"where\">";
358 s_opt( 'BODY', $where, _("Body") );
359 s_opt( 'TEXT', $where, _("Everywhere") );
360 s_opt( 'SUBJECT', $where, _("Subject") );
361 s_opt( 'FROM', $where, _("From") );
362 s_opt( 'CC', $where, _("Cc") );
363 s_opt( 'TO', $where, _("To") );
364 echo " </select>\n" .
366 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
370 "</td></tr></table>\n";
373 do_hook('search_after_form');
376 search all folders option still in the works. returns a table for each
377 folder it finds a match in.
381 if ($allow_thread_sort == TRUE) {
382 $old_value = $allow_thread_sort;
383 $allow_thread_sort = FALSE;
386 if ($search_all == 'all') {
388 $boxcount = count($boxes);
389 echo '<br><center><b>' .
390 _("Search Results") .
391 "</b><center><br>\n";
392 for ($x=0;$x<$boxcount;$x++
) {
393 if (!in_array('noselect', $boxes[$x]['flags'])) {
394 $mailbox = $boxes[$x]['unformatted'];
396 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
397 sqimap_mailbox_select($imapConnection, $mailbox);
398 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
399 array_push($perbox_count, $count_all);
402 for ($i=0;$i<count($perbox_count);$i++
) {
403 if ($perbox_count[$i] != "") {
404 $count_all = "found";
408 if ($count_all != "found") {
410 _("No Messages found") .
415 /* search one folder option */
417 if (($submit == _("Search") ||
$submit == 'Search_no_update') && !empty($what)) {
419 . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n";
420 sqimap_mailbox_select($imapConnection, $mailbox);
421 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
425 /* must have search terms to search */
426 if ($submit == _("Search") && empty($what)) {
428 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
431 $allow_thread_sort = $old_value;
433 do_hook('search_bottom');
434 sqimap_logout ($imapConnection);
435 echo '</body></html>';