]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/attachments.inc
Imported Upstream version 0.3
[roundcube.git] / program / steps / mail / attachments.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/attachments.inc                                    |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Upload, remove, display attachments in compose form                 |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: compose.inc 2081 2008-11-23 12:38:44Z thomasb $
19
20 */
21
22
23 if (!$_SESSION['compose']) {
24   die("Invalid session var!");
25 }
26
27
28 // remove an attachment
29 if ($RCMAIL->action=='remove-attachment')
30 {
31   $id = 'undefined';
32   if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs))
33     $id = $regs[1];
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");
40     }
41   }
42   
43   $OUTPUT->send();
44   exit;
45 }
46
47 if ($RCMAIL->action=='display-attachment')
48 {
49   $id = 'undefined';
50   if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs))
51     $id = $regs[1];
52   if ($attachment = $_SESSION['compose']['attachments'][$id])
53     $attachment = $RCMAIL->plugins->exec_hook('display_attachment', $attachment);
54     
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);
59     
60     if ($attachment['data'])
61       echo $attachment['data'];
62     else if ($attachment['path'])
63       readfile($attachment['path']);
64   }
65   exit;
66 }
67
68 // attachment upload action
69
70 if (!is_array($_SESSION['compose']['attachments'])) {
71   $_SESSION['compose']['attachments'] = array();
72 }
73
74 // clear all stored output properties (like scripts and env vars)
75 $OUTPUT->reset();
76
77 if (is_array($_FILES['_attachments']['tmp_name'])) {
78   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
79     $attachment = array(
80       'path' => $filepath,
81       'name' => $_FILES['_attachments']['name'][$i],
82       'mimetype' => rc_mime_content_type($filepath, $_FILES['_attachments']['name'][$i], $_FILES['_attachments']['type'][$i])
83     );
84
85     $attachment = $RCMAIL->plugins->exec_hook('upload_attachment', $attachment);
86
87     if ($attachment['status'] && !$attachment['abort']) {
88       $id = $attachment['id'];
89       
90       // store new attachment in session
91       unset($attachment['status'], $attachment['abort']);
92       $_SESSION['compose']['attachments'][$id] = $attachment;
93       
94       if (($icon = $_SESSION['compose']['deleteicon']) && is_file($icon)) {
95         $button = html::img(array(
96           'src' => $icon,
97           'alt' => rcube_label('delete')
98         ));
99       }
100       else {
101         $button = Q(rcube_label('delete'));
102       }
103
104       $content = html::a(array(
105         'href' => "#delete",
106         'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id),
107         'title' => rcube_label('delete'),
108       ), $button);
109
110       $content .= Q($attachment['name']);
111       
112       $OUTPUT->command('add2attachment_list', "rcmfile$id", $content);
113     }
114     else {  // upload failed
115       $err = $_FILES['_attachments']['error'][$i];
116       if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
117         $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
118       }
119       else if ($attachment['error']) {
120         $msg = $attachment['error'];
121       }
122       else {
123         $msg = rcube_label('fileuploaderror');
124       }
125     
126       $OUTPUT->command('display_message', $msg, 'error');
127     }
128   }
129 }
130 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
131   $OUTPUT->command('display_message', rcube_label('fileuploaderror'), 'error');
132 }
133
134 // send html page with JS calls as response
135 $OUTPUT->command('show_attachment_form', false);
136 $OUTPUT->command('auto_save_start', false);
137 $OUTPUT->send('iframe');
138
139 ?>