[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/admin/includes/classes/ -> mime.php (source)

   1  <?php
   2  /*

   3  $Id: mime.php 3 2006-05-27 04:59:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax

   9  

  10    mime.php - a class to assist in building mime-HTML eMails

  11  

  12    The original class was made by Richard Heyes <[email protected]>

  13    and can be found here: http://www.phpguru.org

  14  

  15    Renamed and Modified by Jan Wildeboer for osCommerce

  16  */
  17  
  18    class mime {
  19      var $_encoding;
  20      var $_subparts;
  21      var $_encoded;
  22      var $_headers;
  23      var $_body;
  24  
  25  /**

  26   * Constructor.

  27   * 

  28   * Sets up the object.

  29   *

  30   * @param $body   - The body of the mime part if any.

  31   * @param $params - An associative array of parameters:

  32   *                  content_type - The content type for this part eg multipart/mixed

  33   *                  encoding     - The encoding to use, 7bit, base64, or quoted-printable

  34   *                  cid          - Content ID to apply

  35   *                  disposition  - Content disposition, inline or attachment

  36   *                  dfilename    - Optional filename parameter for content disposition

  37   *                  description  - Content description

  38   * @access public

  39   */
  40  
  41      function mime($body, $params = '') {
  42        if ($params == '') $params = array();
  43  
  44  // Make sure we use the correct linfeed sequence

  45        if (EMAIL_LINEFEED == 'CRLF') {
  46          $this->lf = "\r\n";
  47        } else {
  48          $this->lf = "\n";
  49        }
  50  
  51        reset($params);
  52        while (list($key, $value) = each($params)) {
  53          switch ($key) {
  54            case 'content_type':
  55              $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
  56              break;
  57            case 'encoding':
  58              $this->_encoding = $value;
  59              $headers['Content-Transfer-Encoding'] = $value;
  60              break;
  61            case 'cid':
  62              $headers['Content-ID'] = '<' . $value . '>';
  63              break;
  64            case 'disposition':
  65              $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
  66              break;
  67            case 'dfilename':
  68              if (isset($headers['Content-Disposition'])) {
  69                $headers['Content-Disposition'] .= '; filename="' . $value . '"';
  70              } else {
  71                $dfilename = $value;
  72              }
  73              break;
  74            case 'description':
  75              $headers['Content-Description'] = $value;
  76              break;
  77            case 'charset':
  78              if (isset($headers['Content-Type'])) {
  79                $headers['Content-Type'] .= '; charset="' . $value . '"';
  80              } else {
  81                $charset = $value;
  82              }
  83              break;
  84          }
  85        }
  86  
  87  // Default content-type

  88        if (!isset($_headers['Content-Type'])) {
  89          $_headers['Content-Type'] = 'text/plain';
  90        }
  91  
  92  // Assign stuff to member variables

  93        $this->_encoded = array();
  94  /* HPDL PHP3 */

  95  //      $this->_headers  =& $headers;

  96        $this->_headers = $headers;
  97        $this->_body = $body;
  98      }
  99  
 100  /**

 101   * encode()

 102   * 

 103   * Encodes and returns the email. Also stores

 104   * it in the encoded member variable

 105   *

 106   * @return An associative array containing two elements,

 107   *         body and headers. The headers element is itself

 108   *         an indexed array.

 109   * @access public

 110   */
 111  
 112      function encode() {
 113  /* HPDL PHP3 */

 114  //      $encoded =& $this->_encoded;

 115        $encoded = $this->_encoded;
 116  
 117        if (tep_not_null($this->_subparts)) {
 118          $boundary = '=_' . md5(uniqid(tep_rand()) . microtime());
 119          $this->_headers['Content-Type'] .= ';' . $this->lf . chr(9) . 'boundary="' . $boundary . '"';
 120  
 121  // Add body parts to $subparts

 122          for ($i=0; $i<count($this->_subparts); $i++) {
 123            $headers = array();
 124  /* HPDL PHP3 */

 125  //          $tmp = $this->_subparts[$i]->encode();

 126            $_subparts = $this->_subparts[$i];
 127            $tmp = $_subparts->encode();
 128  
 129            reset($tmp['headers']);
 130            while (list($key, $value) = each($tmp['headers'])) {
 131              $headers[] = $key . ': ' . $value;
 132            }
 133  
 134            $subparts[] = implode($this->lf, $headers) . $this->lf . $this->lf . $tmp['body'];
 135          }
 136  
 137          $encoded['body'] = '--' . $boundary . $this->lf . implode('--' . $boundary . $this->lf, $subparts) . '--' . $boundary.'--' . $this->lf;
 138        } else {
 139          $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . $this->lf;
 140        }
 141  
 142  // Add headers to $encoded

 143  /* HPDL PHP3 */

 144  //      $encoded['headers'] =& $this->_headers;

 145        $encoded['headers'] = $this->_headers;
 146  
 147        return $encoded;
 148      }
 149  
 150  /**

 151   * &addSubPart()

 152   * 

 153   * Adds a subpart to current mime part and returns

 154   * a reference to it

 155   *

 156   * @param $body   The body of the subpart, if any.

 157   * @param $params The parameters for the subpart, same

 158   *                as the $params argument for constructor.

 159   * @return A reference to the part you just added. It is

 160   *         crucial if using multipart/* in your subparts that

 161   *         you use =& in your script when calling this function,

 162   *         otherwise you will not be able to add further subparts.

 163   * @access public

 164   */
 165  
 166  /* HPDL PHP3 */

 167  //    function &addSubPart($body, $params) {

 168      function addSubPart($body, $params) {
 169        $this->_subparts[] = new mime($body, $params);
 170  
 171        return $this->_subparts[count($this->_subparts) - 1];
 172      }
 173  
 174  /**

 175   * _getEncodedData()

 176   * 

 177   * Returns encoded data based upon encoding passed to it

 178   *

 179   * @param $data     The data to encode.

 180   * @param $encoding The encoding type to use, 7bit, base64,

 181   *                  or quoted-printable.

 182   * @access private

 183   */
 184  
 185      function _getEncodedData($data, $encoding) {
 186        switch ($encoding) {
 187         case '7bit':
 188           return $data;
 189           break;
 190         case 'quoted-printable':
 191           return $this->_quotedPrintableEncode($data);
 192           break;
 193         case 'base64':
 194           return rtrim(chunk_split(base64_encode($data), 76, $this->lf));
 195           break;
 196        }
 197      }
 198  
 199  /**

 200   * quoteadPrintableEncode()

 201   * 

 202   * Encodes data to quoted-printable standard.

 203   *

 204   * @param $input    The data to encode

 205   * @param $line_max Optional max line length. Should 

 206   *                  not be more than 76 chars

 207   *

 208   * @access private

 209   */
 210  
 211      function _quotedPrintableEncode($input , $line_max = 76) {
 212        $lines = preg_split("/\r\n|\r|\n/", $input);
 213        $eol = $this->lf;
 214        $escape = '=';
 215        $output = '';
 216  
 217        while (list(, $line) = each($lines)) {
 218          $linlen = strlen($line);
 219          $newline = '';
 220  
 221          for ($i = 0; $i < $linlen; $i++) {
 222            $char = substr($line, $i, 1);
 223            $dec = ord($char);
 224  
 225  // convert space at eol only

 226            if ( ($dec == 32) && ($i == ($linlen - 1)) ) {
 227              $char = '=20';
 228            } elseif ($dec == 9) {
 229  // Do nothing if a tab.

 230            } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) {
 231              $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
 232            }
 233  
 234  // $this->lf is not counted

 235            if ((strlen($newline) + strlen($char)) >= $line_max) {
 236  // soft line break; " =\r\n" is okay

 237              $output .= $newline . $escape . $eol;
 238              $newline = '';
 239            }
 240            $newline .= $char;
 241          }
 242          $output .= $newline . $eol;
 243        }
 244  // Don't want last crlf

 245        $output = substr($output, 0, -1 * strlen($eol));
 246  
 247        return $output;
 248      }
 249    }
 250  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7