44ea499de34d6f33af0f8cc70fba90cb7691e57c
[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 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 }
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 }
190
191 /** expunges a mailbox **/
192 function expungeBox($imapConnection, $mailbox) {
193 selectMailbox($imapConnection, $mailbox, $num);
194 fputs($imapConnection, "1 EXPUNGE\n");
195 }
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 }
205
206 /** This function will fetch the body of a given message and format
207 it into our standard format. **/
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
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 **/
231 $count = 0;
232 $useHTML= false;
233 while ($count < count($read)) {
234 $read[$count] = "^^$read[$count]";
235
236 if (strpos(strtolower($read[$count]), "<html>") == true) {
237 $useHTML = true;
238 } else if (strpos(strtolower($read[$count]), "</html>") == true) {
239 $useHTML = false;
240 }
241
242 $read[$count] = substr($read[$count], 2, strlen($read[$count]));
243
244 if ($useHTML == false) {
245 $read[$count] = parsePlainBodyText($read[$count]);
246 } else {
247 $read[$count] = parseHTMLBodyText($read[$count]);
248 }
249
250 $count++;
251 }
252 return $read;
253 }
254
255 function parseHTMLBodyText($line) {
256 return $line;
257 }
258
259 function parsePlainBodyText($line) {
260 $line = "^^$line";
261
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);
267 }
268
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);
272 $line = str_replace(" ", "&nbsp;", $line);
273 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
274
275 /** if >> or > are found at the beginning of a line, I'll assume that was
276 replied text, so make it different colors **/
277 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
278 $line = substr($line, 2, strlen($line));
279 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
280 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
281 $line = substr($line, 2, strlen($line));
282 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
283 } else {
284 $line = substr($line, 2, strlen($line));
285 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
286 }
287
288 /** This translates "http://" into a link. It could be made better to accept
289 "www" and "mailto" also. That should probably be added later. **/
290 if (strpos(strtolower($line), "http://") != false) {
291 $start = strpos(strtolower($line), "http://");
292 $link = substr($line, $start, strlen($line));
293
294 if (strpos($link, "&"))
295 $end = strpos($link, "&");
296 else if (strpos($link, "<"))
297 $end = strpos($link, "<");
298 else
299 $end = strlen($link);
300
301 $link = substr($link, 0, $end);
302
303 $line = str_replace($link, "<A HREF=\"$link\" TARGET=_top>$link</A>", $line);
304 }
305
306 return $line;
307 }
308 ?>