59312ebf5802fd1e99ae9b3f34cda8095d8900fc
6 * This contains all the functions needed to send messages through
9 * @author Marc Groot Koerkamp
10 * @copyright © 1999-2007 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
17 * Deliver Class - called to actually deliver the message
19 * This class is called by compose.php and other code that needs
20 * to send messages. All delivery functionality should be centralized
23 * Do not place UI code in this class, as UI code should be placed in templates
26 * @author Marc Groot Koerkamp
27 * @package squirrelmail
32 * function mail - send the message parts to the SMTP stream
34 * @param Message $message Message object to send
35 * @param resource $stream Handle to the SMTP stream
36 * (when FALSE, nothing will be
37 * written to the stream; this can
38 * be used to determine the actual
39 * number of bytes that will be
40 * written to the stream)
41 * @param string $reply_id Identifies message being replied to
42 * (OPTIONAL; caller should ONLY specify
43 * a value for this when the message
44 * being sent is a reply)
45 * @param string $reply_ent_id Identifies message being replied to
46 * in the case it was an embedded/attached
47 * message inside another (OPTIONAL; caller
48 * should ONLY specify a value for this
49 * when the message being sent is a reply)
50 * @param mixed $extra Any implementation-specific variables
51 * can be passed in here and used in
52 * an overloaded version of this method
55 * @return integer $raw_length The number of bytes written (or that would
56 * have been written) to the output stream
58 function mail($message, $stream=false, $reply_id=0, $reply_ent_id=0,
61 $rfc822_header = $message->rfc822_header
;
62 if (count($message->entities
)) {
63 $boundary = $this->mimeBoundary();
64 $rfc822_header->content_type
->properties
['boundary']='"'.$boundary.'"';
71 // calculate reply header if needed
74 global $imapConnection, $username, $imapServerAddress,
77 if (!is_resource($imapConnection))
78 $imapConnection = sqimap_login($username, FALSE,
79 $imapServerAddress, $imapPort, 0);
81 sqimap_mailbox_select($imapConnection, $mailbox);
82 $reply_message = sqimap_get_message($imapConnection, $reply_id, $mailbox);
85 /* redefine the messsage in case of message/rfc822 */
86 $reply_message = $message->getEntity($reply_ent_id);
87 /* message is an entity which contains the envelope and type0=message
88 * and type1=rfc822. The actual entities are childs from
89 * $reply_message->entities[0]. That's where the encoding and is located
92 $orig_header = $reply_message->rfc822_header
; /* here is the envelope located */
95 $orig_header = $reply_message->rfc822_header
;
98 $message->reply_rfc822_header
= $orig_header;
101 $reply_rfc822_header = (isset($message->reply_rfc822_header
)
102 ?
$message->reply_rfc822_header
: '');
103 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
105 $this->send_mail($message, $header, $boundary, $stream, $raw_length, $extra);
111 * function send_mail - send the message parts to the IMAP stream
113 * @param Message $message Message object to send
114 * @param string $header Headers ready to send
115 * @param string $boundary Message parts boundary
116 * @param resource $stream Handle to the SMTP stream
117 * (when FALSE, nothing will be
118 * written to the stream; this can
119 * be used to determine the actual
120 * number of bytes that will be
121 * written to the stream)
122 * @param int &$raw_length The number of bytes written (or that
123 * would have been written) to the
124 * output stream - NOTE that this is
125 * passed by reference
126 * @param mixed $extra Any implementation-specific variables
127 * can be passed in here and used in
128 * an overloaded version of this method
134 function send_mail($message, $header, $boundary, $stream=false,
135 &$raw_length, $extra=NULL) {
139 $this->preWriteToStream($header);
140 $this->writeToStream($stream, $header);
142 $this->writeBody($message, $stream, $raw_length, $boundary);
146 * function writeBody - generate and write the mime boundaries around each part to the stream
148 * Recursively formats and writes the MIME boundaries of the $message
149 * to the output stream.
151 * @param Message $message Message object to transform
152 * @param resource $stream SMTP output stream
153 * (when FALSE, nothing will be
154 * written to the stream; this can
155 * be used to determine the actual
156 * number of bytes that will be
157 * written to the stream)
158 * @param integer &$length_raw raw length of the message (part)
159 * as returned by mail fn
160 * @param string $boundary custom boundary to call, usually for subparts
164 function writeBody($message, $stream, &$length_raw, $boundary='') {
165 // calculate boundary in case of multidimensional mime structures
166 if ($boundary && $message->entity_id
&& count($message->entities
)) {
167 if (strpos($boundary,'_part_')) {
168 $boundary = substr($boundary,0,strpos($boundary,'_part_'));
170 // the next four lines use strrev to reverse any nested boundaries
171 // because RFC 2046 (5.1.1) says that if a line starts with the outer
172 // boundary string (doesn't matter what the line ends with), that
173 // can be considered a match for the outer boundary; thus the nested
174 // boundary needs to be unique from the outer one
176 } else if (strpos($boundary,'_trap_')) {
177 $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_'));
179 $boundary_new = strrev($boundary . '_part_'.$message->entity_id
);
181 $boundary_new = $boundary;
183 if ($boundary && !$message->rfc822_header
) {
184 $s = '--'.$boundary."\r\n";
185 $s .= $this->prepareMIME_Header($message, $boundary_new);
186 $length_raw +
= strlen($s);
188 $this->preWriteToStream($s);
189 $this->writeToStream($stream, $s);
192 $this->writeBodyPart($message, $stream, $length_raw);
195 for ($i=0, $entCount=count($message->entities
);$i<$entCount;$i++
) {
196 $msg = $this->writeBody($message->entities
[$i], $stream, $length_raw, $boundary_new);
197 if ($i == $entCount-1) $last = true;
199 if ($boundary && $last) {
200 $s = "--".$boundary_new."--\r\n\r\n";
201 $length_raw +
= strlen($s);
203 $this->preWriteToStream($s);
204 $this->writeToStream($stream, $s);
210 * function writeBodyPart - write each individual mimepart
212 * Recursively called by WriteBody to write each mime part to the SMTP stream
214 * @param Message $message Message object to transform
215 * @param resource $stream SMTP output stream
216 * (when FALSE, nothing will be
217 * written to the stream; this can
218 * be used to determine the actual
219 * number of bytes that will be
220 * written to the stream)
221 * @param integer &$length length of the message part
222 * as returned by mail fn
226 function writeBodyPart($message, $stream, &$length) {
227 if ($message->mime_header
) {
228 $type0 = $message->mime_header
->type0
;
230 $type0 = $message->rfc822_header
->content_type
->type0
;
233 $body_part_trailing = $last = '';
238 if ($message->body_part
) {
239 $body_part = $message->body_part
;
240 // remove NUL characters
241 $body_part = str_replace("\0",'',$body_part);
242 $length +
= $this->clean_crlf($body_part);
244 $this->preWriteToStream($body_part);
245 $this->writeToStream($stream, $body_part);
248 } elseif ($message->att_local_name
) {
249 global $username, $attachment_dir;
250 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
251 $filename = $message->att_local_name
;
252 $file = fopen ($hashed_attachment_dir . '/' . $filename, 'rb');
253 while ($body_part = fgets($file, 4096)) {
254 // remove NUL characters
255 $body_part = str_replace("\0",'',$body_part);
256 $length +
= $this->clean_crlf($body_part);
258 $this->preWriteToStream($body_part);
259 $this->writeToStream($stream, $body_part);
267 if ($message->body_part
) {
268 $body_part = $message->body_part
;
269 // remove NUL characters
270 $body_part = str_replace("\0",'',$body_part);
271 $length +
= $this->clean_crlf($body_part);
273 $this->writeToStream($stream, $body_part);
275 } elseif ($message->att_local_name
) {
276 global $username, $attachment_dir;
277 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
278 $filename = $message->att_local_name
;
279 $file = fopen ($hashed_attachment_dir . '/' . $filename, 'rb');
280 while ($tmp = fread($file, 570)) {
281 $body_part = chunk_split(base64_encode($tmp));
282 // Up to 4.3.10 chunk_split always appends a newline,
283 // while in 4.3.11 it doesn't if the string to split
284 // is shorter than the chunk length.
285 if( substr($body_part, -1 , 1 ) != "\n" )
287 $length +
= $this->clean_crlf($body_part);
289 $this->writeToStream($stream, $body_part);
296 $body_part_trailing = '';
297 if ($last && substr($last,-1) != "\n") {
298 $body_part_trailing = "\r\n";
300 if ($body_part_trailing) {
301 $length +
= strlen($body_part_trailing);
303 $this->preWriteToStream($body_part_trailing);
304 $this->writeToStream($stream, $body_part_trailing);
310 * function clean_crlf - change linefeeds and newlines to legal characters
312 * The SMTP format only allows CRLF as line terminators.
313 * This function replaces illegal teminators with the correct terminator.
315 * @param string &$s string to clean linefeeds on
319 function clean_crlf(&$s) {
320 $s = str_replace("\r\n", "\n", $s);
321 $s = str_replace("\r", "\n", $s);
322 $s = str_replace("\n", "\r\n", $s);
327 * function strip_crlf - strip linefeeds and newlines from a string
329 * The SMTP format only allows CRLF as line terminators.
330 * This function strips all line terminators from the string.
332 * @param string &$s string to clean linefeeds on
336 function strip_crlf(&$s) {
337 $s = str_replace("\r\n ", '', $s);
338 $s = str_replace("\r", '', $s);
339 $s = str_replace("\n", '', $s);
343 * function preWriteToStream - reserved for extended functionality
345 * This function is not yet implemented.
346 * Reserved for extended functionality.
348 * @param string &$s string to operate on
352 function preWriteToStream(&$s) {
356 * function writeToStream - write data to the SMTP stream
358 * @param resource $stream SMTP output stream
359 * @param string $data string with data to send to the SMTP stream
363 function writeToStream($stream, $data) {
364 fputs($stream, $data);
368 * function initStream - reserved for extended functionality
370 * This function is not yet implemented.
371 * Reserved for extended functionality.
373 * @param Message $message Message object
374 * @param string $host host name or IP to connect to
375 * @param string $user username to log into the SMTP server with
376 * @param string $pass password to log into the SMTP server with
377 * @param integer $length
379 * @return handle $stream file handle resource to SMTP stream
381 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
386 * function getBCC - reserved for extended functionality
388 * This function is not yet implemented.
389 * Reserved for extended functionality.
397 * function prepareMIME_Header - creates the mime header
399 * @param Message $message Message object to act on
400 * @param string $boundary mime boundary from fn MimeBoundary
402 * @return string $header properly formatted mime header
404 function prepareMIME_Header($message, $boundary) {
405 $mime_header = $message->mime_header
;
409 $contenttype = 'Content-Type: '. $mime_header->type0
.'/'.
411 if (count($message->entities
)) {
412 $contenttype .= ';' . 'boundary="'.$boundary.'"';
414 if (isset($mime_header->parameters
['name'])) {
415 $contenttype .= '; name="'.
416 encodeHeader($mime_header->parameters
['name']). '"';
418 if (isset($mime_header->parameters
['charset'])) {
419 $charset = $mime_header->parameters
['charset'];
420 $contenttype .= '; charset="'.
421 encodeHeader($charset). '"';
424 $header[] = $contenttype . $rn;
425 if ($mime_header->description
) {
426 $header[] = 'Content-Description: ' . $mime_header->description
. $rn;
428 if ($mime_header->encoding
) {
429 $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
431 if ($mime_header->type0
== 'text' ||
$mime_header->type0
== 'message') {
432 $header[] = 'Content-Transfer-Encoding: 8bit' . $rn;
434 $header[] = 'Content-Transfer-Encoding: base64' . $rn;
437 if ($mime_header->id
) {
438 $header[] = 'Content-ID: ' . $mime_header->id
. $rn;
440 if ($mime_header->disposition
) {
441 $disposition = $mime_header->disposition
;
442 $contentdisp = 'Content-Disposition: ' . $disposition->name
;
443 if ($disposition->getProperty('filename')) {
444 $contentdisp .= '; filename="'.
445 encodeHeader($disposition->getProperty('filename')). '"';
447 $header[] = $contentdisp . $rn;
449 if ($mime_header->md5
) {
450 $header[] = 'Content-MD5: ' . $mime_header->md5
. $rn;
452 if ($mime_header->language
) {
453 $header[] = 'Content-Language: ' . $mime_header->language
. $rn;
456 $cnt = count($header);
458 for ($i = 0 ; $i < $cnt ; $i++
) {
459 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
462 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
467 * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s)
469 * This function takes the Rfc822Header object(s) and formats them
470 * into the RFC822Header string to send to the SMTP server as part
471 * of the SMTP message.
473 * @param Rfc822Header $rfc822_header
474 * @param Rfc822Header $reply_rfc822_header
475 * @param integer &$raw_length length of the message
477 * @return string $header
479 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
480 global $domain, $username, $encode_header_key,
481 $edit_identity, $hide_auth_header;
483 /* if server var SERVER_NAME not available, use $domain */
484 if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER
)) {
485 $SERVER_NAME = $domain;
488 sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER
);
489 sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER
);
490 sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER
);
491 sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER
);
492 sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER
);
496 /* This creates an RFC 822 date */
497 $date = date('D, j M Y H:i:s ', time()) . $this->timezone();
498 /* Create a message-id */
499 $message_id = '<' . $REMOTE_PORT . '.';
500 if (isset($encode_header_key) && trim($encode_header_key)!='') {
501 // use encrypted form of remote address
502 $message_id.= OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key));
504 $message_id.= $REMOTE_ADDR;
506 $message_id .= '.' . time() . '.squirrel@' . $SERVER_NAME .'>';
507 /* Make an RFC822 Received: line */
508 if (isset($REMOTE_HOST)) {
509 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
511 $received_from = $REMOTE_ADDR;
513 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
514 if (!isset($HTTP_X_FORWARDED_FOR) ||
$HTTP_X_FORWARDED_FOR == '') {
515 $HTTP_X_FORWARDED_FOR = 'unknown';
517 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
522 * SquirrelMail header
524 * This Received: header provides information that allows to track
525 * user and machine that was used to send email. Don't remove it
526 * unless you understand all possible forging issues or your
527 * webmail installation does not prevent changes in user's email address.
528 * See SquirrelMail bug tracker #847107 for more details about it.
530 * Add $hide_squirrelmail_header as a candidate for config_local.php
531 * to allow completely hiding SquirrelMail participation in message
532 * processing; This is dangerous, especially if users can modify their
533 * account information, as it makes mapping a sent message back to the
534 * original sender almost impossible.
536 $show_sm_header = ( defined('hide_squirrelmail_header') ?
! hide_squirrelmail_header
: 1 );
538 if ( $show_sm_header ) {
539 if (isset($encode_header_key) &&
540 trim($encode_header_key)!='') {
541 // use encoded headers, if encryption key is set and not empty
542 $header[] = 'X-Squirrel-UserHash: '.OneTimePadEncrypt($username,base64_encode($encode_header_key)).$rn;
543 $header[] = 'X-Squirrel-FromHash: '.OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key)).$rn;
544 if (isset($HTTP_X_FORWARDED_FOR))
545 $header[] = 'X-Squirrel-ProxyHash:'.OneTimePadEncrypt($this->ip2hex($HTTP_X_FORWARDED_FOR),base64_encode($encode_header_key)).$rn;
547 // use default received headers
548 $header[] = "Received: from $received_from" . $rn;
549 if ($edit_identity ||
! isset($hide_auth_header) ||
! $hide_auth_header)
550 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
551 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
552 $header[] = " $date" . $rn;
556 /* Insert the rest of the header fields */
557 $header[] = 'Message-ID: '. $message_id . $rn;
558 if (is_object($reply_rfc822_header) &&
559 isset($reply_rfc822_header->message_id
) &&
560 $reply_rfc822_header->message_id
) {
561 $rep_message_id = $reply_rfc822_header->message_id
;
562 // $this->strip_crlf($message_id);
563 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
564 $references = $this->calculate_references($reply_rfc822_header);
565 $header[] = 'References: '.$references . $rn;
567 $header[] = "Date: $date" . $rn;
568 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
569 $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn;
571 // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>"
573 // Do not use foldLine for that.
575 // RFC2822 if from contains more then 1 address
576 if (count($rfc822_header->from
) > 1) {
577 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
579 if (count($rfc822_header->to
)) {
580 $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn;
582 if (count($rfc822_header->cc
)) {
583 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn;
585 if (count($rfc822_header->reply_to
)) {
586 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
588 /* Sendmail should return true. Default = false */
589 $bcc = $this->getBcc();
590 if (count($rfc822_header->bcc
)) {
591 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn;
593 $raw_length +
= strlen($s);
598 /* Identify SquirrelMail */
599 $header[] = 'User-Agent: SquirrelMail/' . SM_VERSION
. $rn;
600 /* Do the MIME-stuff */
601 $header[] = 'MIME-Version: 1.0' . $rn;
602 $contenttype = 'Content-Type: '. $rfc822_header->content_type
->type0
.'/'.
603 $rfc822_header->content_type
->type1
;
604 if (count($rfc822_header->content_type
->properties
)) {
605 foreach ($rfc822_header->content_type
->properties
as $k => $v) {
607 $contenttype .= ';' .$k.'='.$v;
611 $header[] = $contenttype . $rn;
612 if ($encoding = $rfc822_header->encoding
) {
613 $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn;
615 if (isset($rfc822_header->dnt
) && $rfc822_header->dnt
) {
616 $dnt = $rfc822_header->getAddr_s('dnt');
618 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
620 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
622 if ($rfc822_header->priority
) {
623 switch($rfc822_header->priority
)
626 $header[] = 'X-Priority: 1 (Highest)'.$rn;
627 $header[] = 'Importance: High'. $rn; break;
629 $header[] = 'X-Priority: 5 (Lowest)'.$rn;
630 $header[] = 'Importance: Low'. $rn; break;
634 /* Insert headers from the $more_headers array */
635 if(count($rfc822_header->more_headers
)) {
636 reset($rfc822_header->more_headers
);
637 foreach ($rfc822_header->more_headers
as $k => $v) {
638 $header[] = $k.': '.$v .$rn;
641 $cnt = count($header);
644 for ($i = 0 ; $i < $cnt ; $i++
) {
645 $sKey = substr($header[$i],0,strpos($header[$i],':'));
650 $hdr_s .= $header[$i];
653 $sRefs = substr($header[$i],12);
654 $aRefs = explode(' ',$sRefs);
655 $sLine = 'References:';
656 foreach ($aRefs as $sReference) {
657 if ( trim($sReference) == '' ) {
658 /* Don't add spaces. */
659 } elseif (strlen($sLine)+
strlen($sReference) >76) {
661 $sLine = $rn . ' ' . $sReference;
663 $sLine .= ' '. $sReference;
672 $hdr_s .= $header[$i];
674 default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break;
678 $header .= $rn; /* One blank line to separate header and body */
679 $raw_length +
= strlen($header);
684 * function foldLine - for cleanly folding of headerlines
686 * @param string $line
687 * @param integer $length length to fold the line at
688 * @param string $pre prefix the line with...
690 * @return string $line folded line with trailing CRLF
692 function foldLine($line, $length, $pre='') {
693 $line = substr($line,0, -2);
694 $length -= 2; /* do not fold between \r and \n */
695 $cnt = strlen($line);
696 if ($cnt > $length) { /* try folding */
697 $fold_string = "\r\n " . $pre;
699 $aFoldLine = array();
700 while (strlen($line) > $length) {
702 /* handle encoded parts */
703 if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) {
704 $fold_tmp = $regs[1];
705 if (!trim($regs[5])) {
706 $fold_tmp .= $regs[5];
708 $iPosEnc = strpos($line,$fold_tmp);
709 $iLengthEnc = strlen($fold_tmp);
710 $iPosEncEnd = $iPosEnc+
$iLengthEnc;
711 if ($iPosEnc < $length && (($iPosEncEnd) > $length)) {
713 /* fold just before the start of the encoded string */
715 $aFoldLine[] = substr($line,0,$iPosEnc);
717 $line = substr($line,$iPosEnc);
720 $length -= strlen($fold_string);
722 if ($iLengthEnc > $length) { /* place the encoded
723 string on a separate line and do not fold inside it*/
724 /* minimize foldstring */
725 $fold_string = "\r\n ";
726 $aFoldLine[] = substr($line,0,$iLengthEnc);
727 $line = substr($line,$iLengthEnc);
729 } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */
731 $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
732 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) ||
!preg_match('/[=,;\s]/',$sLineRem)) {
733 /*impossible to fold clean in the next part -> fold after the enc string */
734 $aFoldLine[] = substr($line,0,$iPosEncEnd);
735 $line = substr($line,$iPosEncEnd);
739 $length -= strlen($fold_string);
745 $line_tmp = substr($line,0,$length);
747 /* try to fold at logical places */
750 case ($iFoldPos = strrpos($line_tmp,',')): break;
751 case ($iFoldPos = strrpos($line_tmp,';')): break;
752 case ($iFoldPos = strrpos($line_tmp,' ')): break;
753 case ($iFoldPos = strrpos($line_tmp,'=')): break;
757 if (!$iFoldPos) { /* clean folding didn't work */
760 $aFoldLine[] = substr($line,0,$iFoldPos+
1);
761 $line = substr($line,$iFoldPos+
1);
764 $length -= strlen($fold_string);
768 /*$reconstruct the line */
770 $aFoldLine[] = $line;
772 $line = implode($fold_string,$aFoldLine);
778 * function mimeBoundary - calculates the mime boundary to use
780 * This function will generate a random mime boundary base part
781 * for the message if the boundary has not already been set.
783 * @return string $mimeBoundaryString random mime boundary string
785 function mimeBoundary () {
786 static $mimeBoundaryString;
788 if ( !isset( $mimeBoundaryString ) ||
789 $mimeBoundaryString == '') {
790 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
791 mt_rand( 10000, 99999 );
793 return $mimeBoundaryString;
797 * function timezone - Time offset for correct timezone
799 * @return string $result with timezone and offset
801 function timezone () {
804 $diff_second = date('Z');
806 $diff_second = - $diff_second;
808 if ($diff_second > 0) {
813 $diff_second = abs($diff_second);
814 $diff_hour = floor ($diff_second / 3600);
815 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
816 $zonename = '('.strftime('%Z').')';
817 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
823 * function calculate_references - calculate correct References string
824 * Adds the current message ID, and makes sure it doesn't grow forever,
825 * to that extent it drops message-ID's in a smart way until the string
826 * length is under the recommended value of 1000 ("References: <986>\r\n").
827 * It always keeps the first and the last three ID's.
829 * @param Rfc822Header $hdr message header to calculate from
831 * @return string $refer concatenated and trimmed References string
833 function calculate_references($hdr) {
834 $aReferences = preg_split('/\s+/', $hdr->references
);
835 $message_id = $hdr->message_id
;
836 $in_reply_to = $hdr->in_reply_to
;
838 // if References already exists, add the current message ID at the end.
839 // no References exists; if we know a IRT, add that aswell
840 if (count($aReferences) == 0 && $in_reply_to) {
841 $aReferences[] = $in_reply_to;
843 $aReferences[] = $message_id;
845 // sanitize the array: trim whitespace, remove dupes
846 array_walk($aReferences, 'sq_trim_value');
847 $aReferences = array_unique($aReferences);
849 while ( count($aReferences) > 4 && strlen(implode(' ', $aReferences)) >= 986 ) {
850 $aReferences = array_merge(array_slice($aReferences,0,1),array_slice($aReferences,2));
852 return implode(' ', $aReferences);
856 * Converts ip address to hexadecimal string
858 * Function is used to convert ipv4 and ipv6 addresses to hex strings.
859 * It removes all delimiter symbols from ip addresses, converts decimal
860 * ipv4 numbers to hex and pads strings in order to present full length
861 * address. ipv4 addresses are represented as 8 byte strings, ipv6 addresses
862 * are represented as 32 byte string.
864 * If function fails to detect address format, it returns unprocessed string.
865 * @param string $string ip address string
866 * @return string processed ip address string
867 * @since 1.5.1 and 1.4.5
869 function ip2hex($string) {
870 if (preg_match("/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/",$string,$match)) {
872 $ret = str_pad(dechex($match[1]),2,'0',STR_PAD_LEFT
)
873 . str_pad(dechex($match[2]),2,'0',STR_PAD_LEFT
)
874 . str_pad(dechex($match[3]),2,'0',STR_PAD_LEFT
)
875 . str_pad(dechex($match[4]),2,'0',STR_PAD_LEFT
);
876 } elseif (preg_match("/^([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)$/i",$string,$match)) {
878 $ret = str_pad($match[1],4,'0',STR_PAD_LEFT
)
879 . str_pad($match[2],4,'0',STR_PAD_LEFT
)
880 . str_pad($match[3],4,'0',STR_PAD_LEFT
)
881 . str_pad($match[4],4,'0',STR_PAD_LEFT
)
882 . str_pad($match[5],4,'0',STR_PAD_LEFT
)
883 . str_pad($match[6],4,'0',STR_PAD_LEFT
)
884 . str_pad($match[7],4,'0',STR_PAD_LEFT
)
885 . str_pad($match[8],4,'0',STR_PAD_LEFT
);
886 } elseif (preg_match("/^\:\:([0-9a-h\:]+)$/i",$string,$match)) {
887 // short ipv6 with all starting symbols nulled
888 $aAddr=explode(':',$match[1]);
890 foreach ($aAddr as $addr) {
891 $ret.=str_pad($addr,4,'0',STR_PAD_LEFT
);
893 $ret=str_pad($ret,32,'0',STR_PAD_LEFT
);
894 } elseif (preg_match("/^([0-9a-h\:]+)::([0-9a-h\:]+)$/i",$string,$match)) {
895 // short ipv6 with middle part nulled
896 $aStart=explode(':',$match[1]);
898 foreach($aStart as $addr) {
899 $sStart.=str_pad($addr,4,'0',STR_PAD_LEFT
);
901 $aEnd = explode(':',$match[2]);
903 foreach($aEnd as $addr) {
904 $sEnd.=str_pad($addr,4,'0',STR_PAD_LEFT
);
907 . str_pad('',(32 - strlen($sStart . $sEnd)),'0',STR_PAD_LEFT
)
910 // unknown addressing