Source for file Deliver.class.php

Documentation is available at Deliver.class.php

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

Documentation generated on Sat, 07 Oct 2006 16:30:52 +0300 by phpDocumentor 1.3.0RC6