6 * This contains all the functions needed to send messages through
9 * @author Marc Groot Koerkamp
10 * @copyright © 1999-2006 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 class to send
35 * @param resource $stream file handle to the SMTP stream
37 * @return integer $raw_length
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.'"';
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);
53 $this->preWriteToStream($header);
54 $this->writeToStream($stream, $header);
56 $this->writeBody($message, $stream, $raw_length, $boundary);
61 * function writeBody - generate and write the mime boundaries around each part to the stream
63 * Recursively formats and writes the MIME boundaries of the $message
64 * to the output stream.
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
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_'));
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
86 } else if (strpos($boundary,'_trap_')) {
87 $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_'));
89 $boundary_new = strrev($boundary . '_part_'.$message->entity_id
);
91 $boundary_new = $boundary;
93 if ($boundary && !$message->rfc822_header
) {
94 $s = '--'.$boundary."\r\n";
95 $s .= $this->prepareMIME_Header($message, $boundary_new);
96 $length_raw +
= strlen($s);
98 $this->preWriteToStream($s);
99 $this->writeToStream($stream, $s);
102 $this->writeBodyPart($message, $stream, $length_raw);
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;
109 if ($boundary && $last) {
110 $s = "--".$boundary_new."--\r\n\r\n";
111 $length_raw +
= strlen($s);
113 $this->preWriteToStream($s);
114 $this->writeToStream($stream, $s);
120 * function writeBodyPart - write each individual mimepart
122 * Recursively called by WriteBody to write each mime part to the SMTP stream
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
131 function writeBodyPart($message, $stream, &$length) {
132 if ($message->mime_header
) {
133 $type0 = $message->mime_header
->type0
;
135 $type0 = $message->rfc822_header
->content_type
->type0
;
138 $body_part_trailing = $last = '';
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);
149 $this->preWriteToStream($body_part);
150 $this->writeToStream($stream, $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);
161 $this->preWriteToStream($body_part);
162 $this->writeToStream($stream, $body_part);
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);
176 $this->writeToStream($stream, $body_part);
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 // Up to 4.3.10 chunk_split always appends a newline,
184 // while in 4.3.11 it doesn't if the string to split
185 // is shorter than the chunk length.
186 if( substr($body_part, -1 , 1 ) != "\n" )
188 $length +
= $this->clean_crlf($body_part);
190 $this->writeToStream($stream, $body_part);
197 $body_part_trailing = '';
198 if ($last && substr($last,-1) != "\n") {
199 $body_part_trailing = "\r\n";
201 if ($body_part_trailing) {
202 $length +
= strlen($body_part_trailing);
204 $this->preWriteToStream($body_part_trailing);
205 $this->writeToStream($stream, $body_part_trailing);
211 * function clean_crlf - change linefeeds and newlines to legal characters
213 * The SMTP format only allows CRLF as line terminators.
214 * This function replaces illegal teminators with the correct terminator.
216 * @param string &$s string to clean linefeeds on
220 function clean_crlf(&$s) {
221 $s = str_replace("\r\n", "\n", $s);
222 $s = str_replace("\r", "\n", $s);
223 $s = str_replace("\n", "\r\n", $s);
228 * function strip_crlf - strip linefeeds and newlines from a string
230 * The SMTP format only allows CRLF as line terminators.
231 * This function strips all line terminators from the string.
233 * @param string &$s string to clean linefeeds on
237 function strip_crlf(&$s) {
238 $s = str_replace("\r\n ", '', $s);
239 $s = str_replace("\r", '', $s);
240 $s = str_replace("\n", '', $s);
244 * function preWriteToStream - reserved for extended functionality
246 * This function is not yet implemented.
247 * Reserved for extended functionality.
249 * @param string &$s string to operate on
253 function preWriteToStream(&$s) {
257 * function writeToStream - write data to the SMTP stream
259 * @param resource $stream SMTP output stream
260 * @param string $data string with data to send to the SMTP stream
264 function writeToStream($stream, $data) {
265 fputs($stream, $data);
269 * function initStream - reserved for extended functionality
271 * This function is not yet implemented.
272 * Reserved for extended functionality.
274 * @param Message $message Message object
275 * @param string $host host name or IP to connect to
276 * @param string $user username to log into the SMTP server with
277 * @param string $pass password to log into the SMTP server with
278 * @param integer $length
280 * @return handle $stream file handle resource to SMTP stream
282 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
287 * function getBCC - reserved for extended functionality
289 * This function is not yet implemented.
290 * Reserved for extended functionality.
298 * function prepareMIME_Header - creates the mime header
300 * @param Message $message Message object to act on
301 * @param string $boundary mime boundary from fn MimeBoundary
303 * @return string $header properly formatted mime header
305 function prepareMIME_Header($message, $boundary) {
306 $mime_header = $message->mime_header
;
310 $contenttype = 'Content-Type: '. $mime_header->type0
.'/'.
312 if (count($message->entities
)) {
313 $contenttype .= ';' . 'boundary="'.$boundary.'"';
315 if (isset($mime_header->parameters
['name'])) {
316 $contenttype .= '; name="'.
317 encodeHeader($mime_header->parameters
['name']). '"';
319 if (isset($mime_header->parameters
['charset'])) {
320 $charset = $mime_header->parameters
['charset'];
321 $contenttype .= '; charset="'.
322 encodeHeader($charset). '"';
325 $header[] = $contenttype . $rn;
326 if ($mime_header->description
) {
327 $header[] = 'Content-Description: ' . $mime_header->description
. $rn;
329 if ($mime_header->encoding
) {
330 $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding
. $rn;
332 if ($mime_header->type0
== 'text' ||
$mime_header->type0
== 'message') {
333 $header[] = 'Content-Transfer-Encoding: 8bit' . $rn;
335 $header[] = 'Content-Transfer-Encoding: base64' . $rn;
338 if ($mime_header->id
) {
339 $header[] = 'Content-ID: ' . $mime_header->id
. $rn;
341 if ($mime_header->disposition
) {
342 $disposition = $mime_header->disposition
;
343 $contentdisp = 'Content-Disposition: ' . $disposition->name
;
344 if ($disposition->getProperty('filename')) {
345 $contentdisp .= '; filename="'.
346 encodeHeader($disposition->getProperty('filename')). '"';
348 $header[] = $contentdisp . $rn;
350 if ($mime_header->md5
) {
351 $header[] = 'Content-MD5: ' . $mime_header->md5
. $rn;
353 if ($mime_header->language
) {
354 $header[] = 'Content-Language: ' . $mime_header->language
. $rn;
357 $cnt = count($header);
359 for ($i = 0 ; $i < $cnt ; $i++
) {
360 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
363 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
368 * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s)
370 * This function takes the Rfc822Header object(s) and formats them
371 * into the RFC822Header string to send to the SMTP server as part
372 * of the SMTP message.
374 * @param Rfc822Header $rfc822_header
375 * @param Rfc822Header $reply_rfc822_header
376 * @param integer &$raw_length length of the message
378 * @return string $header
380 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
381 global $domain, $version, $username, $encode_header_key,
382 $edit_identity, $hide_auth_header;
384 /* if server var SERVER_NAME not available, use $domain */
385 if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER
)) {
386 $SERVER_NAME = $domain;
389 sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER
);
390 sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER
);
391 sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER
);
392 sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER
);
393 sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER
);
397 /* This creates an RFC 822 date */
398 $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone();
399 /* Create a message-id */
400 $message_id = '<' . $REMOTE_PORT . '.';
401 if (isset($encode_header_key) && trim($encode_header_key)!='') {
402 // use encrypted form of remote address
403 $message_id.= OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key));
405 $message_id.= $REMOTE_ADDR;
407 $message_id .= '.' . time() . '.squirrel@' . $SERVER_NAME .'>';
408 /* Make an RFC822 Received: line */
409 if (isset($REMOTE_HOST)) {
410 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
412 $received_from = $REMOTE_ADDR;
414 if (isset($HTTP_VIA) ||
isset ($HTTP_X_FORWARDED_FOR)) {
415 if (!isset($HTTP_X_FORWARDED_FOR) ||
$HTTP_X_FORWARDED_FOR == '') {
416 $HTTP_X_FORWARDED_FOR = 'unknown';
418 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
423 * SquirrelMail header
425 * This Received: header provides information that allows to track
426 * user and machine that was used to send email. Don't remove it
427 * unless you understand all possible forging issues or your
428 * webmail installation does not prevent changes in user's email address.
429 * See SquirrelMail bug tracker #847107 for more details about it.
431 * Add $hide_squirrelmail_header as a candidate for config_local.php
432 * to allow completely hiding SquirrelMail participation in message
433 * processing; This is dangerous, especially if users can modify their
434 * account information, as it makes mapping a sent message back to the
435 * original sender almost impossible.
437 $show_sm_header = ( defined('hide_squirrelmail_header') ?
! hide_squirrelmail_header
: 1 );
439 if ( $show_sm_header ) {
440 if (isset($encode_header_key) &&
441 trim($encode_header_key)!='') {
442 // use encoded headers, if encryption key is set and not empty
443 $header[] = 'X-Squirrel-UserHash: '.OneTimePadEncrypt($username,base64_encode($encode_header_key)).$rn;
444 $header[] = 'X-Squirrel-FromHash: '.OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key)).$rn;
445 if (isset($HTTP_X_FORWARDED_FOR))
446 $header[] = 'X-Squirrel-ProxyHash:'.OneTimePadEncrypt($this->ip2hex($HTTP_X_FORWARDED_FOR),base64_encode($encode_header_key)).$rn;
448 // use default received headers
449 $header[] = "Received: from $received_from" . $rn;
450 if ($edit_identity ||
! isset($hide_auth_header) ||
! $hide_auth_header)
451 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
452 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
453 $header[] = " $date" . $rn;
457 /* Insert the rest of the header fields */
458 $header[] = 'Message-ID: '. $message_id . $rn;
459 if (is_object($reply_rfc822_header) &&
460 isset($reply_rfc822_header->message_id
) &&
461 $reply_rfc822_header->message_id
) {
462 $rep_message_id = $reply_rfc822_header->message_id
;
463 // $this->strip_crlf($message_id);
464 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
465 $references = $this->calculate_references($reply_rfc822_header);
466 $header[] = 'References: '.$references . $rn;
468 $header[] = "Date: $date" . $rn;
469 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject
) . $rn;
470 $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn;
472 // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>"
474 // Do not use foldLine for that.
476 // RFC2822 if from contains more then 1 address
477 if (count($rfc822_header->from
) > 1) {
478 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
480 if (count($rfc822_header->to
)) {
481 $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn;
483 if (count($rfc822_header->cc
)) {
484 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn;
486 if (count($rfc822_header->reply_to
)) {
487 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
489 /* Sendmail should return true. Default = false */
490 $bcc = $this->getBcc();
491 if (count($rfc822_header->bcc
)) {
492 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn;
494 $raw_length +
= strlen($s);
499 /* Identify SquirrelMail */
500 $header[] = 'User-Agent: SquirrelMail/' . $version . $rn;
501 /* Do the MIME-stuff */
502 $header[] = 'MIME-Version: 1.0' . $rn;
503 $contenttype = 'Content-Type: '. $rfc822_header->content_type
->type0
.'/'.
504 $rfc822_header->content_type
->type1
;
505 if (count($rfc822_header->content_type
->properties
)) {
506 foreach ($rfc822_header->content_type
->properties
as $k => $v) {
508 $contenttype .= ';' .$k.'='.$v;
512 $header[] = $contenttype . $rn;
513 if ($encoding = $rfc822_header->encoding
) {
514 $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn;
516 if ($rfc822_header->dnt
) {
517 $dnt = $rfc822_header->getAddr_s('dnt');
519 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
521 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
523 if ($rfc822_header->priority
) {
524 switch($rfc822_header->priority
)
527 $header[] = 'X-Priority: 1 (Highest)'.$rn;
528 $header[] = 'Importance: High'. $rn; break;
530 $header[] = 'X-Priority: 5 (Lowest)'.$rn;
531 $header[] = 'Importance: Low'. $rn; break;
535 /* Insert headers from the $more_headers array */
536 if(count($rfc822_header->more_headers
)) {
537 reset($rfc822_header->more_headers
);
538 foreach ($rfc822_header->more_headers
as $k => $v) {
539 $header[] = $k.': '.$v .$rn;
542 $cnt = count($header);
545 for ($i = 0 ; $i < $cnt ; $i++
) {
546 $sKey = substr($header[$i],0,strpos($header[$i],':'));
551 $hdr_s .= $header[$i];
554 $sRefs = substr($header[$i],12);
555 $aRefs = explode(' ',$sRefs);
556 $sLine = 'References:';
557 foreach ($aRefs as $sReference) {
558 if (strlen($sLine)+
strlen($sReference) >76) {
560 $sLine = $rn . ' ' . $sReference;
562 $sLine .= ' '. $sReference;
571 $hdr_s .= $header[$i];
573 default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break;
577 $header .= $rn; /* One blank line to separate header and body */
578 $raw_length +
= strlen($header);
583 * function foldLine - for cleanly folding of headerlines
585 * @param string $line
586 * @param integer $length length to fold the line at
587 * @param string $pre prefix the line with...
589 * @return string $line folded line with trailing CRLF
591 function foldLine($line, $length, $pre='') {
592 $line = substr($line,0, -2);
593 $length -= 2; /* do not fold between \r and \n */
594 $cnt = strlen($line);
595 if ($cnt > $length) { /* try folding */
596 $fold_string = "\r\n " . $pre;
598 $aFoldLine = array();
599 while (strlen($line) > $length) {
601 /* handle encoded parts */
602 if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) {
603 $fold_tmp = $regs[1];
604 if (!trim($regs[5])) {
605 $fold_tmp .= $regs[5];
607 $iPosEnc = strpos($line,$fold_tmp);
608 $iLengthEnc = strlen($fold_tmp);
609 $iPosEncEnd = $iPosEnc+
$iLengthEnc;
610 if ($iPosEnc < $length && (($iPosEncEnd) > $length)) {
612 /* fold just before the start of the encoded string */
614 $aFoldLine[] = substr($line,0,$iPosEnc);
616 $line = substr($line,$iPosEnc);
619 $length -= strlen($fold_string);
621 if ($iLengthEnc > $length) { /* place the encoded
622 string on a separate line and do not fold inside it*/
623 /* minimize foldstring */
624 $fold_string = "\r\n ";
625 $aFoldLine[] = substr($line,0,$iLengthEnc);
626 $line = substr($line,$iLengthEnc);
628 } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */
630 $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
631 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) ||
!preg_match('/[=,;\s]/',$sLineRem)) {
632 /*impossible to fold clean in the next part -> fold after the enc string */
633 $aFoldLine[] = substr($line,0,$iPosEncEnd);
634 $line = substr($line,$iPosEncEnd);
638 $length -= strlen($fold_string);
644 $line_tmp = substr($line,0,$length);
646 /* try to fold at logical places */
649 case ($iFoldPos = strrpos($line_tmp,',')): break;
650 case ($iFoldPos = strrpos($line_tmp,';')): break;
651 case ($iFoldPos = strrpos($line_tmp,' ')): break;
652 case ($iFoldPos = strrpos($line_tmp,'=')): break;
656 if (!$iFoldPos) { /* clean folding didn't work */
659 $aFoldLine[] = substr($line,0,$iFoldPos+
1);
660 $line = substr($line,$iFoldPos+
1);
663 $length -= strlen($fold_string);
667 /*$reconstruct the line */
669 $aFoldLine[] = $line;
671 $line = implode($fold_string,$aFoldLine);
677 * function mimeBoundary - calculates the mime boundary to use
679 * This function will generate a random mime boundary base part
680 * for the message if the boundary has not already been set.
682 * @return string $mimeBoundaryString random mime boundary string
684 function mimeBoundary () {
685 static $mimeBoundaryString;
687 if ( !isset( $mimeBoundaryString ) ||
688 $mimeBoundaryString == '') {
689 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
690 mt_rand( 10000, 99999 );
692 return $mimeBoundaryString;
696 * function timezone - Time offset for correct timezone
698 * @return string $result with timezone and offset
700 function timezone () {
703 $diff_second = date('Z');
705 $diff_second = - $diff_second;
707 if ($diff_second > 0) {
712 $diff_second = abs($diff_second);
713 $diff_hour = floor ($diff_second / 3600);
714 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
715 $zonename = '('.strftime('%Z').')';
716 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
722 * function calculate_references - calculate correct References string
723 * Adds the current message ID, and makes sure it doesn't grow forever,
724 * to that extent it drops message-ID's in a smart way until the string
725 * length is under the recommended value of 1000 ("References: <986>\r\n").
726 * It always keeps the first and the last three ID's.
728 * @param Rfc822Header $hdr message header to calculate from
730 * @return string $refer concatenated and trimmed References string
732 function calculate_references($hdr) {
733 $aReferences = preg_split('/\s+/', $hdr->references
);
734 $message_id = $hdr->message_id
;
735 $in_reply_to = $hdr->in_reply_to
;
737 // if References already exists, add the current message ID at the end.
738 // no References exists; if we know a IRT, add that aswell
739 if (count($aReferences) == 0 && $in_reply_to) {
740 $aReferences[] = $in_reply_to;
742 $aReferences[] = $message_id;
744 // sanitize the array: trim whitespace, remove dupes
745 array_walk($aReferences, 'trim_value');
746 $aReferences = array_unique($aReferences);
748 while ( count($aReferences) > 4 && strlen(implode(' ', $aReferences)) >= 986 ) {
749 $aReferences = array_merge(array_slice($aReferences,0,1),array_slice($aReferences,2));
751 return implode(' ', $aReferences);
755 * Callback function to trim whitespace from a value, to be used in array_walk
757 function trim_value ( &$value ) {
758 $value = trim($value);
762 * Converts ip address to hexadecimal string
764 * Function is used to convert ipv4 and ipv6 addresses to hex strings.
765 * It removes all delimiter symbols from ip addresses, converts decimal
766 * ipv4 numbers to hex and pads strings in order to present full length
767 * address. ipv4 addresses are represented as 8 byte strings, ipv6 addresses
768 * are represented as 32 byte string.
770 * If function fails to detect address format, it returns unprocessed string.
771 * @param string $string ip address string
772 * @return string processed ip address string
773 * @since 1.5.1 and 1.4.5
775 function ip2hex($string) {
776 if (preg_match("/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/",$string,$match)) {
778 $ret = str_pad(dechex($match[1]),2,'0',STR_PAD_LEFT
)
779 . str_pad(dechex($match[2]),2,'0',STR_PAD_LEFT
)
780 . str_pad(dechex($match[3]),2,'0',STR_PAD_LEFT
)
781 . str_pad(dechex($match[4]),2,'0',STR_PAD_LEFT
);
782 } 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)) {
784 $ret = str_pad($match[1],4,'0',STR_PAD_LEFT
)
785 . str_pad($match[2],4,'0',STR_PAD_LEFT
)
786 . str_pad($match[3],4,'0',STR_PAD_LEFT
)
787 . str_pad($match[4],4,'0',STR_PAD_LEFT
)
788 . str_pad($match[5],4,'0',STR_PAD_LEFT
)
789 . str_pad($match[6],4,'0',STR_PAD_LEFT
)
790 . str_pad($match[7],4,'0',STR_PAD_LEFT
)
791 . str_pad($match[8],4,'0',STR_PAD_LEFT
);
792 } elseif (preg_match("/^\:\:([0-9a-h\:]+)$/i",$string,$match)) {
793 // short ipv6 with all starting symbols nulled
794 $aAddr=explode(':',$match[1]);
796 foreach ($aAddr as $addr) {
797 $ret.=str_pad($addr,4,'0',STR_PAD_LEFT
);
799 $ret=str_pad($ret,32,'0',STR_PAD_LEFT
);
800 } elseif (preg_match("/^([0-9a-h\:]+)::([0-9a-h\:]+)$/i",$string,$match)) {
801 // short ipv6 with middle part nulled
802 $aStart=explode(':',$match[1]);
804 foreach($aStart as $addr) {
805 $sStart.=str_pad($addr,4,'0',STR_PAD_LEFT
);
807 $aEnd = explode(':',$match[2]);
809 foreach($aEnd as $addr) {
810 $sEnd.=str_pad($addr,4,'0',STR_PAD_LEFT
);
813 . str_pad('',(32 - strlen($sStart . $sEnd)),'0',STR_PAD_LEFT
)
816 // unknown addressing