PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/htdocs/includes/nusoap/lib/Mail/mimePart.php

https://bitbucket.org/speedealing/speedealing
PHP | 350 lines | 136 code | 37 blank | 177 comment | 21 complexity | 2d2941bb2551e54d2c5d091a4aded9cd MD5 | raw file
  1<?php
  2// +-----------------------------------------------------------------------+
  3// | Copyright (c) 2002-2003  Richard Heyes                                     |
  4// | All rights reserved.                                                  |
  5// |                                                                       |
  6// | Redistribution and use in source and binary forms, with or without    |
  7// | modification, are permitted provided that the following conditions    |
  8// | are met:                                                              |
  9// |                                                                       |
 10// | o Redistributions of source code must retain the above copyright      |
 11// |   notice, this list of conditions and the following disclaimer.       |
 12// | o Redistributions in binary form must reproduce the above copyright   |
 13// |   notice, this list of conditions and the following disclaimer in the |
 14// |   documentation and/or other materials provided with the distribution.|
 15// | o The names of the authors may not be used to endorse or promote      |
 16// |   products derived from this software without specific prior written  |
 17// |   permission.                                                         |
 18// |                                                                       |
 19// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
 20// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
 21// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
 22// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
 23// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
 24// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
 25// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
 26// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
 27// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
 28// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
 29// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
 30// |                                                                       |
 31// +-----------------------------------------------------------------------+
 32// | Author: Richard Heyes <richard@phpguru.org>                           |
 33// +-----------------------------------------------------------------------+
 34
 35/**
 36*
 37*  Raw mime encoding class
 38*
 39* What is it?
 40*   This class enables you to manipulate and build
 41*   a mime email from the ground up.
 42*
 43* Why use this instead of mime.php?
 44*   mime.php is a userfriendly api to this class for
 45*   people who aren't interested in the internals of
 46*   mime mail. This class however allows full control
 47*   over the email.
 48*
 49* Eg.
 50*
 51* // Since multipart/mixed has no real body, (the body is
 52* // the subpart), we set the body argument to blank.
 53*
 54* $params['content_type'] = 'multipart/mixed';
 55* $email = new Mail_mimePart('', $params);
 56*
 57* // Here we add a text part to the multipart we have
 58* // already. Assume $body contains plain text.
 59*
 60* $params['content_type'] = 'text/plain';
 61* $params['encoding']     = '7bit';
 62* $text = $email->addSubPart($body, $params);
 63*
 64* // Now add an attachment. Assume $attach is
 65* the contents of the attachment
 66*
 67* $params['content_type'] = 'application/zip';
 68* $params['encoding']     = 'base64';
 69* $params['disposition']  = 'attachment';
 70* $params['dfilename']    = 'example.zip';
 71* $attach =& $email->addSubPart($body, $params);
 72*
 73* // Now build the email. Note that the encode
 74* // function returns an associative array containing two
 75* // elements, body and headers. You will need to add extra
 76* // headers, (eg. Mime-Version) before sending.
 77*
 78* $email = $message->encode();
 79* $email['headers'][] = 'Mime-Version: 1.0';
 80*
 81*
 82* Further examples are available at http://www.phpguru.org
 83*
 84* TODO:
 85*  - Set encode() to return the $obj->encoded if encode()
 86*    has already been run. Unless a flag is passed to specifically
 87*    re-build the message.
 88*
 89* @author  Richard Heyes <richard@phpguru.org>
 90* @package Mail
 91*/
 92
 93class Mail_mimePart {
 94
 95   /**
 96    * The encoding type of this part
 97    * @var string
 98    */
 99    var $_encoding;
100
101   /**
102    * An array of subparts
103    * @var array
104    */
105    var $_subparts;
106
107   /**
108    * The output of this part after being built
109    * @var string
110    */
111    var $_encoded;
112
113   /**
114    * Headers for this part
115    * @var array
116    */
117    var $_headers;
118
119   /**
120    * The body of this part (not encoded)
121    * @var string
122    */
123    var $_body;
124
125    /**
126     * Constructor.
127     *
128     * Sets up the object.
129     *
130     * @param $body   - The body of the mime part if any.
131     * @param $params - An associative array of parameters:
132     *                  content_type - The content type for this part eg multipart/mixed
133     *                  encoding     - The encoding to use, 7bit, 8bit, base64, or quoted-printable
134     *                  cid          - Content ID to apply
135     *                  disposition  - Content disposition, inline or attachment
136     *                  dfilename    - Optional filename parameter for content disposition
137     *                  description  - Content description
138     *                  charset      - Character set to use
139     * @access public
140     */
141    function Mail_mimePart($body = '', $params = array())
142    {
143        if (!defined('MAIL_MIMEPART_CRLF')) {
144            define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", TRUE);
145        }
146
147        foreach ($params as $key => $value) {
148            switch ($key) {
149                case 'content_type':
150                    $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
151                    break;
152
153                case 'encoding':
154                    $this->_encoding = $value;
155                    $headers['Content-Transfer-Encoding'] = $value;
156                    break;
157
158                case 'cid':
159                    $headers['Content-ID'] = '<' . $value . '>';
160                    break;
161
162                case 'disposition':
163                    $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
164                    break;
165
166                case 'dfilename':
167                    if (isset($headers['Content-Disposition'])) {
168                        $headers['Content-Disposition'] .= '; filename="' . $value . '"';
169                    } else {
170                        $dfilename = $value;
171                    }
172                    break;
173
174                case 'description':
175                    $headers['Content-Description'] = $value;
176                    break;
177
178                case 'charset':
179                    if (isset($headers['Content-Type'])) {
180                        $headers['Content-Type'] .= '; charset="' . $value . '"';
181                    } else {
182                        $charset = $value;
183                    }
184                    break;
185            }
186        }
187
188        // Default content-type
189        if (!isset($headers['Content-Type'])) {
190            $headers['Content-Type'] = 'text/plain';
191        }
192
193        //Default encoding
194        if (!isset($this->_encoding)) {
195            $this->_encoding = '7bit';
196        }
197
198        // Assign stuff to member variables
199        $this->_encoded  = array();
200        $this->_headers  = $headers;
201        $this->_body     = $body;
202    }
203
204    /**
205     * encode()
206     *
207     * Encodes and returns the email. Also stores
208     * it in the encoded member variable
209     *
210     * @return An associative array containing two elements,
211     *         body and headers. The headers element is itself
212     *         an indexed array.
213     * @access public
214     */
215    function encode()
216    {
217        $encoded =& $this->_encoded;
218
219        if (!empty($this->_subparts)) {
220            srand((double)microtime()*1000000);
221            $boundary = '=_' . md5(rand() . microtime());
222            $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"';
223
224            // Add body parts to $subparts
225            for ($i = 0; $i < count($this->_subparts); $i++) {
226                $headers = array();
227                $tmp = $this->_subparts[$i]->encode();
228                foreach ($tmp['headers'] as $key => $value) {
229                    $headers[] = $key . ': ' . $value;
230                }
231                $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
232            }
233
234            $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .
235                               implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) .
236                               '--' . $boundary.'--' . MAIL_MIMEPART_CRLF;
237
238        } else {
239            $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF;
240        }
241
242        // Add headers to $encoded
243        $encoded['headers'] =& $this->_headers;
244
245        return $encoded;
246    }
247
248    /**
249     * &addSubPart()
250     *
251     * Adds a subpart to current mime part and returns
252     * a reference to it
253     *
254     * @param $body   The body of the subpart, if any.
255     * @param $params The parameters for the subpart, same
256     *                as the $params argument for constructor.
257     * @return A reference to the part you just added. It is
258     *         crucial if using multipart/* in your subparts that
259     *         you use =& in your script when calling this function,
260     *         otherwise you will not be able to add further subparts.
261     * @access public
262     */
263    function &addSubPart($body, $params)
264    {
265        $this->_subparts[] = new Mail_mimePart($body, $params);
266        return $this->_subparts[count($this->_subparts) - 1];
267    }
268
269    /**
270     * _getEncodedData()
271     *
272     * Returns encoded data based upon encoding passed to it
273     *
274     * @param $data     The data to encode.
275     * @param $encoding The encoding type to use, 7bit, base64,
276     *                  or quoted-printable.
277     * @access private
278     */
279    function _getEncodedData($data, $encoding)
280    {
281        switch ($encoding) {
282            case '8bit':
283            case '7bit':
284                return $data;
285                break;
286
287            case 'quoted-printable':
288                return $this->_quotedPrintableEncode($data);
289                break;
290
291            case 'base64':
292                return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF));
293                break;
294
295            default:
296                return $data;
297        }
298    }
299
300    /**
301     * quoteadPrintableEncode()
302     *
303     * Encodes data to quoted-printable standard.
304     *
305     * @param $input    The data to encode
306     * @param $line_max Optional max line length. Should
307     *                  not be more than 76 chars
308     *
309     * @access private
310     */
311    function _quotedPrintableEncode($input , $line_max = 76)
312    {
313        $lines  = preg_split("/\r?\n/", $input);
314        $eol    = MAIL_MIMEPART_CRLF;
315        $escape = '=';
316        $output = '';
317
318        while(list(, $line) = each($lines)){
319
320            $linlen     = strlen($line);
321            $newline = '';
322
323            for ($i = 0; $i < $linlen; $i++) {
324                $char = substr($line, $i, 1);
325                $dec  = ord($char);
326
327                if (($dec == 32) AND ($i == ($linlen - 1))){    // convert space at eol only
328                    $char = '=20';
329
330                } elseif(($dec == 9) AND ($i == ($linlen - 1))) {  // convert tab at eol only
331                    $char = '=09';
332                } elseif($dec == 9) {
333                    ; // Do nothing if a tab.
334                } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
335                    $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
336                }
337
338                if ((strlen($newline) + strlen($char)) >= $line_max) {        // MAIL_MIMEPART_CRLF is not counted
339                    $output  .= $newline . $escape . $eol;                    // soft line break; " =\r\n" is okay
340                    $newline  = '';
341                }
342                $newline .= $char;
343            } // end of for
344            $output .= $newline . $eol;
345        }
346        $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf
347        return $output;
348    }
349} // End of class
350?>