send.php 17.1 KB
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
<?php

/**
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 |                                                                       |
 | Copyright (C) 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.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Compose a new mail message and send it or store as draft            |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
*/

class rcmail_action_mail_send extends rcmail_action
{
    /**
     * Request handler.
     *
     * @param array $args Arguments from the previous step(s)
     */
    public function run($args = [])
    {
        $rcmail = rcmail::get_instance();

        // remove all scripts and act as called in frame
        $rcmail->output->reset();
        $rcmail->output->framed = true;

        $COMPOSE_ID = rcube_utils::get_input_value('_id', rcube_utils::INPUT_GPC);
        $COMPOSE    =& $_SESSION['compose_data_'.$COMPOSE_ID];

        // Sanity checks
        if (!isset($COMPOSE['id'])) {
            rcube::raise_error([
                    'code' => 500,
                    'file' => __FILE__,
                    'line' => __LINE__,
                    'message' => "Invalid compose ID"
                ], true, false
            );

            $rcmail->output->show_message('internalerror', 'error');
            $rcmail->output->send('iframe');
        }

        $saveonly  = !empty($_GET['_saveonly']);
        $savedraft = !empty($_POST['_draft']) && !$saveonly;
        $SENDMAIL  = new rcmail_sendmail($COMPOSE, [
                'sendmail'      => true,
                'saveonly'      => $saveonly,
                'savedraft'     => $savedraft,
                'error_handler' => function() use ($rcmail) {
                    call_user_func_array([$rcmail->output, 'show_message'], func_get_args());
                    $rcmail->output->send('iframe');
                }
        ]);

        if (!isset($COMPOSE['attachments'])) {
            $COMPOSE['attachments'] = [];
        }

        // Collect input for message headers
        $headers = $SENDMAIL->headers_input();

        $COMPOSE['param']['message-id'] = $headers['Message-ID'];

        $message_id      = $headers['Message-ID'];
        $message_charset = $SENDMAIL->options['charset'];
        $message_body    = rcube_utils::get_input_value('_message', rcube_utils::INPUT_POST, true, $message_charset);
        $isHtml          = (bool) rcube_utils::get_input_value('_is_html', rcube_utils::INPUT_POST);

        // Reset message body and attachments in Mailvelope mode
        if (isset($_POST['_pgpmime'])) {
            $pgp_mime     = rcube_utils::get_input_value('_pgpmime', rcube_utils::INPUT_POST);
            $isHtml       = false;
            $message_body = '';

            // clear unencrypted attachments
            if (!empty($COMPOSE['attachments'])) {
                foreach ((array) $COMPOSE['attachments'] as $attach) {
                    $rcmail->plugins->exec_hook('attachment_delete', $attach);
                }
            }

            $COMPOSE['attachments'] = [];
        }

        if ($isHtml) {
            $bstyle = [];

            if ($font_size = $rcmail->config->get('default_font_size')) {
                $bstyle[] = 'font-size: ' . $font_size;
            }
            if ($font_family = $rcmail->config->get('default_font')) {
                $bstyle[] = 'font-family: ' . self::font_defs($font_family);
            }

            // append doctype and html/body wrappers
            $bstyle       = !empty($bstyle) ? (" style='" . implode('; ', $bstyle) . "'") : '';
            $message_body = '<html><head>'
                . '<meta http-equiv="Content-Type" content="text/html; charset='
                . ($message_charset ?: RCUBE_CHARSET) . '" /></head>'
                . "<body" . $bstyle . ">\r\n" . $message_body;
        }

        if (!$savedraft) {
            if ($isHtml) {
                $b_style   = 'padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0';
                $pre_style = 'margin: 0; padding: 0; font-family: monospace';

                $message_body = preg_replace(
                    [
                        // remove empty signature div
                        '/<div id="_rc_sig">(&nbsp;)?<\/div>[\s\r\n]*$/',
                        // replace signature's div ID (#6073)
                        '/ id="_rc_sig"/',
                        // add inline css for blockquotes and container
                        '/<blockquote>/',
                        '/<div class="pre">/',
                        // convert TinyMCE's new-line sequences (#1490463)
                        '/<p>&nbsp;<\/p>/',
                    ],
                    [
                        '',
                        ' id="signature"',
                        '<blockquote type="cite" style="'.$b_style.'">',
                        '<div class="pre" style="'.$pre_style.'">',
                        '<p><br /></p>',
                    ],
                    $message_body
                );

                rcube_utils::preg_error([
                        'line'    => __LINE__,
                        'file'    => __FILE__,
                        'message' => "Could not format HTML!"
                    ], true);
            }

            // Check spelling before send
            if (
                $rcmail->config->get('spellcheck_before_send')
                && $rcmail->config->get('enable_spellcheck')
                && empty($COMPOSE['spell_checked'])
                && !empty($message_body)
            ) {
                $language     = rcube_utils::get_input_value('_lang', rcube_utils::INPUT_GPC);
                $message_body = str_replace("\r\n", "\n", $message_body);
                $spellchecker = new rcube_spellchecker($language);
                $spell_result = $spellchecker->check($message_body, $isHtml);

                if ($error = $spellchecker->error()) {
                    rcube::raise_error([
                            'code' => 500, 'file' => __FILE__, 'line' => __LINE__,
                            'message' => "Spellcheck error: " . $error
                        ],
                        true, false
                    );
                }
                else {
                    $COMPOSE['spell_checked'] = true;

                    if (!$spell_result) {
                        if ($isHtml) {
                            $result['words']      = $spellchecker->get();
                            $result['dictionary'] = (bool) $rcmail->config->get('spellcheck_dictionary');
                        }
                        else {
                            $result = $spellchecker->get_xml();
                        }

                        $rcmail->output->show_message('mispellingsfound', 'error');
                        $rcmail->output->command('spellcheck_resume', $result);
                        $rcmail->output->send('iframe');
                    }
                }
            }

            // generic footer for all messages
            if ($footer = $SENDMAIL->generic_message_footer($isHtml)) {
                $message_body .= "\r\n" . $footer;
            }
        }

        if ($isHtml) {
            $message_body .= "\r\n</body></html>\r\n";
        }

        // sort attachments to make sure the order is the same as in the UI (#1488423)
        if ($files = rcube_utils::get_input_value('_attachments', rcube_utils::INPUT_POST)) {
            $files = explode(',', $files);
            $files = array_flip($files);
            foreach ($files as $idx => $val) {
                if (!empty($COMPOSE['attachments'][$idx])) {
                    $files[$idx] = $COMPOSE['attachments'][$idx];
                    unset($COMPOSE['attachments'][$idx]);
                }
            }

            $COMPOSE['attachments'] = array_merge(array_filter($files), (array) $COMPOSE['attachments']);
        }

        // Since we can handle big messages with disk usage, we need more time to work
        @set_time_limit(360);

        // create PEAR::Mail_mime instance, set headers, body and params
        $MAIL_MIME = $SENDMAIL->create_message($headers, $message_body, $isHtml, $COMPOSE['attachments']);

        // add stored attachments, if any
        if (is_array($COMPOSE['attachments'])) {
            self::add_attachments($SENDMAIL, $MAIL_MIME, $COMPOSE['attachments'], $isHtml);
        }

        // compose PGP/Mime message
        if (!empty($pgp_mime)) {
            $MAIL_MIME->addAttachment(new Mail_mimePart('Version: 1', [
                    'content_type' => 'application/pgp-encrypted',
                    'description'  => 'PGP/MIME version identification',
            ]));

            $MAIL_MIME->addAttachment(new Mail_mimePart($pgp_mime, [
                    'content_type' => 'application/octet-stream',
                    'filename'     => 'encrypted.asc',
                    'disposition'  => 'inline',
            ]));

            $MAIL_MIME->setContentType('multipart/encrypted', ['protocol' => 'application/pgp-encrypted']);
            $MAIL_MIME->setParam('preamble', 'This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156)');
        }

        // This hook allows to modify the message before send or save action
        $plugin    = $rcmail->plugins->exec_hook('message_ready', ['message' => $MAIL_MIME]);
        $MAIL_MIME = $plugin['message'];

        // Deliver the message over SMTP
        if (!$savedraft && !$saveonly) {
            $sent = $SENDMAIL->deliver_message($MAIL_MIME);
        }

        // Save the message in Drafts/Sent
        $saved = $SENDMAIL->save_message($MAIL_MIME);

        // raise error if saving failed
        if (!$saved && $savedraft) {
            self::display_server_error('errorsaving');
            // start the auto-save timer again
            $rcmail->output->command('auto_save_start');
            $rcmail->output->send('iframe');
        }

        $store_target = $SENDMAIL->options['store_target'];
        $store_folder = $SENDMAIL->options['store_folder'];

        // delete previous saved draft
        $drafts_mbox = $rcmail->config->get('drafts_mbox');
        $old_id      = rcube_utils::get_input_value('_draft_saveid', rcube_utils::INPUT_POST);

        if ($old_id && (!empty($sent) || $saved)) {
            $deleted = $rcmail->storage->delete_message($old_id, $drafts_mbox);

            // raise error if deletion of old draft failed
            if (!$deleted) {
                rcube::raise_error([
                        'code'    => 800,
                        'type'    => 'imap',
                        'file'    => __FILE__,
                        'line'    => __LINE__,
                        'message' => "Could not delete message from $drafts_mbox"
                    ],
                    true, false
                );
            }
        }

        if ($savedraft) {
            // remember new draft-uid ($saved could be an UID or true/false here)
            if ($saved && is_bool($saved)) {
                $index = $rcmail->storage->search_once($drafts_mbox, 'HEADER Message-ID ' . $message_id);
                $saved = $index->max();
            }

            if ($saved) {
                $plugin = $rcmail->plugins->exec_hook('message_draftsaved', [
                        'msgid'  => $message_id,
                        'uid'    => $saved,
                        'folder' => $store_target
                ]);

                // display success
                $rcmail->output->show_message(!empty($plugin['message']) ? $plugin['message'] : 'messagesaved', 'confirmation');

                // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning
                $COMPOSE['param']['draft_uid'] = $plugin['uid'];
                $rcmail->output->command('set_draft_id', $plugin['uid']);
                $rcmail->output->command('compose_field_hash', true);
            }

            // start the auto-save timer again
            $rcmail->output->command('auto_save_start');
        }
        else {
            // Collect folders which could contain the composed message,
            // we'll refresh the list if currently opened folder is one of them (#1490238)
            $folders    = [];
            $save_error = false;

            if (!$saveonly) {
                if (in_array($COMPOSE['mode'], ['reply', 'forward', 'draft'])) {
                    $folders[] = $COMPOSE['mailbox'];
                }
                if (!empty($COMPOSE['param']['draft_uid']) && $drafts_mbox) {
                    $folders[] = $drafts_mbox;
                }
            }

            if ($store_folder && !$saved) {
                $params = $saveonly ? null : ['prefix' => true];
                self::display_server_error('errorsavingsent', null, null, $params);

                if ($saveonly) {
                    $rcmail->output->send('iframe');
                }

                $save_error = true;
            }
            else {
                $rcmail->plugins->exec_hook('attachments_cleanup', ['group' => $COMPOSE_ID]);
                $rcmail->session->remove('compose_data_' . $COMPOSE_ID);
                $_SESSION['last_compose_session'] = $COMPOSE_ID;

                $rcmail->output->command('remove_compose_data', $COMPOSE_ID);

                if ($store_folder) {
                    $folders[] = $store_target;
                }
            }

            $msg = $rcmail->gettext($saveonly ? 'successfullysaved' : 'messagesent');

            $rcmail->output->command('sent_successfully', 'confirmation', $msg, $folders, $save_error);
        }

        $rcmail->output->send('iframe');
    }

