summaryrefslogtreecommitdiff
path: root/plugins/enigma/lib/enigma_mime_message.php
blob: feed78e03c0bfd335f2e83738eb5a7ce4587e52b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/*
 +-------------------------------------------------------------------------+
 | Mail_mime wrapper for the Enigma Plugin                                 |
 |                                                                         |
 | Copyright (C) 2010-2015 The Roundcube Dev Team                          |
 |                                                                         |
 | Licensed under the GNU General Public License version 3 or              |
 | any later version with exceptions for skins & plugins.                  |
 | See the README file for a full license statement.                       |
 |                                                                         |
 +-------------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                              |
 +-------------------------------------------------------------------------+
*/

class enigma_mime_message extends Mail_mime
{
    const PGP_SIGNED    = 1;
    const PGP_ENCRYPTED = 2;

    protected $_type;
    protected $_message;
    protected $_body;
    protected $_signature;
    protected $_encrypted;


    /**
     * Object constructor
     *
     * @param Mail_mime Original message
     * @param int       Output message type
     */
    function __construct($message, $type)
    {
        $this->_message = $message;
        $this->_type    = $type;

        // clone parameters
        foreach (array_keys($this->_build_params) as $param) {
            $this->_build_params[$param] = $message->getParam($param);
        }

        // clone headers
        $this->_headers = $message->_headers;

/*
        if ($message->getParam('delay_file_io')) {
            // use common temp dir
            $temp_dir    = $this->config->get('temp_dir');
            $body_file   = tempnam($temp_dir, 'rcmMsg');
            $mime_result = $message->saveMessageBody($body_file);

            if (is_a($mime_result, 'PEAR_Error')) {
                self::raise_error(array('code' => 650, 'type' => 'php',
                    'file' => __FILE__, 'line' => __LINE__,
                    'message' => "Could not create message: ".$mime_result->getMessage()),
                    true, false);
                return false;
            }

            $msg_body = fopen($body_file, 'r');
        }
        else {
*/
            // \r\n is must-have here
            $this->_body = $message->get() . "\r\n";
/*
        }
*/
    }

    /**
     * Check if the message is multipart (requires PGP/MIME)
     *
     * @return bool True if it is multipart, otherwise False
     */
    function isMultipart()
    {
        return $this->_message instanceof enigma_mime_message
            || !empty($this->_message->_parts) || $this->_message->getHTMLBody();
    }

    /**
     * Get e-mail address of message sender
     *
     * @return string Sender address
     */
    function getFromAddress()
    {
        // get sender address
        $headers = $this->_message->headers();
        $from    = rcube_mime::decode_address_list($headers['From'], 1, false, null, true);
        $from    = $from[1];

        return $from;
    }

    /**
     * Get recipients' e-mail addresses
     *
     * @return array Recipients' addresses
     */
    function getRecipients()
    {
        // get sender address
        $headers = $this->_message->headers();
        $to      = rcube_mime::decode_address_list($headers['To'], null, false, null, true);
        $cc      = rcube_mime::decode_address_list($headers['Cc'], null, false, null, true);
        $bcc     = rcube_mime::decode_address_list($headers['Bcc'], null, false, null, true);

        $recipients = array_unique(array_merge($to, $cc, $bcc));
        $recipients = array_diff($recipients, array('undisclosed-recipients:'));

        return $recipients;
    }

    /**
     * Get original message body, to be encrypted/signed
     *
     * @return string Message body
     */
    function getOrigBody()
    {
        $_headers = $this->_message->headers();
        $headers  = array();

        if ($_headers['Content-Transfer-Encoding']) {
            $headers[] = 'Content-Transfer-Encoding: ' . $_headers['Content-Transfer-Encoding'];
        }
        $headers[] = 'Content-Type: ' . $_headers['Content-Type'];

        return implode("\r\n", $headers) . "\r\n\r\n" . $this->_body;
    }

    /**
     * Register signature attachment
     *
     * @param string Signature body
     */
    function addPGPSignature($body)
    {
        $this->_signature = $body;
    }

    /**
     * Register encrypted body
     *
     * @param string Encrypted body
     */
    function setPGPEncryptedBody($body)
    {
        $this->_encrypted = $body;
    }

