commit | author | age
|
a99c34
|
1 |
<?php |
58c279
|
2 |
|
AM |
3 |
/** |
a99c34
|
4 |
+-------------------------------------------------------------------------+ |
AM |
5 |
| Mail_mime wrapper for the Enigma Plugin | |
|
6 |
| | |
|
7 |
| Copyright (C) 2010-2015 The Roundcube Dev Team | |
|
8 |
| | |
|
9 |
| Licensed under the GNU General Public License version 3 or | |
|
10 |
| any later version with exceptions for skins & plugins. | |
|
11 |
| See the README file for a full license statement. | |
|
12 |
| | |
|
13 |
+-------------------------------------------------------------------------+ |
|
14 |
| Author: Aleksander Machniak <alec@alec.pl> | |
|
15 |
+-------------------------------------------------------------------------+ |
|
16 |
*/ |
|
17 |
|
|
18 |
class enigma_mime_message extends Mail_mime |
|
19 |
{ |
|
20 |
const PGP_SIGNED = 1; |
|
21 |
const PGP_ENCRYPTED = 2; |
|
22 |
|
7d4932
|
23 |
protected $type; |
AM |
24 |
protected $message; |
|
25 |
protected $body; |
|
26 |
protected $signature; |
|
27 |
protected $encrypted; |
a99c34
|
28 |
|
AM |
29 |
|
|
30 |
/** |
|
31 |
* Object constructor |
|
32 |
* |
|
33 |
* @param Mail_mime Original message |
|
34 |
* @param int Output message type |
|
35 |
*/ |
|
36 |
function __construct($message, $type) |
|
37 |
{ |
7d4932
|
38 |
$this->message = $message; |
AM |
39 |
$this->type = $type; |
a99c34
|
40 |
|
AM |
41 |
// clone parameters |
7d4932
|
42 |
foreach (array_keys($this->build_params) as $param) { |
AM |
43 |
$this->build_params[$param] = $message->getParam($param); |
a99c34
|
44 |
} |
AM |
45 |
|
|
46 |
// clone headers |
7d4932
|
47 |
$this->headers = $message->headers(); |
a99c34
|
48 |
/* |
AM |
49 |
if ($message->getParam('delay_file_io')) { |
|
50 |
// use common temp dir |
|
51 |
$temp_dir = $this->config->get('temp_dir'); |
|
52 |
$body_file = tempnam($temp_dir, 'rcmMsg'); |
|
53 |
$mime_result = $message->saveMessageBody($body_file); |
|
54 |
|
|
55 |
if (is_a($mime_result, 'PEAR_Error')) { |
|
56 |
self::raise_error(array('code' => 650, 'type' => 'php', |
|
57 |
'file' => __FILE__, 'line' => __LINE__, |
|
58 |
'message' => "Could not create message: ".$mime_result->getMessage()), |
|
59 |
true, false); |
|
60 |
return false; |
|
61 |
} |
|
62 |
|
|
63 |
$msg_body = fopen($body_file, 'r'); |
|
64 |
} |
|
65 |
else { |
|
66 |
*/ |
|
67 |
// \r\n is must-have here |
7d4932
|
68 |
$this->body = $message->get() . "\r\n"; |
a99c34
|
69 |
/* |
AM |
70 |
} |
|
71 |
*/ |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Check if the message is multipart (requires PGP/MIME) |
|
76 |
* |
|
77 |
* @return bool True if it is multipart, otherwise False |
|
78 |
*/ |
7d4932
|
79 |
public function isMultipart() |
a99c34
|
80 |
{ |
7d4932
|
81 |
return $this->message instanceof enigma_mime_message |
AM |
82 |
|| $this->message->isMultipart() || $this->message->getHTMLBody(); |
a99c34
|
83 |
} |
AM |
84 |
|
|
85 |
/** |
|
86 |
* Get e-mail address of message sender |
|
87 |
* |
|
88 |
* @return string Sender address |
|
89 |
*/ |
7d4932
|
90 |
public function getFromAddress() |
a99c34
|
91 |
{ |
AM |
92 |
// get sender address |
7d4932
|
93 |
$headers = $this->message->headers(); |
a99c34
|
94 |
$from = rcube_mime::decode_address_list($headers['From'], 1, false, null, true); |
AM |
95 |
$from = $from[1]; |
|
96 |
|
|
97 |
return $from; |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* Get recipients' e-mail addresses |
|
102 |
* |
|
103 |
* @return array Recipients' addresses |
|
104 |
*/ |
7d4932
|
105 |
public function getRecipients() |
a99c34
|
106 |
{ |
AM |
107 |
// get sender address |
7d4932
|
108 |
$headers = $this->message->headers(); |
a99c34
|
109 |
$to = rcube_mime::decode_address_list($headers['To'], null, false, null, true); |
AM |
110 |
$cc = rcube_mime::decode_address_list($headers['Cc'], null, false, null, true); |
|
111 |
$bcc = rcube_mime::decode_address_list($headers['Bcc'], null, false, null, true); |
|
112 |
|
|
113 |
$recipients = array_unique(array_merge($to, $cc, $bcc)); |
|
114 |
$recipients = array_diff($recipients, array('undisclosed-recipients:')); |
|
115 |
|
|
116 |
return $recipients; |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* Get original message body, to be encrypted/signed |
|
121 |
* |
|
122 |
* @return string Message body |
|
123 |
*/ |
7d4932
|
124 |
public function getOrigBody() |
a99c34
|
125 |
{ |
7d4932
|
126 |
$_headers = $this->message->headers(); |
a99c34
|
127 |
$headers = array(); |
AM |
128 |
|
|
129 |
if ($_headers['Content-Transfer-Encoding']) { |
|
130 |
$headers[] = 'Content-Transfer-Encoding: ' . $_headers['Content-Transfer-Encoding']; |
|
131 |
} |
|
132 |
$headers[] = 'Content-Type: ' . $_headers['Content-Type']; |
|
133 |
|
7d4932
|
134 |
return implode("\r\n", $headers) . "\r\n\r\n" . $this->body; |
a99c34
|
135 |
} |
AM |
136 |
|
|
137 |
/** |
|
138 |
* Register signature attachment |
|
139 |
* |
|
140 |
* @param string Signature body |
|
141 |
*/ |
7d4932
|
142 |
public function addPGPSignature($body) |
a99c34
|
143 |
{ |
7d4932
|
144 |
$this->signature = $body; |
a99c34
|
145 |
} |
AM |
146 |
|
|
147 |
/** |
|
148 |
* Register encrypted body |
|
149 |
* |
|
150 |
* @param string Encrypted body |
|
151 |
*/ |
7d4932
|
152 |
public function setPGPEncryptedBody($body) |
a99c34
|
153 |
{ |
7d4932
|
154 |
$this->encrypted = $body; |
a99c34
|
155 |
} |
AM |
156 |
|
|
157 |
/** |
|
158 |
* Builds the multipart message. |
|
159 |
* |
|
160 |
* @param array $params Build parameters that change the way the email |
|
161 |
* is built. Should be associative. See $_build_params. |
|
162 |
* @param resource $filename Output file where to save the message instead of |
|
163 |
* returning it |
|
164 |
* @param boolean $skip_head True if you want to return/save only the message |
|
165 |
* without headers |
|
166 |
* |
|
167 |
* @return mixed The MIME message content string, null or PEAR error object |
|
168 |
*/ |
7d4932
|
169 |
public function get($params = null, $filename = null, $skip_head = false) |
a99c34
|
170 |
{ |
AM |
171 |
if (isset($params)) { |
|
172 |
while (list($key, $value) = each($params)) { |
7d4932
|
173 |
$this->build_params[$key] = $value; |
a99c34
|
174 |
} |
AM |
175 |
} |
|
176 |
|
7d4932
|
177 |
$this->checkParams(); |
a99c34
|
178 |
|
7d4932
|
179 |
if ($this->type == self::PGP_SIGNED) { |
a99c34
|
180 |
$body = "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)"; |
AM |
181 |
$params = array( |
|
182 |
'content_type' => "multipart/signed; micalg=pgp-sha1; protocol=\"application/pgp-signature\"", |
7d4932
|
183 |
'eol' => $this->build_params['eol'], |
a99c34
|
184 |
); |
AM |
185 |
|
|
186 |
$message = new Mail_mimePart($body, $params); |
|
187 |
|
7d4932
|
188 |
if (!empty($this->body)) { |
AM |
189 |
$headers = $this->message->headers(); |
a99c34
|
190 |
$params = array('content_type' => $headers['Content-Type']); |
AM |
191 |
|
|
192 |
if ($headers['Content-Transfer-Encoding']) { |
|
193 |
$params['encoding'] = $headers['Content-Transfer-Encoding']; |
|
194 |
} |
|
195 |
|
7d4932
|
196 |
$message->addSubpart($this->body, $params); |
a99c34
|
197 |
} |
AM |
198 |
|
7d4932
|
199 |
if (!empty($this->signature)) { |
AM |
200 |
$message->addSubpart($this->signature, array( |
a99c34
|
201 |
'filename' => 'signature.asc', |
AM |
202 |
'content_type' => 'application/pgp-signature', |
|
203 |
'disposition' => 'attachment', |
|
204 |
'description' => 'OpenPGP digital signature', |
|
205 |
)); |
|
206 |
} |
|
207 |
} |
7d4932
|
208 |
else if ($this->type == self::PGP_ENCRYPTED) { |
a99c34
|
209 |
$body = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)"; |
AM |
210 |
$params = array( |
|
211 |
'content_type' => "multipart/encrypted; protocol=\"application/pgp-encrypted\"", |
7d4932
|
212 |
'eol' => $this->build_params['eol'], |
a99c34
|
213 |
); |
AM |
214 |
|
|
215 |
$message = new Mail_mimePart($body, $params); |
|
216 |
|
|
217 |
$message->addSubpart('Version: 1', array( |
|
218 |
'content_type' => 'application/pgp-encrypted', |
|
219 |
'description' => 'PGP/MIME version identification', |
|
220 |
)); |
|
221 |
|
7d4932
|
222 |
$message->addSubpart($this->encrypted, array( |
a99c34
|
223 |
'content_type' => 'application/octet-stream', |
AM |
224 |
'description' => 'PGP/MIME encrypted message', |
|
225 |
'disposition' => 'inline', |
|
226 |
'filename' => 'encrypted.asc', |
|
227 |
)); |
|
228 |
} |
|
229 |
|
|
230 |
// Use saved boundary |
7d4932
|
231 |
if (!empty($this->build_params['boundary'])) { |
AM |
232 |
$boundary = $this->build_params['boundary']; |
a99c34
|
233 |
} |
AM |
234 |
else { |
|
235 |
$boundary = null; |
|
236 |
} |
|
237 |
|
|
238 |
// Write output to file |
|
239 |
if ($filename) { |
|
240 |
// Append mimePart message headers and body into file |
|
241 |
$headers = $message->encodeToFile($filename, $boundary, $skip_head); |
7d4932
|
242 |
if ($this->isError($headers)) { |
a99c34
|
243 |
return $headers; |
AM |
244 |
} |
7d4932
|
245 |
$this->headers = array_merge($this->headers, $headers); |
a99c34
|
246 |
return null; |
AM |
247 |
} |
|
248 |
else { |
|
249 |
$output = $message->encode($boundary, $skip_head); |
7d4932
|
250 |
if ($this->isError($output)) { |
a99c34
|
251 |
return $output; |
AM |
252 |
} |
7d4932
|
253 |
$this->headers = array_merge($this->headers, $output['headers']); |
a99c34
|
254 |
return $output['body']; |
AM |
255 |
} |
|
256 |
} |
|
257 |
|
|
258 |
/** |
|
259 |
* Get Content-Type and Content-Transfer-Encoding headers of the message |
|
260 |
* |
|
261 |
* @return array Headers array |
|
262 |
*/ |
7d4932
|
263 |
protected function contentHeaders() |
a99c34
|
264 |
{ |
7d4932
|
265 |
$this->checkParams(); |
a99c34
|
266 |
|
9f1f75
|
267 |
$eol = $this->build_params['eol'] ?: "\r\n"; |
a99c34
|
268 |
|
AM |
269 |
// multipart message: and boundary |
7d4932
|
270 |
if (!empty($this->build_params['boundary'])) { |
AM |
271 |
$boundary = $this->build_params['boundary']; |
a99c34
|
272 |
} |
7d4932
|
273 |
else if (!empty($this->headers['Content-Type']) |
AM |
274 |
&& preg_match('/boundary="([^"]+)"/', $this->headers['Content-Type'], $m) |
a99c34
|
275 |
) { |
AM |
276 |
$boundary = $m[1]; |
|
277 |
} |
|
278 |
else { |
|
279 |
$boundary = '=_' . md5(rand() . microtime()); |
|
280 |
} |
|
281 |
|
7d4932
|
282 |
$this->build_params['boundary'] = $boundary; |
a99c34
|
283 |
|
7d4932
|
284 |
if ($this->type == self::PGP_SIGNED) { |
a99c34
|
285 |
$headers['Content-Type'] = "multipart/signed; micalg=pgp-sha1;$eol" |
AM |
286 |
." protocol=\"application/pgp-signature\";$eol" |
|
287 |
." boundary=\"$boundary\""; |
|
288 |
} |
7d4932
|
289 |
else if ($this->type == self::PGP_ENCRYPTED) { |
a99c34
|
290 |
$headers['Content-Type'] = "multipart/encrypted;$eol" |
AM |
291 |
." protocol=\"application/pgp-encrypted\";$eol" |
|
292 |
." boundary=\"$boundary\""; |
|
293 |
} |
|
294 |
|
|
295 |
return $headers; |
|
296 |
} |
|
297 |
} |