    public static function add_attachments($SENDMAIL, $message, $attachments, $isHtml)
    {
        $rcmail = rcmail::get_instance();

        foreach ($attachments as $id => $attachment) {
            // This hook retrieves the attachment contents from the file storage backend
            $attachment = $rcmail->plugins->exec_hook('attachment_get', $attachment);
            $is_inline  = false;
            $dispurl    = null;

            if ($isHtml) {
                $dispurl      = '/[\'"]\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\'"]/';
                $message_body = $message->getHTMLBody();
                $is_inline    = preg_match($dispurl, $message_body);
            }

            $ctype = isset($attachment['mimetype']) ? $attachment['mimetype'] : '';
            $ctype = str_replace('image/pjpeg', 'image/jpeg', $ctype); // #1484914

            // inline image
            if ($is_inline) {
                // Mail_Mime does not support many inline attachments with the same name (#1489406)
                // we'll generate cid: urls here to workaround this
                $cid = preg_replace('/[^0-9a-zA-Z]/', '', uniqid(time(), true));
                if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $SENDMAIL->options['from'], $matches)) {
                    $cid .= $matches[1];
                }
                else {
                    $cid .= '@localhost';
                }

                if ($dispurl && !empty($message_body)) {
                    $message_body = preg_replace($dispurl, '"cid:' . $cid . '"', $message_body);

                    rcube_utils::preg_error([
                            'line'    => __LINE__,
                            'file'    => __FILE__,
                            'message' => "Could not replace an image reference!"
                        ], true
                    );

                    $message->setHTMLBody($message_body);
                }

                if (!empty($attachment['data'])) {
                    $message->addHTMLImage($attachment['data'], $ctype, $attachment['name'], false, $cid);
                }
                else {
                    $message->addHTMLImage($attachment['path'], $ctype, $attachment['name'], true, $cid);
                }
            }
            else {
                $file    = !empty($attachment['data']) ? $attachment['data'] : $attachment['path'];
                $folding = (int) $rcmail->config->get('mime_param_folding');

                $message->addAttachment($file,
                    $ctype,
                    $attachment['name'],
                    !empty($attachment['data']) ? false : true,
                    $ctype == 'message/rfc822' ? '8bit' : 'base64',
                    'attachment',
                    isset($attachment['charset']) ? $attachment['charset'] : null,
                    '', '',
                    $folding ? 'quoted-printable' : null,
                    $folding == 2 ? 'quoted-printable' : null,
                    '', RCUBE_CHARSET
                );
            }
        }
    }
}