Correct mistaken use of rfc822_header->date field that was being treated as a date...
[squirrelmail.git] / class / deliver / Deliver.class.php
CommitLineData
4960ec8e 1<?php
4b4abf93 2
69298c28 3/**
4 * Deliver.class.php
5 *
69298c28 6 * This contains all the functions needed to send messages through
7 * a delivery backend.
8 *
4b4abf93 9 * @author Marc Groot Koerkamp
8ed19238 10 * @copyright 1999-2019 The SquirrelMail Project Team
4b4abf93 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
883d9cd3 12 * @version $Id$
72b004e6 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 *
0fdb0aa1 34 * @param Message $message Message object to send
10adeb76 35 * NOTE that this is passed by
36 * reference and will be modified
37 * upon return with updated
38 * fields such as Message ID, References,
39 * In-Reply-To and Date headers.
40 * @param resource $stream Handle to the outgoing stream
e2ccf284 41 * (when FALSE, nothing will be
42 * written to the stream; this can
43 * be used to determine the actual
44 * number of bytes that will be
45 * written to the stream)
0fdb0aa1 46 * @param string $reply_id Identifies message being replied to
47 * (OPTIONAL; caller should ONLY specify
48 * a value for this when the message
49 * being sent is a reply)
50 * @param string $reply_ent_id Identifies message being replied to
51 * in the case it was an embedded/attached
52 * message inside another (OPTIONAL; caller
53 * should ONLY specify a value for this
54 * when the message being sent is a reply)
69bb88aa 55 * @param resource $imap_stream If there is an open IMAP stream in
56 * the caller's context, it should be
57 * passed in here. This is OPTIONAL,
58 * as one will be created if not given,
59 * but as some IMAP servers may baulk
60 * at opening more than one connection
61 * at a time, the caller should always
62 * abide if possible. Currently, this
63 * stream is only used when $reply_id
64 * is also non-zero, but that is subject
65 * to change.
33f0da43 66 * @param mixed $extra Any implementation-specific variables
67 * can be passed in here and used in
68 * an overloaded version of this method
69 * if needed.
72b004e6 70 *
10adeb76 71 * @return integer The number of bytes written (or that would have been
72 * written) to the output stream.
b67d61ee 73 *
72b004e6 74 */
10adeb76 75 function mail(&$message, $stream=false, $reply_id=0, $reply_ent_id=0,
33f0da43 76 $imap_stream=NULL, $extra=NULL) {
e2ccf284 77
10adeb76 78 $rfc822_header = &$message->rfc822_header;
79
78f39e78 80 if (count($message->entities)) {
81 $boundary = $this->mimeBoundary();
82 $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"';
83 } else {
84 $boundary='';
85 }
86 $raw_length = 0;
0fdb0aa1 87
88
89 // calculate reply header if needed
90 //
91 if ($reply_id) {
92 global $imapConnection, $username, $imapServerAddress,
a9805897 93 $imapPort, $imap_stream_options, $mailbox;
e2ccf284 94
69bb88aa 95 // try our best to use an existing IMAP handle
96 //
97 $close_imap_stream = FALSE;
98 if (is_resource($imap_stream)) {
99 $my_imap_stream = $imap_stream;
100
101 } else if (is_resource($imapConnection)) {
102 $my_imap_stream = $imapConnection;
103
104 } else {
105 $close_imap_stream = TRUE;
9aaa9ae2 106 $my_imap_stream = sqimap_login($username, FALSE, $imapServerAddress,
a9805897 107 $imapPort, 0, $imap_stream_options);
69bb88aa 108 }
109
110 sqimap_mailbox_select($my_imap_stream, $mailbox);
111 $reply_message = sqimap_get_message($my_imap_stream, $reply_id, $mailbox);
0fdb0aa1 112
69bb88aa 113 if ($close_imap_stream) {
114 sqimap_logout($my_imap_stream);
115 }
0fdb0aa1 116
117 if ($reply_ent_id) {
118 /* redefine the messsage in case of message/rfc822 */
119 $reply_message = $message->getEntity($reply_ent_id);
120 /* message is an entity which contains the envelope and type0=message
121 * and type1=rfc822. The actual entities are childs from
122 * $reply_message->entities[0]. That's where the encoding and is located
123 */
124
125 $orig_header = $reply_message->rfc822_header; /* here is the envelope located */
126
127 } else {
128 $orig_header = $reply_message->rfc822_header;
129 }
08e33475 130 $message->reply_rfc822_header = $orig_header;
0fdb0aa1 131 }
0fdb0aa1 132
133
72b004e6 134 $reply_rfc822_header = (isset($message->reply_rfc822_header)
78f39e78 135 ? $message->reply_rfc822_header : '');
136 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
75b14a70 137
e2ccf284 138 $this->send_mail($message, $header, $boundary, $stream, $raw_length, $extra);
139
10adeb76 140 return $raw_length;
e2ccf284 141 }
142
143 /**
144 * function send_mail - send the message parts to the IMAP stream
145 *
146 * @param Message $message Message object to send
147 * @param string $header Headers ready to send
148 * @param string $boundary Message parts boundary
149 * @param resource $stream Handle to the SMTP stream
150 * (when FALSE, nothing will be
151 * written to the stream; this can
152 * be used to determine the actual
153 * number of bytes that will be
154 * written to the stream)
155 * @param int &$raw_length The number of bytes written (or that
156 * would have been written) to the
157 * output stream - NOTE that this is
158 * passed by reference
159 * @param mixed $extra Any implementation-specific variables
160 * can be passed in here and used in
161 * an overloaded version of this method
162 * if needed.
163 *
164 * @return void
165 *
166 */
167 function send_mail($message, $header, $boundary, $stream=false,
168 &$raw_length, $extra=NULL) {
169
170
78f39e78 171 if ($stream) {
59387d1c 172 $this->preWriteToStream($header);
173 $this->writeToStream($stream, $header);
78f39e78 174 }
175 $this->writeBody($message, $stream, $raw_length, $boundary);
3b53d87f 176 }
78f39e78 177
72b004e6 178 /**
179 * function writeBody - generate and write the mime boundaries around each part to the stream
180 *
181 * Recursively formats and writes the MIME boundaries of the $message
182 * to the output stream.
183 *
184 * @param Message $message Message object to transform
185 * @param resource $stream SMTP output stream
e2ccf284 186 * (when FALSE, nothing will be
187 * written to the stream; this can
188 * be used to determine the actual
189 * number of bytes that will be
190 * written to the stream)
72b004e6 191 * @param integer &$length_raw raw length of the message (part)
192 * as returned by mail fn
193 * @param string $boundary custom boundary to call, usually for subparts
194 *
3a70ee56 195 * @return void
72b004e6 196 */
3b53d87f 197 function writeBody($message, $stream, &$length_raw, $boundary='') {
caf6a9a4 198 // calculate boundary in case of multidimensional mime structures
199 if ($boundary && $message->entity_id && count($message->entities)) {
200 if (strpos($boundary,'_part_')) {
201 $boundary = substr($boundary,0,strpos($boundary,'_part_'));
ba86230e 202
593d083d 203 // the next four lines use strrev to reverse any nested boundaries
204 // because RFC 2046 (5.1.1) says that if a line starts with the outer
205 // boundary string (doesn't matter what the line ends with), that
206 // can be considered a match for the outer boundary; thus the nested
207 // boundary needs to be unique from the outer one
ba86230e 208 //
209 } else if (strpos($boundary,'_trap_')) {
210 $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_'));
caf6a9a4 211 }
ba86230e 212 $boundary_new = strrev($boundary . '_part_'.$message->entity_id);
caf6a9a4 213 } else {
214 $boundary_new = $boundary;
215 }
1274f430 216 if ($boundary && !$message->rfc822_header) {
78f39e78 217 $s = '--'.$boundary."\r\n";
caf6a9a4 218 $s .= $this->prepareMIME_Header($message, $boundary_new);
78f39e78 219 $length_raw += strlen($s);
220 if ($stream) {
3b53d87f 221 $this->preWriteToStream($s);
78f39e78 222 $this->writeToStream($stream, $s);
223 }
224 }
225 $this->writeBodyPart($message, $stream, $length_raw);
72b004e6 226
78f39e78 227 $last = false;
228 for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) {
caf6a9a4 229 $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary_new);
78f39e78 230 if ($i == $entCount-1) $last = true;
231 }
1274f430 232 if ($boundary && $last) {
dcc5c913 233 $s = "--".$boundary_new."--\r\n\r\n";
78f39e78 234 $length_raw += strlen($s);
235 if ($stream) {
3b53d87f 236 $this->preWriteToStream($s);
78f39e78 237 $this->writeToStream($stream, $s);
238 }
239 }
3b53d87f 240 }
241
72b004e6 242 /**
243 * function writeBodyPart - write each individual mimepart
244 *
245 * Recursively called by WriteBody to write each mime part to the SMTP stream
246 *
247 * @param Message $message Message object to transform
248 * @param resource $stream SMTP output stream
e2ccf284 249 * (when FALSE, nothing will be
250 * written to the stream; this can
251 * be used to determine the actual
252 * number of bytes that will be
253 * written to the stream)
72b004e6 254 * @param integer &$length length of the message part
255 * as returned by mail fn
256 *
3a70ee56 257 * @return void
72b004e6 258 */
3b53d87f 259 function writeBodyPart($message, $stream, &$length) {
1274f430 260 if ($message->mime_header) {
78f39e78 261 $type0 = $message->mime_header->type0;
262 } else {
263 $type0 = $message->rfc822_header->content_type->type0;
264 }
265
266 $body_part_trailing = $last = '';
267 switch ($type0)
268 {
269 case 'text':
270 case 'message':
271 if ($message->body_part) {
272 $body_part = $message->body_part;
4cf85ddd 273 // remove NUL characters
274 $body_part = str_replace("\0",'',$body_part);
78f39e78 275 $length += $this->clean_crlf($body_part);
276 if ($stream) {
72b004e6 277 $this->preWriteToStream($body_part);
78f39e78 278 $this->writeToStream($stream, $body_part);
279 }
280 $last = $body_part;
281 } elseif ($message->att_local_name) {
1f270d3c 282 global $username, $attachment_dir;
283 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
78f39e78 284 $filename = $message->att_local_name;
89f19004 285
286 // inspect attached file for lines longer than allowed by RFC,
287 // in which case we'll be using base64 encoding (so we can split
288 // the lines up without corrupting them) instead of 8bit unencoded...
289 // (see RFC 2822/2.1.1)
290 //
291 // using 990 because someone somewhere is folding lines at
292 // 990 instead of 998 and I'm too lazy to find who it is
293 //
294 $file_has_long_lines = file_has_long_lines($hashed_attachment_dir
295 . '/' . $filename, 990);
296
1f270d3c 297 $file = fopen ($hashed_attachment_dir . '/' . $filename, 'rb');
89f19004 298
299 // long lines were found, need to use base64 encoding
300 //
301 if ($file_has_long_lines) {
302 while ($tmp = fread($file, 570)) {
303 $body_part = chunk_split(base64_encode($tmp));
304 // Up to 4.3.10 chunk_split always appends a newline,
305 // while in 4.3.11 it doesn't if the string to split
306 // is shorter than the chunk length.
307 if( substr($body_part, -1 , 1 ) != "\n" )
308 $body_part .= "\n";
309 $length += $this->clean_crlf($body_part);
310 if ($stream) {
311 $this->writeToStream($stream, $body_part);
312 }
313 }
314 }
315
316 // no excessively long lines - normal 8bit
317 //
318 else {
319 while ($body_part = fgets($file, 4096)) {
320 // remove NUL characters
321 $body_part = str_replace("\0",'',$body_part);
322 $length += $this->clean_crlf($body_part);
323 if ($stream) {
324 $this->preWriteToStream($body_part);
325 $this->writeToStream($stream, $body_part);
326 }
327 $last = $body_part;
78f39e78 328 }
78f39e78 329 }
89f19004 330
78f39e78 331 fclose($file);
332 }
333 break;
334 default:
335 if ($message->body_part) {
336 $body_part = $message->body_part;
257dea3f 337 // remove NUL characters
338 $body_part = str_replace("\0",'',$body_part);
78f39e78 339 $length += $this->clean_crlf($body_part);
340 if ($stream) {
341 $this->writeToStream($stream, $body_part);
342 }
343 } elseif ($message->att_local_name) {
1f270d3c 344 global $username, $attachment_dir;
345 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
78f39e78 346 $filename = $message->att_local_name;
1f270d3c 347 $file = fopen ($hashed_attachment_dir . '/' . $filename, 'rb');
78f39e78 348 while ($tmp = fread($file, 570)) {
27ff4efb 349 $body_part = chunk_split(base64_encode($tmp));
350 // Up to 4.3.10 chunk_split always appends a newline,
351 // while in 4.3.11 it doesn't if the string to split
352 // is shorter than the chunk length.
353 if( substr($body_part, -1 , 1 ) != "\n" )
354 $body_part .= "\n";
78f39e78 355 $length += $this->clean_crlf($body_part);
356 if ($stream) {
357 $this->writeToStream($stream, $body_part);
358 }
359 }
360 fclose($file);
3b53d87f 361 }
78f39e78 362 break;
363 }
364 $body_part_trailing = '';
365 if ($last && substr($last,-1) != "\n") {
366 $body_part_trailing = "\r\n";
367 }
368 if ($body_part_trailing) {
369 $length += strlen($body_part_trailing);
370 if ($stream) {
371 $this->preWriteToStream($body_part_trailing);
372 $this->writeToStream($stream, $body_part_trailing);
373 }
374 }
3b53d87f 375 }
78f39e78 376
72b004e6 377 /**
378 * function clean_crlf - change linefeeds and newlines to legal characters
379 *
380 * The SMTP format only allows CRLF as line terminators.
381 * This function replaces illegal teminators with the correct terminator.
382 *
383 * @param string &$s string to clean linefeeds on
384 *
3a70ee56 385 * @return void
72b004e6 386 */
69298c28 387 function clean_crlf(&$s) {
3b53d87f 388 $s = str_replace("\r\n", "\n", $s);
389 $s = str_replace("\r", "\n", $s);
390 $s = str_replace("\n", "\r\n", $s);
78f39e78 391 return strlen($s);
3b53d87f 392 }
78f39e78 393
72b004e6 394 /**
395 * function strip_crlf - strip linefeeds and newlines from a string
396 *
397 * The SMTP format only allows CRLF as line terminators.
398 * This function strips all line terminators from the string.
399 *
400 * @param string &$s string to clean linefeeds on
401 *
3a70ee56 402 * @return void
72b004e6 403 */
e4f9307a 404 function strip_crlf(&$s) {
405 $s = str_replace("\r\n ", '', $s);
78f39e78 406 $s = str_replace("\r", '', $s);
407 $s = str_replace("\n", '', $s);
e4f9307a 408 }
3b53d87f 409
72b004e6 410 /**
411 * function preWriteToStream - reserved for extended functionality
412 *
413 * This function is not yet implemented.
414 * Reserved for extended functionality.
415 *
416 * @param string &$s string to operate on
417 *
3a70ee56 418 * @return void
72b004e6 419 */
69298c28 420 function preWriteToStream(&$s) {
3b53d87f 421 }
78f39e78 422
72b004e6 423 /**
424 * function writeToStream - write data to the SMTP stream
425 *
426 * @param resource $stream SMTP output stream
427 * @param string $data string with data to send to the SMTP stream
428 *
3a70ee56 429 * @return void
72b004e6 430 */
3b53d87f 431 function writeToStream($stream, $data) {
78f39e78 432 fputs($stream, $data);
3b53d87f 433 }
78f39e78 434
72b004e6 435 /**
436 * function initStream - reserved for extended functionality
437 *
438 * This function is not yet implemented.
439 * Reserved for extended functionality.
653a7e87 440 * UPDATE: It is implemented in Deliver_SMTP and Deliver_SendMail classes,
441 * but it remains unimplemented in this base class (and thus not
442 * in Deliver_IMAP or other child classes that don't define it)
443 *
444 * NOTE: some parameters are specific to the child class
445 * that is implementing this method
72b004e6 446 *
447 * @param Message $message Message object
653a7e87 448 * @param string $domain
449 * @param integer $length
72b004e6 450 * @param string $host host name or IP to connect to
653a7e87 451 * @param integer $port
72b004e6 452 * @param string $user username to log into the SMTP server with
453 * @param string $pass password to log into the SMTP server with
653a7e87 454 * @param boolean $authpop whether or not to use POP-before-SMTP authorization
455 * @param string $pop_host host name or IP to connect to for POP-before-SMTP authorization
1c5dd6f2 456 * @param array $stream_options Stream context options, see config_local.example.php for more details (OPTIONAL)
72b004e6 457 *
3a70ee56 458 * @return handle $stream file handle resource to SMTP stream
72b004e6 459 */
a9805897 460 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false, $pop_host='', $stream_options=array()) {
78f39e78 461 return $stream;
3b53d87f 462 }
72b004e6 463
464 /**
465 * function getBCC - reserved for extended functionality
466 *
467 * This function is not yet implemented.
468 * Reserved for extended functionality.
469 *
470 */
471 function getBCC() {
78f39e78 472 return false;
59387d1c 473 }
3b53d87f 474
72b004e6 475 /**
476 * function prepareMIME_Header - creates the mime header
477 *
478 * @param Message $message Message object to act on
479 * @param string $boundary mime boundary from fn MimeBoundary
480 *
3a70ee56 481 * @return string $header properly formatted mime header
72b004e6 482 */
3b53d87f 483 function prepareMIME_Header($message, $boundary) {
78f39e78 484 $mime_header = $message->mime_header;
485 $rn="\r\n";
486 $header = array();
1274f430 487
78f39e78 488 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
489 $mime_header->type1;
490 if (count($message->entities)) {
fcce1b3c 491 $contenttype .= ';' . 'boundary="'.$boundary.'"';
78f39e78 492 }
493 if (isset($mime_header->parameters['name'])) {
494 $contenttype .= '; name="'.
495 encodeHeader($mime_header->parameters['name']). '"';
496 }
497 if (isset($mime_header->parameters['charset'])) {
498 $charset = $mime_header->parameters['charset'];
499 $contenttype .= '; charset="'.
500 encodeHeader($charset). '"';
501 }
1274f430 502
78f39e78 503 $header[] = $contenttype . $rn;
504 if ($mime_header->description) {
eb72e151 505 $header[] = 'Content-Description: ' . $mime_header->description . $rn;
78f39e78 506 }
507 if ($mime_header->encoding) {
eb72e151 508 $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
78f39e78 509 } else {
89f19004 510
511 // inspect attached file for lines longer than allowed by RFC,
512 // in which case we'll be using base64 encoding (so we can split
513 // the lines up without corrupting them) instead of 8bit unencoded...
514 // (see RFC 2822/2.1.1)
515 //
516 if (!empty($message->att_local_name)) { // is this redundant? I have no idea
517 global $username, $attachment_dir;
518 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
519 $filename = $hashed_attachment_dir . '/' . $message->att_local_name;
520
521 // using 990 because someone somewhere is folding lines at
522 // 990 instead of 998 and I'm too lazy to find who it is
523 //
524 $file_has_long_lines = file_has_long_lines($filename, 990);
525 } else
526 $file_has_long_lines = FALSE;
527
528 if ($mime_header->type0 == 'multipart' || $mime_header->type0 == 'alternative') {
c6dd29ba 529 /* no-op; no encoding needed */
89f19004 530 } else if (($mime_header->type0 == 'text' || $mime_header->type0 == 'message')
531 && !$file_has_long_lines) {
532 $header[] = 'Content-Transfer-Encoding: 8bit' . $rn;
78f39e78 533 } else {
eb72e151 534 $header[] = 'Content-Transfer-Encoding: base64' . $rn;
78f39e78 535 }
536 }
537 if ($mime_header->id) {
eb72e151 538 $header[] = 'Content-ID: ' . $mime_header->id . $rn;
78f39e78 539 }
540 if ($mime_header->disposition) {
541 $disposition = $mime_header->disposition;
542 $contentdisp = 'Content-Disposition: ' . $disposition->name;
543 if ($disposition->getProperty('filename')) {
544 $contentdisp .= '; filename="'.
545 encodeHeader($disposition->getProperty('filename')). '"';
546 }
72b004e6 547 $header[] = $contentdisp . $rn;
78f39e78 548 }
549 if ($mime_header->md5) {
eb72e151 550 $header[] = 'Content-MD5: ' . $mime_header->md5 . $rn;
78f39e78 551 }
552 if ($mime_header->language) {
eb72e151 553 $header[] = 'Content-Language: ' . $mime_header->language . $rn;
78f39e78 554 }
4960ec8e 555
78f39e78 556 $cnt = count($header);
557 $hdr_s = '';
558 for ($i = 0 ; $i < $cnt ; $i++) {
b1fbb25f 559 $hdr_s .= $this->foldLine($header[$i], 78);
78f39e78 560 }
561 $header = $hdr_s;
562 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
563 return $header;
564 }
4960ec8e 565
72b004e6 566 /**
567 * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s)
568 *
569 * This function takes the Rfc822Header object(s) and formats them
570 * into the RFC822Header string to send to the SMTP server as part
571 * of the SMTP message.
572 *
573 * @param Rfc822Header $rfc822_header
574 * @param Rfc822Header $reply_rfc822_header
575 * @param integer &$raw_length length of the message
576 *
3a70ee56 577 * @return string $header
72b004e6 578 */
10adeb76 579 function prepareRFC822_Header(&$rfc822_header, $reply_rfc822_header, &$raw_length) {
b37e457f 580 global $domain, $username, $encode_header_key,
e930c9fe 581 $edit_identity, $hide_auth_header;
60a46e65 582
49c7f411 583 /* if server var SERVER_NAME not available, or contains
584 ":" (e.g. IPv6) which is illegal in a Message-ID, use $domain */
585 if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER) ||
586 strpos($SERVER_NAME,':') !== FALSE) {
60a46e65 587 $SERVER_NAME = $domain;
78f39e78 588 }
60a46e65 589
78f39e78 590 sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER);
591 sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER);
592 sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER);
593 sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER);
594 sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER);
60a46e65 595
78f39e78 596 $rn = "\r\n";
223872cb 597
78f39e78 598 /* This creates an RFC 822 date */
42190655 599 $now = time();
600 $now_date = date('D, j M Y H:i:s ', $now) . $this->timezone();
601 // TODO: Do we really want to preserve possibly old date? Date header should always have "now"... but here is not where this decision should be made -- the caller really should blank out $rfc822_header->date even for drafts being re-edited or sent
602 if (!empty($rfc822_header->date) && $rfc822_header->date != -1)
603 $message_date = date('D, j M Y H:i:s ', $rfc822_header->date) . $this->timezone();
604 else {
605 $message_date = $now_date;
606 $rfc822_header->date = $now;
607 }
49c7f411 608
78f39e78 609 /* Create a message-id */
10adeb76 610 $message_id = 'MESSAGE ID GENERATION ERROR! PLEASE CONTACT SQUIRRELMAIL DEVELOPERS';
611 if (empty($rfc822_header->message_id)) {
dda811a1 612 $message_id = '<'
613 . md5(GenerateRandomString(16, '', 7) . uniqid(mt_rand(),true))
614 . '.squirrel@' . $SERVER_NAME .'>';
10adeb76 615 }
b67d61ee 616
78f39e78 617 /* Make an RFC822 Received: line */
618 if (isset($REMOTE_HOST)) {
619 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
620 } else {
621 $received_from = $REMOTE_ADDR;
622 }
623 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
624 if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') {
625 $HTTP_X_FORWARDED_FOR = 'unknown';
626 }
627 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
628 }
629 $header = array();
432db2fc 630
631 /**
632 * SquirrelMail header
633 *
634 * This Received: header provides information that allows to track
635 * user and machine that was used to send email. Don't remove it
636 * unless you understand all possible forging issues or your
637 * webmail installation does not prevent changes in user's email address.
638 * See SquirrelMail bug tracker #847107 for more details about it.
bb08da84 639 *
904849cc 640 * Add hide_squirrelmail_header as a candidate for config_local.php
641 * (must be defined as a constant: define('hide_squirrelmail_header', 1);
bb08da84 642 * to allow completely hiding SquirrelMail participation in message
539a323f 643 * processing; This is dangerous, especially if users can modify their
e930c9fe 644 * account information, as it makes mapping a sent message back to the
645 * original sender almost impossible.
432db2fc 646 */
e930c9fe 647 $show_sm_header = ( defined('hide_squirrelmail_header') ? ! hide_squirrelmail_header : 1 );
bb08da84 648
10adeb76 649 // FIXME: The following headers may generate slightly differently between the message sent to the destination and that stored in the Sent folder because this code will be called before both actions. This is not necessarily a big problem, but other headers such as Message-ID and Date are preserved between both actions
bb08da84 650 if ( $show_sm_header ) {
651 if (isset($encode_header_key) &&
432db2fc 652 trim($encode_header_key)!='') {
653 // use encoded headers, if encryption key is set and not empty
b81efcb4 654 $header[] = 'X-Squirrel-UserHash: '.OneTimePadEncrypt($username,base64_encode($encode_header_key)).$rn;
655 $header[] = 'X-Squirrel-FromHash: '.OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key)).$rn;
432db2fc 656 if (isset($HTTP_X_FORWARDED_FOR))
b81efcb4 657 $header[] = 'X-Squirrel-ProxyHash:'.OneTimePadEncrypt($this->ip2hex($HTTP_X_FORWARDED_FOR),base64_encode($encode_header_key)).$rn;
bb08da84 658 } else {
432db2fc 659 // use default received headers
660 $header[] = "Received: from $received_from" . $rn;
2405c716 661 if (!isset($hide_auth_header) || !$hide_auth_header)
432db2fc 662 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
663 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
42190655 664 $header[] = " $now_date" . $rn;
bb08da84 665 }
35aaf666 666 }
432db2fc 667
59387d1c 668 /* Insert the rest of the header fields */
10adeb76 669
670 if (!empty($rfc822_header->message_id)) {
671 $header[] = 'Message-ID: '. $rfc822_header->message_id . $rn;
672 } else {
673 $header[] = 'Message-ID: '. $message_id . $rn;
674 $rfc822_header->message_id = $message_id;
675 }
676
ea87de81 677 if (is_object($reply_rfc822_header) &&
b4316b34 678 isset($reply_rfc822_header->message_id) &&
679 $reply_rfc822_header->message_id) {
78f39e78 680 $rep_message_id = $reply_rfc822_header->message_id;
78f39e78 681 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
10adeb76 682 $rfc822_header->in_reply_to = $rep_message_id;
78f39e78 683 $references = $this->calculate_references($reply_rfc822_header);
684 $header[] = 'References: '.$references . $rn;
10adeb76 685 $rfc822_header->references = $references;
78f39e78 686 }
10adeb76 687
42190655 688 $header[] = "Date: $message_date" . $rn;
10adeb76 689
59387d1c 690 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
ac47827c 691 $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn;
692
ea87de81 693 // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>"
694 // as delimiter
ac47827c 695 // Do not use foldLine for that.
696
697 // RFC2822 if from contains more then 1 address
59387d1c 698 if (count($rfc822_header->from) > 1) {
215de78c 699 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
78f39e78 700 }
701 if (count($rfc822_header->to)) {
ac47827c 702 $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn;
78f39e78 703 }
704 if (count($rfc822_header->cc)) {
ac47827c 705 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn;
78f39e78 706 }
707 if (count($rfc822_header->reply_to)) {
215de78c 708 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
78f39e78 709 }
710 /* Sendmail should return true. Default = false */
711 $bcc = $this->getBcc();
712 if (count($rfc822_header->bcc)) {
ac47827c 713 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn;
78f39e78 714 if (!$bcc) {
78f39e78 715 $raw_length += strlen($s);
716 } else {
717 $header[] = $s;
718 }
719 }
72b004e6 720 /* Identify SquirrelMail */
b37e457f 721 $header[] = 'User-Agent: SquirrelMail/' . SM_VERSION . $rn;
78f39e78 722 /* Do the MIME-stuff */
723 $header[] = 'MIME-Version: 1.0' . $rn;
724 $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
59387d1c 725 $rfc822_header->content_type->type1;
78f39e78 726 if (count($rfc822_header->content_type->properties)) {
727 foreach ($rfc822_header->content_type->properties as $k => $v) {
728 if ($k && $v) {
72b004e6 729 $contenttype .= ';' .$k.'='.$v;
78f39e78 730 }
731 }
732 }
59387d1c 733 $header[] = $contenttype . $rn;
78f39e78 734 if ($encoding = $rfc822_header->encoding) {
23301b33 735 $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn;
72b004e6 736 }
539a323f 737 if (isset($rfc822_header->dnt) && $rfc822_header->dnt) {
72b004e6 738 $dnt = $rfc822_header->getAddr_s('dnt');
78f39e78 739 /* Pegasus Mail */
740 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
741 /* RFC 2298 */
742 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
743 }
744 if ($rfc822_header->priority) {
d1db3699 745 switch($rfc822_header->priority)
78f39e78 746 {
d1db3699 747 case 1:
91e0dccc 748 $header[] = 'X-Priority: 1 (Highest)'.$rn;
749 $header[] = 'Importance: High'. $rn; break;
d1db3699 750 case 5:
91e0dccc 751 $header[] = 'X-Priority: 5 (Lowest)'.$rn;
752 $header[] = 'Importance: Low'. $rn; break;
78f39e78 753 default: break;
754 }
755 }
72b004e6 756 /* Insert headers from the $more_headers array */
78f39e78 757 if(count($rfc822_header->more_headers)) {
758 reset($rfc822_header->more_headers);
759 foreach ($rfc822_header->more_headers as $k => $v) {
760 $header[] = $k.': '.$v .$rn;
761 }
762 }
763 $cnt = count($header);
764 $hdr_s = '';
5d2a7b15 765
78f39e78 766 for ($i = 0 ; $i < $cnt ; $i++) {
5d2a7b15 767 $sKey = substr($header[$i],0,strpos($header[$i],':'));
768 switch ($sKey)
769 {
770 case 'Message-ID':
771 case 'In-Reply_To':
772 $hdr_s .= $header[$i];
773 break;
774 case 'References':
775 $sRefs = substr($header[$i],12);
776 $aRefs = explode(' ',$sRefs);
777 $sLine = 'References:';
778 foreach ($aRefs as $sReference) {
cc201b46 779 if ( trim($sReference) == '' ) {
780 /* Don't add spaces. */
781 } elseif (strlen($sLine)+strlen($sReference) >76) {
5d2a7b15 782 $hdr_s .= $sLine;
783 $sLine = $rn . ' ' . $sReference;
784 } else {
785 $sLine .= ' '. $sReference;
786 }
787 }
788 $hdr_s .= $sLine;
789 break;
ac47827c 790 case 'To':
791 case 'Cc':
792 case 'Bcc':
793 case 'From':
794 $hdr_s .= $header[$i];
795 break;
b1fbb25f 796 default: $hdr_s .= $this->foldLine($header[$i], 78); break;
5d2a7b15 797 }
78f39e78 798 }
78f39e78 799 $header = $hdr_s;
800 $header .= $rn; /* One blank line to separate header and body */
801 $raw_length += strlen($header);
802 return $header;
4960ec8e 803 }
804
72b004e6 805 /**
9bcf508d 806 * Fold header lines per RFC 2822/2.2.3 and RFC 822/3.1.1
807 *
808 * Herein "soft" folding/wrapping (with whitespace tokens) is
809 * what we refer to as the preferred method of wrapping - that
810 * which we'd like to do within the $soft_wrap limit, but if
811 * not possible, we will try to do as soon as possible after
812 * $soft_wrap up to the $hard_wrap limit. Encoded words don't
813 * need to be detected in this phase, since they cannot contain
814 * spaces.
815 *
816 * "Hard" folding/wrapping (with "hard" tokens) is what we refer
817 * to as less ideal wrapping that will be done to keep within
818 * the $hard_wrap limit. This adds other syntactical breaking
819 * elements such as commas and encoded words.
820 *
821 * @param string $header The header content being folded
822 * @param integer $soft_wrap The desirable maximum line length
823 * (OPTIONAL; default is 78, per RFC)
824 * @param string $indent Wrapped lines will already have
825 * whitespace following the CRLF wrap,
826 * but you can add more indentation (or
827 * whatever) with this. The use of this
828 * parameter is DISCOURAGED, since it
829 * can corrupt the redisplay (unfolding)
830 * of headers whose content is space-
831 * sensitive, like subjects, etc.
832 * (OPTIONAL; default is an empty string)
833 * @param string $hard_wrap The absolute maximum line length
834 * (OPTIONAL; default is 998, per RFC)
835 *
836 * @return string The folded header content, with a trailing CRLF.
837 *
838 */
839 function foldLine($header, $soft_wrap=78, $indent='', $hard_wrap=998) {
840
841 // the "hard" token list can be altered if desired,
842 // for example, by adding ":"
843 // (in the future, we can take optional arguments
844 // for overriding or adding elements to the "hard"
845 // token list if we want to get fancy)
846 //
847 // the order of these is significant - preferred
848 // fold points should be listed first
849 //
850 // it is advised that the "=" always come first
851 // since it also finds encoded words, thus if it
852 // comes after some other token that happens to
853 // fall within the encoded word, the encoded word
854 // could be inadvertently broken in half, which
855 // is not allowable per RFC
856 //
857 $hard_break_tokens = array(
858 '=', // includes encoded word detection
859 ',',
860 ';',
861 );
862
863 // the order of these is significant too
864 //
865 $whitespace = array(
866 ' ',
867 "\t",
868 );
869
870 $CRLF = "\r\n";
871
872 $folded_header = '';
873
874 // if using an indent string, reduce wrap limits by its size
875 //
876 if (!empty($indent)) {
877 $soft_wrap -= strlen($indent);
878 $hard_wrap -= strlen($indent);
879 }
880
881 while (strlen($header) > $soft_wrap) {
882
883 $soft_wrapped_line = substr($header, 0, $soft_wrap);
884
885 // look for a token as close to the end of the soft wrap limit as possible
886 //
887 foreach ($whitespace as $token) {
888
889 // note that this if statement also fails when $pos === 0,
890 // which is intended, since blank lines are not allowed
891 //
892 if ($pos = strrpos($soft_wrapped_line, $token))
893 {
894 $new_fold = substr($header, 0, $pos);
895
896 // make sure proposed fold doesn't create a blank line
897 //
898 if (!trim($new_fold)) continue;
899
900 // with whitespace breaks, we fold BEFORE the token
901 //
902 $folded_header .= $new_fold . $CRLF . $indent;
903 $header = substr($header, $pos);
904
905 // ready for next while() iteration
906 //
907 continue 2;
908
909 }
910
911 }
912
913 // we were unable to find a wrapping point within the soft
914 // wrap limit, so now we'll try to find the first possible
915 // soft wrap point within the hard wrap limit
916 //
917 $hard_wrapped_line = substr($header, 0, $hard_wrap);
918
919 // look for a *SOFT* token as close to the
920 // beginning of the hard wrap limit as possible
921 //
922 foreach ($whitespace as $token) {
923
924 // use while loop instead of if block because it
925 // is possible we don't want the first one we find
926 //
927 $pos = $soft_wrap - 1; // -1 is corrected by +1 on next line
928 while ($pos = strpos($hard_wrapped_line, $token, $pos + 1))
929 {
930
931 $new_fold = substr($header, 0, $pos);
932
933 // make sure proposed fold doesn't create a blank line
934 //
935 if (!trim($new_fold)) continue;
936
937 // with whitespace breaks, we fold BEFORE the token
938 //
939 $folded_header .= $new_fold . $CRLF . $indent;
940 $header = substr($header, $pos);
941
942 // ready for next outter while() iteration
943 //
944 continue 3;
945
946 }
947
948 }
949
950 // we were still unable to find a soft wrapping point within
951 // both the soft and hard wrap limits, so if the length of
952 // what is left is no more than the hard wrap limit, we'll
953 // simply take the whole thing
954 //
955 if (strlen($header) <= strlen($hard_wrapped_line))
956 break;
957
958 // otherwise, we can't quit yet - look for a "hard" token
959 // as close to the end of the hard wrap limit as possible
960 //
961 foreach ($hard_break_tokens as $token) {
962
963 // note that this if statement also fails when $pos === 0,
964 // which is intended, since blank lines are not allowed
965 //
966 if ($pos = strrpos($hard_wrapped_line, $token))
967 {
968
969 // if we found a "=" token, we must determine whether,
970 // if it is part of an encoded word, it is the beginning
971 // or middle of one, where we need to readjust $pos a bit
972 //
973 if ($token == '=') {
974
975 // if we found the beginning of an encoded word,
976 // we want to break BEFORE the token
977 //
b1fbb25f 978 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)/Ui',
9bcf508d 979 substr($header, $pos))) {
980 $pos--;
72b004e6 981 }
9bcf508d 982
983 // check if we found this token in the *middle*
984 // of an encoded word, in which case we have to
985 // ignore it, pushing back to the token that
986 // starts the encoded word instead
987 //
988 // of course, this is only possible if there is
989 // more content after the next hard wrap
990 //
991 // then look for the end of an encoded word in
992 // the next part (past the next hard wrap)
993 //
994 // then see if it is in fact part of a legitimate
995 // encoded word
996 //
997 else if (strlen($header) > $hard_wrap
998 && ($end_pos = strpos(substr($header, $hard_wrap), '?=')) !== FALSE
b1fbb25f 999 && preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)$/Ui',
9bcf508d 1000 substr($header, 0, $hard_wrap + $end_pos + 2),
1001 $matches)) {
1002
1003 $pos = $hard_wrap + $end_pos + 2 - strlen($matches[1]) - 1;
1004
72b004e6 1005 }
72b004e6 1006
fb1d6cb6 1007 }
9bcf508d 1008
1009 // $pos could have been changed; make sure it's
1010 // not at the beginning of the line, as blank
1011 // lines are not allowed
1012 //
1013 if ($pos === 0) continue;
1014
1015 // we are dealing with a simple token break...
1016 //
1017 // for non-whitespace breaks, we fold AFTER the token
1018 // and add a space after the fold if not immediately
1019 // followed by a whitespace character in the next part
1020 //
1021 $folded_header .= substr($header, 0, $pos + 1) . $CRLF;
1022
1023 // don't go beyond end of $header, though
1024 //
1025 if (strlen($header) > $pos + 1) {
1026 $header = substr($header, $pos + 1);
1027 if (!in_array($header{0}, $whitespace))
1028 $header = ' ' . $indent . $header;
1029 } else {
1030 $header = '';
fb1d6cb6 1031 }
9bcf508d 1032
1033 // ready for next while() iteration
1034 //
1035 continue 2;
1036
fb1d6cb6 1037 }
9bcf508d 1038
fb1d6cb6 1039 }
9bcf508d 1040
1041 // finally, we just couldn't find anything to fold on, so we
1042 // have to just cut it off at the hard limit
1043 //
1044 $folded_header .= $hard_wrapped_line . $CRLF;
1045
1046 // is there more?
1047 //
1048 if (strlen($header) > strlen($hard_wrapped_line)) {
1049 $header = substr($header, strlen($hard_wrapped_line));
1050 if (!in_array($header{0}, $whitespace))
1051 $header = ' ' . $indent . $header;
1052 } else {
1053 $header = '';
fb1d6cb6 1054 }
9bcf508d 1055
fb1d6cb6 1056 }
9bcf508d 1057
1058
1059 // add any left-overs
1060 //
1061 $folded_header .= $header;
1062
1063
1064 // make sure it ends with a CRLF
1065 //
1066 if (substr($folded_header, -2) != $CRLF) $folded_header .= $CRLF;
1067
1068
1069 return $folded_header;
fb1d6cb6 1070 }
3b53d87f 1071
72b004e6 1072 /**
1073 * function mimeBoundary - calculates the mime boundary to use
1074 *
1075 * This function will generate a random mime boundary base part
1076 * for the message if the boundary has not already been set.
1077 *
3a70ee56 1078 * @return string $mimeBoundaryString random mime boundary string
72b004e6 1079 */
59387d1c 1080 function mimeBoundary () {
78f39e78 1081 static $mimeBoundaryString;
3b53d87f 1082
78f39e78 1083 if ( !isset( $mimeBoundaryString ) ||
1084 $mimeBoundaryString == '') {
1085 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
1086 mt_rand( 10000, 99999 );
1087 }
1088 return $mimeBoundaryString;
3b53d87f 1089 }
1090
72b004e6 1091 /**
1092 * function timezone - Time offset for correct timezone
1093 *
3a70ee56 1094 * @return string $result with timezone and offset
72b004e6 1095 */
59387d1c 1096 function timezone () {
0bbf8622 1097 global $invert_time, $show_timezone_name;
78f39e78 1098
1099 $diff_second = date('Z');
1100 if ($invert_time) {
1101 $diff_second = - $diff_second;
1102 }
1103 if ($diff_second > 0) {
1104 $sign = '+';
1105 } else {
1106 $sign = '-';
1107 }
1108 $diff_second = abs($diff_second);
1109 $diff_hour = floor ($diff_second / 3600);
1110 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
0bbf8622 1111
1112 // If an administrator wants to add the timezone name to the
1113 // end of the date header, they can set $show_timezone_name
1114 // to boolean TRUE in config/config_local.php, but that is
1115 // NOT RFC-822 compliant (see section 5.1). Moreover, some
1116 // Windows users reported that strftime('%Z') was returning
1117 // the full zone name (not the abbreviation) which in some
1118 // cases included 8-bit characters (not allowed as is in headers).
1119 // The PHP manual actually does NOT promise what %Z will return
1120 // for strftime!: "The time zone offset/abbreviation option NOT
1121 // given by %z (depends on operating system)"
1122 //
1123 if ($show_timezone_name) {
1124 $zonename = '('.strftime('%Z').')';
1125 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
1126 } else {
1127 $result = sprintf ("%s%02d%02d", $sign, $diff_hour, $diff_minute);
1128 }
78f39e78 1129 return ($result);
59387d1c 1130 }
3b53d87f 1131
72b004e6 1132 /**
f2ac3325 1133 * function calculate_references - calculate correct References string
1134 * Adds the current message ID, and makes sure it doesn't grow forever,
1135 * to that extent it drops message-ID's in a smart way until the string
1136 * length is under the recommended value of 1000 ("References: <986>\r\n").
1137 * It always keeps the first and the last three ID's.
72b004e6 1138 *
1139 * @param Rfc822Header $hdr message header to calculate from
1140 *
f2ac3325 1141 * @return string $refer concatenated and trimmed References string
72b004e6 1142 */
1274f430 1143 function calculate_references($hdr) {
f2ac3325 1144 $aReferences = preg_split('/\s+/', $hdr->references);
78f39e78 1145 $message_id = $hdr->message_id;
1146 $in_reply_to = $hdr->in_reply_to;
539a323f 1147
f2ac3325 1148 // if References already exists, add the current message ID at the end.
1149 // no References exists; if we know a IRT, add that aswell
1150 if (count($aReferences) == 0 && $in_reply_to) {
1151 $aReferences[] = $in_reply_to;
1152 }
1153 $aReferences[] = $message_id;
1154
1155 // sanitize the array: trim whitespace, remove dupes
86e6a9eb 1156 array_walk($aReferences, 'sq_trim_value');
f2ac3325 1157 $aReferences = array_unique($aReferences);
1158
1159 while ( count($aReferences) > 4 && strlen(implode(' ', $aReferences)) >= 986 ) {
1160 $aReferences = array_merge(array_slice($aReferences,0,1),array_slice($aReferences,2));
78f39e78 1161 }
f2ac3325 1162 return implode(' ', $aReferences);
1163 }
1164
1165 /**
432db2fc 1166 * Converts ip address to hexadecimal string
1167 *
1168 * Function is used to convert ipv4 and ipv6 addresses to hex strings.
1169 * It removes all delimiter symbols from ip addresses, converts decimal
1170 * ipv4 numbers to hex and pads strings in order to present full length
ea87de81 1171 * address. ipv4 addresses are represented as 8 byte strings, ipv6 addresses
432db2fc 1172 * are represented as 32 byte string.
1173 *
1174 * If function fails to detect address format, it returns unprocessed string.
1175 * @param string $string ip address string
1176 * @return string processed ip address string
94511d23 1177 * @since 1.5.1 and 1.4.5
432db2fc 1178 */
1179 function ip2hex($string) {
1180 if (preg_match("/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/",$string,$match)) {
1181 // ipv4 address
1182 $ret = str_pad(dechex($match[1]),2,'0',STR_PAD_LEFT)
1183 . str_pad(dechex($match[2]),2,'0',STR_PAD_LEFT)
1184 . str_pad(dechex($match[3]),2,'0',STR_PAD_LEFT)
1185 . str_pad(dechex($match[4]),2,'0',STR_PAD_LEFT);
1186 } 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)) {
1187 // full ipv6 address
1188 $ret = str_pad($match[1],4,'0',STR_PAD_LEFT)
1189 . str_pad($match[2],4,'0',STR_PAD_LEFT)
1190 . str_pad($match[3],4,'0',STR_PAD_LEFT)
1191 . str_pad($match[4],4,'0',STR_PAD_LEFT)
1192 . str_pad($match[5],4,'0',STR_PAD_LEFT)
1193 . str_pad($match[6],4,'0',STR_PAD_LEFT)
1194 . str_pad($match[7],4,'0',STR_PAD_LEFT)
1195 . str_pad($match[8],4,'0',STR_PAD_LEFT);
1196 } elseif (preg_match("/^\:\:([0-9a-h\:]+)$/i",$string,$match)) {
1197 // short ipv6 with all starting symbols nulled
1198 $aAddr=explode(':',$match[1]);
1199 $ret='';
1200 foreach ($aAddr as $addr) {
1201 $ret.=str_pad($addr,4,'0',STR_PAD_LEFT);
1202 }
1203 $ret=str_pad($ret,32,'0',STR_PAD_LEFT);
1204 } elseif (preg_match("/^([0-9a-h\:]+)::([0-9a-h\:]+)$/i",$string,$match)) {
1205 // short ipv6 with middle part nulled
1206 $aStart=explode(':',$match[1]);
1207 $sStart='';
1208 foreach($aStart as $addr) {
1209 $sStart.=str_pad($addr,4,'0',STR_PAD_LEFT);
1210 }
1211 $aEnd = explode(':',$match[2]);
1212 $sEnd='';
1213 foreach($aEnd as $addr) {
1214 $sEnd.=str_pad($addr,4,'0',STR_PAD_LEFT);
1215 }
1216 $ret = $sStart
1217 . str_pad('',(32 - strlen($sStart . $sEnd)),'0',STR_PAD_LEFT)
1218 . $sEnd;
1219 } else {
1220 // unknown addressing
1221 $ret = $string;
1222 }
1223 return $ret;
1224 }
59387d1c 1225}