6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
14 require_once('../functions/imap.php');
15 require_once('../functions/date.php');
16 require_once('../functions/array.php');
17 require_once('../functions/mailbox_display.php');
18 require_once('../functions/mime.php');
20 function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color, $search_position = '') {
22 global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
25 $pos = $search_position;
27 $urlMailbox = urlencode($mailbox);
29 /* Construct the Search QuERY */
31 # account for multiple search terms
33 $multi_search = array ();
34 $search_what = ereg_replace("[ ]{2,}", " ", $search_what);
35 $multi_search = split (" ", $search_what);
36 if (count($multi_search)==1) {
37 $search_string = $search_where . " " . "\"" . $multi_search[0] . "\"";
41 $count = count($multi_search);
42 for ($x=0;$x<$count;$x++
) {
43 $search_string = $search_string . " " . $search_where . " " . "\"" . $multi_search[$x] . "\"";
46 $search_string = ereg_replace("^ ", "", $search_string);
48 # now use $search_string in the imap search
50 if (isset($languages[$squirrelmail_language]['CHARSET']) &&
51 $languages[$squirrelmail_language]['CHARSET']) {
52 $ss = "SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_string";
54 $ss .= "SEARCH ALL $search_string\"";
57 /* Read Data Back From IMAP */
58 $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
59 if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
60 // $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"";
61 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
62 $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
65 unset($messagelist); $msgs=""; $c = 0;
67 /* Keep going till we find the SEARCH responce */
68 while ($c < count( $readin )) {
70 /* Check to see if a SEARCH Responce was recived */
71 if (substr($readin[$c],0,9) == "* SEARCH ")
72 $messagelist = explode(" ",substr($readin[$c],9));
73 else if (isset($errors))
74 $errors = $errors.$readin[$c];
76 $errors = $readin[$c];
80 /* If nothing is found * SEARCH should be the first error else echo errors */
81 if (isset($errors) && strstr($errors,"* SEARCH")) {
82 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
84 } else if (isset($errors)) {
85 echo "<!-- ".$errors." -->";
89 HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
90 exsitising code with a search true/false varible.
94 for ($q = 0; $q < count($messagelist); $q++
) {
95 $id[$q] = trim($messagelist[$q]);
97 $issent = ($mailbox == $sent_folder);
98 $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
99 $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
100 foreach ($hdr_list as $hdr) {
101 $from[] = $hdr->from
;
102 $date[] = $hdr->date
;
103 $subject[] = $hdr->subject
;
105 $priority[] = $hdr->priority
;
107 $size[] = $hdr->size
;
108 $type[] = $hdr->type0
;
112 while ($j < count($messagelist)) {
113 $date[$j] = ereg_replace(' ', ' ', $date[$j]);
114 $tmpdate = explode(" ", trim($date[$j]));
116 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
117 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
118 $messages[$j]["ID"] = $id[$j];
119 $messages[$j]["FROM"] = decodeHeader($from[$j]);
120 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
121 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
122 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
123 $messages[$j]["TO"] = decodeHeader($to[$j]);
124 $messages[$j]["PRIORITY"] = $priority[$j];
125 $messages[$j]["CC"] = $cc[$j];
126 $messages[$j]["SIZE"] = $size[$j];
127 $messages[$j]["TYPE0"] = $type[$j];
130 while ($num < count($flags[$j])) {
131 if ($flags[$j][$num] == 'Deleted') {
132 $messages[$j]['FLAG_DELETED'] = true;
133 } else if ($flags[$j][$num] == 'Answered') {
134 $messages[$j]['FLAG_ANSWERED'] = true;
135 } else if ($flags[$j][$num] == 'Seen') {
136 $messages[$j]['FLAG_SEEN'] = true;
137 } else if ($flags[$j][$num] == 'Flagged') {
138 $messages[$j]['FLAG_FLAGGED'] = true;
145 /* Find and remove the ones that are deleted */
148 while ($j < count($messagelist)) {
149 if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
153 $msgs[$i] = $messages[$j];
160 /* There's gotta be messages in the array for it to sort them. */
162 if (count($messagelist) > 0) {
168 mail_message_listing_beginning( $imapConnection,
169 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
172 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
173 get_selectall_link($start_msg, $sort) );
176 while ($j < count($msgs)) {
177 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
180 echo '</table></td></tr></table></form>';