9cf091aaa832f162d9275760679f1333717da648
[squirrelmail.git] / class / deliver / Deliver.class.php
1 <?php
2 /**
3 * Deliver.class.php
4 *
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
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 * @version $Id$
12 * @author Marc Groot Koerkamp
13 * @package squirrelmail
14 */
15
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 */
29 class Deliver {
30
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 *
37 * @return integer $raw_length
38 */
39 function mail($message, $stream=false) {
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;
48 $reply_rfc822_header = (isset($message->reply_rfc822_header)
49 ? $message->reply_rfc822_header : '');
50 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
51
52 if ($stream) {
53 $this->preWriteToStream($header);
54 $this->writeToStream($stream, $header);
55 }
56 $this->writeBody($message, $stream, $raw_length, $boundary);
57 return $raw_length;
58 }
59
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 *
72 * @return void
73 */
74 function writeBody($message, $stream, &$length_raw, $boundary='') {
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 // 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
85 //
86 } else if (strpos($boundary,'_trap_')) {
87 $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_'));
88 }
89 $boundary_new = strrev($boundary . '_part_'.$message->entity_id);
90 } else {
91 $boundary_new = $boundary;
92 }
93 if ($boundary && !$message->rfc822_header) {
94 $s = '--'.$boundary."\r\n";
95 $s .= $this->prepareMIME_Header($message, $boundary_new);
96 $length_raw += strlen($s);
97 if ($stream) {
98 $this->preWriteToStream($s);
99 $this->writeToStream($stream, $s);
100 }
101 }
102 $this->writeBodyPart($message, $stream, $length_raw);
103
104 $last = false;
105 for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) {
106 $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary_new);
107 if ($i == $entCount-1) $last = true;
108 }
109 if ($boundary && $last) {
110 $s = "--".$boundary_new."--\r\n\r\n";
111 $length_raw += strlen($s);
112 if ($stream) {
113 $this->preWriteToStream($s);
114 $this->writeToStream($stream, $s);
115 }
116 }
117 }
118
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 *
129 * @return void
130 */
131 function writeBodyPart($message, $stream, &$length) {
132 if ($message->mime_header) {
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;
145 // remove NUL characters
146 $body_part = str_replace("\0",'',$body_part);
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 } elseif ($message->att_local_name) {
154 $filename = $message->att_local_name;
155 $file = fopen ($filename, 'rb');
156 while ($body_part = fgets($file, 4096)) {
157 // remove NUL characters
158 $body_part = str_replace("\0",'',$body_part);
159 $length += $this->clean_crlf($body_part);
160 if ($stream) {
161 $this->preWriteToStream($body_part);
162 $this->writeToStream($stream, $body_part);
163 }
164 $last = $body_part;
165 }
166 fclose($file);
167 }
168 break;
169 default:
170 if ($message->body_part) {
171 $body_part = $message->body_part;
172 // remove NUL characters
173 $body_part = str_replace("\0",'',$body_part);
174 $length += $this->clean_crlf($body_part);
175 if ($stream) {
176 $this->writeToStream($stream, $body_part);
177 }
178 } elseif ($message->att_local_name) {
179 $filename = $message->att_local_name;
180 $file = fopen ($filename, 'rb');
181 while ($tmp = fread($file, 570)) {
182 $body_part = chunk_split(base64_encode($tmp));
183 $length += $this->clean_crlf($body_part);
184 if ($stream) {
185 $this->writeToStream($stream, $body_part);
186 }
187 }
188 fclose($file);
189 }
190 break;
191 }
192 $body_part_trailing = '';
193 if ($last && substr($last,-1) != "\n") {
194 $body_part_trailing = "\r\n";
195 }
196 if ($body_part_trailing) {
197 $length += strlen($body_part_trailing);
198 if ($stream) {
199 $this->preWriteToStream($body_part_trailing);
200 $this->writeToStream($stream, $body_part_trailing);
201 }
202 }
203 }
204
205 /**
206 * function clean_crlf - change linefeeds and newlines to legal characters
207 *
208 * The SMTP format only allows CRLF as line terminators.
209 * This function replaces illegal teminators with the correct terminator.
210 *
211 * @param string &$s string to clean linefeeds on
212 *
213 * @return void
214 */
215 function clean_crlf(&$s) {
216 $s = str_replace("\r\n", "\n", $s);
217 $s = str_replace("\r", "\n", $s);
218 $s = str_replace("\n", "\r\n", $s);
219 return strlen($s);
220 }
221
222 /**
223 * function strip_crlf - strip linefeeds and newlines from a string
224 *
225 * The SMTP format only allows CRLF as line terminators.
226 * This function strips all line terminators from the string.
227 *
228 * @param string &$s string to clean linefeeds on
229 *
230 * @return void
231 */
232 function strip_crlf(&$s) {
233 $s = str_replace("\r\n ", '', $s);
234 $s = str_replace("\r", '', $s);
235 $s = str_replace("\n", '', $s);
236 }
237
238 /**
239 * function preWriteToStream - reserved for extended functionality
240 *
241 * This function is not yet implemented.
242 * Reserved for extended functionality.
243 *
244 * @param string &$s string to operate on
245 *
246 * @return void
247 */
248 function preWriteToStream(&$s) {
249 }
250
251 /**
252 * function writeToStream - write data to the SMTP stream
253 *
254 * @param resource $stream SMTP output stream
255 * @param string $data string with data to send to the SMTP stream
256 *
257 * @return void
258 */
259 function writeToStream($stream, $data) {
260 fputs($stream, $data);
261 }
262
263 /**
264 * function initStream - reserved for extended functionality
265 *
266 * This function is not yet implemented.
267 * Reserved for extended functionality.
268 *
269 * @param Message $message Message object
270 * @param string $host host name or IP to connect to
271 * @param string $user username to log into the SMTP server with
272 * @param string $pass password to log into the SMTP server with
273 * @param integer $length
274 *
275 * @return handle $stream file handle resource to SMTP stream
276 */
277 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
278 return $stream;
279 }
280
281 /**
282 * function getBCC - reserved for extended functionality
283 *
284 * This function is not yet implemented.
285 * Reserved for extended functionality.
286 *
287 */
288 function getBCC() {
289 return false;
290 }
291
292 /**
293 * function prepareMIME_Header - creates the mime header
294 *
295 * @param Message $message Message object to act on
296 * @param string $boundary mime boundary from fn MimeBoundary
297 *
298 * @return string $header properly formatted mime header
299 */
300 function prepareMIME_Header($message, $boundary) {
301 $mime_header = $message->mime_header;
302 $rn="\r\n";
303 $header = array();
304
305 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
306 $mime_header->type1;
307 if (count($message->entities)) {
308 $contenttype .= ';' . 'boundary="'.$boundary.'"';
309 }
310 if (isset($mime_header->parameters['name'])) {
311 $contenttype .= '; name="'.
312 encodeHeader($mime_header->parameters['name']). '"';
313 }
314 if (isset($mime_header->parameters['charset'])) {
315 $charset = $mime_header->parameters['charset'];
316 $contenttype .= '; charset="'.
317 encodeHeader($charset). '"';
318 }
319
320 $header[] = $contenttype . $rn;
321 if ($mime_header->description) {
322 $header[] = 'Content-Description: ' . $mime_header->description . $rn;
323 }
324 if ($mime_header->encoding) {
325 $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
326 } else {
327 if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') {
328 $header[] = 'Content-Transfer-Encoding: 8bit' . $rn;
329 } else {
330 $header[] = 'Content-Transfer-Encoding: base64' . $rn;
331 }
332 }
333 if ($mime_header->id) {
334 $header[] = 'Content-ID: ' . $mime_header->id . $rn;
335 }
336 if ($mime_header->disposition) {
337 $disposition = $mime_header->disposition;
338 $contentdisp = 'Content-Disposition: ' . $disposition->name;
339 if ($disposition->getProperty('filename')) {
340 $contentdisp .= '; filename="'.
341 encodeHeader($disposition->getProperty('filename')). '"';
342 }
343 $header[] = $contentdisp . $rn;
344 }
345 if ($mime_header->md5) {
346 $header[] = 'Content-MD5: ' . $mime_header->md5 . $rn;
347 }
348 if ($mime_header->language) {
349 $header[] = 'Content-Language: ' . $mime_header->language . $rn;
350 }
351
352 $cnt = count($header);
353 $hdr_s = '';
354 for ($i = 0 ; $i < $cnt ; $i++) {
355 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
356 }
357 $header = $hdr_s;
358 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
359 return $header;
360 }
361
362 /**
363 * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s)
364 *
365 * This function takes the Rfc822Header object(s) and formats them
366 * into the RFC822Header string to send to the SMTP server as part
367 * of the SMTP message.
368 *
369 * @param Rfc822Header $rfc822_header
370 * @param Rfc822Header $reply_rfc822_header
371 * @param integer &$raw_length length of the message
372 *
373 * @return string $header
374 */
375 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
376 global $domain, $version, $username, $encode_header_key, $edit_identity, $hide_auth_header;
377
378 /* if server var SERVER_NAME not available, use $domain */
379 if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) {
380 $SERVER_NAME = $domain;
381 }
382
383 sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER);
384 sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER);
385 sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER);
386 sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER);
387 sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER);
388
389 $rn = "\r\n";
390
391 /* This creates an RFC 822 date */
392 $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone();
393 /* Create a message-id */
394 $message_id = '<' . $REMOTE_PORT . '.';
395 if (isset($encode_header_key) && trim($encode_header_key)!='') {
396 // use encrypted form of remote address
397 $message_id.= OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key));
398 } else {
399 $message_id.= $REMOTE_ADDR;
400 }
401 $message_id .= '.' . time() . '.squirrel@' . $SERVER_NAME .'>';
402 /* Make an RFC822 Received: line */
403 if (isset($REMOTE_HOST)) {
404 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
405 } else {
406 $received_from = $REMOTE_ADDR;
407 }
408 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
409 if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') {
410 $HTTP_X_FORWARDED_FOR = 'unknown';
411 }
412 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
413 }
414 $header = array();
415
416 /**
417 * SquirrelMail header
418 *
419 * This Received: header provides information that allows to track
420 * user and machine that was used to send email. Don't remove it
421 * unless you understand all possible forging issues or your
422 * webmail installation does not prevent changes in user's email address.
423 * See SquirrelMail bug tracker #847107 for more details about it.
424 */
425 if (isset($encode_header_key) &&
426 trim($encode_header_key)!='') {
427 // use encoded headers, if encryption key is set and not empty
428 $header[].= 'X-Squirrel-UserHash: '.OneTimePadEncrypt($username,base64_encode($encode_header_key)).$rn;
429 $header[].= 'X-Squirrel-FromHash: '.OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key)).$rn;
430 if (isset($HTTP_X_FORWARDED_FOR))
431 $header[].= 'X-Squirrel-ProxyHash:'.OneTimePadEncrypt($this->ip2hex($HTTP_X_FORWARDED_FOR),base64_encode($encode_header_key)).$rn;
432 } else {
433 // use default received headers
434 $header[] = "Received: from $received_from" . $rn;
435 if ($edit_identity || ! isset($hide_auth_header) || ! $hide_auth_header)
436 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
437 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
438 $header[] = " $date" . $rn;
439 }
440
441 /* Insert the rest of the header fields */
442 $header[] = 'Message-ID: '. $message_id . $rn;
443 if ($reply_rfc822_header->message_id) {
444 $rep_message_id = $reply_rfc822_header->message_id;
445 // $this->strip_crlf($message_id);
446 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
447 $references = $this->calculate_references($reply_rfc822_header);
448 $header[] = 'References: '.$references . $rn;
449 }
450 $header[] = "Date: $date" . $rn;
451 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
452 $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn;
453
454 // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>" as delimiter
455 // Do not use foldLine for that.
456
457 // RFC2822 if from contains more then 1 address
458 if (count($rfc822_header->from) > 1) {
459 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
460 }
461 if (count($rfc822_header->to)) {
462 $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn;
463 }
464 if (count($rfc822_header->cc)) {
465 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn;
466 }
467 if (count($rfc822_header->reply_to)) {
468 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
469 }
470 /* Sendmail should return true. Default = false */
471 $bcc = $this->getBcc();
472 if (count($rfc822_header->bcc)) {
473 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn;
474 if (!$bcc) {
475 $raw_length += strlen($s);
476 } else {
477 $header[] = $s;
478 }
479 }
480 /* Identify SquirrelMail */
481 $header[] = 'User-Agent: SquirrelMail/' . $version . $rn;
482 /* Do the MIME-stuff */
483 $header[] = 'MIME-Version: 1.0' . $rn;
484 $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
485 $rfc822_header->content_type->type1;
486 if (count($rfc822_header->content_type->properties)) {
487 foreach ($rfc822_header->content_type->properties as $k => $v) {
488 if ($k && $v) {
489 $contenttype .= ';' .$k.'='.$v;
490 }
491 }
492 }
493 $header[] = $contenttype . $rn;
494 if ($encoding = $rfc822_header->encoding) {
495 $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn;
496 }
497 if ($rfc822_header->dnt) {
498 $dnt = $rfc822_header->getAddr_s('dnt');
499 /* Pegasus Mail */
500 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
501 /* RFC 2298 */
502 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
503 }
504 if ($rfc822_header->priority) {
505 switch($rfc822_header->priority)
506 {
507 case 1:
508 $header[] = 'X-Priority: 1 (Highest)'.$rn;
509 $header[] = 'Importance: High'. $rn; break;
510 case 5:
511 $header[] = 'X-Priority: 5 (Lowest)'.$rn;
512 $header[] = 'Importance: Low'. $rn; break;
513 default: break;
514 }
515 }
516 /* Insert headers from the $more_headers array */
517 if(count($rfc822_header->more_headers)) {
518 reset($rfc822_header->more_headers);
519 foreach ($rfc822_header->more_headers as $k => $v) {
520 $header[] = $k.': '.$v .$rn;
521 }
522 }
523 $cnt = count($header);
524 $hdr_s = '';
525
526 for ($i = 0 ; $i < $cnt ; $i++) {
527 $sKey = substr($header[$i],0,strpos($header[$i],':'));
528 switch ($sKey)
529 {
530 case 'Message-ID':
531 case 'In-Reply_To':
532 $hdr_s .= $header[$i];
533 break;
534 case 'References':
535 $sRefs = substr($header[$i],12);
536 $aRefs = explode(' ',$sRefs);
537 $sLine = 'References:';
538 foreach ($aRefs as $sReference) {
539 if (strlen($sLine)+strlen($sReference) >76) {
540 $hdr_s .= $sLine;
541 $sLine = $rn . ' ' . $sReference;
542 } else {
543 $sLine .= ' '. $sReference;
544 }
545 }
546 $hdr_s .= $sLine;
547 break;
548 case 'To':
549 case 'Cc':
550 case 'Bcc':
551 case 'From':
552 $hdr_s .= $header[$i];
553 break;
554 default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break;
555 }
556 }
557 $header = $hdr_s;
558 $header .= $rn; /* One blank line to separate header and body */
559 $raw_length += strlen($header);
560 return $header;
561 }
562
563 /**
564 * function foldLine - for cleanly folding of headerlines
565 *
566 * @param string $line
567 * @param integer $length length to fold the line at
568 * @param string $pre prefix the line with...
569 *
570 * @return string $line folded line with trailing CRLF
571 */
572 function foldLine($line, $length, $pre='') {
573 $line = substr($line,0, -2);
574 $length -= 2; /* do not fold between \r and \n */
575 $cnt = strlen($line);
576 if ($cnt > $length) { /* try folding */
577 $fold_string = "\r\n " . $pre;
578 $bFirstFold = false;
579 $aFoldLine = array();
580 while (strlen($line) > $length) {
581 $fold = false;
582 /* handle encoded parts */
583 if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) {
584 $fold_tmp = $regs[1];
585 if (!trim($regs[5])) {
586 $fold_tmp .= $regs[5];
587 }
588 $iPosEnc = strpos($line,$fold_tmp);
589 $iLengthEnc = strlen($fold_tmp);
590 $iPosEncEnd = $iPosEnc+$iLengthEnc;
591 if ($iPosEnc < $length && (($iPosEncEnd) > $length)) {
592 $fold = true;
593 /* fold just before the start of the encoded string */
594 if ($iPosEnc) {
595 $aFoldLine[] = substr($line,0,$iPosEnc);
596 }
597 $line = substr($line,$iPosEnc);
598 if (!$bFirstFold) {
599 $bFirstFold = true;
600 $length -= strlen($fold_string);
601 }
602 if ($iLengthEnc > $length) { /* place the encoded
603 string on a separate line and do not fold inside it*/
604 /* minimize foldstring */
605 $fold_string = "\r\n ";
606 $aFoldLine[] = substr($line,0,$iLengthEnc);
607 $line = substr($line,$iLengthEnc);
608 }
609 } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */
610 /*remainder */
611 $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
612 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) || !preg_match('/[=,;\s]/',$sLineRem)) {
613 /*impossible to fold clean in the next part -> fold after the enc string */
614 $aFoldLine[] = substr($line,0,$iPosEncEnd);
615 $line = substr($line,$iPosEncEnd);
616 $fold = true;
617 if (!$bFirstFold) {
618 $bFirstFold = true;
619 $length -= strlen($fold_string);
620 }
621 }
622 }
623 }
624 if (!$fold) {
625 $line_tmp = substr($line,0,$length);
626 $iFoldPos = false;
627 /* try to fold at logical places */
628 switch (true)
629 {
630 case ($iFoldPos = strrpos($line_tmp,',')): break;
631 case ($iFoldPos = strrpos($line_tmp,';')): break;
632 case ($iFoldPos = strrpos($line_tmp,' ')): break;
633 case ($iFoldPos = strrpos($line_tmp,'=')): break;
634 default: break;
635 }
636
637 if (!$iFoldPos) { /* clean folding didn't work */
638 $iFoldPos = $length;
639 }
640 $aFoldLine[] = substr($line,0,$iFoldPos+1);
641 $line = substr($line,$iFoldPos+1);
642 if (!$bFirstFold) {
643 $bFirstFold = true;
644 $length -= strlen($fold_string);
645 }
646 }
647 }
648 /*$reconstruct the line */
649 if ($line) {
650 $aFoldLine[] = $line;
651 }
652 $line = implode($fold_string,$aFoldLine);
653 }
654 return $line."\r\n";
655 }
656
657 /**
658 * function mimeBoundary - calculates the mime boundary to use
659 *
660 * This function will generate a random mime boundary base part
661 * for the message if the boundary has not already been set.
662 *
663 * @return string $mimeBoundaryString random mime boundary string
664 */
665 function mimeBoundary () {
666 static $mimeBoundaryString;
667
668 if ( !isset( $mimeBoundaryString ) ||
669 $mimeBoundaryString == '') {
670 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
671 mt_rand( 10000, 99999 );
672 }
673 return $mimeBoundaryString;
674 }
675
676 /**
677 * function timezone - Time offset for correct timezone
678 *
679 * @return string $result with timezone and offset
680 */
681 function timezone () {
682 global $invert_time;
683
684 $diff_second = date('Z');
685 if ($invert_time) {
686 $diff_second = - $diff_second;
687 }
688 if ($diff_second > 0) {
689 $sign = '+';
690 } else {
691 $sign = '-';
692 }
693 $diff_second = abs($diff_second);
694 $diff_hour = floor ($diff_second / 3600);
695 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
696 $zonename = '('.strftime('%Z').')';
697 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
698 $zonename);
699 return ($result);
700 }
701
702 /**
703 * function calculate_references - calculate correct Referer string
704 *
705 * @param Rfc822Header $hdr message header to calculate from
706 *
707 * @return string $refer concatenated and trimmed Referer string
708 */
709 function calculate_references($hdr) {
710 $refer = $hdr->references;
711 $message_id = $hdr->message_id;
712 $in_reply_to = $hdr->in_reply_to;
713 if (strlen($refer) > 2) {
714 $refer .= ' ' . $message_id;
715 } else {
716 if ($in_reply_to) {
717 $refer .= $in_reply_to . ' ' . $message_id;
718 } else {
719 $refer .= $message_id;
720 }
721 }
722 trim($refer);
723 return $refer;
724 }
725
726 /**
727 * Converts ip address to hexadecimal string
728 *
729 * Function is used to convert ipv4 and ipv6 addresses to hex strings.
730 * It removes all delimiter symbols from ip addresses, converts decimal
731 * ipv4 numbers to hex and pads strings in order to present full length
732 * address. ipv4 addresses are represented as 8 byte strings, ipv6 addresses
733 * are represented as 32 byte string.
734 *
735 * If function fails to detect address format, it returns unprocessed string.
736 * @param string $string ip address string
737 * @return string processed ip address string
738 * @since 1.5.1
739 */
740 function ip2hex($string) {
741 if (preg_match("/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/",$string,$match)) {
742 // ipv4 address
743 $ret = str_pad(dechex($match[1]),2,'0',STR_PAD_LEFT)
744 . str_pad(dechex($match[2]),2,'0',STR_PAD_LEFT)
745 . str_pad(dechex($match[3]),2,'0',STR_PAD_LEFT)
746 . str_pad(dechex($match[4]),2,'0',STR_PAD_LEFT);
747 } 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)) {
748 // full ipv6 address
749 $ret = str_pad($match[1],4,'0',STR_PAD_LEFT)
750 . str_pad($match[2],4,'0',STR_PAD_LEFT)
751 . str_pad($match[3],4,'0',STR_PAD_LEFT)
752 . str_pad($match[4],4,'0',STR_PAD_LEFT)
753 . str_pad($match[5],4,'0',STR_PAD_LEFT)
754 . str_pad($match[6],4,'0',STR_PAD_LEFT)
755 . str_pad($match[7],4,'0',STR_PAD_LEFT)
756 . str_pad($match[8],4,'0',STR_PAD_LEFT);
757 } elseif (preg_match("/^\:\:([0-9a-h\:]+)$/i",$string,$match)) {
758 // short ipv6 with all starting symbols nulled
759 $aAddr=explode(':',$match[1]);
760 $ret='';
761 foreach ($aAddr as $addr) {
762 $ret.=str_pad($addr,4,'0',STR_PAD_LEFT);
763 }
764 $ret=str_pad($ret,32,'0',STR_PAD_LEFT);
765 } elseif (preg_match("/^([0-9a-h\:]+)::([0-9a-h\:]+)$/i",$string,$match)) {
766 // short ipv6 with middle part nulled
767 $aStart=explode(':',$match[1]);
768 $sStart='';
769 foreach($aStart as $addr) {
770 $sStart.=str_pad($addr,4,'0',STR_PAD_LEFT);
771 }
772 $aEnd = explode(':',$match[2]);
773 $sEnd='';
774 foreach($aEnd as $addr) {
775 $sEnd.=str_pad($addr,4,'0',STR_PAD_LEFT);
776 }
777 $ret = $sStart
778 . str_pad('',(32 - strlen($sStart . $sEnd)),'0',STR_PAD_LEFT)
779 . $sEnd;
780 } else {
781 // unknown addressing
782 $ret = $string;
783 }
784 return $ret;
785 }
786 }
787 ?>