9092744d48cad7db4c440c5f72d202217c533c7d
[squirrelmail.git] / functions / mailbox.php
1 <?
2 /**
3 ** mailbox.php
4 **
5 ** This contains functions that request information about a mailbox. Including
6 ** reading and parsing headers, getting folder information, etc.
7 **
8 **/
9
10 function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
11 // select mailbox
12 fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
13 $read = fgets($imapConnection, 1024);
14 while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
15 if (substr(Chop($read), -6) == "EXISTS") {
16 $array = explode(" ", $read);
17 $numberOfMessages = $array[1];
18 }
19 $read = fgets($imapConnection, 1024);
20 }
21 }
22
23 /** This function sends a request to the IMAP server for headers, 50 at a time
24 ** until $end is reached. I originally had it do them all at one time, but found
25 ** it slightly faster to do it this way.
26 **
27 ** Originally we had getMessageHeaders get the headers for one message at a time.
28 ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800
29 ** messages) to about 3.5 seconds.
30 **/
31 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
32 $rel_start = $start;
33 if (($start > $end) || ($start < 1)) {
34 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
35 exit;
36 }
37
38 $pos = 0;
39 while ($rel_start <= $end) {
40 if ($end - $rel_start > 50) {
41 $rel_end = $rel_start + 49;
42 } else {
43 $rel_end = $end;
44 }
45 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
46 $read = fgets($imapConnection, 1024);
47
48 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
49
50 if (substr($read, 0, 5) == "From:") {
51 $read = ereg_replace("<", "EMAILSTART--", $read);
52 $read = ereg_replace(">", "--EMAILEND", $read);
53 $from[$pos] = substr($read, 5, strlen($read) - 6);
54 }
55 else if (substr($read, 0, 5) == "Date:") {
56 $read = ereg_replace("<", "&lt;", $read);
57 $read = ereg_replace(">", "&gt;", $read);
58 $date[$pos] = substr($read, 5, strlen($read) - 6);
59 }
60 else if (substr($read, 0, 8) == "Subject:") {
61 $read = ereg_replace("<", "&lt;", $read);
62 $read = ereg_replace(">", "&gt;", $read);
63 $subject[$pos] = substr($read, 8, strlen($read) - 9);
64 if (strlen(Chop($subject[$pos])) == 0)
65 $subject[$pos] = "(no subject)";
66 }
67 else if (substr($read, 0, 1) == ")") {
68 if ($subject[$pos] == "")
69 $subject[$pos] = "(no subject)";
70 else if ($from[$pos] == "")
71 $from[$pos] = "(unknown sender)";
72 else if ($date[$pos] == "")
73 $from[$pos] = gettimeofday();
74
75 $pos++;
76 }
77
78 $read = fgets($imapConnection, 1024);
79 }
80 $rel_start = $rel_start + 50;
81 }
82 }
83
84 function setMessageFlag($imapConnection, $i, $q, $flag) {
85 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
86 }
87
88 /** This function gets the flags for message $j. It does only one message at a
89 ** time, rather than doing groups of messages (like getMessageHeaders does).
90 ** I found it one or two seconds quicker (on a box of 800 messages) to do it
91 ** individually. I'm not sure why it happens like that, but that's what my
92 ** testing found. Perhaps later I will be proven wrong and this will change.
93 **/
94 function getMessageFlags($imapConnection, $j, &$flags) {
95 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
96 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
97 $read = fgets($imapConnection, 1024);
98 $count = 0;
99 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
100 if (strpos($read, "FLAGS")) {
101 $read = ereg_replace("\(", "", $read);
102 $read = ereg_replace("\)", "", $read);
103 $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
104 $read = trim($read);
105 $flags = explode(" ", $read);;
106 $s = 0;
107 while ($s < count($flags)) {
108 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
109 $s++;
110 }
111 } else {
112 $flags[0] = "None";
113 }
114 $count++;
115 $read = fgets($imapConnection, 1024);
116 }
117 }
118
119 function decodeEmailAddr($sender) {
120 $emailAddr = getEmailAddr($sender);
121 $emailStart = strpos($emailAddr, "EMAILSTART--");
122 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
123
124 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
125 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
126 return $emailAddr;
127 }
128
129 function getEmailAddr($sender) {
130 if (strpos($sender, "EMAILSTART--") == false)
131 return "";
132
133 $start = strpos($sender, "EMAILSTART--");
134 $emailAddr = substr($sender, $start, strlen($sender));
135
136 return $emailAddr;
137 }
138
139 function getSender($sender) {
140 if (strpos($sender, "EMAILSTART--") == false)
141 return "";
142
143 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
144 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
145 return "$first$second";
146 }
147
148 function getSenderName($sender) {
149 $name = getSender($sender);
150 $emailAddr = getEmailAddr($sender);
151 $emailStart = strpos($emailAddr, "EMAILSTART--");
152 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
153
154 if (($emailAddr == "") && ($name == "")) {
155 $from = $sender;
156 }
157 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
158 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
159 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
160 $from = $emailAddr;
161 }
162 else if (strlen($name) > 0) {
163 $from = $name;
164 }
165 else if (strlen($emailAddr > 0)) {
166 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
167 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
168 $from = $emailAddr;
169 }
170
171 $from = trim($from);
172
173 // strip out any quotes if they exist
174 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
175 $from = substr($from, 1, strlen($from) - 2);
176
177 return $from;
178 }
179
180 /** returns "true" if the copy was completed successfully.
181 ** returns "false" with an error message if unsuccessful.
182 **/
183 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
184 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
185 $read = fgets($imapConnection, 1024);
186 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
187 $read = fgets($imapConnection, 1024);
188 }
189
190 if (substr($read, 0, 15) == "mailboxStore NO") {
191 echo "ERROR... $read<BR>";
192 return false;
193 } else if (substr($read, 0, 15) == "mailboxStore OK") {
194 return true;
195 }
196
197 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
198 return false;
199 }
200
201 /** expunges a mailbox **/
202 function expungeBox($imapConnection, $mailbox) {
203 selectMailbox($imapConnection, $mailbox, $num);
204 fputs($imapConnection, "1 EXPUNGE\n");
205 }
206
207 function getFolderNameMinusINBOX($mailbox) {
208 if (substr($mailbox, 0, 6) == "INBOX.")
209 $box = substr($mailbox, 6, strlen($mailbox));
210 else
211 $box = $mailbox;
212
213 return $box;
214 }
215
216 /** This function will fetch the body of a given message and format
217 it into our standard format. **/
218 function fetchBody($imapConnection, $id) {
219 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
220 $count = 0;
221 $read[$count] = fgets($imapConnection, 1024);
222 while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
223 $count++;
224 $read[$count] = fgets($imapConnection, 1024);
225 }
226
227 /** this loop removes the first line, and the last two which
228 are IMAP information that we don't need. **/
229 $i = 0;
230 $j = 0;
231 while ($i < count($read)) {
232 if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
233 $readtmp[$j] = $read[$i];
234 $j++;
235 }
236 $i++;
237 }
238 $read = $readtmp;
239
240 /** This loop formats the text, creating links out of linkable stuff too **/
241 $count = 0;
242 $useHTML= false;
243 while ($count < count($read)) {
244 $read[$count] = "^^$read[$count]";
245
246 if (strpos(strtolower($read[$count]), "<html>") == true) {
247 $useHTML = true;
248 } else if (strpos(strtolower($read[$count]), "</html>") == true) {
249 $useHTML = false;
250 }
251
252 $read[$count] = substr($read[$count], 2, strlen($read[$count]));
253
254 if ($useHTML == false) {
255 $read[$count] = parsePlainBodyText($read[$count]);
256 } else {
257 $read[$count] = parseHTMLBodyText($read[$count]);
258 }
259
260 $count++;
261 }
262 return $read;
263 }
264
265 function parseHTMLBodyText($line) {
266 return $line;
267 }
268
269 function parsePlainBodyText($line) {
270 $line = "^^$line";
271
272 if ((strpos(strtolower($line), "<!") == false) &&
273 (strpos(strtolower($line), "<html>") == false) &&
274 (strpos(strtolower($line), "</html>") == false)) {
275 $line = str_replace("<", "&lt;", $line);
276 $line = str_replace(">", "&gt;", $line);
277 }
278
279 $wrap_at = 80; // Make this configurable int the config file some time
280 if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
281 $line = wordWrap($line, $wrap_at);
282
283 $line = str_replace(" ", "&nbsp;", $line);
284 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
285
286 /** if >> or > are found at the beginning of a line, I'll assume that was
287 replied text, so make it different colors **/
288 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
289 $line = substr($line, 2, strlen($line));
290 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
291 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
292 $line = substr($line, 2, strlen($line));
293 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
294 } else {
295 $line = substr($line, 2, strlen($line));
296 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
297 }
298
299 /** This translates "http://" into a link. It could be made better to accept
300 "www" and "mailto" also. That should probably be added later. **/
301 if (strpos(strtolower($line), "http://") != false) {
302 $start = strpos(strtolower($line), "http://");
303 $text = substr($line, $start, strlen($line));
304 $link = ereg_replace("<BR>", "", $text);
305
306 if (strpos($link, "&"))
307 $end = strpos($link, "&");
308 else if (strpos($link, "<"))
309 $end = strpos($link, "<");
310 else
311 $end = strlen($link);
312
313 $link = substr($link, 0, $end);
314 $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$text</A>", $line);
315 }
316
317 return $line;
318 }
319 ?>