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')
31 if (preg_match('/^rcmfile([0-9]+)$/', $_POST['_file'], $regs))
34 if (is_array($_SESSION['compose']['attachments'][$id]))
36 @unlink($_SESSION['compose']['attachments'][$id]['path']);
37 unset($_SESSION['compose']['attachments'][$id]);
38 $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
45 if ($RCMAIL->action=='display-attachment')
47 if (preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs))
50 if (is_array($_SESSION['compose']['attachments'][$id]))
52 $apath = $_SESSION['compose']['attachments'][$id]['path'];
53 header('Content-Type: ' . $_SESSION['compose']['attachments'][$id]['mimetype']);
54 header('Content-Length: ' . filesize($apath));
61 // attachment upload action
63 // use common temp dir for file uploads
64 $temp_dir = unslashify($CONFIG['temp_dir']);
66 // #1484529: we need absolute path on Windows for move_uploaded_file()
67 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
68 $temp_dir = realpath($temp_dir);
71 if (!is_array($_SESSION['compose']['attachments'])) {
72 $_SESSION['compose']['attachments'] = array();
75 // clear all stored output properties (like scripts and env vars)
78 if (is_array($_FILES['_attachments']['tmp_name'])) {
79 foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
80 $tmpfname = tempnam($temp_dir, 'rcmAttmnt');
81 if (move_uploaded_file($filepath, $tmpfname) && file_exists($tmpfname)) {
82 $id = count($_SESSION['compose']['attachments']);
83 $_SESSION['compose']['attachments'][] = array(
84 'name' => $_FILES['_attachments']['name'][$i],
85 'mimetype' => rc_mime_content_type($tmpfname, $_FILES['_attachments']['name'][$i], $_FILES['_attachments']['type'][$i]),
89 if (is_file($icon = $CONFIG['skin_path'] . '/images/icons/remove-attachment.png')) {
90 $button = html::img(array(
92 'alt' => rcube_label('delete'),
93 'style' => "padding-right:2px;vertical-align:middle",
97 $button = Q(rcube_label('delete'));
100 $content = html::a(array(
102 'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%d', this)", JS_OBJECT_NAME, $id),
103 'title' => rcube_label('delete'),
106 $content .= Q($_FILES['_attachments']['name'][$i]);
108 $OUTPUT->command('add2attachment_list', "rcmfile$id", $content);
110 else { // upload failed
111 $err = $_FILES['_attachments']['error'][$i];
112 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
113 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
116 $msg = rcube_label('fileuploaderror');
119 $OUTPUT->command('display_message', $msg, 'error');
123 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
124 $OUTPUT->command('display_message', rcube_label('fileuploaderror'), 'error');
127 // send html page with JS calls as response
128 $OUTPUT->command('show_attachment_form', false);
129 $OUTPUT->command('auto_save_start', false);
130 $OUTPUT->send('iframe');