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