alecpl
2011-12-05 9b28484859b663e0321a1d49a6525b8ef34cd300
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
133bb0 5  | program/steps/mail/attachments.inc                                    |
4e17e6 6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
133bb0 12  |   Upload, remove, display attachments in compose form                 |
4e17e6 13  |                                                                       |
T 14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
580ff9 18  $Id$
4e17e6 19
T 20 */
21
4171c5 22 // Upload progress update
A 23 if (!empty($_GET['_progress'])) {
24   rcube_upload_progress();
25 }
4e17e6 26
4591de 27 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
9b2848 28 $COMPOSE    = null;
4591de 29
9b2848 30 if ($COMPOSE_ID && $_SESSION['compose_data_'.$COMPOSE_ID])
A 31   $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID];
32
33 if (!$COMPOSE) {
7ffc08 34   die("Invalid session var!");
T 35 }
4e17e6 36
T 37
133bb0 38 // remove an attachment
A 39 if ($RCMAIL->action=='remove-attachment')
40 {
cc97ea 41   $id = 'undefined';
T 42   if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs))
133bb0 43     $id = $regs[1];
9b2848 44   if ($attachment = $COMPOSE['attachments'][$id])
e6ce00 45     $attachment = $RCMAIL->plugins->exec_hook('attachment_delete', $attachment);
cc97ea 46   if ($attachment['status']) {
9b2848 47     if (is_array($COMPOSE['attachments'][$id])) {
A 48       unset($COMPOSE['attachments'][$id]);
133bb0 49       $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
A 50     }
51   }
9b2848 52
cc97ea 53   $OUTPUT->send();
133bb0 54   exit;
A 55 }
56
57 if ($RCMAIL->action=='display-attachment')
58 {
cc97ea 59   $id = 'undefined';
T 60   if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs))
133bb0 61     $id = $regs[1];
9b2848 62   if ($attachment = $COMPOSE['attachments'][$id])
e6ce00 63     $attachment = $RCMAIL->plugins->exec_hook('attachment_display', $attachment);
9b2848 64
cc97ea 65   if ($attachment['status']) {
91790e 66     if (empty($attachment['size']))
A 67       $attachment['size'] = $attachment['data'] ? strlen($attachment['data']) : @filesize($attachment['path']);
68
cc97ea 69     header('Content-Type: ' . $attachment['mimetype']);
91790e 70     header('Content-Length: ' . $attachment['size']);
9b2848 71
cc97ea 72     if ($attachment['data'])
T 73       echo $attachment['data'];
74     else if ($attachment['path'])
75       readfile($attachment['path']);
133bb0 76   }
A 77   exit;
78 }
79
80 // attachment upload action
779cbe 81
9b2848 82 if (!is_array($COMPOSE['attachments'])) {
A 83   $COMPOSE['attachments'] = array();
6d5dba 84 }
4e17e6 85
7ffc08 86 // clear all stored output properties (like scripts and env vars)
T 87 $OUTPUT->reset();
4e17e6 88
ebf872 89 $uploadid = get_input_value('_uploadid', RCUBE_INPUT_GET);
A 90
6d5dba 91 if (is_array($_FILES['_attachments']['tmp_name'])) {
T 92   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
569701 93     // Process uploaded attachment if there is no error
A 94     $err = $_FILES['_attachments']['error'][$i];
4e17e6 95
569701 96     if (!$err) {
A 97       $attachment = array(
98         'path' => $filepath,
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,
103       );
1fb587 104
569701 105       $attachment = $RCMAIL->plugins->exec_hook('attachment_upload', $attachment);
A 106     }
107
108     if (!$err && $attachment['status'] && !$attachment['abort']) {
cc97ea 109       $id = $attachment['id'];
569701 110
cc97ea 111       // store new attachment in session
c793c6 112       unset($attachment['status'], $attachment['abort']);
9b2848 113       $COMPOSE['attachments'][$id] = $attachment;
569701 114
9b2848 115       if (($icon = $COMPOSE['deleteicon']) && is_file($icon)) {
6d5dba 116         $button = html::img(array(
T 117           'src' => $icon,
1fb587 118           'alt' => rcube_label('delete')
6d5dba 119         ));
599843 120       }
6d5dba 121       else {
T 122         $button = Q(rcube_label('delete'));
123       }
124
125       $content = html::a(array(
126         'href' => "#delete",
cc97ea 127         'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id),
6d5dba 128         'title' => rcube_label('delete'),
T 129       ), $button);
cc97ea 130
T 131       $content .= Q($attachment['name']);
569701 132
01ffe0 133       $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
T 134         'html' => $content,
135         'name' => $attachment['name'],
136         'mimetype' => $attachment['mimetype'],
137         'complete' => true), $uploadid);
6d5dba 138     }
T 139     else {  // upload failed
140       if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
599843 141         $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
6d5dba 142       }
c793c6 143       else if ($attachment['error']) {
T 144         $msg = $attachment['error'];
145       }
6d5dba 146       else {
599843 147         $msg = rcube_label('fileuploaderror');
6d5dba 148       }
569701 149
599843 150       $OUTPUT->command('display_message', $msg, 'error');
ebf872 151       $OUTPUT->command('remove_from_attachment_list', $uploadid);
86df15 152     }
4e17e6 153   }
6d5dba 154 }
T 155 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
7df0e3 156   // if filesize exceeds post_max_size then $_FILES array is empty,
A 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)))));
160   else
161     $msg = rcube_label('fileuploaderror');
162   $OUTPUT->command('display_message', $msg, 'error');
ebf872 163   $OUTPUT->command('remove_from_attachment_list', $uploadid);
6d5dba 164 }
4e17e6 165
T 166 // send html page with JS calls as response
f11541 167 $OUTPUT->command('auto_save_start', false);
T 168 $OUTPUT->send('iframe');
4e17e6 169