4 +-----------------------------------------------------------------------+
5 | program/steps/mail/attachments.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2009, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Upload, remove, display attachments in compose form |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: attachments.inc 5543 2011-12-05 07:24:36Z alec $
22 // Upload progress update
23 if (!empty($_GET['_progress'])) {
24 rcube_upload_progress();
27 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
30 if ($COMPOSE_ID && $_SESSION['compose_data_'.$COMPOSE_ID])
31 $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID];
34 die("Invalid session var!");
38 // remove an attachment
39 if ($RCMAIL->action=='remove-attachment')
42 if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs))
44 if ($attachment = $COMPOSE['attachments'][$id])
45 $attachment = $RCMAIL->plugins->exec_hook('attachment_delete', $attachment);
46 if ($attachment['status']) {
47 if (is_array($COMPOSE['attachments'][$id])) {
48 unset($COMPOSE['attachments'][$id]);
49 $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
57 if ($RCMAIL->action=='display-attachment')
60 if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs))
62 if ($attachment = $COMPOSE['attachments'][$id])
63 $attachment = $RCMAIL->plugins->exec_hook('attachment_display', $attachment);
65 if ($attachment['status']) {
66 if (empty($attachment['size']))
67 $attachment['size'] = $attachment['data'] ? strlen($attachment['data']) : @filesize($attachment['path']);
69 header('Content-Type: ' . $attachment['mimetype']);
70 header('Content-Length: ' . $attachment['size']);
72 if ($attachment['data'])
73 echo $attachment['data'];
74 else if ($attachment['path'])
75 readfile($attachment['path']);
80 // attachment upload action
82 if (!is_array($COMPOSE['attachments'])) {
83 $COMPOSE['attachments'] = array();
86 // clear all stored output properties (like scripts and env vars)
89 $uploadid = get_input_value('_uploadid', RCUBE_INPUT_GET);
91 if (is_array($_FILES['_attachments']['tmp_name'])) {
92 foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
93 // Process uploaded attachment if there is no error
94 $err = $_FILES['_attachments']['error'][$i];
99 'size' => $_FILES['_attachments']['size'][$i],
100 'name' => $_FILES['_attachments']['name'][$i],
101 'mimetype' => rc_mime_content_type($filepath, $_FILES['_attachments']['name'][$i], $_FILES['_attachments']['type'][$i]),
102 'group' => $COMPOSE_ID,
105 $attachment = $RCMAIL->plugins->exec_hook('attachment_upload', $attachment);
108 if (!$err && $attachment['status'] && !$attachment['abort']) {
109 $id = $attachment['id'];
111 // store new attachment in session
112 unset($attachment['status'], $attachment['abort']);
113 $COMPOSE['attachments'][$id] = $attachment;
115 if (($icon = $COMPOSE['deleteicon']) && is_file($icon)) {
116 $button = html::img(array(
118 'alt' => rcube_label('delete')
122 $button = Q(rcube_label('delete'));
125 $content = html::a(array(
127 'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id),
128 'title' => rcube_label('delete'),
131 $content .= Q($attachment['name']);
133 $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
135 'name' => $attachment['name'],
136 'mimetype' => $attachment['mimetype'],
137 'complete' => true), $uploadid);
139 else { // upload failed
140 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
141 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
143 else if ($attachment['error']) {
144 $msg = $attachment['error'];
147 $msg = rcube_label('fileuploaderror');
150 $OUTPUT->command('display_message', $msg, 'error');
151 $OUTPUT->command('remove_from_attachment_list', $uploadid);
155 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
156 // if filesize exceeds post_max_size then $_FILES array is empty,
157 // show filesizeerror instead of fileuploaderror
158 if ($maxsize = ini_get('post_max_size'))
159 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
161 $msg = rcube_label('fileuploaderror');
162 $OUTPUT->command('display_message', $msg, 'error');
163 $OUTPUT->command('remove_from_attachment_list', $uploadid);
166 // send html page with JS calls as response
167 $OUTPUT->command('auto_save_start', false);
168 $OUTPUT->send('iframe');