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