Added MUCH better composing
[squirrelmail.git] / functions / mailbox.php
CommitLineData
3302d0d4 1<?
2 /**
a09387f4 3 ** mailbox.php
3302d0d4 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
0e919368 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 **/
3e054c43 31 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
3e054c43 32 $rel_start = $start;
3e054c43 33 if (($start > $end) || ($start < 1)) {
34 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
35 exit;
36 }
3302d0d4 37
e550d551 38 $pos = 0;
3e054c43 39 while ($rel_start <= $end) {
40 if ($end - $rel_start > 50) {
e550d551 41 $rel_end = $rel_start + 49;
3e054c43 42 } else {
43 $rel_end = $end;
44 }
45 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
3302d0d4 46 $read = fgets($imapConnection, 1024);
3e054c43 47
3e054c43 48 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
e550d551 49
3e054c43 50 if (substr($read, 0, 5) == "From:") {
51 $read = ereg_replace("<", "EMAILSTART--", $read);
52 $read = ereg_replace(">", "--EMAILEND", $read);
e550d551 53 $from[$pos] = substr($read, 5, strlen($read) - 6);
3e054c43 54 }
55 else if (substr($read, 0, 5) == "Date:") {
df978e80 56 $read = ereg_replace("<", "&lt;", $read);
57 $read = ereg_replace(">", "&gt;", $read);
e550d551 58 $date[$pos] = substr($read, 5, strlen($read) - 6);
3e054c43 59 }
60 else if (substr($read, 0, 8) == "Subject:") {
df978e80 61 $read = ereg_replace("<", "&lt;", $read);
62 $read = ereg_replace(">", "&gt;", $read);
e550d551 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++;
3e054c43 76 }
e550d551 77
3e054c43 78 $read = fgets($imapConnection, 1024);
79 }
80 $rel_start = $rel_start + 50;
3302d0d4 81 }
82 }
83
61a4ac35 84 function setMessageFlag($imapConnection, $i, $q, $flag) {
85 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
aa4c3749 86 }
87
0e919368 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 **/
3e054c43 94 function getMessageFlags($imapConnection, $j, &$flags) {
3302d0d4 95 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
3e054c43 96 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
aa4c3749 97 $read = fgets($imapConnection, 1024);
3e054c43 98 $count = 0;
3302d0d4 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);;
54a8ae32 106 $s = 0;
107 while ($s < count($flags)) {
108 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
109 $s++;
3302d0d4 110 }
111 } else {
112 $flags[0] = "None";
113 }
3e054c43 114 $count++;
3302d0d4 115 $read = fgets($imapConnection, 1024);
116 }
117 }
118
119 function getEmailAddr($sender) {
120 if (strpos($sender, "EMAILSTART--") == false)
121 return "";
122
123 $start = strpos($sender, "EMAILSTART--");
124 $emailAddr = substr($sender, $start, strlen($sender));
125
126 return $emailAddr;
127 }
128
129 function getSender($sender) {
130 if (strpos($sender, "EMAILSTART--") == false)
131 return "";
132
133 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
134 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
135 return "$first$second";
136 }
137
138 function getSenderName($sender) {
139 $name = getSender($sender);
140 $emailAddr = getEmailAddr($sender);
141 $emailStart = strpos($emailAddr, "EMAILSTART--");
142 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
143
144 if (($emailAddr == "") && ($name == "")) {
145 $from = $sender;
146 }
147 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
148 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
149 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
150 $from = $emailAddr;
151 }
152 else if (strlen($name) > 0) {
153 $from = $name;
154 }
155 else if (strlen($emailAddr > 0)) {
156 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
157 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
158 $from = $emailAddr;
159 }
160
161 $from = trim($from);
162
163 // strip out any quotes if they exist
164 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
165 $from = substr($from, 1, strlen($from) - 2);
166
167 return $from;
168 }
aa4c3749 169
170 /** returns "true" if the copy was completed successfully.
171 ** returns "false" with an error message if unsuccessful.
172 **/
173 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
174 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
175 $read = fgets($imapConnection, 1024);
176 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
177 $read = fgets($imapConnection, 1024);
178 }
179
180 if (substr($read, 0, 15) == "mailboxStore NO") {
181 echo "ERROR... $read<BR>";
182 return false;
183 } else if (substr($read, 0, 15) == "mailboxStore OK") {
184 return true;
185 }
186
187 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
188 return false;
189 }
61a4ac35 190
191 /** expunges a mailbox **/
192 function expungeBox($imapConnection, $mailbox) {
193 selectMailbox($imapConnection, $mailbox, $num);
194 fputs($imapConnection, "1 EXPUNGE\n");
195 }
8c7dfc99 196
197 function getFolderNameMinusINBOX($mailbox) {
198 if (substr($mailbox, 0, 6) == "INBOX.")
199 $box = substr($mailbox, 6, strlen($mailbox));
200 else
201 $box = $mailbox;
202
203 return $box;
204 }
05207a68 205
e550d551 206 /** This function will fetch the body of a given message and format
207 it into our standard format. **/
05207a68 208 function fetchBody($imapConnection, $id) {
209 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
210 $count = 0;
211 $read[$count] = fgets($imapConnection, 1024);
212 while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
213 $count++;
214 $read[$count] = fgets($imapConnection, 1024);
215 }
216
e550d551 217 /** this loop removes the first line, and the last two which
218 are IMAP information that we don't need. **/
219 $i = 0;
220 $j = 0;
221 while ($i < count($read)) {
222 if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
223 $readtmp[$j] = $read[$i];
224 $j++;
225 }
226 $i++;
227 }
228 $read = $readtmp;
229
230 /** This loop formats the text, creating links out of linkable stuff too **/
05207a68 231 $count = 0;
232 $useHTML= false;
233 while ($count < count($read)) {
234 $read[$count] = "^^$read[$count]";
e550d551 235
236 if (strpos(strtolower($read[$count]), "<html>") == true) {
05207a68 237 $useHTML = true;
e550d551 238 } else if (strpos(strtolower($read[$count]), "</html>") == true) {
239 $useHTML = false;
05207a68 240 }
e550d551 241
05207a68 242 $read[$count] = substr($read[$count], 2, strlen($read[$count]));
243
244 if ($useHTML == false) {
e550d551 245 $read[$count] = parsePlainBodyText($read[$count]);
246 } else {
247 $read[$count] = parseHTMLBodyText($read[$count]);
248 }
05207a68 249
e550d551 250 $count++;
251 }
252 return $read;
253 }
05207a68 254
e550d551 255 function parseHTMLBodyText($line) {
256 return $line;
257 }
05207a68 258
e550d551 259 function parsePlainBodyText($line) {
260 $line = "^^$line";
05207a68 261
e550d551 262 if ((strpos(strtolower($line), "<!") == false) &&
263 (strpos(strtolower($line), "<html>") == false) &&
264 (strpos(strtolower($line), "</html>") == false)) {
265 $line = str_replace("<", "&lt;", $line);
266 $line = str_replace(">", "&gt;", $line);
05207a68 267 }
268
8467bf00 269 $wrap_at = 80; // Make this configurable int the config file some time
270 if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
271 $line = wordWrap($line, $wrap_at);
b8ea4ed6 272
e550d551 273 $line = str_replace(" ", "&nbsp;", $line);
274 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
275
276 /** if >> or > are found at the beginning of a line, I'll assume that was
277 replied text, so make it different colors **/
278 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
279 $line = substr($line, 2, strlen($line));
280 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
281 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
282 $line = substr($line, 2, strlen($line));
283 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
284 } else {
285 $line = substr($line, 2, strlen($line));
286 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
287 }
288
289 /** This translates "http://" into a link. It could be made better to accept
290 "www" and "mailto" also. That should probably be added later. **/
291 if (strpos(strtolower($line), "http://") != false) {
292 $start = strpos(strtolower($line), "http://");
b8ea4ed6 293 $text = substr($line, $start, strlen($line));
294 $link = ereg_replace("<BR>", "", $text);
e550d551 295
296 if (strpos($link, "&"))
297 $end = strpos($link, "&");
298 else if (strpos($link, "<"))
299 $end = strpos($link, "<");
300 else
301 $end = strlen($link);
302
303 $link = substr($link, 0, $end);
b8ea4ed6 304 $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$text</A>", $line);
e550d551 305 }
306
307 return $line;
05207a68 308 }
a09387f4 309?>