Some fixup:
[squirrelmail.git] / functions / imap_search.php
1 <?php
2 /******************************************************************
3 ** IMAP SEARCH ROUTIES
4 ** $Id$
5 *****************************************************************/
6
7 require_once('../functions/imap.php');
8 require_once('../functions/date.php');
9 require_once('../functions/array.php');
10 require_once('../functions/mailbox_display.php');
11 require_once('../functions/mime.php');
12
13
14 function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
15 global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
16
17 $urlMailbox = urlencode($mailbox);
18 $isid = sqimap_session_id();
19
20 /* Construct the Search QuERY */
21 $ss = $isid;
22 if (isset($languages[$squirrelmail_language]['CHARSET']) &&
23 $languages[$squirrelmail_language]['CHARSET']) {
24 $ss .= " SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_where \"$search_what\"\r\n";
25 } else {
26 $ss .= " SEARCH ALL $search_where \"$search_what\"\r\n";
27 }
28 fputs($imapConnection,$ss);
29
30 # Read Data Back From IMAP
31 $readin = sqimap_read_data ($imapConnection, $isid, false, $result, $message);
32 if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
33 $ss = $isid . " SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
34 fputs ($imapConnection, $ss);
35 $readin = sqimap_read_data ($imapConnection, $isid, true, $result, $message);
36 }
37
38 unset($messagelist); $msgs=""; $c = 0;
39
40 /* Keep going till we find the SEARCH responce */
41 while ($c < count( $readin )) {
42
43 /* Check to see if a SEARCH Responce was recived */
44 if (substr($readin[$c],0,9) == "* SEARCH ")
45 $messagelist = explode(" ",substr($readin[$c],9));
46 else if (isset($errors))
47 $errors = $errors.$readin[$c];
48 else
49 $errors = $readin[$c];
50 $c++;
51 }
52
53 #If nothing is found * SEARCH should be the first error else echo errors
54 if (isset($errors) && strstr($errors,"* SEARCH")) {
55 echo "<br><CENTER>No Messages Found</CENTER>";
56 return;
57 } else if (isset($errors)) {
58 echo "<!-- ".$errors." -->";
59 }
60
61 # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
62 # exsitising code with a search true/false varible.
63
64
65 global $sent_folder;
66 for ($q = 0; $q < count($messagelist); $q++) {
67 $id[$q] = trim($messagelist[$q]);
68 }
69 $issent = ($mailbox == $sent_folder);
70 $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
71 $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
72 foreach ($hdr_list as $hdr) {
73 $from[] = $hdr->from;
74 $date[] = $hdr->date;
75 $subject[] = $hdr->subject;
76 $to[] = $hdr->to;
77 $priority[] = $hdr->priority;
78 $cc[] = $hdr->cc;
79 $size[] = $hdr->size;
80 $type[] = $hdr->type0;
81 }
82
83 $j = 0;
84 while ($j < count($messagelist)) {
85 $date[$j] = ereg_replace(' ', ' ', $date[$j]);
86 $tmpdate = explode(" ", trim($date[$j]));
87
88 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
89 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
90 $messages[$j]["ID"] = $id[$j];
91 $messages[$j]["FROM"] = decodeHeader($from[$j]);
92 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
93 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
94 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
95 $messages[$j]["TO"] = decodeHeader($to[$j]);
96 $messages[$j]["PRIORITY"] = $priority[$j];
97 $messages[$j]["CC"] = $cc[$j];
98 $messages[$j]["SIZE"] = $size[$j];
99 $messages[$j]["TYPE0"] = $type[$j];
100
101 $num = 0;
102 while ($num < count($flags[$j])) {
103 if ($flags[$j][$num] == "Deleted") {
104 $messages[$j]["FLAG_DELETED"] = true;
105 }
106 else if ($flags[$j][$num] == "Answered") {
107 $messages[$j]["FLAG_ANSWERED"] = true;
108 }
109 else if ($flags[$j][$num] == "Seen") {
110 $messages[$j]["FLAG_SEEN"] = true;
111 }
112 else if ($flags[$j][$num] == "Flagged") {
113 $messages[$j]["FLAG_FLAGGED"] = true;
114 }
115 $num++;
116 }
117 $j++;
118 }
119
120 /** Find and remove the ones that are deleted */
121 $i = 0;
122 $j = 0;
123 while ($j < count($messagelist)) {
124 if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
125 $j++;
126 continue;
127 }
128 $msgs[$i] = $messages[$j];
129
130 $i++;
131 $j++;
132 }
133 $numMessages = $i;
134
135 // There's gotta be messages in the array for it to sort them.
136
137 if (count($messagelist) > 0) {
138 $j=0;
139 if (!isset ($msg)) { $msg = ""; }
140 mail_message_listing_beginning($imapConnection,
141 "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
142 $mailbox, -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
143 '&nbsp;');
144
145
146 while ($j < count($msgs)) {
147 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
148 $j++;
149 }
150 echo '</table></tr></td></table>';
151 }
152 }
153
154 ?>