4 +-----------------------------------------------------------------------+
5 | program/steps/mail/attachments.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland |
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: compose.inc 2081 2008-11-23 12:38:44Z thomasb $
23 if (!$_SESSION['compose']) {
24 die("Invalid session var!");
28 // remove an attachment
29 if ($RCMAIL->action=='remove-attachment')
32 if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs))
34 if ($attachment = $_SESSION['compose']['attachments'][$id])
35 $attachment = $RCMAIL->plugins->exec_hook('remove_attachment', $attachment);
36 if ($attachment['status']) {
37 if (is_array($_SESSION['compose']['attachments'][$id])) {
38 unset($_SESSION['compose']['attachments'][$id]);
39 $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
47 if ($RCMAIL->action=='display-attachment')
50 if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs))
52 if ($attachment = $_SESSION['compose']['attachments'][$id])
53 $attachment = $RCMAIL->plugins->exec_hook('display_attachment', $attachment);
55 if ($attachment['status']) {
56 $size = $attachment['data'] ? strlen($attachment['data']) : @filesize($attachment['path']);
57 header('Content-Type: ' . $attachment['mimetype']);
58 header('Content-Length: ' . $size);
60 if ($attachment['data'])
61 echo $attachment['data'];
62 else if ($attachment['path'])
63 readfile($attachment['path']);
68 // attachment upload action
70 if (!is_array($_SESSION['compose']['attachments'])) {
71 $_SESSION['compose']['attachments'] = array();
74 // clear all stored output properties (like scripts and env vars)
77 $uploadid = get_input_value('_uploadid', RCUBE_INPUT_GET);
79 if (is_array($_FILES['_attachments']['tmp_name'])) {
80 foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
83 'name' => $_FILES['_attachments']['name'][$i],
84 'mimetype' => rc_mime_content_type($filepath, $_FILES['_attachments']['name'][$i], $_FILES['_attachments']['type'][$i])
87 $attachment = $RCMAIL->plugins->exec_hook('upload_attachment', $attachment);
89 if ($attachment['status'] && !$attachment['abort']) {
90 $id = $attachment['id'];
92 // store new attachment in session
93 unset($attachment['status'], $attachment['abort']);
94 $_SESSION['compose']['attachments'][$id] = $attachment;
96 if (($icon = $_SESSION['compose']['deleteicon']) && is_file($icon)) {
97 $button = html::img(array(
99 'alt' => rcube_label('delete')
103 $button = Q(rcube_label('delete'));
106 $content = html::a(array(
108 'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id),
109 'title' => rcube_label('delete'),
112 $content .= Q($attachment['name']);
114 $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
116 'name' => $attachment['name'],
117 'mimetype' => $attachment['mimetype'],
118 'complete' => true), $uploadid);
120 else { // upload failed
121 $err = $_FILES['_attachments']['error'][$i];
122 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
123 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
125 else if ($attachment['error']) {
126 $msg = $attachment['error'];
129 $msg = rcube_label('fileuploaderror');
132 $OUTPUT->command('display_message', $msg, 'error');
133 $OUTPUT->command('remove_from_attachment_list', $uploadid);
137 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
138 // if filesize exceeds post_max_size then $_FILES array is empty,
139 // show filesizeerror instead of fileuploaderror
140 if ($maxsize = ini_get('post_max_size'))
141 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
143 $msg = rcube_label('fileuploaderror');
144 $OUTPUT->command('display_message', $msg, 'error');
145 $OUTPUT->command('remove_from_attachment_list', $uploadid);
148 // send html page with JS calls as response
149 $OUTPUT->command('auto_save_start', false);
150 $OUTPUT->send('iframe');