PageRenderTime 94ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 34ms app.codeStats 1ms

/library/Zend/Mime.php

https://bitbucket.org/baruffaldi/webapp-urltube
PHP | 252 lines | 136 code | 21 blank | 95 comment | 11 complexity | b0ff066547446a375e2ff39508234e92 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Mime
 17 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 */
 20
 21
 22/**
 23 * Support class for MultiPart Mime Messages
 24 *
 25 * @category   Zend
 26 * @package    Zend_Mime
 27 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 28 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 29 */
 30class Zend_Mime
 31{
 32    const TYPE_OCTETSTREAM = 'application/octet-stream';
 33    const TYPE_TEXT = 'text/plain';
 34    const TYPE_HTML = 'text/html';
 35    const ENCODING_7BIT = '7bit';
 36    const ENCODING_8BIT = '8bit';
 37    const ENCODING_QUOTEDPRINTABLE = 'quoted-printable';
 38    const ENCODING_BASE64 = 'base64';
 39    const DISPOSITION_ATTACHMENT = 'attachment';
 40    const DISPOSITION_INLINE = 'inline';
 41    const LINELENGTH = 74;
 42    const LINEEND = "\n";
 43    const MULTIPART_ALTERNATIVE = 'multipart/alternative';
 44    const MULTIPART_MIXED = 'multipart/mixed';
 45    const MULTIPART_RELATED = 'multipart/related';
 46
 47    protected $_boundary;
 48    protected static $makeUnique = 0;
 49
 50    // lookup-Tables for QuotedPrintable
 51    public static $qpKeys = array(
 52        "\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
 53        "\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F",
 54        "\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
 55        "\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F",
 56        "\x7F","\x80","\x81","\x82","\x83","\x84","\x85","\x86",
 57        "\x87","\x88","\x89","\x8A","\x8B","\x8C","\x8D","\x8E",
 58        "\x8F","\x90","\x91","\x92","\x93","\x94","\x95","\x96",
 59        "\x97","\x98","\x99","\x9A","\x9B","\x9C","\x9D","\x9E",
 60        "\x9F","\xA0","\xA1","\xA2","\xA3","\xA4","\xA5","\xA6",
 61        "\xA7","\xA8","\xA9","\xAA","\xAB","\xAC","\xAD","\xAE",
 62        "\xAF","\xB0","\xB1","\xB2","\xB3","\xB4","\xB5","\xB6",
 63        "\xB7","\xB8","\xB9","\xBA","\xBB","\xBC","\xBD","\xBE",
 64        "\xBF","\xC0","\xC1","\xC2","\xC3","\xC4","\xC5","\xC6",
 65        "\xC7","\xC8","\xC9","\xCA","\xCB","\xCC","\xCD","\xCE",
 66        "\xCF","\xD0","\xD1","\xD2","\xD3","\xD4","\xD5","\xD6",
 67        "\xD7","\xD8","\xD9","\xDA","\xDB","\xDC","\xDD","\xDE",
 68        "\xDF","\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6",
 69        "\xE7","\xE8","\xE9","\xEA","\xEB","\xEC","\xED","\xEE",
 70        "\xEF","\xF0","\xF1","\xF2","\xF3","\xF4","\xF5","\xF6",
 71        "\xF7","\xF8","\xF9","\xFA","\xFB","\xFC","\xFD","\xFE",
 72        "\xFF"
 73        );
 74
 75    public static $qpReplaceValues = array(
 76        "=00","=01","=02","=03","=04","=05","=06","=07",
 77        "=08","=09","=0A","=0B","=0C","=0D","=0E","=0F",
 78        "=10","=11","=12","=13","=14","=15","=16","=17",
 79        "=18","=19","=1A","=1B","=1C","=1D","=1E","=1F",
 80        "=7F","=80","=81","=82","=83","=84","=85","=86",
 81        "=87","=88","=89","=8A","=8B","=8C","=8D","=8E",
 82        "=8F","=90","=91","=92","=93","=94","=95","=96",
 83        "=97","=98","=99","=9A","=9B","=9C","=9D","=9E",
 84        "=9F","=A0","=A1","=A2","=A3","=A4","=A5","=A6",
 85        "=A7","=A8","=A9","=AA","=AB","=AC","=AD","=AE",
 86        "=AF","=B0","=B1","=B2","=B3","=B4","=B5","=B6",
 87        "=B7","=B8","=B9","=BA","=BB","=BC","=BD","=BE",
 88        "=BF","=C0","=C1","=C2","=C3","=C4","=C5","=C6",
 89        "=C7","=C8","=C9","=CA","=CB","=CC","=CD","=CE",
 90        "=CF","=D0","=D1","=D2","=D3","=D4","=D5","=D6",
 91        "=D7","=D8","=D9","=DA","=DB","=DC","=DD","=DE",
 92        "=DF","=E0","=E1","=E2","=E3","=E4","=E5","=E6",
 93        "=E7","=E8","=E9","=EA","=EB","=EC","=ED","=EE",
 94        "=EF","=F0","=F1","=F2","=F3","=F4","=F5","=F6",
 95        "=F7","=F8","=F9","=FA","=FB","=FC","=FD","=FE",
 96        "=FF"
 97        );
 98
 99    public static $qpKeysString =
100         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
101
102    /**
103     * Check if the given string is "printable"
104     *
105     * Checks that a string contains no unprintable characters. If this returns
106     * false, encode the string for secure delivery.
107     *
108     * @param string $str
109     * @return boolean
110     */
111    public static function isPrintable($str)
112    {
113        return (strcspn($str, self::$qpKeysString) == strlen($str));
114    }
115
116    /**
117     * Encode a given string with the QUOTED_PRINTABLE mechanism
118     *
119     * @param string $str
120     * @param int $lineLength Defaults to {@link LINELENGTH}
121     * @param int $lineEnd Defaults to {@link LINEEND}
122     * @return string
123     */
124    public static function encodeQuotedPrintable($str,
125        $lineLength = self::LINELENGTH,
126        $lineEnd = self::LINEEND)
127    {
128        $out = '';
129        $str = str_replace('=', '=3D', $str);
130        $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
131        $str = rtrim($str);
132
133        // Split encoded text into separate lines
134        while ($str) {
135            $ptr = strlen($str);
136            if ($ptr > $lineLength) {
137                $ptr = $lineLength;
138            }
139
140            // Ensure we are not splitting across an encoded character
141            $pos = strrpos(substr($str, 0, $ptr), '=');
142            if ($pos !== false && $pos >= $ptr - 2) {
143                $ptr = $pos;
144            }
145
146            // Check if there is a space at the end of the line and rewind
147            if ($ptr > 0 && $str[$ptr - 1] == ' ') {
148                --$ptr;
149            }
150
151            // Add string and continue
152            $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
153            $str = substr($str, $ptr);
154        }
155
156        $out = rtrim($out, $lineEnd);
157        $out = rtrim($out, '=');
158        return $out;
159    }
160
161    /**
162     * Encode a given string in base64 encoding and break lines
163     * according to the maximum linelength.
164     *
165     * @param string $str
166     * @param int $lineLength Defaults to {@link LINELENGTH}
167     * @param int $lineEnd Defaults to {@link LINEEND}
168     * @return string
169     */
170    public static function encodeBase64($str,
171        $lineLength = self::LINELENGTH,
172        $lineEnd = self::LINEEND)
173    {
174        return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
175    }
176
177    /**
178     * Constructor
179     *
180     * @param null|string $boundary
181     * @access public
182     * @return void
183     */
184    public function __construct($boundary = null)
185    {
186        // This string needs to be somewhat unique
187        if ($boundary === null) {
188            $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++);
189        } else {
190            $this->_boundary = $boundary;
191        }
192    }
193
194    /**
195     * Encode the given string with the given encoding.
196     *
197     * @param string $str
198     * @param string $encoding
199     * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
200     * @return string
201     */
202    public static function encode($str, $encoding, $EOL = self::LINEEND)
203    {
204        switch ($encoding) {
205            case self::ENCODING_BASE64:
206                return self::encodeBase64($str, self::LINELENGTH, $EOL);
207
208            case self::ENCODING_QUOTEDPRINTABLE:
209                return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL);
210
211            default:
212                /**
213                 * @todo 7Bit and 8Bit is currently handled the same way.
214                 */
215                return $str;
216        }
217    }
218
219    /**
220     * Return a MIME boundary
221     *
222     * @access public
223     * @return string
224     */
225    public function boundary()
226    {
227        return $this->_boundary;
228    }
229
230    /**
231     * Return a MIME boundary line
232     *
233     * @param mixed $EOL Defaults to {@link LINEEND}
234     * @access public
235     * @return string
236     */
237    public function boundaryLine($EOL = self::LINEEND)
238    {
239        return $EOL . '--' . $this->_boundary . $EOL;
240    }
241
242    /**
243     * Return MIME ending
244     *
245     * @access public
246     * @return string
247     */
248    public function mimeEnd($EOL = self::LINEEND)
249    {
250        return $EOL . '--' . $this->_boundary . '--' . $EOL;
251    }
252}