    /**
     * Builds the multipart message.
     *
     * @param array    $params    Build parameters that change the way the email
     *                            is built. Should be associative. See $_build_params.
     * @param resource $filename  Output file where to save the message instead of
     *                            returning it
     * @param boolean  $skip_head True if you want to return/save only the message
     *                            without headers
     *
     * @return mixed The MIME message content string, null or PEAR error object
     * @access public
     */
    function get($params = null, $filename = null, $skip_head = false)
    {
        if (isset($params)) {
            while (list($key, $value) = each($params)) {
                $this->_build_params[$key] = $value;
            }
        }

        $this->_checkParams();

        if ($this->_type == self::PGP_SIGNED) {
            $body   = "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)";
            $params = array(
                'content_type' => "multipart/signed; micalg=pgp-sha1; protocol=\"application/pgp-signature\"",
                'eol'          => $this->_build_params['eol'],
            );

            $message = new Mail_mimePart($body, $params);

            if (!empty($this->_body)) {
                $headers = $this->_message->headers();
                $params  = array('content_type' => $headers['Content-Type']);

                if ($headers['Content-Transfer-Encoding']) {
                    $params['encoding'] = $headers['Content-Transfer-Encoding'];
                }

                $message->addSubpart($this->_body, $params);
            }

            if (!empty($this->_signature)) {
                $message->addSubpart($this->_signature, array(
                    'filename'     => 'signature.asc',
                    'content_type' => 'application/pgp-signature',
                    'disposition'  => 'attachment',
                    'description'  => 'OpenPGP digital signature',
                ));
            }
        }
        else if ($this->_type == self::PGP_ENCRYPTED) {
            $body   = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)";
            $params = array(
                'content_type' => "multipart/encrypted; protocol=\"application/pgp-encrypted\"",
                'eol'          => $this->_build_params['eol'],
            );

            $message = new Mail_mimePart($body, $params);

            $message->addSubpart('Version: 1', array(
                    'content_type' => 'application/pgp-encrypted',
                    'description'  => 'PGP/MIME version identification',
            ));

            $message->addSubpart($this->_encrypted, array(
                    'content_type' => 'application/octet-stream',
                    'description'  => 'PGP/MIME encrypted message',
                    'disposition'  => 'inline',
                    'filename'     => 'encrypted.asc',
            ));
        }

        // Use saved boundary
        if (!empty($this->_build_params['boundary'])) {
            $boundary = $this->_build_params['boundary'];
        }
        else {
            $boundary = null;
        }

        // Write output to file
        if ($filename) {
            // Append mimePart message headers and body into file
            $headers = $message->encodeToFile($filename, $boundary, $skip_head);
            if ($this->_isError($headers)) {
                return $headers;
            }
            $this->_headers = array_merge($this->_headers, $headers);
            return null;
        }
        else {
            $output = $message->encode($boundary, $skip_head);
            if ($this->_isError($output)) {
                return $output;
            }
            $this->_headers = array_merge($this->_headers, $output['headers']);
            return $output['body'];
        }
    }

    /**
     * Get Content-Type and Content-Transfer-Encoding headers of the message
     *
     * @return array Headers array
     * @access private
     */
    function _contentHeaders()
    {
        $this->_checkParams();

        $eol = !empty($this->_build_params['eol']) ? $this->_build_params['eol'] : "\r\n";

        // multipart message: and boundary
        if (!empty($this->_build_params['boundary'])) {
            $boundary = $this->_build_params['boundary'];
        }
        else if (!empty($this->_headers['Content-Type'])
            && preg_match('/boundary="([^"]+)"/', $this->_headers['Content-Type'], $m)
        ) {
            $boundary = $m[1];
        }
        else {
            $boundary = '=_' . md5(rand() . microtime());
        }

        $this->_build_params['boundary'] = $boundary;

        if ($this->_type == self::PGP_SIGNED) {
            $headers['Content-Type'] = "multipart/signed; micalg=pgp-sha1;$eol"
                ." protocol=\"application/pgp-signature\";$eol"
                ." boundary=\"$boundary\"";
        }
        else if ($this->_type == self::PGP_ENCRYPTED) {
            $headers['Content-Type'] = "multipart/encrypted;$eol"
                ." protocol=\"application/pgp-encrypted\";$eol"
                ." boundary=\"$boundary\"";
        }

        return $headers;
    }
}