alecpl
2011-03-17 569701d7002e71fbf76e9acaa6c83e5bee90b411
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
22
4591de 23 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
T 24 $_SESSION['compose'] = $_SESSION['compose_data'][$COMPOSE_ID];
25
7ffc08 26 if (!$_SESSION['compose']) {
T 27   die("Invalid session var!");
28 }
4e17e6 29
T 30
133bb0 31 // remove an attachment
A 32 if ($RCMAIL->action=='remove-attachment')
33 {
cc97ea 34   $id = 'undefined';
T 35   if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs))
133bb0 36     $id = $regs[1];
cc97ea 37   if ($attachment = $_SESSION['compose']['attachments'][$id])
e6ce00 38     $attachment = $RCMAIL->plugins->exec_hook('attachment_delete', $attachment);
cc97ea 39   if ($attachment['status']) {
T 40     if (is_array($_SESSION['compose']['attachments'][$id])) {
133bb0 41       unset($_SESSION['compose']['attachments'][$id]);
A 42       $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
43     }
44   }
cc97ea 45   
T 46   $OUTPUT->send();
133bb0 47   exit;
A 48 }
49
50 if ($RCMAIL->action=='display-attachment')
51 {
cc97ea 52   $id = 'undefined';
T 53   if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs))
133bb0 54     $id = $regs[1];
cc97ea 55   if ($attachment = $_SESSION['compose']['attachments'][$id])
e6ce00 56     $attachment = $RCMAIL->plugins->exec_hook('attachment_display', $attachment);
cc97ea 57     
T 58   if ($attachment['status']) {
91790e 59     if (empty($attachment['size']))
A 60       $attachment['size'] = $attachment['data'] ? strlen($attachment['data']) : @filesize($attachment['path']);
61
cc97ea 62     header('Content-Type: ' . $attachment['mimetype']);
91790e 63     header('Content-Length: ' . $attachment['size']);
cc97ea 64     
T 65     if ($attachment['data'])
66       echo $attachment['data'];
67     else if ($attachment['path'])
68       readfile($attachment['path']);
133bb0 69   }
A 70   exit;
71 }
72
73 // attachment upload action
779cbe 74
6d5dba 75 if (!is_array($_SESSION['compose']['attachments'])) {
4e17e6 76   $_SESSION['compose']['attachments'] = array();
6d5dba 77 }
4e17e6 78
7ffc08 79 // clear all stored output properties (like scripts and env vars)
T 80 $OUTPUT->reset();
4e17e6 81
ebf872 82 $uploadid = get_input_value('_uploadid', RCUBE_INPUT_GET);
A 83
6d5dba 84 if (is_array($_FILES['_attachments']['tmp_name'])) {
T 85   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
569701 86     // Process uploaded attachment if there is no error
A 87     $err = $_FILES['_attachments']['error'][$i];
4e17e6 88
569701 89     if (!$err) {
A 90       $attachment = array(
91         'path' => $filepath,
92         'size' => $_FILES['_attachments']['size'][$i],
93         'name' => $_FILES['_attachments']['name'][$i],
94         'mimetype' => rc_mime_content_type($filepath, $_FILES['_attachments']['name'][$i], $_FILES['_attachments']['type'][$i]),
95         'group' => $COMPOSE_ID,
96       );
1fb587 97
569701 98       $attachment = $RCMAIL->plugins->exec_hook('attachment_upload', $attachment);
A 99     }
100
101     if (!$err && $attachment['status'] && !$attachment['abort']) {
cc97ea 102       $id = $attachment['id'];
569701 103
cc97ea 104       // store new attachment in session
c793c6 105       unset($attachment['status'], $attachment['abort']);
cc97ea 106       $_SESSION['compose']['attachments'][$id] = $attachment;
569701 107
991a25 108       if (($icon = $_SESSION['compose']['deleteicon']) && is_file($icon)) {
6d5dba 109         $button = html::img(array(
T 110           'src' => $icon,
1fb587 111           'alt' => rcube_label('delete')
6d5dba 112         ));
599843 113       }
6d5dba 114       else {
T 115         $button = Q(rcube_label('delete'));
116       }
117
118       $content = html::a(array(
119         'href' => "#delete",
cc97ea 120         'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id),
6d5dba 121         'title' => rcube_label('delete'),
T 122       ), $button);
cc97ea 123
T 124       $content .= Q($attachment['name']);
569701 125
01ffe0 126       $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
T 127         'html' => $content,
128         'name' => $attachment['name'],
129         'mimetype' => $attachment['mimetype'],
130         'complete' => true), $uploadid);
6d5dba 131     }
T 132     else {  // upload failed
133       if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
599843 134         $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
6d5dba 135       }
c793c6 136       else if ($attachment['error']) {
T 137         $msg = $attachment['error'];
138       }
6d5dba 139       else {
599843 140         $msg = rcube_label('fileuploaderror');
6d5dba 141       }
569701 142
599843 143       $OUTPUT->command('display_message', $msg, 'error');
ebf872 144       $OUTPUT->command('remove_from_attachment_list', $uploadid);
86df15 145     }
4e17e6 146   }
6d5dba 147 }
T 148 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
7df0e3 149   // if filesize exceeds post_max_size then $_FILES array is empty,
A 150   // show filesizeerror instead of fileuploaderror
151   if ($maxsize = ini_get('post_max_size'))
152     $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
153   else
154     $msg = rcube_label('fileuploaderror');
155   $OUTPUT->command('display_message', $msg, 'error');
ebf872 156   $OUTPUT->command('remove_from_attachment_list', $uploadid);
6d5dba 157 }
4e17e6 158
T 159 // send html page with JS calls as response
f11541 160 $OUTPUT->command('auto_save_start', false);
T 161 $OUTPUT->send('iframe');
4e17e6 162