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