annotate vendor/pear/mail_mime/Mail/mimePart.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1 <?php
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3 * The Mail_mimePart class is used to create MIME E-mail messages
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
5 * This class enables you to manipulate and build a mime email
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
6 * from the ground up. The Mail_Mime class is a userfriendly api
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
7 * to this class for people who aren't interested in the internals
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
8 * of mime mail.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
9 * This class however allows full control over the email.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
10 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
11 * Compatible with PHP version 5 and 7
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
12 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
13 * LICENSE: This LICENSE is in the BSD license style.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
14 * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
15 * Copyright (c) 2003-2006, PEAR <pear-group@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
16 * All rights reserved.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
17 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
18 * Redistribution and use in source and binary forms, with or
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
19 * without modification, are permitted provided that the following
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
20 * conditions are met:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
21 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
22 * - Redistributions of source code must retain the above copyright
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
23 * notice, this list of conditions and the following disclaimer.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
24 * - Redistributions in binary form must reproduce the above copyright
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
25 * notice, this list of conditions and the following disclaimer in the
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
26 * documentation and/or other materials provided with the distribution.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
27 * - Neither the name of the authors, nor the names of its contributors
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
28 * may be used to endorse or promote products derived from this
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
29 * software without specific prior written permission.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
30 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
41 * THE POSSIBILITY OF SUCH DAMAGE.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
42 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
43 * @category Mail
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
44 * @package Mail_Mime
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
45 * @author Richard Heyes <richard@phpguru.org>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
46 * @author Cipriano Groenendal <cipri@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
47 * @author Sean Coates <sean@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
48 * @author Aleksander Machniak <alec@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
49 * @copyright 2003-2006 PEAR <pear-group@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
50 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
51 * @version Release: @package_version@
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
52 * @link http://pear.php.net/package/Mail_mime
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
53 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
54
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
55 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
56 * Require PEAR
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
57 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
58 * This package depends on PEAR to raise errors.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
59 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
60 require_once 'PEAR.php';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
61
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
62 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
63 * The Mail_mimePart class is used to create MIME E-mail messages
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
64 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
65 * This class enables you to manipulate and build a mime email
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
66 * from the ground up. The Mail_Mime class is a userfriendly api
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
67 * to this class for people who aren't interested in the internals
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
68 * of mime mail.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
69 * This class however allows full control over the email.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
70 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
71 * @category Mail
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
72 * @package Mail_Mime
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
73 * @author Richard Heyes <richard@phpguru.org>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
74 * @author Cipriano Groenendal <cipri@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
75 * @author Sean Coates <sean@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
76 * @author Aleksander Machniak <alec@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
77 * @copyright 2003-2006 PEAR <pear-group@php.net>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
78 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
79 * @version Release: @package_version@
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
80 * @link http://pear.php.net/package/Mail_mime
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
81 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
82 class Mail_mimePart
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
83 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
84 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
85 * The encoding type of this part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
86 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
87 * @var string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
88 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
89 protected $encoding;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
90
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
91 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
92 * An array of subparts
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
93 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
94 * @var array
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
95 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
96 protected $subparts = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
97
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
98 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
99 * The output of this part after being built
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
100 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
101 * @var string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
102 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
103 protected $encoded;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
104
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
105 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
106 * Headers for this part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
107 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
108 * @var array
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
109 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
110 protected $headers = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
111
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
112 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
113 * The body of this part (not encoded)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
114 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
115 * @var string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
116 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
117 protected $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
118
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
119 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
120 * The location of file with body of this part (not encoded)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
121 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
122 * @var string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
123 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
124 protected $body_file;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
125
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
126 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
127 * The short text of multipart part preamble (RFC2046 5.1.1)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
128 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
129 * @var string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
130 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
131 protected $preamble;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
132
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
133 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
134 * The end-of-line sequence
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
135 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
136 * @var string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
137 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
138 protected $eol = "\r\n";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
139
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
140
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
141 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
142 * Constructor.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
143 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
144 * Sets up the object.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
145 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
146 * @param string $body The body of the mime part if any.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
147 * @param array $params An associative array of optional parameters:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
148 * content_type - The content type for this part eg multipart/mixed
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
149 * encoding - The encoding to use, 7bit, 8bit,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
150 * base64, or quoted-printable
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
151 * charset - Content character set
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
152 * cid - Content ID to apply
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
153 * disposition - Content disposition, inline or attachment
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
154 * filename - Filename parameter for content disposition
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
155 * description - Content description
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
156 * name_encoding - Encoding of the attachment name (Content-Type)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
157 * By default filenames are encoded using RFC2231
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
158 * Here you can set RFC2047 encoding (quoted-printable
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
159 * or base64) instead
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
160 * filename_encoding - Encoding of the attachment filename (Content-Disposition)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
161 * See 'name_encoding'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
162 * headers_charset - Charset of the headers e.g. filename, description.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
163 * If not set, 'charset' will be used
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
164 * eol - End of line sequence. Default: "\r\n"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
165 * headers - Hash array with additional part headers. Array keys
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
166 * can be in form of <header_name>:<parameter_name>
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
167 * body_file - Location of file with part's body (instead of $body)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
168 * preamble - short text of multipart part preamble (RFC2046 5.1.1)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
169 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
170 public function __construct($body = '', $params = array())
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
171 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
172 if (!empty($params['eol'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
173 $this->eol = $params['eol'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
174 } else if (defined('MAIL_MIMEPART_CRLF')) { // backward-copat.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
175 $this->eol = MAIL_MIMEPART_CRLF;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
176 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
177
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
178 // Additional part headers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
179 if (!empty($params['headers']) && is_array($params['headers'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
180 $headers = $params['headers'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
181 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
182
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
183 foreach ($params as $key => $value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
184 switch ($key) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
185 case 'encoding':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
186 $this->encoding = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
187 $headers['Content-Transfer-Encoding'] = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
188 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
189
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
190 case 'cid':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
191 $headers['Content-ID'] = '<' . $value . '>';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
192 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
193
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
194 case 'location':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
195 $headers['Content-Location'] = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
196 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
197
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
198 case 'body_file':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
199 $this->body_file = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
200 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
201
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
202 case 'preamble':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
203 $this->preamble = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
204 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
205
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
206 // for backward compatibility
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
207 case 'dfilename':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
208 $params['filename'] = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
209 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
210 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
211 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
212
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
213 // Default content-type
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
214 if (empty($params['content_type'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
215 $params['content_type'] = 'text/plain';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
216 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
217
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
218 // Content-Type
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
219 $headers['Content-Type'] = $params['content_type'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
220 if (!empty($params['charset'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
221 $charset = "charset={$params['charset']}";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
222 // place charset parameter in the same line, if possible
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
223 if ((strlen($headers['Content-Type']) + strlen($charset) + 16) <= 76) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
224 $headers['Content-Type'] .= '; ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
225 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
226 $headers['Content-Type'] .= ';' . $this->eol . ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
227 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
228 $headers['Content-Type'] .= $charset;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
229
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
230 // Default headers charset
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
231 if (!isset($params['headers_charset'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
232 $params['headers_charset'] = $params['charset'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
233 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
234 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
235
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
236 // header values encoding parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
237 $h_charset = !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
238 $h_language = !empty($params['language']) ? $params['language'] : null;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
239 $h_encoding = !empty($params['name_encoding']) ? $params['name_encoding'] : null;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
240
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
241 if (!empty($params['filename'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
242 $headers['Content-Type'] .= ';' . $this->eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
243 $headers['Content-Type'] .= $this->buildHeaderParam(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
244 'name', $params['filename'], $h_charset, $h_language, $h_encoding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
245 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
246 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
247
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
248 // Content-Disposition
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
249 if (!empty($params['disposition'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
250 $headers['Content-Disposition'] = $params['disposition'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
251 if (!empty($params['filename'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
252 $headers['Content-Disposition'] .= ';' . $this->eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
253 $headers['Content-Disposition'] .= $this->buildHeaderParam(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
254 'filename', $params['filename'], $h_charset, $h_language,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
255 !empty($params['filename_encoding']) ? $params['filename_encoding'] : null
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
256 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
257 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
258
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
259 // add attachment size
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
260 $size = $this->body_file ? filesize($this->body_file) : strlen($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
261 if ($size) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
262 $headers['Content-Disposition'] .= ';' . $this->eol . ' size=' . $size;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
263 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
264 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
265
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
266 if (!empty($params['description'])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
267 $headers['Content-Description'] = $this->encodeHeader(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
268 'Content-Description', $params['description'], $h_charset, $h_encoding,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
269 $this->eol
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
270 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
271 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
272
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
273 // Search and add existing headers' parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
274 foreach ($headers as $key => $value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
275 $items = explode(':', $key);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
276 if (count($items) == 2) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
277 $header = $items[0];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
278 $param = $items[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
279 if (isset($headers[$header])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
280 $headers[$header] .= ';' . $this->eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
281 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
282 $headers[$header] .= $this->buildHeaderParam(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
283 $param, $value, $h_charset, $h_language, $h_encoding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
284 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
285 unset($headers[$key]);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
286 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
287 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
288
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
289 // Default encoding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
290 if (!isset($this->encoding)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
291 $this->encoding = '7bit';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
292 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
293
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
294 // Assign stuff to member variables
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
295 $this->encoded = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
296 $this->headers = $headers;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
297 $this->body = $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
298 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
299
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
300 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
301 * Encodes and returns the email. Also stores
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
302 * it in the encoded member variable
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
303 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
304 * @param string $boundary Pre-defined boundary string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
305 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
306 * @return An associative array containing two elements,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
307 * body and headers. The headers element is itself
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
308 * an indexed array. On error returns PEAR error object.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
309 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
310 public function encode($boundary=null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
311 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
312 $encoded =& $this->encoded;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
313
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
314 if (count($this->subparts)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
315 $boundary = $boundary ? $boundary : '=_' . md5(rand() . microtime());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
316 $eol = $this->eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
317
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
318 $this->headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
319
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
320 $encoded['body'] = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
321
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
322 if ($this->preamble) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
323 $encoded['body'] .= $this->preamble . $eol . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
324 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
325
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
326 for ($i = 0; $i < count($this->subparts); $i++) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
327 $encoded['body'] .= '--' . $boundary . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
328 $tmp = $this->subparts[$i]->encode();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
329 if (is_a($tmp, 'PEAR_Error')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
330 return $tmp;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
331 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
332 foreach ($tmp['headers'] as $key => $value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
333 $encoded['body'] .= $key . ': ' . $value . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
334 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
335 $encoded['body'] .= $eol . $tmp['body'] . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
336 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
337
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
338 $encoded['body'] .= '--' . $boundary . '--' . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
339 } else if ($this->body) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
340 $encoded['body'] = $this->getEncodedData($this->body, $this->encoding);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
341 } else if ($this->body_file) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
342 // Temporarily reset magic_quotes_runtime for file reads and writes
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
343 if ($magic_quote_setting = get_magic_quotes_runtime()) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
344 @ini_set('magic_quotes_runtime', 0);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
345 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
346 $body = $this->getEncodedDataFromFile($this->body_file, $this->encoding);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
347 if ($magic_quote_setting) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
348 @ini_set('magic_quotes_runtime', $magic_quote_setting);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
349 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
350
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
351 if (is_a($body, 'PEAR_Error')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
352 return $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
353 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
354 $encoded['body'] = $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
355 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
356 $encoded['body'] = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
357 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
358
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
359 // Add headers to $encoded
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
360 $encoded['headers'] =& $this->headers;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
361
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
362 return $encoded;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
363 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
364
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
365 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
366 * Encodes and saves the email into file or stream.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
367 * Data will be appended to the file/stream.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
368 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
369 * @param mixed $filename Existing file location
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
370 * or file pointer resource
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
371 * @param string $boundary Pre-defined boundary string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
372 * @param boolean $skip_head True if you don't want to save headers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
373 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
374 * @return array An associative array containing message headers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
375 * or PEAR error object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
376 * @since 1.6.0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
377 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
378 public function encodeToFile($filename, $boundary = null, $skip_head = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
379 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
380 if (!is_resource($filename)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
381 if (file_exists($filename) && !is_writable($filename)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
382 $err = self::raiseError('File is not writeable: ' . $filename);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
383 return $err;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
384 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
385
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
386 if (!($fh = fopen($filename, 'ab'))) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
387 $err = self::raiseError('Unable to open file: ' . $filename);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
388 return $err;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
389 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
390 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
391 $fh = $filename;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
392 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
393
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
394 // Temporarily reset magic_quotes_runtime for file reads and writes
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
395 if ($magic_quote_setting = get_magic_quotes_runtime()) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
396 @ini_set('magic_quotes_runtime', 0);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
397 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
398
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
399 $res = $this->encodePartToFile($fh, $boundary, $skip_head);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
400
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
401 if (!is_resource($filename)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
402 fclose($fh);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
403 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
404
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
405 if ($magic_quote_setting) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
406 @ini_set('magic_quotes_runtime', $magic_quote_setting);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
407 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
408
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
409 return is_a($res, 'PEAR_Error') ? $res : $this->headers;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
410 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
411
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
412 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
413 * Encodes given email part into file
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
414 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
415 * @param string $fh Output file handle
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
416 * @param string $boundary Pre-defined boundary string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
417 * @param boolean $skip_head True if you don't want to save headers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
418 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
419 * @return array True on sucess or PEAR error object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
420 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
421 protected function encodePartToFile($fh, $boundary = null, $skip_head = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
422 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
423 $eol = $this->eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
424
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
425 if (count($this->subparts)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
426 $boundary = $boundary ? $boundary : '=_' . md5(rand() . microtime());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
427 $this->headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
428 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
429
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
430 if (!$skip_head) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
431 foreach ($this->headers as $key => $value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
432 fwrite($fh, $key . ': ' . $value . $eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
433 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
434 $f_eol = $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
435 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
436 $f_eol = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
437 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
438
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
439 if (count($this->subparts)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
440 if ($this->preamble) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
441 fwrite($fh, $f_eol . $this->preamble . $eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
442 $f_eol = $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
443 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
444
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
445 for ($i = 0; $i < count($this->subparts); $i++) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
446 fwrite($fh, $f_eol . '--' . $boundary . $eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
447 $res = $this->subparts[$i]->encodePartToFile($fh);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
448 if (is_a($res, 'PEAR_Error')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
449 return $res;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
450 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
451 $f_eol = $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
452 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
453
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
454 fwrite($fh, $eol . '--' . $boundary . '--' . $eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
455 } else if ($this->body) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
456 fwrite($fh, $f_eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
457 fwrite($fh, $this->getEncodedData($this->body, $this->encoding));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
458 } else if ($this->body_file) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
459 fwrite($fh, $f_eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
460 $res = $this->getEncodedDataFromFile(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
461 $this->body_file, $this->encoding, $fh
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
462 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
463 if (is_a($res, 'PEAR_Error')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
464 return $res;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
465 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
466 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
467
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
468 return true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
469 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
470
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
471 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
472 * Adds a subpart to current mime part and returns
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
473 * a reference to it
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
474 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
475 * @param mixed $body The body of the subpart or Mail_mimePart object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
476 * @param array $params The parameters for the subpart, same
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
477 * as the $params argument for constructor
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
478 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
479 * @return Mail_mimePart A reference to the part you just added.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
480 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
481 public function addSubpart($body, $params = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
482 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
483 if ($body instanceof Mail_mimePart) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
484 $part = $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
485 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
486 $part = new Mail_mimePart($body, $params);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
487 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
488
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
489 $this->subparts[] = $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
490
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
491 return $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
492 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
493
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
494 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
495 * Returns encoded data based upon encoding passed to it
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
496 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
497 * @param string $data The data to encode.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
498 * @param string $encoding The encoding type to use, 7bit, base64,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
499 * or quoted-printable.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
500 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
501 * @return string Encoded data string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
502 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
503 protected function getEncodedData($data, $encoding)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
504 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
505 switch ($encoding) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
506 case 'quoted-printable':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
507 return self::quotedPrintableEncode($data, 76, $this->eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
508 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
509
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
510 case 'base64':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
511 return rtrim(chunk_split(base64_encode($data), 76, $this->eol));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
512 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
513
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
514 case '8bit':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
515 case '7bit':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
516 default:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
517 return $data;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
518 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
519 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
520
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
521 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
522 * Returns encoded data based upon encoding passed to it
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
523 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
524 * @param string $filename Data file location
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
525 * @param string $encoding The encoding type to use, 7bit, base64,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
526 * or quoted-printable.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
527 * @param resource $fh Output file handle. If set, data will be
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
528 * stored into it instead of returning it
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
529 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
530 * @return string Encoded data or PEAR error object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
531 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
532 protected function getEncodedDataFromFile($filename, $encoding, $fh = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
533 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
534 if (!is_readable($filename)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
535 $err = self::raiseError('Unable to read file: ' . $filename);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
536 return $err;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
537 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
538
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
539 if (!($fd = fopen($filename, 'rb'))) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
540 $err = self::raiseError('Could not open file: ' . $filename);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
541 return $err;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
542 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
543
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
544 $data = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
545
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
546 switch ($encoding) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
547 case 'quoted-printable':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
548 while (!feof($fd)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
549 $buffer = self::quotedPrintableEncode(fgets($fd), 76, $this->eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
550 if ($fh) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
551 fwrite($fh, $buffer);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
552 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
553 $data .= $buffer;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
554 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
555 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
556 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
557
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
558 case 'base64':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
559 while (!feof($fd)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
560 // Should read in a multiple of 57 bytes so that
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
561 // the output is 76 bytes per line. Don't use big chunks
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
562 // because base64 encoding is memory expensive
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
563 $buffer = fread($fd, 57 * 9198); // ca. 0.5 MB
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
564 $buffer = base64_encode($buffer);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
565 $buffer = chunk_split($buffer, 76, $this->eol);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
566 if (feof($fd)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
567 $buffer = rtrim($buffer);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
568 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
569
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
570 if ($fh) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
571 fwrite($fh, $buffer);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
572 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
573 $data .= $buffer;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
574 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
575 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
576 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
577
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
578 case '8bit':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
579 case '7bit':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
580 default:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
581 while (!feof($fd)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
582 $buffer = fread($fd, 1048576); // 1 MB
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
583 if ($fh) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
584 fwrite($fh, $buffer);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
585 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
586 $data .= $buffer;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
587 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
588 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
589 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
590
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
591 fclose($fd);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
592
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
593 if (!$fh) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
594 return $data;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
595 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
596 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
597
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
598 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
599 * Encodes data to quoted-printable standard.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
600 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
601 * @param string $input The data to encode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
602 * @param int $line_max Optional max line length. Should
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
603 * not be more than 76 chars
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
604 * @param string $eol End-of-line sequence. Default: "\r\n"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
605 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
606 * @return string Encoded data
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
607 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
608 public static function quotedPrintableEncode($input , $line_max = 76, $eol = "\r\n")
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
609 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
610 /*
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
611 // imap_8bit() is extremely fast, but doesn't handle properly some characters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
612 if (function_exists('imap_8bit') && $line_max == 76) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
613 $input = preg_replace('/\r?\n/', "\r\n", $input);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
614 $input = imap_8bit($input);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
615 if ($eol != "\r\n") {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
616 $input = str_replace("\r\n", $eol, $input);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
617 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
618 return $input;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
619 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
620 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
621 $lines = preg_split("/\r?\n/", $input);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
622 $escape = '=';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
623 $output = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
624
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
625 foreach ($lines as $idx => $line) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
626 $newline = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
627 $i = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
628
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
629 while (isset($line[$i])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
630 $char = $line[$i];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
631 $dec = ord($char);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
632 $i++;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
633
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
634 if (($dec == 32) && (!isset($line[$i]))) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
635 // convert space at eol only
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
636 $char = '=20';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
637 } elseif ($dec == 9 && isset($line[$i])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
638 ; // Do nothing if a TAB is not on eol
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
639 } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
640 $char = $escape . sprintf('%02X', $dec);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
641 } elseif (($dec == 46) && (($newline == '')
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
642 || ((strlen($newline) + strlen("=2E")) >= $line_max))
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
643 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
644 // Bug #9722: convert full-stop at bol,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
645 // some Windows servers need this, won't break anything (cipri)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
646 // Bug #11731: full-stop at bol also needs to be encoded
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
647 // if this line would push us over the line_max limit.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
648 $char = '=2E';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
649 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
650
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
651 // Note, when changing this line, also change the ($dec == 46)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
652 // check line, as it mimics this line due to Bug #11731
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
653 // EOL is not counted
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
654 if ((strlen($newline) + strlen($char)) >= $line_max) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
655 // soft line break; " =\r\n" is okay
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
656 $output .= $newline . $escape . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
657 $newline = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
658 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
659
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
660 $newline .= $char;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
661 } // end of for
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
662
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
663 $output .= $newline . $eol;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
664 unset($lines[$idx]);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
665 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
666
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
667 // Don't want last crlf
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
668 $output = substr($output, 0, -1 * strlen($eol));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
669
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
670 return $output;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
671 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
672
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
673 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
674 * Encodes the parameter of a header.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
675 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
676 * @param string $name The name of the header-parameter
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
677 * @param string $value The value of the paramter
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
678 * @param string $charset The characterset of $value
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
679 * @param string $language The language used in $value
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
680 * @param string $encoding Parameter encoding. If not set, parameter value
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
681 * is encoded according to RFC2231
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
682 * @param int $maxLength The maximum length of a line. Defauls to 75
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
683 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
684 * @return string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
685 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
686 protected function buildHeaderParam($name, $value, $charset = null,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
687 $language = null, $encoding = null, $maxLength = 75
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
688 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
689 // RFC 2045:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
690 // value needs encoding if contains non-ASCII chars or is longer than 78 chars
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
691 if (!preg_match('#[^\x20-\x7E]#', $value)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
692 $token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
693 . '\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
694 if (!preg_match($token_regexp, $value)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
695 // token
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
696 if (strlen($name) + strlen($value) + 3 <= $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
697 return " {$name}={$value}";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
698 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
699 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
700 // quoted-string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
701 $quoted = addcslashes($value, '\\"');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
702 if (strlen($name) + strlen($quoted) + 5 <= $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
703 return " {$name}=\"{$quoted}\"";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
704 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
705 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
706 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
707
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
708 // RFC2047: use quoted-printable/base64 encoding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
709 if ($encoding == 'quoted-printable' || $encoding == 'base64') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
710 return $this->buildRFC2047Param($name, $value, $charset, $encoding);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
711 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
712
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
713 // RFC2231:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
714 $encValue = preg_replace_callback(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
715 '/([^\x21\x23\x24\x26\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E])/',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
716 array($this, 'encodeReplaceCallback'), $value
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
717 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
718 $value = "$charset'$language'$encValue";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
719
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
720 $header = " {$name}*={$value}";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
721 if (strlen($header) <= $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
722 return $header;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
723 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
724
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
725 $preLength = strlen(" {$name}*0*=");
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
726 $maxLength = max(16, $maxLength - $preLength - 3);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
727 $maxLengthReg = "|(.{0,$maxLength}[^\%][^\%])|";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
728
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
729 $headers = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
730 $headCount = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
731 while ($value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
732 $matches = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
733 $found = preg_match($maxLengthReg, $value, $matches);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
734 if ($found) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
735 $headers[] = " {$name}*{$headCount}*={$matches[0]}";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
736 $value = substr($value, strlen($matches[0]));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
737 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
738 $headers[] = " {$name}*{$headCount}*={$value}";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
739 $value = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
740 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
741 $headCount++;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
742 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
743
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
744 $headers = implode(';' . $this->eol, $headers);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
745 return $headers;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
746 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
747
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
748 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
749 * Encodes header parameter as per RFC2047 if needed
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
750 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
751 * @param string $name The parameter name
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
752 * @param string $value The parameter value
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
753 * @param string $charset The parameter charset
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
754 * @param string $encoding Encoding type (quoted-printable or base64)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
755 * @param int $maxLength Encoded parameter max length. Default: 76
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
756 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
757 * @return string Parameter line
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
758 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
759 protected function buildRFC2047Param($name, $value, $charset,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
760 $encoding = 'quoted-printable', $maxLength = 76
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
761 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
762 // WARNING: RFC 2047 says: "An 'encoded-word' MUST NOT be used in
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
763 // parameter of a MIME Content-Type or Content-Disposition field",
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
764 // but... it's supported by many clients/servers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
765 $quoted = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
766
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
767 if ($encoding == 'base64') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
768 $value = base64_encode($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
769 $prefix = '=?' . $charset . '?B?';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
770 $suffix = '?=';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
771
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
772 // 2 x SPACE, 2 x '"', '=', ';'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
773 $add_len = strlen($prefix . $suffix) + strlen($name) + 6;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
774 $len = $add_len + strlen($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
775
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
776 while ($len > $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
777 // We can cut base64-encoded string every 4 characters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
778 $real_len = floor(($maxLength - $add_len) / 4) * 4;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
779 $_quote = substr($value, 0, $real_len);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
780 $value = substr($value, $real_len);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
781
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
782 $quoted .= $prefix . $_quote . $suffix . $this->eol . ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
783 $add_len = strlen($prefix . $suffix) + 4; // 2 x SPACE, '"', ';'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
784 $len = strlen($value) + $add_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
785 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
786 $quoted .= $prefix . $value . $suffix;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
787
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
788 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
789 // quoted-printable
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
790 $value = $this->encodeQP($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
791 $prefix = '=?' . $charset . '?Q?';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
792 $suffix = '?=';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
793
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
794 // 2 x SPACE, 2 x '"', '=', ';'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
795 $add_len = strlen($prefix . $suffix) + strlen($name) + 6;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
796 $len = $add_len + strlen($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
797
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
798 while ($len > $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
799 $length = $maxLength - $add_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
800 // don't break any encoded letters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
801 if (preg_match("/^(.{0,$length}[^\=][^\=])/", $value, $matches)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
802 $_quote = $matches[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
803 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
804
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
805 $quoted .= $prefix . $_quote . $suffix . $this->eol . ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
806 $value = substr($value, strlen($_quote));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
807 $add_len = strlen($prefix . $suffix) + 4; // 2 x SPACE, '"', ';'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
808 $len = strlen($value) + $add_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
809 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
810
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
811 $quoted .= $prefix . $value . $suffix;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
812 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
813
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
814 return " {$name}=\"{$quoted}\"";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
815 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
816
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
817 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
818 * Encodes a header as per RFC2047
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
819 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
820 * @param string $name The header name
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
821 * @param string $value The header data to encode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
822 * @param string $charset Character set name
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
823 * @param string $encoding Encoding name (base64 or quoted-printable)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
824 * @param string $eol End-of-line sequence. Default: "\r\n"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
825 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
826 * @return string Encoded header data (without a name)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
827 * @since 1.6.1
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
828 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
829 public static function encodeHeader($name, $value, $charset = 'ISO-8859-1',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
830 $encoding = 'quoted-printable', $eol = "\r\n"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
831 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
832 // Structured headers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
833 $comma_headers = array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
834 'from', 'to', 'cc', 'bcc', 'sender', 'reply-to',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
835 'resent-from', 'resent-to', 'resent-cc', 'resent-bcc',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
836 'resent-sender', 'resent-reply-to',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
837 'mail-reply-to', 'mail-followup-to',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
838 'return-receipt-to', 'disposition-notification-to',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
839 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
840 $other_headers = array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
841 'references', 'in-reply-to', 'message-id', 'resent-message-id',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
842 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
843
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
844 $name = strtolower($name);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
845
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
846 if (in_array($name, $comma_headers)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
847 $separator = ',';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
848 } else if (in_array($name, $other_headers)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
849 $separator = ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
850 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
851
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
852 if (!$charset) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
853 $charset = 'ISO-8859-1';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
854 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
855
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
856 // exploding quoted strings as well as some regexes below do not
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
857 // work properly with some charset e.g. ISO-2022-JP, we'll use UTF-8
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
858 $mb = $charset != 'UTF-8' && function_exists('mb_convert_encoding');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
859
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
860 // Structured header (make sure addr-spec inside is not encoded)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
861 if (!empty($separator)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
862 // Simple e-mail address regexp
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
863 $email_regexp = '([^\s<]+|("[^\r\n"]+"))@\S+';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
864
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
865 if ($mb) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
866 $value = mb_convert_encoding($value, 'UTF-8', $charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
867 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
868
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
869 $parts = Mail_mimePart::explodeQuotedString("[\t$separator]", $value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
870 $value = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
871
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
872 foreach ($parts as $part) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
873 $part = preg_replace('/\r?\n[\s\t]*/', $eol . ' ', $part);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
874 $part = trim($part);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
875
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
876 if (!$part) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
877 continue;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
878 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
879 if ($value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
880 $value .= $separator == ',' ? $separator . ' ' : ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
881 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
882 $value = $name . ': ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
883 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
884
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
885 // let's find phrase (name) and/or addr-spec
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
886 if (preg_match('/^<' . $email_regexp . '>$/', $part)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
887 $value .= $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
888 } else if (preg_match('/^' . $email_regexp . '$/', $part)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
889 // address without brackets and without name
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
890 $value .= $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
891 } else if (preg_match('/<*' . $email_regexp . '>*$/', $part, $matches)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
892 // address with name (handle name)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
893 $address = $matches[0];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
894 $word = str_replace($address, '', $part);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
895 $word = trim($word);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
896
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
897 // check if phrase requires quoting
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
898 if ($word) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
899 // non-ASCII: require encoding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
900 if (preg_match('#([^\s\x21-\x7E]){1}#', $word)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
901 if ($word[0] == '"' && $word[strlen($word)-1] == '"') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
902 // de-quote quoted-string, encoding changes
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
903 // string to atom
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
904 $word = substr($word, 1, -1);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
905 $word = preg_replace('/\\\\([\\\\"])/', '$1', $word);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
906 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
907 if ($mb) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
908 $word = mb_convert_encoding($word, $charset, 'UTF-8');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
909 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
910
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
911 // find length of last line
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
912 if (($pos = strrpos($value, $eol)) !== false) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
913 $last_len = strlen($value) - $pos;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
914 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
915 $last_len = strlen($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
916 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
917
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
918 $word = Mail_mimePart::encodeHeaderValue(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
919 $word, $charset, $encoding, $last_len, $eol
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
920 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
921 } else if (($word[0] != '"' || $word[strlen($word)-1] != '"')
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
922 && preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $word)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
923 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
924 // ASCII: quote string if needed
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
925 $word = '"'.addcslashes($word, '\\"').'"';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
926 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
927 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
928
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
929 $value .= $word.' '.$address;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
930 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
931 if ($mb) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
932 $part = mb_convert_encoding($part, $charset, 'UTF-8');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
933 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
934 // addr-spec not found, don't encode (?)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
935 $value .= $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
936 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
937
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
938 // RFC2822 recommends 78 characters limit, use 76 from RFC2047
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
939 $value = wordwrap($value, 76, $eol . ' ');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
940 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
941
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
942 // remove header name prefix (there could be EOL too)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
943 $value = preg_replace(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
944 '/^'.$name.':('.preg_quote($eol, '/').')* /', '', $value
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
945 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
946 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
947 // Unstructured header
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
948 // non-ASCII: require encoding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
949 if (preg_match('#([^\s\x21-\x7E]){1}#', $value)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
950 if ($value[0] == '"' && $value[strlen($value)-1] == '"') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
951 if ($mb) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
952 $value = mb_convert_encoding($value, 'UTF-8', $charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
953 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
954 // de-quote quoted-string, encoding changes
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
955 // string to atom
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
956 $value = substr($value, 1, -1);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
957 $value = preg_replace('/\\\\([\\\\"])/', '$1', $value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
958 if ($mb) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
959 $value = mb_convert_encoding($value, $charset, 'UTF-8');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
960 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
961 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
962
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
963 $value = Mail_mimePart::encodeHeaderValue(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
964 $value, $charset, $encoding, strlen($name) + 2, $eol
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
965 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
966 } else if (strlen($name.': '.$value) > 78) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
967 // ASCII: check if header line isn't too long and use folding
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
968 $value = preg_replace('/\r?\n[\s\t]*/', $eol . ' ', $value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
969 $tmp = wordwrap($name . ': ' . $value, 78, $eol . ' ');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
970 $value = preg_replace('/^' . $name . ':\s*/', '', $tmp);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
971 // hard limit 998 (RFC2822)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
972 $value = wordwrap($value, 998, $eol . ' ', true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
973 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
974 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
975
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
976 return $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
977 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
978
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
979 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
980 * Explode quoted string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
981 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
982 * @param string $delimiter Delimiter expression string for preg_match()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
983 * @param string $string Input string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
984 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
985 * @return array String tokens array
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
986 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
987 protected static function explodeQuotedString($delimiter, $string)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
988 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
989 $result = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
990 $strlen = strlen($string);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
991 $quoted_string = '"(?:[^"\\\\]|\\\\.)*"';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
992
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
993 for ($p=$i=0; $i < $strlen; $i++) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
994 if ($string[$i] === '"') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
995 $r = preg_match("/$quoted_string/", $string, $matches, 0, $i);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
996 if (!$r || empty($matches[0])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
997 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
998 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
999 $i += strlen($matches[0]) - 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1000 } else if (preg_match("/$delimiter/", $string[$i])) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1001 $result[] = substr($string, $p, $i - $p);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1002 $p = $i + 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1003 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1004 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1005 $result[] = substr($string, $p);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1006 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1007 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1008
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1009 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1010 * Encodes a header value as per RFC2047
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1011 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1012 * @param string $value The header data to encode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1013 * @param string $charset Character set name
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1014 * @param string $encoding Encoding name (base64 or quoted-printable)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1015 * @param int $prefix_len Prefix length. Default: 0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1016 * @param string $eol End-of-line sequence. Default: "\r\n"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1017 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1018 * @return string Encoded header data
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1019 * @since 1.6.1
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1020 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1021 public static function encodeHeaderValue($value, $charset, $encoding, $prefix_len = 0, $eol = "\r\n")
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1022 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1023 // #17311: Use multibyte aware method (requires mbstring extension)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1024 if ($result = Mail_mimePart::encodeMB($value, $charset, $encoding, $prefix_len, $eol)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1025 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1026 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1027
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1028 // Generate the header using the specified params and dynamicly
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1029 // determine the maximum length of such strings.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1030 // 75 is the value specified in the RFC.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1031 $encoding = $encoding == 'base64' ? 'B' : 'Q';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1032 $prefix = '=?' . $charset . '?' . $encoding .'?';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1033 $suffix = '?=';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1034 $maxLength = 75 - strlen($prefix . $suffix);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1035 $maxLength1stLine = $maxLength - $prefix_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1036
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1037 if ($encoding == 'B') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1038 // Base64 encode the entire string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1039 $value = base64_encode($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1040
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1041 // We can cut base64 every 4 characters, so the real max
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1042 // we can get must be rounded down.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1043 $maxLength = $maxLength - ($maxLength % 4);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1044 $maxLength1stLine = $maxLength1stLine - ($maxLength1stLine % 4);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1045
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1046 $cutpoint = $maxLength1stLine;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1047 $output = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1048
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1049 while ($value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1050 // Split translated string at every $maxLength
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1051 $part = substr($value, 0, $cutpoint);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1052 $value = substr($value, $cutpoint);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1053 $cutpoint = $maxLength;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1054 // RFC 2047 specifies that any split header should
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1055 // be separated by a CRLF SPACE.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1056 if ($output) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1057 $output .= $eol . ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1058 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1059 $output .= $prefix . $part . $suffix;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1060 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1061 $value = $output;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1062 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1063 // quoted-printable encoding has been selected
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1064 $value = Mail_mimePart::encodeQP($value);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1065
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1066 // This regexp will break QP-encoded text at every $maxLength
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1067 // but will not break any encoded letters.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1068 $reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1069 $reg2nd = "|(.{0,$maxLength}[^\=][^\=])|";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1070
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1071 if (strlen($value) > $maxLength1stLine) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1072 // Begin with the regexp for the first line.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1073 $reg = $reg1st;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1074 $output = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1075 while ($value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1076 // Split translated string at every $maxLength
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1077 // But make sure not to break any translated chars.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1078 $found = preg_match($reg, $value, $matches);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1079
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1080 // After this first line, we need to use a different
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1081 // regexp for the first line.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1082 $reg = $reg2nd;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1083
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1084 // Save the found part and encapsulate it in the
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1085 // prefix & suffix. Then remove the part from the
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1086 // $value_out variable.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1087 if ($found) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1088 $part = $matches[0];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1089 $len = strlen($matches[0]);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1090 $value = substr($value, $len);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1091 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1092 $part = $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1093 $value = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1094 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1095
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1096 // RFC 2047 specifies that any split header should
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1097 // be separated by a CRLF SPACE
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1098 if ($output) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1099 $output .= $eol . ' ';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1100 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1101 $output .= $prefix . $part . $suffix;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1102 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1103 $value = $output;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1104 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1105 $value = $prefix . $value . $suffix;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1106 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1107 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1108
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1109 return $value;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1110 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1111
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1112 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1113 * Encodes the given string using quoted-printable
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1114 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1115 * @param string $str String to encode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1116 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1117 * @return string Encoded string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1118 * @since 1.6.0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1119 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1120 public static function encodeQP($str)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1121 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1122 // Bug #17226 RFC 2047 restricts some characters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1123 // if the word is inside a phrase, permitted chars are only:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1124 // ASCII letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1125
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1126 // "=", "_", "?" must be encoded
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1127 $regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1128 $str = preg_replace_callback(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1129 $regexp, array('Mail_mimePart', 'qpReplaceCallback'), $str
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1130 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1131
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1132 return str_replace(' ', '_', $str);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1133 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1134
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1135 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1136 * Encodes the given string using base64 or quoted-printable.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1137 * This method makes sure that encoded-word represents an integral
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1138 * number of characters as per RFC2047.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1139 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1140 * @param string $str String to encode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1141 * @param string $charset Character set name
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1142 * @param string $encoding Encoding name (base64 or quoted-printable)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1143 * @param int $prefix_len Prefix length. Default: 0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1144 * @param string $eol End-of-line sequence. Default: "\r\n"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1145 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1146 * @return string Encoded string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1147 * @since 1.8.0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1148 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1149 public static function encodeMB($str, $charset, $encoding, $prefix_len=0, $eol="\r\n")
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1150 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1151 if (!function_exists('mb_substr') || !function_exists('mb_strlen')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1152 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1153 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1154
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1155 $encoding = $encoding == 'base64' ? 'B' : 'Q';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1156 // 75 is the value specified in the RFC
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1157 $prefix = '=?' . $charset . '?'.$encoding.'?';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1158 $suffix = '?=';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1159 $maxLength = 75 - strlen($prefix . $suffix);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1160
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1161 // A multi-octet character may not be split across adjacent encoded-words
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1162 // So, we'll loop over each character
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1163 // mb_stlen() with wrong charset will generate a warning here and return null
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1164 $length = mb_strlen($str, $charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1165 $result = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1166 $line_length = $prefix_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1167
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1168 if ($encoding == 'B') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1169 // base64
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1170 $start = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1171 $prev = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1172
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1173 for ($i=1; $i<=$length; $i++) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1174 // See #17311
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1175 $chunk = mb_substr($str, $start, $i-$start, $charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1176 $chunk = base64_encode($chunk);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1177 $chunk_len = strlen($chunk);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1178
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1179 if ($line_length + $chunk_len == $maxLength || $i == $length) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1180 if ($result) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1181 $result .= "\n";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1182 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1183 $result .= $chunk;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1184 $line_length = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1185 $start = $i;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1186 } else if ($line_length + $chunk_len > $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1187 if ($result) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1188 $result .= "\n";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1189 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1190 if ($prev) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1191 $result .= $prev;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1192 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1193 $line_length = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1194 $start = $i - 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1195 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1196 $prev = $chunk;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1197 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1198 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1199 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1200 // quoted-printable
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1201 // see encodeQP()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1202 $regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1203
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1204 for ($i=0; $i<=$length; $i++) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1205 $char = mb_substr($str, $i, 1, $charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1206 // RFC recommends underline (instead of =20) in place of the space
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1207 // that's one of the reasons why we're not using iconv_mime_encode()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1208 if ($char == ' ') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1209 $char = '_';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1210 $char_len = 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1211 } else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1212 $char = preg_replace_callback(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1213 $regexp, array('Mail_mimePart', 'qpReplaceCallback'), $char
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1214 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1215 $char_len = strlen($char);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1216 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1217
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1218 if ($line_length + $char_len > $maxLength) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1219 if ($result) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1220 $result .= "\n";
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1221 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1222 $line_length = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1223 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1224
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1225 $result .= $char;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1226 $line_length += $char_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1227 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1228 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1229
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1230 if ($result) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1231 $result = $prefix
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1232 .str_replace("\n", $suffix.$eol.' '.$prefix, $result).$suffix;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1233 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1234
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1235 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1236 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1237
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1238 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1239 * Callback function to replace extended characters (\x80-xFF) with their
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1240 * ASCII values (RFC2047: quoted-printable)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1241 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1242 * @param array $matches Preg_replace's matches array
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1243 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1244 * @return string Encoded character string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1245 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1246 protected static function qpReplaceCallback($matches)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1247 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1248 return sprintf('=%02X', ord($matches[1]));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1249 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1250
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1251 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1252 * Callback function to replace extended characters (\x80-xFF) with their
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1253 * ASCII values (RFC2231)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1254 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1255 * @param array $matches Preg_replace's matches array
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1256 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1257 * @return string Encoded character string
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1258 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1259 protected static function encodeReplaceCallback($matches)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1260 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1261 return sprintf('%%%02X', ord($matches[1]));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1262 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1263
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1264 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1265 * PEAR::raiseError implementation
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1266 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1267 * @param string $message A text error message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1268 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1269 * @return PEAR_Error Instance of PEAR_Error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1270 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1271 public static function raiseError($message)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1272 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1273 // PEAR::raiseError() is not PHP 5.4 compatible
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1274 return new PEAR_Error($message);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1275 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1276 }