Aleksander Machniak
2016-03-28 46f7b7096450939fe03c95aa81ce06ae4bfca89d
program/steps/mail/attachments.inc
@@ -1,6 +1,6 @@
<?php
/*
/**
 +-----------------------------------------------------------------------+
 | program/steps/mail/attachments.inc                                    |
 |                                                                       |
@@ -38,7 +38,7 @@
// remove an attachment
if ($RCMAIL->action=='remove-attachment') {
if ($RCMAIL->action == 'remove-attachment') {
    $id = 'undefined';
    if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs)) {
@@ -67,67 +67,7 @@
        $id = $regs[1];
    }
    if ($attachment = $COMPOSE['attachments'][$id]) {
        $attachment = $RCMAIL->plugins->exec_hook('attachment_display', $attachment);
    }
    if ($attachment['status']) {
        if (empty($attachment['size'])) {
            $attachment['size'] = $attachment['data'] ? strlen($attachment['data']) : @filesize($attachment['path']);
        }
        // generate image thumbnail for file browser in HTML editor
        if (!empty($_GET['_thumbnail'])) {
            $temp_dir       = $RCMAIL->config->get('temp_dir');
            $thumbnail_size = 80;
            list(,$ext)     = explode('/', $attachment['mimetype']);
            $mimetype       = $attachment['mimetype'];
            $file_ident     = $attachment['id'] . ':' . $attachment['mimetype'] . ':' . $attachment['size'];
            $cache_basename = $temp_dir . '/' . md5($file_ident . ':' . $RCMAIL->user->ID . ':' . $thumbnail_size);
            $cache_file     = $cache_basename . '.' . $ext;
            // render thumbnail image if not done yet
            if (!is_file($cache_file)) {
                if (!$attachment['path']) {
                    $orig_name = $filename = $cache_basename . '.orig.' . $ext;
                    file_put_contents($orig_name, $attachment['data']);
                }
                else {
                    $filename = $attachment['path'];
                }
                $image = new rcube_image($filename);
                if ($imgtype = $image->resize($thumbnail_size, $cache_file, true)) {
                    $mimetype = 'image/' . $imgtype;
                    if ($orig_name) {
                        unlink($orig_name);
                    }
                }
            }
            if (is_file($cache_file)) {
                // cache for 1h
                $RCMAIL->output->future_expire_header(3600);
                header('Content-Type: ' . $mimetype);
                header('Content-Length: ' . filesize($cache_file));
                readfile($cache_file);
                exit;
            }
        }
        header('Content-Type: ' . $attachment['mimetype']);
        header('Content-Length: ' . $attachment['size']);
        if ($attachment['data']) {
            echo $attachment['data'];
        }
        else if ($attachment['path']) {
            readfile($attachment['path']);
        }
    }
    $RCMAIL->display_uploaded_file($COMPOSE['attachments'][$id]);
    exit;
}
@@ -136,8 +76,41 @@
// clear all stored output properties (like scripts and env vars)
$OUTPUT->reset();
$uploadid = rcube_utils::get_input_value('_uploadid', rcube_utils::INPUT_GET);
$uploadid = rcube_utils::get_input_value('_uploadid', rcube_utils::INPUT_GPC);
$uri      = rcube_utils::get_input_value('_uri', rcube_utils::INPUT_POST);
// handle dropping a reference to an attachment part of some message
if ($uri) {
    $url = parse_url($uri);
    parse_str($url['query'], $params);
    if (strlen($params['_mbox']) && $params['_uid'] && $params['_part']) {
        // @TODO: at some point we might support drag-n-drop between
        // two different accounts on the same server, for now make sure
        // this is the same server and the same user
        list($host, $port) = explode(':', $_SERVER['HTTP_HOST']);
        if ($host == $url['host'] && $port == $url['port']
            && $RCMAIL->get_user_name() == rawurldecode($url['user'])
        ) {
            $message = new rcube_message($params['_uid'], $params['_mbox']);
        }
    }
    if ($message && !empty($message->headers)
        && ($attachment = rcmail_save_attachment($message, $params['_part'], $COMPOSE_ID))
    ) {
        rcmail_attachment_success($attachment, $uploadid);
    }
    else {
        $OUTPUT->command('display_message', $RCMAIL->gettext('filelinkerror'), 'error');
        $OUTPUT->command('remove_from_attachment_list', $uploadid);
    }
    $OUTPUT->send();
    return;
}
// handle file(s) upload
if (is_array($_FILES['_attachments']['tmp_name'])) {
    $multiple = count($_FILES['_attachments']['tmp_name']) > 1;
@@ -156,41 +129,11 @@
        }
        if (!$err && $attachment['status'] && !$attachment['abort']) {
            $id = $attachment['id'];
            // store new attachment in session
            unset($attachment['status'], $attachment['abort']);
            $RCMAIL->session->append($SESSION_KEY.'.attachments', $id, $attachment);
            $RCMAIL->session->append($SESSION_KEY . '.attachments', $attachment['id'], $attachment);
            if (($icon = $COMPOSE['deleteicon']) && is_file($icon)) {
                $button = html::img(array(
                    'src' => $icon,
                    'alt' => $RCMAIL->gettext('delete')
                ));
            }
            else if ($COMPOSE['textbuttons']) {
                $button = rcube::Q($RCMAIL->gettext('delete'));
            }
            else {
                $button = '';
            }
            $content = html::a(array(
                'href'    => "#delete",
                'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", rcmail_output::JS_OBJECT_NAME, $id),
                'title'   => $RCMAIL->gettext('delete'),
                'class'   => 'delete',
                'aria-label' => $RCMAIL->gettext('delete') . ' ' . $attachment['name'],
            ), $button);
            $content .= rcube::Q($attachment['name']);
            $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
                'html'      => $content,
                'name'      => $attachment['name'],
                'mimetype'  => $attachment['mimetype'],
                'classname' => rcube_utils::file2class($attachment['mimetype'], $attachment['name']),
                'complete'  => true), $uploadid);
            rcmail_attachment_success($attachment, $uploadid);
        }
        else {  // upload failed
            if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
@@ -231,3 +174,42 @@
// send html page with JS calls as response
$OUTPUT->command('auto_save_start', false);
$OUTPUT->send('iframe');
function rcmail_attachment_success($attachment, $uploadid)
{
    global $RCMAIL, $COMPOSE;
    $id = $attachment['id'];
    if (($icon = $COMPOSE['deleteicon']) && is_file($icon)) {
        $button = html::img(array(
            'src' => $icon,
            'alt' => $RCMAIL->gettext('delete')
        ));
    }
    else if ($COMPOSE['textbuttons']) {
        $button = rcube::Q($RCMAIL->gettext('delete'));
    }
    else {
        $button = '';
    }
    $content = html::a(array(
        'href'    => "#delete",
        'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", rcmail_output::JS_OBJECT_NAME, $id),
        'title'   => $RCMAIL->gettext('delete'),
        'class'   => 'delete',
        'aria-label' => $RCMAIL->gettext('delete') . ' ' . $attachment['name'],
    ), $button);
    $content .= sprintf('%s <span class="attachment-size">(%s)</span>',
        rcube::Q($attachment['name']), $RCMAIL->show_bytes($attachment['size']));
    $RCMAIL->output->command('add2attachment_list', "rcmfile$id", array(
        'html'      => $content,
        'name'      => $attachment['name'],
        'mimetype'  => $attachment['mimetype'],
        'classname' => rcube_utils::file2class($attachment['mimetype'], $attachment['name']),
        'complete'  => true), $uploadid);
}