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)) { |
157 | $length += $this->clean_crlf($body_part); |
158 | if ($stream) { |
159 | $this->preWriteToStream($body_part); |
160 | $this->writeToStream($stream, $body_part); |
161 | } |
162 | $last = $body_part; |
163 | } |
164 | fclose($file); |
165 | } |
166 | break; |
167 | default: |
168 | if ($message->body_part) { |
169 | $body_part = $message->body_part; |
170 | $length += $this->clean_crlf($body_part); |
171 | if ($stream) { |
172 | $this->writeToStream($stream, $body_part); |
173 | } |
174 | } elseif ($message->att_local_name) { |
175 | $filename = $message->att_local_name; |
176 | $file = fopen ($filename, 'rb'); |
78f39e78 |
177 | while ($tmp = fread($file, 570)) { |
178 | $body_part = chunk_split(base64_encode($tmp)); |
179 | $length += $this->clean_crlf($body_part); |
180 | if ($stream) { |
181 | $this->writeToStream($stream, $body_part); |
182 | } |
183 | } |
184 | fclose($file); |
3b53d87f |
185 | } |
78f39e78 |
186 | break; |
187 | } |
188 | $body_part_trailing = ''; |
189 | if ($last && substr($last,-1) != "\n") { |
190 | $body_part_trailing = "\r\n"; |
191 | } |
192 | if ($body_part_trailing) { |
193 | $length += strlen($body_part_trailing); |
194 | if ($stream) { |
195 | $this->preWriteToStream($body_part_trailing); |
196 | $this->writeToStream($stream, $body_part_trailing); |
197 | } |
198 | } |
3b53d87f |
199 | } |
78f39e78 |
200 | |
72b004e6 |
201 | /** |
202 | * function clean_crlf - change linefeeds and newlines to legal characters |
203 | * |
204 | * The SMTP format only allows CRLF as line terminators. |
205 | * This function replaces illegal teminators with the correct terminator. |
206 | * |
207 | * @param string &$s string to clean linefeeds on |
208 | * |
3a70ee56 |
209 | * @return void |
72b004e6 |
210 | */ |
69298c28 |
211 | function clean_crlf(&$s) { |
3b53d87f |
212 | $s = str_replace("\r\n", "\n", $s); |
213 | $s = str_replace("\r", "\n", $s); |
214 | $s = str_replace("\n", "\r\n", $s); |
78f39e78 |
215 | return strlen($s); |
3b53d87f |
216 | } |
78f39e78 |
217 | |
72b004e6 |
218 | /** |
219 | * function strip_crlf - strip linefeeds and newlines from a string |
220 | * |
221 | * The SMTP format only allows CRLF as line terminators. |
222 | * This function strips all line terminators from the string. |
223 | * |
224 | * @param string &$s string to clean linefeeds on |
225 | * |
3a70ee56 |
226 | * @return void |
72b004e6 |
227 | */ |
e4f9307a |
228 | function strip_crlf(&$s) { |
229 | $s = str_replace("\r\n ", '', $s); |
78f39e78 |
230 | $s = str_replace("\r", '', $s); |
231 | $s = str_replace("\n", '', $s); |
e4f9307a |
232 | } |
3b53d87f |
233 | |
72b004e6 |
234 | /** |
235 | * function preWriteToStream - reserved for extended functionality |
236 | * |
237 | * This function is not yet implemented. |
238 | * Reserved for extended functionality. |
239 | * |
240 | * @param string &$s string to operate on |
241 | * |
3a70ee56 |
242 | * @return void |
72b004e6 |
243 | */ |
69298c28 |
244 | function preWriteToStream(&$s) { |
3b53d87f |
245 | } |
78f39e78 |
246 | |
72b004e6 |
247 | /** |
248 | * function writeToStream - write data to the SMTP stream |
249 | * |
250 | * @param resource $stream SMTP output stream |
251 | * @param string $data string with data to send to the SMTP stream |
252 | * |
3a70ee56 |
253 | * @return void |
72b004e6 |
254 | */ |
3b53d87f |
255 | function writeToStream($stream, $data) { |
78f39e78 |
256 | fputs($stream, $data); |
3b53d87f |
257 | } |
78f39e78 |
258 | |
72b004e6 |
259 | /** |
260 | * function initStream - reserved for extended functionality |
261 | * |
262 | * This function is not yet implemented. |
263 | * Reserved for extended functionality. |
264 | * |
265 | * @param Message $message Message object |
266 | * @param string $host host name or IP to connect to |
267 | * @param string $user username to log into the SMTP server with |
268 | * @param string $pass password to log into the SMTP server with |
269 | * @param integer $length |
270 | * |
3a70ee56 |
271 | * @return handle $stream file handle resource to SMTP stream |
72b004e6 |
272 | */ |
3b53d87f |
273 | function initStream($message, $length=0, $host='', $port='', $user='', $pass='') { |
78f39e78 |
274 | return $stream; |
3b53d87f |
275 | } |
72b004e6 |
276 | |
277 | /** |
278 | * function getBCC - reserved for extended functionality |
279 | * |
280 | * This function is not yet implemented. |
281 | * Reserved for extended functionality. |
282 | * |
283 | */ |
284 | function getBCC() { |
78f39e78 |
285 | return false; |
59387d1c |
286 | } |
3b53d87f |
287 | |
72b004e6 |
288 | /** |
289 | * function prepareMIME_Header - creates the mime header |
290 | * |
291 | * @param Message $message Message object to act on |
292 | * @param string $boundary mime boundary from fn MimeBoundary |
293 | * |
3a70ee56 |
294 | * @return string $header properly formatted mime header |
72b004e6 |
295 | */ |
3b53d87f |
296 | function prepareMIME_Header($message, $boundary) { |
78f39e78 |
297 | $mime_header = $message->mime_header; |
298 | $rn="\r\n"; |
299 | $header = array(); |
1274f430 |
300 | |
78f39e78 |
301 | $contenttype = 'Content-Type: '. $mime_header->type0 .'/'. |
302 | $mime_header->type1; |
303 | if (count($message->entities)) { |
fcce1b3c |
304 | $contenttype .= ';' . 'boundary="'.$boundary.'"'; |
78f39e78 |
305 | } |
306 | if (isset($mime_header->parameters['name'])) { |
307 | $contenttype .= '; name="'. |
308 | encodeHeader($mime_header->parameters['name']). '"'; |
309 | } |
310 | if (isset($mime_header->parameters['charset'])) { |
311 | $charset = $mime_header->parameters['charset']; |
312 | $contenttype .= '; charset="'. |
313 | encodeHeader($charset). '"'; |
314 | } |
1274f430 |
315 | |
78f39e78 |
316 | $header[] = $contenttype . $rn; |
317 | if ($mime_header->description) { |
eb72e151 |
318 | $header[] = 'Content-Description: ' . $mime_header->description . $rn; |
78f39e78 |
319 | } |
320 | if ($mime_header->encoding) { |
eb72e151 |
321 | $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn; |
78f39e78 |
322 | } else { |
323 | if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') { |
eb72e151 |
324 | $header[] = 'Content-Transfer-Encoding: 8bit' . $rn; |
78f39e78 |
325 | } else { |
eb72e151 |
326 | $header[] = 'Content-Transfer-Encoding: base64' . $rn; |
78f39e78 |
327 | } |
328 | } |
329 | if ($mime_header->id) { |
eb72e151 |
330 | $header[] = 'Content-ID: ' . $mime_header->id . $rn; |
78f39e78 |
331 | } |
332 | if ($mime_header->disposition) { |
333 | $disposition = $mime_header->disposition; |
334 | $contentdisp = 'Content-Disposition: ' . $disposition->name; |
335 | if ($disposition->getProperty('filename')) { |
336 | $contentdisp .= '; filename="'. |
337 | encodeHeader($disposition->getProperty('filename')). '"'; |
338 | } |
72b004e6 |
339 | $header[] = $contentdisp . $rn; |
78f39e78 |
340 | } |
341 | if ($mime_header->md5) { |
eb72e151 |
342 | $header[] = 'Content-MD5: ' . $mime_header->md5 . $rn; |
78f39e78 |
343 | } |
344 | if ($mime_header->language) { |
eb72e151 |
345 | $header[] = 'Content-Language: ' . $mime_header->language . $rn; |
78f39e78 |
346 | } |
4960ec8e |
347 | |
78f39e78 |
348 | $cnt = count($header); |
349 | $hdr_s = ''; |
350 | for ($i = 0 ; $i < $cnt ; $i++) { |
351 | $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4)); |
352 | } |
353 | $header = $hdr_s; |
354 | $header .= $rn; /* One blank line to separate mimeheader and body-entity */ |
355 | return $header; |
356 | } |
4960ec8e |
357 | |
72b004e6 |
358 | /** |
359 | * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s) |
360 | * |
361 | * This function takes the Rfc822Header object(s) and formats them |
362 | * into the RFC822Header string to send to the SMTP server as part |
363 | * of the SMTP message. |
364 | * |
365 | * @param Rfc822Header $rfc822_header |
366 | * @param Rfc822Header $reply_rfc822_header |
367 | * @param integer &$raw_length length of the message |
368 | * |
3a70ee56 |
369 | * @return string $header |
72b004e6 |
370 | */ |
75b14a70 |
371 | function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) { |
35aaf666 |
372 | global $domain, $version, $username, $skip_SM_header; |
60a46e65 |
373 | |
78f39e78 |
374 | /* if server var SERVER_NAME not available, use $domain */ |
60a46e65 |
375 | if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) { |
376 | $SERVER_NAME = $domain; |
78f39e78 |
377 | } |
60a46e65 |
378 | |
78f39e78 |
379 | sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER); |
380 | sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER); |
381 | sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER); |
382 | sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER); |
383 | sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER); |
60a46e65 |
384 | |
78f39e78 |
385 | $rn = "\r\n"; |
223872cb |
386 | |
78f39e78 |
387 | /* This creates an RFC 822 date */ |
388 | $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone(); |
389 | /* Create a message-id */ |
390 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
485e1181 |
391 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
78f39e78 |
392 | /* Make an RFC822 Received: line */ |
393 | if (isset($REMOTE_HOST)) { |
394 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
395 | } else { |
396 | $received_from = $REMOTE_ADDR; |
397 | } |
398 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
399 | if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') { |
400 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
401 | } |
402 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
403 | } |
404 | $header = array(); |
35aaf666 |
405 | if ( !isset($skip_SM_header) || !$skip_SM_header ) |
406 | { |
407 | $header[] = "Received: from $received_from" . $rn; |
40a55a1b |
408 | $header[] = " (SquirrelMail authenticated user $username)" . $rn; |
35aaf666 |
409 | $header[] = " by $SERVER_NAME with HTTP;" . $rn; |
410 | $header[] = " $date" . $rn; |
411 | } |
59387d1c |
412 | /* Insert the rest of the header fields */ |
413 | $header[] = 'Message-ID: '. $message_id . $rn; |
1274f430 |
414 | if ($reply_rfc822_header->message_id) { |
78f39e78 |
415 | $rep_message_id = $reply_rfc822_header->message_id; |
416 | // $this->strip_crlf($message_id); |
417 | $header[] = 'In-Reply-To: '.$rep_message_id . $rn; |
418 | $references = $this->calculate_references($reply_rfc822_header); |
419 | $header[] = 'References: '.$references . $rn; |
420 | } |
421 | $header[] = "Date: $date" . $rn; |
59387d1c |
422 | $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn; |
ac47827c |
423 | $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn; |
424 | |
425 | // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>" as delimiter |
426 | // Do not use foldLine for that. |
427 | |
428 | // RFC2822 if from contains more then 1 address |
59387d1c |
429 | if (count($rfc822_header->from) > 1) { |
215de78c |
430 | $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn; |
78f39e78 |
431 | } |
432 | if (count($rfc822_header->to)) { |
ac47827c |
433 | $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn; |
78f39e78 |
434 | } |
435 | if (count($rfc822_header->cc)) { |
ac47827c |
436 | $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn; |
78f39e78 |
437 | } |
438 | if (count($rfc822_header->reply_to)) { |
215de78c |
439 | $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn; |
78f39e78 |
440 | } |
441 | /* Sendmail should return true. Default = false */ |
442 | $bcc = $this->getBcc(); |
443 | if (count($rfc822_header->bcc)) { |
ac47827c |
444 | $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn; |
78f39e78 |
445 | if (!$bcc) { |
78f39e78 |
446 | $raw_length += strlen($s); |
447 | } else { |
448 | $header[] = $s; |
449 | } |
450 | } |
72b004e6 |
451 | /* Identify SquirrelMail */ |
caf6a9a4 |
452 | $header[] = 'User-Agent: SquirrelMail/' . $version . $rn; |
78f39e78 |
453 | /* Do the MIME-stuff */ |
454 | $header[] = 'MIME-Version: 1.0' . $rn; |
455 | $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'. |
59387d1c |
456 | $rfc822_header->content_type->type1; |
78f39e78 |
457 | if (count($rfc822_header->content_type->properties)) { |
458 | foreach ($rfc822_header->content_type->properties as $k => $v) { |
459 | if ($k && $v) { |
72b004e6 |
460 | $contenttype .= ';' .$k.'='.$v; |
78f39e78 |
461 | } |
462 | } |
463 | } |
59387d1c |
464 | $header[] = $contenttype . $rn; |
78f39e78 |
465 | if ($encoding = $rfc822_header->encoding) { |
23301b33 |
466 | $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn; |
72b004e6 |
467 | } |
59387d1c |
468 | if ($rfc822_header->dnt) { |
72b004e6 |
469 | $dnt = $rfc822_header->getAddr_s('dnt'); |
78f39e78 |
470 | /* Pegasus Mail */ |
471 | $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn; |
472 | /* RFC 2298 */ |
473 | $header[] = 'Disposition-Notification-To: '.$dnt. $rn; |
474 | } |
475 | if ($rfc822_header->priority) { |
d1db3699 |
476 | switch($rfc822_header->priority) |
78f39e78 |
477 | { |
d1db3699 |
478 | case 1: |
91e0dccc |
479 | $header[] = 'X-Priority: 1 (Highest)'.$rn; |
480 | $header[] = 'Importance: High'. $rn; break; |
d1db3699 |
481 | case 5: |
91e0dccc |
482 | $header[] = 'X-Priority: 5 (Lowest)'.$rn; |
483 | $header[] = 'Importance: Low'. $rn; break; |
78f39e78 |
484 | default: break; |
485 | } |
486 | } |
72b004e6 |
487 | /* Insert headers from the $more_headers array */ |
78f39e78 |
488 | if(count($rfc822_header->more_headers)) { |
489 | reset($rfc822_header->more_headers); |
490 | foreach ($rfc822_header->more_headers as $k => $v) { |
491 | $header[] = $k.': '.$v .$rn; |
492 | } |
493 | } |
494 | $cnt = count($header); |
495 | $hdr_s = ''; |
5d2a7b15 |
496 | |
78f39e78 |
497 | for ($i = 0 ; $i < $cnt ; $i++) { |
5d2a7b15 |
498 | $sKey = substr($header[$i],0,strpos($header[$i],':')); |
499 | switch ($sKey) |
500 | { |
501 | case 'Message-ID': |
502 | case 'In-Reply_To': |
503 | $hdr_s .= $header[$i]; |
504 | break; |
505 | case 'References': |
506 | $sRefs = substr($header[$i],12); |
507 | $aRefs = explode(' ',$sRefs); |
508 | $sLine = 'References:'; |
509 | foreach ($aRefs as $sReference) { |
510 | if (strlen($sLine)+strlen($sReference) >76) { |
511 | $hdr_s .= $sLine; |
512 | $sLine = $rn . ' ' . $sReference; |
513 | } else { |
514 | $sLine .= ' '. $sReference; |
515 | } |
516 | } |
517 | $hdr_s .= $sLine; |
518 | break; |
ac47827c |
519 | case 'To': |
520 | case 'Cc': |
521 | case 'Bcc': |
522 | case 'From': |
523 | $hdr_s .= $header[$i]; |
524 | break; |
5d2a7b15 |
525 | default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break; |
526 | } |
78f39e78 |
527 | } |
78f39e78 |
528 | $header = $hdr_s; |
529 | $header .= $rn; /* One blank line to separate header and body */ |
530 | $raw_length += strlen($header); |
531 | return $header; |
4960ec8e |
532 | } |
533 | |
72b004e6 |
534 | /** |
535 | * function foldLine - for cleanly folding of headerlines |
536 | * |
537 | * @param string $line |
538 | * @param integer $length length to fold the line at |
539 | * @param string $pre prefix the line with... |
540 | * |
3a70ee56 |
541 | * @return string $line folded line with trailing CRLF |
72b004e6 |
542 | */ |
1274f430 |
543 | function foldLine($line, $length, $pre='') { |
e4f9307a |
544 | $line = substr($line,0, -2); |
fb1d6cb6 |
545 | $length -= 2; /* do not fold between \r and \n */ |
546 | $cnt = strlen($line); |
547 | if ($cnt > $length) { /* try folding */ |
548 | $fold_string = "\r\n " . $pre; |
549 | $bFirstFold = false; |
550 | $aFoldLine = array(); |
551 | while (strlen($line) > $length) { |
552 | $fold = false; |
553 | /* handle encoded parts */ |
7060c7f0 |
554 | if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) { |
fb1d6cb6 |
555 | $fold_tmp = $regs[1]; |
7060c7f0 |
556 | if (!trim($regs[5])) { |
557 | $fold_tmp .= $regs[5]; |
558 | } |
fb1d6cb6 |
559 | $iPosEnc = strpos($line,$fold_tmp); |
560 | $iLengthEnc = strlen($fold_tmp); |
c422e824 |
561 | $iPosEncEnd = $iPosEnc+$iLengthEnc; |
562 | if ($iPosEnc < $length && (($iPosEncEnd) > $length)) { |
fb1d6cb6 |
563 | $fold = true; |
564 | /* fold just before the start of the encoded string */ |
7060c7f0 |
565 | if ($iPosEnc) { |
566 | $aFoldLine[] = substr($line,0,$iPosEnc); |
567 | } |
fb1d6cb6 |
568 | $line = substr($line,$iPosEnc); |
569 | if (!$bFirstFold) { |
570 | $bFirstFold = true; |
571 | $length -= strlen($fold_string); |
572 | } |
573 | if ($iLengthEnc > $length) { /* place the encoded |
574 | string on a separate line and do not fold inside it*/ |
223872cb |
575 | /* minimize foldstring */ |
576 | $fold_string = "\r\n "; |
577 | $aFoldLine[] = substr($line,0,$iLengthEnc); |
578 | $line = substr($line,$iLengthEnc); |
72b004e6 |
579 | } |
c422e824 |
580 | } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */ |
fb1d6cb6 |
581 | /*remainder */ |
fb1d6cb6 |
582 | $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd); |
c422e824 |
583 | if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) || !preg_match('/[=,;\s]/',$sLineRem)) { |
fb1d6cb6 |
584 | /*impossible to fold clean in the next part -> fold after the enc string */ |
7060c7f0 |
585 | $aFoldLine[] = substr($line,0,$iPosEncEnd); |
586 | $line = substr($line,$iPosEncEnd); |
fb1d6cb6 |
587 | $fold = true; |
588 | if (!$bFirstFold) { |
589 | $bFirstFold = true; |
590 | $length -= strlen($fold_string); |
591 | } |
72b004e6 |
592 | } |
fb1d6cb6 |
593 | } |
594 | } |
595 | if (!$fold) { |
596 | $line_tmp = substr($line,0,$length); |
597 | $iFoldPos = false; |
598 | /* try to fold at logical places */ |
599 | switch (true) |
600 | { |
601 | case ($iFoldPos = strrpos($line_tmp,',')): break; |
602 | case ($iFoldPos = strrpos($line_tmp,';')): break; |
603 | case ($iFoldPos = strrpos($line_tmp,' ')): break; |
604 | case ($iFoldPos = strrpos($line_tmp,'=')): break; |
605 | default: break; |
606 | } |
72b004e6 |
607 | |
fb1d6cb6 |
608 | if (!$iFoldPos) { /* clean folding didn't work */ |
609 | $iFoldPos = $length; |
610 | } |
611 | $aFoldLine[] = substr($line,0,$iFoldPos+1); |
612 | $line = substr($line,$iFoldPos+1); |
613 | if (!$bFirstFold) { |
614 | $bFirstFold = true; |
615 | $length -= strlen($fold_string); |
616 | } |
617 | } |
618 | } |
619 | /*$reconstruct the line */ |
620 | if ($line) { |
621 | $aFoldLine[] = $line; |
622 | } |
623 | $line = implode($fold_string,$aFoldLine); |
624 | } |
625 | return $line."\r\n"; |
626 | } |
3b53d87f |
627 | |
72b004e6 |
628 | /** |
629 | * function mimeBoundary - calculates the mime boundary to use |
630 | * |
631 | * This function will generate a random mime boundary base part |
632 | * for the message if the boundary has not already been set. |
633 | * |
3a70ee56 |
634 | * @return string $mimeBoundaryString random mime boundary string |
72b004e6 |
635 | */ |
59387d1c |
636 | function mimeBoundary () { |
78f39e78 |
637 | static $mimeBoundaryString; |
3b53d87f |
638 | |
78f39e78 |
639 | if ( !isset( $mimeBoundaryString ) || |
640 | $mimeBoundaryString == '') { |
641 | $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' . |
642 | mt_rand( 10000, 99999 ); |
643 | } |
644 | return $mimeBoundaryString; |
3b53d87f |
645 | } |
646 | |
72b004e6 |
647 | /** |
648 | * function timezone - Time offset for correct timezone |
649 | * |
3a70ee56 |
650 | * @return string $result with timezone and offset |
72b004e6 |
651 | */ |
59387d1c |
652 | function timezone () { |
78f39e78 |
653 | global $invert_time; |
654 | |
655 | $diff_second = date('Z'); |
656 | if ($invert_time) { |
657 | $diff_second = - $diff_second; |
658 | } |
659 | if ($diff_second > 0) { |
660 | $sign = '+'; |
661 | } else { |
662 | $sign = '-'; |
663 | } |
664 | $diff_second = abs($diff_second); |
665 | $diff_hour = floor ($diff_second / 3600); |
666 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
667 | $zonename = '('.strftime('%Z').')'; |
72b004e6 |
668 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, |
3b53d87f |
669 | $zonename); |
78f39e78 |
670 | return ($result); |
59387d1c |
671 | } |
3b53d87f |
672 | |
72b004e6 |
673 | /** |
674 | * function calculate_references - calculate correct Referer string |
675 | * |
676 | * @param Rfc822Header $hdr message header to calculate from |
677 | * |
3a70ee56 |
678 | * @return string $refer concatenated and trimmed Referer string |
72b004e6 |
679 | */ |
1274f430 |
680 | function calculate_references($hdr) { |
681 | $refer = $hdr->references; |
78f39e78 |
682 | $message_id = $hdr->message_id; |
683 | $in_reply_to = $hdr->in_reply_to; |
684 | if (strlen($refer) > 2) { |
685 | $refer .= ' ' . $message_id; |
686 | } else { |
687 | if ($in_reply_to) { |
688 | $refer .= $in_reply_to . ' ' . $message_id; |
689 | } else { |
690 | $refer .= $message_id; |
691 | } |
692 | } |
693 | trim($refer); |
694 | return $refer; |
59387d1c |
695 | } |
696 | } |
f8a1ed5a |
697 | ?> |