4960ec8e |
1 | <?php |
69298c28 |
2 | /** |
3 | * Deliver.class.php |
4 | * |
6c84ba1e |
5 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
69298c28 |
6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | * |
8 | * This contains all the functions needed to send messages through |
9 | * a delivery backend. |
10 | * |
883d9cd3 |
11 | * @version $Id$ |
72b004e6 |
12 | * @author Marc Groot Koerkamp |
13 | * @package squirrelmail |
69298c28 |
14 | */ |
15 | |
72b004e6 |
16 | /** |
17 | * Deliver Class - called to actually deliver the message |
18 | * |
19 | * This class is called by compose.php and other code that needs |
20 | * to send messages. All delivery functionality should be centralized |
21 | * in this class. |
22 | * |
23 | * Do not place UI code in this class, as UI code should be placed in templates |
24 | * going forward. |
25 | * |
26 | * @author Marc Groot Koerkamp |
27 | * @package squirrelmail |
28 | */ |
4960ec8e |
29 | class Deliver { |
30 | |
72b004e6 |
31 | /** |
32 | * function mail - send the message parts to the SMTP stream |
33 | * |
34 | * @param Message $message Message class to send |
35 | * @param resource $stream file handle to the SMTP stream |
36 | * |
3a70ee56 |
37 | * @return integer $raw_length |
72b004e6 |
38 | */ |
59387d1c |
39 | function mail($message, $stream=false) { |
78f39e78 |
40 | $rfc822_header = $message->rfc822_header; |
41 | if (count($message->entities)) { |
42 | $boundary = $this->mimeBoundary(); |
43 | $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"'; |
44 | } else { |
45 | $boundary=''; |
46 | } |
47 | $raw_length = 0; |
72b004e6 |
48 | $reply_rfc822_header = (isset($message->reply_rfc822_header) |
78f39e78 |
49 | ? $message->reply_rfc822_header : ''); |
50 | $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length); |
75b14a70 |
51 | |
78f39e78 |
52 | if ($stream) { |
59387d1c |
53 | $this->preWriteToStream($header); |
54 | $this->writeToStream($stream, $header); |
78f39e78 |
55 | } |
56 | $this->writeBody($message, $stream, $raw_length, $boundary); |
57 | return $raw_length; |
3b53d87f |
58 | } |
78f39e78 |
59 | |
72b004e6 |
60 | /** |
61 | * function writeBody - generate and write the mime boundaries around each part to the stream |
62 | * |
63 | * Recursively formats and writes the MIME boundaries of the $message |
64 | * to the output stream. |
65 | * |
66 | * @param Message $message Message object to transform |
67 | * @param resource $stream SMTP output stream |
68 | * @param integer &$length_raw raw length of the message (part) |
69 | * as returned by mail fn |
70 | * @param string $boundary custom boundary to call, usually for subparts |
71 | * |
3a70ee56 |
72 | * @return void |
72b004e6 |
73 | */ |
3b53d87f |
74 | function writeBody($message, $stream, &$length_raw, $boundary='') { |
caf6a9a4 |
75 | // calculate boundary in case of multidimensional mime structures |
76 | if ($boundary && $message->entity_id && count($message->entities)) { |
77 | if (strpos($boundary,'_part_')) { |
78 | $boundary = substr($boundary,0,strpos($boundary,'_part_')); |
ba86230e |
79 | |
593d083d |
80 | // the next four lines use strrev to reverse any nested boundaries |
81 | // because RFC 2046 (5.1.1) says that if a line starts with the outer |
82 | // boundary string (doesn't matter what the line ends with), that |
83 | // can be considered a match for the outer boundary; thus the nested |
84 | // boundary needs to be unique from the outer one |
ba86230e |
85 | // |
86 | } else if (strpos($boundary,'_trap_')) { |
87 | $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_')); |
caf6a9a4 |
88 | } |
ba86230e |
89 | $boundary_new = strrev($boundary . '_part_'.$message->entity_id); |
caf6a9a4 |
90 | } else { |
91 | $boundary_new = $boundary; |
92 | } |
1274f430 |
93 | if ($boundary && !$message->rfc822_header) { |
78f39e78 |
94 | $s = '--'.$boundary."\r\n"; |
caf6a9a4 |
95 | $s .= $this->prepareMIME_Header($message, $boundary_new); |
78f39e78 |
96 | $length_raw += strlen($s); |
97 | if ($stream) { |
3b53d87f |
98 | $this->preWriteToStream($s); |
78f39e78 |
99 | $this->writeToStream($stream, $s); |
100 | } |
101 | } |
102 | $this->writeBodyPart($message, $stream, $length_raw); |
72b004e6 |
103 | |
78f39e78 |
104 | $last = false; |
105 | for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) { |
caf6a9a4 |
106 | $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary_new); |
78f39e78 |
107 | if ($i == $entCount-1) $last = true; |
108 | } |
1274f430 |
109 | if ($boundary && $last) { |
dcc5c913 |
110 | $s = "--".$boundary_new."--\r\n\r\n"; |
78f39e78 |
111 | $length_raw += strlen($s); |
112 | if ($stream) { |
3b53d87f |
113 | $this->preWriteToStream($s); |
78f39e78 |
114 | $this->writeToStream($stream, $s); |
115 | } |
116 | } |
3b53d87f |
117 | } |
118 | |
72b004e6 |
119 | /** |
120 | * function writeBodyPart - write each individual mimepart |
121 | * |
122 | * Recursively called by WriteBody to write each mime part to the SMTP stream |
123 | * |
124 | * @param Message $message Message object to transform |
125 | * @param resource $stream SMTP output stream |
126 | * @param integer &$length length of the message part |
127 | * as returned by mail fn |
128 | * |
3a70ee56 |
129 | * @return void |
72b004e6 |
130 | */ |
3b53d87f |
131 | function writeBodyPart($message, $stream, &$length) { |
1274f430 |
132 | if ($message->mime_header) { |
78f39e78 |
133 | $type0 = $message->mime_header->type0; |
134 | } else { |
135 | $type0 = $message->rfc822_header->content_type->type0; |
136 | } |
137 | |
138 | $body_part_trailing = $last = ''; |
139 | switch ($type0) |
140 | { |
141 | case 'text': |
142 | case 'message': |
143 | if ($message->body_part) { |
144 | $body_part = $message->body_part; |
4cf85ddd |
145 | // remove NUL characters |
146 | $body_part = str_replace("\0",'',$body_part); |
78f39e78 |
147 | $length += $this->clean_crlf($body_part); |
148 | if ($stream) { |
72b004e6 |
149 | $this->preWriteToStream($body_part); |
78f39e78 |
150 | $this->writeToStream($stream, $body_part); |
151 | } |
152 | $last = $body_part; |
153 | } elseif ($message->att_local_name) { |
154 | $filename = $message->att_local_name; |
155 | $file = fopen ($filename, 'rb'); |
156 | while ($body_part = fgets($file, 4096)) { |
257dea3f |
157 | // remove NUL characters |
158 | $body_part = str_replace("\0",'',$body_part); |
78f39e78 |
159 | $length += $this->clean_crlf($body_part); |
160 | if ($stream) { |
161 | $this->preWriteToStream($body_part); |
162 | $this->writeToStream($stream, $body_part); |
163 | } |
164 | $last = $body_part; |
165 | } |
166 | fclose($file); |
167 | } |
168 | break; |
169 | default: |
170 | if ($message->body_part) { |
171 | $body_part = $message->body_part; |
257dea3f |
172 | // remove NUL characters |
173 | $body_part = str_replace("\0",'',$body_part); |
78f39e78 |
174 | $length += $this->clean_crlf($body_part); |
175 | if ($stream) { |
176 | $this->writeToStream($stream, $body_part); |
177 | } |
178 | } elseif ($message->att_local_name) { |
179 | $filename = $message->att_local_name; |
180 | $file = fopen ($filename, 'rb'); |
78f39e78 |
181 | while ($tmp = fread($file, 570)) { |
182 | $body_part = chunk_split(base64_encode($tmp)); |
183 | $length += $this->clean_crlf($body_part); |
184 | if ($stream) { |
185 | $this->writeToStream($stream, $body_part); |
186 | } |
187 | } |
188 | fclose($file); |
3b53d87f |
189 | } |
78f39e78 |
190 | break; |
191 | } |
192 | $body_part_trailing = ''; |
193 | if ($last && substr($last,-1) != "\n") { |
194 | $body_part_trailing = "\r\n"; |
195 | } |
196 | if ($body_part_trailing) { |
197 | $length += strlen($body_part_trailing); |
198 | if ($stream) { |
199 | $this->preWriteToStream($body_part_trailing); |
200 | $this->writeToStream($stream, $body_part_trailing); |
201 | } |
202 | } |
3b53d87f |
203 | } |
78f39e78 |
204 | |
72b004e6 |
205 | /** |
206 | * function clean_crlf - change linefeeds and newlines to legal characters |
207 | * |
208 | * The SMTP format only allows CRLF as line terminators. |
209 | * This function replaces illegal teminators with the correct terminator. |
210 | * |
211 | * @param string &$s string to clean linefeeds on |
212 | * |
3a70ee56 |
213 | * @return void |
72b004e6 |
214 | */ |
69298c28 |
215 | function clean_crlf(&$s) { |
3b53d87f |
216 | $s = str_replace("\r\n", "\n", $s); |
217 | $s = str_replace("\r", "\n", $s); |
218 | $s = str_replace("\n", "\r\n", $s); |
78f39e78 |
219 | return strlen($s); |
3b53d87f |
220 | } |
78f39e78 |
221 | |
72b004e6 |
222 | /** |
223 | * function strip_crlf - strip linefeeds and newlines from a string |
224 | * |
225 | * The SMTP format only allows CRLF as line terminators. |
226 | * This function strips all line terminators from the string. |
227 | * |
228 | * @param string &$s string to clean linefeeds on |
229 | * |
3a70ee56 |
230 | * @return void |
72b004e6 |
231 | */ |
e4f9307a |
232 | function strip_crlf(&$s) { |
233 | $s = str_replace("\r\n ", '', $s); |
78f39e78 |
234 | $s = str_replace("\r", '', $s); |
235 | $s = str_replace("\n", '', $s); |
e4f9307a |
236 | } |
3b53d87f |
237 | |
72b004e6 |
238 | /** |
239 | * function preWriteToStream - reserved for extended functionality |
240 | * |
241 | * This function is not yet implemented. |
242 | * Reserved for extended functionality. |
243 | * |
244 | * @param string &$s string to operate on |
245 | * |
3a70ee56 |
246 | * @return void |
72b004e6 |
247 | */ |
69298c28 |
248 | function preWriteToStream(&$s) { |
3b53d87f |
249 | } |
78f39e78 |
250 | |
72b004e6 |
251 | /** |
252 | * function writeToStream - write data to the SMTP stream |
253 | * |
254 | * @param resource $stream SMTP output stream |
255 | * @param string $data string with data to send to the SMTP stream |
256 | * |
3a70ee56 |
257 | * @return void |
72b004e6 |
258 | */ |
3b53d87f |
259 | function writeToStream($stream, $data) { |
78f39e78 |
260 | fputs($stream, $data); |
3b53d87f |
261 | } |
78f39e78 |
262 | |
72b004e6 |
263 | /** |
264 | * function initStream - reserved for extended functionality |
265 | * |
266 | * This function is not yet implemented. |
267 | * Reserved for extended functionality. |
268 | * |
269 | * @param Message $message Message object |
270 | * @param string $host host name or IP to connect to |
271 | * @param string $user username to log into the SMTP server with |
272 | * @param string $pass password to log into the SMTP server with |
273 | * @param integer $length |
274 | * |
3a70ee56 |
275 | * @return handle $stream file handle resource to SMTP stream |
72b004e6 |
276 | */ |
3b53d87f |
277 | function initStream($message, $length=0, $host='', $port='', $user='', $pass='') { |
78f39e78 |
278 | return $stream; |
3b53d87f |
279 | } |
72b004e6 |
280 | |
281 | /** |
282 | * function getBCC - reserved for extended functionality |
283 | * |
284 | * This function is not yet implemented. |
285 | * Reserved for extended functionality. |
286 | * |
287 | */ |
288 | function getBCC() { |
78f39e78 |
289 | return false; |
59387d1c |
290 | } |
3b53d87f |
291 | |
72b004e6 |
292 | /** |
293 | * function prepareMIME_Header - creates the mime header |
294 | * |
295 | * @param Message $message Message object to act on |
296 | * @param string $boundary mime boundary from fn MimeBoundary |
297 | * |
3a70ee56 |
298 | * @return string $header properly formatted mime header |
72b004e6 |
299 | */ |
3b53d87f |
300 | function prepareMIME_Header($message, $boundary) { |
78f39e78 |
301 | $mime_header = $message->mime_header; |
302 | $rn="\r\n"; |
303 | $header = array(); |
1274f430 |
304 | |
78f39e78 |
305 | $contenttype = 'Content-Type: '. $mime_header->type0 .'/'. |
306 | $mime_header->type1; |
307 | if (count($message->entities)) { |
fcce1b3c |
308 | $contenttype .= ';' . 'boundary="'.$boundary.'"'; |
78f39e78 |
309 | } |
310 | if (isset($mime_header->parameters['name'])) { |
311 | $contenttype .= '; name="'. |
312 | encodeHeader($mime_header->parameters['name']). '"'; |
313 | } |
314 | if (isset($mime_header->parameters['charset'])) { |
315 | $charset = $mime_header->parameters['charset']; |
316 | $contenttype .= '; charset="'. |
317 | encodeHeader($charset). '"'; |
318 | } |
1274f430 |
319 | |
78f39e78 |
320 | $header[] = $contenttype . $rn; |
321 | if ($mime_header->description) { |
eb72e151 |
322 | $header[] = 'Content-Description: ' . $mime_header->description . $rn; |
78f39e78 |
323 | } |
324 | if ($mime_header->encoding) { |
eb72e151 |
325 | $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn; |
78f39e78 |
326 | } else { |
327 | if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') { |
eb72e151 |
328 | $header[] = 'Content-Transfer-Encoding: 8bit' . $rn; |
78f39e78 |
329 | } else { |
eb72e151 |
330 | $header[] = 'Content-Transfer-Encoding: base64' . $rn; |
78f39e78 |
331 | } |
332 | } |
333 | if ($mime_header->id) { |
eb72e151 |
334 | $header[] = 'Content-ID: ' . $mime_header->id . $rn; |
78f39e78 |
335 | } |
336 | if ($mime_header->disposition) { |
337 | $disposition = $mime_header->disposition; |
338 | $contentdisp = 'Content-Disposition: ' . $disposition->name; |
339 | if ($disposition->getProperty('filename')) { |
340 | $contentdisp .= '; filename="'. |
341 | encodeHeader($disposition->getProperty('filename')). '"'; |
342 | } |
72b004e6 |
343 | $header[] = $contentdisp . $rn; |
78f39e78 |
344 | } |
345 | if ($mime_header->md5) { |
eb72e151 |
346 | $header[] = 'Content-MD5: ' . $mime_header->md5 . $rn; |
78f39e78 |
347 | } |
348 | if ($mime_header->language) { |
eb72e151 |
349 | $header[] = 'Content-Language: ' . $mime_header->language . $rn; |
78f39e78 |
350 | } |
4960ec8e |
351 | |
78f39e78 |
352 | $cnt = count($header); |
353 | $hdr_s = ''; |
354 | for ($i = 0 ; $i < $cnt ; $i++) { |
355 | $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4)); |
356 | } |
357 | $header = $hdr_s; |
358 | $header .= $rn; /* One blank line to separate mimeheader and body-entity */ |
359 | return $header; |
360 | } |
4960ec8e |
361 | |
72b004e6 |
362 | /** |
363 | * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s) |
364 | * |
365 | * This function takes the Rfc822Header object(s) and formats them |
366 | * into the RFC822Header string to send to the SMTP server as part |
367 | * of the SMTP message. |
368 | * |
369 | * @param Rfc822Header $rfc822_header |
370 | * @param Rfc822Header $reply_rfc822_header |
371 | * @param integer &$raw_length length of the message |
372 | * |
3a70ee56 |
373 | * @return string $header |
72b004e6 |
374 | */ |
75b14a70 |
375 | function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) { |
35aaf666 |
376 | global $domain, $version, $username, $skip_SM_header; |
60a46e65 |
377 | |
78f39e78 |
378 | /* if server var SERVER_NAME not available, use $domain */ |
60a46e65 |
379 | if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) { |
380 | $SERVER_NAME = $domain; |
78f39e78 |
381 | } |
60a46e65 |
382 | |
78f39e78 |
383 | sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER); |
384 | sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER); |
385 | sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER); |
386 | sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER); |
387 | sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER); |
60a46e65 |
388 | |
78f39e78 |
389 | $rn = "\r\n"; |
223872cb |
390 | |
78f39e78 |
391 | /* This creates an RFC 822 date */ |
392 | $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone(); |
393 | /* Create a message-id */ |
394 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
485e1181 |
395 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
78f39e78 |
396 | /* Make an RFC822 Received: line */ |
397 | if (isset($REMOTE_HOST)) { |
398 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
399 | } else { |
400 | $received_from = $REMOTE_ADDR; |
401 | } |
402 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
403 | if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') { |
404 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
405 | } |
406 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
407 | } |
408 | $header = array(); |
35aaf666 |
409 | if ( !isset($skip_SM_header) || !$skip_SM_header ) |
410 | { |
411 | $header[] = "Received: from $received_from" . $rn; |
40a55a1b |
412 | $header[] = " (SquirrelMail authenticated user $username)" . $rn; |
35aaf666 |
413 | $header[] = " by $SERVER_NAME with HTTP;" . $rn; |
414 | $header[] = " $date" . $rn; |
415 | } |
59387d1c |
416 | /* Insert the rest of the header fields */ |
417 | $header[] = 'Message-ID: '. $message_id . $rn; |
1274f430 |
418 | if ($reply_rfc822_header->message_id) { |
78f39e78 |
419 | $rep_message_id = $reply_rfc822_header->message_id; |
420 | // $this->strip_crlf($message_id); |
421 | $header[] = 'In-Reply-To: '.$rep_message_id . $rn; |
422 | $references = $this->calculate_references($reply_rfc822_header); |
423 | $header[] = 'References: '.$references . $rn; |
424 | } |
425 | $header[] = "Date: $date" . $rn; |
59387d1c |
426 | $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn; |
ac47827c |
427 | $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn; |
428 | |
429 | // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>" as delimiter |
430 | // Do not use foldLine for that. |
431 | |
432 | // RFC2822 if from contains more then 1 address |
59387d1c |
433 | if (count($rfc822_header->from) > 1) { |
215de78c |
434 | $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn; |
78f39e78 |
435 | } |
436 | if (count($rfc822_header->to)) { |
ac47827c |
437 | $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn; |
78f39e78 |
438 | } |
439 | if (count($rfc822_header->cc)) { |
ac47827c |
440 | $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn; |
78f39e78 |
441 | } |
442 | if (count($rfc822_header->reply_to)) { |
215de78c |
443 | $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn; |
78f39e78 |
444 | } |
445 | /* Sendmail should return true. Default = false */ |
446 | $bcc = $this->getBcc(); |
447 | if (count($rfc822_header->bcc)) { |
ac47827c |
448 | $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn; |
78f39e78 |
449 | if (!$bcc) { |
78f39e78 |
450 | $raw_length += strlen($s); |
451 | } else { |
452 | $header[] = $s; |
453 | } |
454 | } |
72b004e6 |
455 | /* Identify SquirrelMail */ |
caf6a9a4 |
456 | $header[] = 'User-Agent: SquirrelMail/' . $version . $rn; |
78f39e78 |
457 | /* Do the MIME-stuff */ |
458 | $header[] = 'MIME-Version: 1.0' . $rn; |
459 | $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'. |
59387d1c |
460 | $rfc822_header->content_type->type1; |
78f39e78 |
461 | if (count($rfc822_header->content_type->properties)) { |
462 | foreach ($rfc822_header->content_type->properties as $k => $v) { |
463 | if ($k && $v) { |
72b004e6 |
464 | $contenttype .= ';' .$k.'='.$v; |
78f39e78 |
465 | } |
466 | } |
467 | } |
59387d1c |
468 | $header[] = $contenttype . $rn; |
78f39e78 |
469 | if ($encoding = $rfc822_header->encoding) { |
23301b33 |
470 | $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn; |
72b004e6 |
471 | } |
59387d1c |
472 | if ($rfc822_header->dnt) { |
72b004e6 |
473 | $dnt = $rfc822_header->getAddr_s('dnt'); |
78f39e78 |
474 | /* Pegasus Mail */ |
475 | $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn; |
476 | /* RFC 2298 */ |
477 | $header[] = 'Disposition-Notification-To: '.$dnt. $rn; |
478 | } |
479 | if ($rfc822_header->priority) { |
d1db3699 |
480 | switch($rfc822_header->priority) |
78f39e78 |
481 | { |
d1db3699 |
482 | case 1: |
91e0dccc |
483 | $header[] = 'X-Priority: 1 (Highest)'.$rn; |
484 | $header[] = 'Importance: High'. $rn; break; |
d1db3699 |
485 | case 5: |
91e0dccc |
486 | $header[] = 'X-Priority: 5 (Lowest)'.$rn; |
487 | $header[] = 'Importance: Low'. $rn; break; |
78f39e78 |
488 | default: break; |
489 | } |
490 | } |
72b004e6 |
491 | /* Insert headers from the $more_headers array */ |
78f39e78 |
492 | if(count($rfc822_header->more_headers)) { |
493 | reset($rfc822_header->more_headers); |
494 | foreach ($rfc822_header->more_headers as $k => $v) { |
495 | $header[] = $k.': '.$v .$rn; |
496 | } |
497 | } |
498 | $cnt = count($header); |
499 | $hdr_s = ''; |
5d2a7b15 |
500 | |
78f39e78 |
501 | for ($i = 0 ; $i < $cnt ; $i++) { |
5d2a7b15 |
502 | $sKey = substr($header[$i],0,strpos($header[$i],':')); |
503 | switch ($sKey) |
504 | { |
505 | case 'Message-ID': |
506 | case 'In-Reply_To': |
507 | $hdr_s .= $header[$i]; |
508 | break; |
509 | case 'References': |
510 | $sRefs = substr($header[$i],12); |
511 | $aRefs = explode(' ',$sRefs); |
512 | $sLine = 'References:'; |
513 | foreach ($aRefs as $sReference) { |
514 | if (strlen($sLine)+strlen($sReference) >76) { |
515 | $hdr_s .= $sLine; |
516 | $sLine = $rn . ' ' . $sReference; |
517 | } else { |
518 | $sLine .= ' '. $sReference; |
519 | } |
520 | } |
521 | $hdr_s .= $sLine; |
522 | break; |
ac47827c |
523 | case 'To': |
524 | case 'Cc': |
525 | case 'Bcc': |
526 | case 'From': |
527 | $hdr_s .= $header[$i]; |
528 | break; |
5d2a7b15 |
529 | default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break; |
530 | } |
78f39e78 |
531 | } |
78f39e78 |
532 | $header = $hdr_s; |
533 | $header .= $rn; /* One blank line to separate header and body */ |
534 | $raw_length += strlen($header); |
535 | return $header; |
4960ec8e |
536 | } |
537 | |
72b004e6 |
538 | /** |
539 | * function foldLine - for cleanly folding of headerlines |
540 | * |
541 | * @param string $line |
542 | * @param integer $length length to fold the line at |
543 | * @param string $pre prefix the line with... |
544 | * |
3a70ee56 |
545 | * @return string $line folded line with trailing CRLF |
72b004e6 |
546 | */ |
1274f430 |
547 | function foldLine($line, $length, $pre='') { |
e4f9307a |
548 | $line = substr($line,0, -2); |
fb1d6cb6 |
549 | $length -= 2; /* do not fold between \r and \n */ |
550 | $cnt = strlen($line); |
551 | if ($cnt > $length) { /* try folding */ |
552 | $fold_string = "\r\n " . $pre; |
553 | $bFirstFold = false; |
554 | $aFoldLine = array(); |
555 | while (strlen($line) > $length) { |
556 | $fold = false; |
557 | /* handle encoded parts */ |
7060c7f0 |
558 | if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) { |
fb1d6cb6 |
559 | $fold_tmp = $regs[1]; |
7060c7f0 |
560 | if (!trim($regs[5])) { |
561 | $fold_tmp .= $regs[5]; |
562 | } |
fb1d6cb6 |
563 | $iPosEnc = strpos($line,$fold_tmp); |
564 | $iLengthEnc = strlen($fold_tmp); |
c422e824 |
565 | $iPosEncEnd = $iPosEnc+$iLengthEnc; |
566 | if ($iPosEnc < $length && (($iPosEncEnd) > $length)) { |
fb1d6cb6 |
567 | $fold = true; |
568 | /* fold just before the start of the encoded string */ |
7060c7f0 |
569 | if ($iPosEnc) { |
570 | $aFoldLine[] = substr($line,0,$iPosEnc); |
571 | } |
fb1d6cb6 |
572 | $line = substr($line,$iPosEnc); |
573 | if (!$bFirstFold) { |
574 | $bFirstFold = true; |
575 | $length -= strlen($fold_string); |
576 | } |
577 | if ($iLengthEnc > $length) { /* place the encoded |
578 | string on a separate line and do not fold inside it*/ |
223872cb |
579 | /* minimize foldstring */ |
580 | $fold_string = "\r\n "; |
581 | $aFoldLine[] = substr($line,0,$iLengthEnc); |
582 | $line = substr($line,$iLengthEnc); |
72b004e6 |
583 | } |
c422e824 |
584 | } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */ |
fb1d6cb6 |
585 | /*remainder */ |
fb1d6cb6 |
586 | $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd); |
c422e824 |
587 | if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) || !preg_match('/[=,;\s]/',$sLineRem)) { |
fb1d6cb6 |
588 | /*impossible to fold clean in the next part -> fold after the enc string */ |
7060c7f0 |
589 | $aFoldLine[] = substr($line,0,$iPosEncEnd); |
590 | $line = substr($line,$iPosEncEnd); |
fb1d6cb6 |
591 | $fold = true; |
592 | if (!$bFirstFold) { |
593 | $bFirstFold = true; |
594 | $length -= strlen($fold_string); |
595 | } |
72b004e6 |
596 | } |
fb1d6cb6 |
597 | } |
598 | } |
599 | if (!$fold) { |
600 | $line_tmp = substr($line,0,$length); |
601 | $iFoldPos = false; |
602 | /* try to fold at logical places */ |
603 | switch (true) |
604 | { |
605 | case ($iFoldPos = strrpos($line_tmp,',')): break; |
606 | case ($iFoldPos = strrpos($line_tmp,';')): break; |
607 | case ($iFoldPos = strrpos($line_tmp,' ')): break; |
608 | case ($iFoldPos = strrpos($line_tmp,'=')): break; |
609 | default: break; |
610 | } |
72b004e6 |
611 | |
fb1d6cb6 |
612 | if (!$iFoldPos) { /* clean folding didn't work */ |
613 | $iFoldPos = $length; |
614 | } |
615 | $aFoldLine[] = substr($line,0,$iFoldPos+1); |
616 | $line = substr($line,$iFoldPos+1); |
617 | if (!$bFirstFold) { |
618 | $bFirstFold = true; |
619 | $length -= strlen($fold_string); |
620 | } |
621 | } |
622 | } |
623 | /*$reconstruct the line */ |
624 | if ($line) { |
625 | $aFoldLine[] = $line; |
626 | } |
627 | $line = implode($fold_string,$aFoldLine); |
628 | } |
629 | return $line."\r\n"; |
630 | } |
3b53d87f |
631 | |
72b004e6 |
632 | /** |
633 | * function mimeBoundary - calculates the mime boundary to use |
634 | * |
635 | * This function will generate a random mime boundary base part |
636 | * for the message if the boundary has not already been set. |
637 | * |
3a70ee56 |
638 | * @return string $mimeBoundaryString random mime boundary string |
72b004e6 |
639 | */ |
59387d1c |
640 | function mimeBoundary () { |
78f39e78 |
641 | static $mimeBoundaryString; |
3b53d87f |
642 | |
78f39e78 |
643 | if ( !isset( $mimeBoundaryString ) || |
644 | $mimeBoundaryString == '') { |
645 | $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' . |
646 | mt_rand( 10000, 99999 ); |
647 | } |
648 | return $mimeBoundaryString; |
3b53d87f |
649 | } |
650 | |
72b004e6 |
651 | /** |
652 | * function timezone - Time offset for correct timezone |
653 | * |
3a70ee56 |
654 | * @return string $result with timezone and offset |
72b004e6 |
655 | */ |
59387d1c |
656 | function timezone () { |
78f39e78 |
657 | global $invert_time; |
658 | |
659 | $diff_second = date('Z'); |
660 | if ($invert_time) { |
661 | $diff_second = - $diff_second; |
662 | } |
663 | if ($diff_second > 0) { |
664 | $sign = '+'; |
665 | } else { |
666 | $sign = '-'; |
667 | } |
668 | $diff_second = abs($diff_second); |
669 | $diff_hour = floor ($diff_second / 3600); |
670 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
671 | $zonename = '('.strftime('%Z').')'; |
72b004e6 |
672 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, |
3b53d87f |
673 | $zonename); |
78f39e78 |
674 | return ($result); |
59387d1c |
675 | } |
3b53d87f |
676 | |
72b004e6 |
677 | /** |
678 | * function calculate_references - calculate correct Referer string |
679 | * |
680 | * @param Rfc822Header $hdr message header to calculate from |
681 | * |
3a70ee56 |
682 | * @return string $refer concatenated and trimmed Referer string |
72b004e6 |
683 | */ |
1274f430 |
684 | function calculate_references($hdr) { |
685 | $refer = $hdr->references; |
78f39e78 |
686 | $message_id = $hdr->message_id; |
687 | $in_reply_to = $hdr->in_reply_to; |
688 | if (strlen($refer) > 2) { |
689 | $refer .= ' ' . $message_id; |
690 | } else { |
691 | if ($in_reply_to) { |
692 | $refer .= $in_reply_to . ' ' . $message_id; |
693 | } else { |
694 | $refer .= $message_id; |
695 | } |
696 | } |
697 | trim($refer); |
698 | return $refer; |
59387d1c |
699 | } |
700 | } |
f8a1ed5a |
701 | ?> |