source: subversion/trunk/roundcubemail/program/steps/mail/attachments.inc @ 5542

Last change on this file since 5542 was 5542, checked in by alec, 19 months ago
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
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, The Roundcube Dev Team                       |
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$
19
20*/
21
22// Upload progress update
23if (!empty($_GET['_progress'])) {
24  rcube_upload_progress();
25}
26
27$COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
28$COMPOSE    = null;
29
30if ($COMPOSE_ID && $_SESSION['compose_data_'.$COMPOSE_ID])
31  $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID];
32
33if (!$COMPOSE) {
34  die("Invalid session var!");
35}
36
37
38// remove an attachment
39if ($RCMAIL->action=='remove-attachment')
40{
41  $id = 'undefined';
42  if (preg_match('/^rcmfile(\w+)$/', $_POST['_file'], $regs))
43    $id = $regs[1];
44  if ($attachment = $COMPOSE['attachments'][$id])
45    $attachment = $RCMAIL->plugins->exec_hook('attachment_delete', $attachment);
46  if ($attachment['status']) {
47    if (is_array($COMPOSE['attachments'][$id])) {
48      unset($COMPOSE['attachments'][$id]);
49      $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
50    }
51  }
52
53  $OUTPUT->send();
54  exit;
55}
56
57if ($RCMAIL->action=='display-attachment')
58{
59  $id = 'undefined';
60  if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs))
61    $id = $regs[1];
62  if ($attachment = $COMPOSE['attachments'][$id])
63    $attachment = $RCMAIL->plugins->exec_hook('attachment_display', $attachment);
64
65  if ($attachment['status']) {
66    if (empty($attachment['size']))
67      $attachment['size'] = $attachment['data'] ? strlen($attachment['data']) : @filesize($attachment['path']);
68
69    header('Content-Type: ' . $attachment['mimetype']);
70    header('Content-Length: ' . $attachment['size']);
71
72    if ($attachment['data'])
73      echo $attachment['data'];
74    else if ($attachment['path'])
75      readfile($attachment['path']);
76  }
77  exit;
78}
79
80// attachment upload action
81
82if (!is_array($COMPOSE['attachments'])) {
83  $COMPOSE['attachments'] = array();
84}
85
86// clear all stored output properties (like scripts and env vars)
87$OUTPUT->reset();
88
89$uploadid = get_input_value('_uploadid', RCUBE_INPUT_GET);
90
91if (is_array($_FILES['_attachments']['tmp_name'])) {
92  foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
93    // Process uploaded attachment if there is no error
94    $err = $_FILES['_attachments']['error'][$i];
95
96    if (!$err) {
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      );
104
105      $attachment = $RCMAIL->plugins->exec_hook('attachment_upload', $attachment);
106    }
107
108    if (!$err && $attachment['status'] && !$attachment['abort']) {
109      $id = $attachment['id'];
110
111      // store new attachment in session
112      unset($attachment['status'], $attachment['abort']);
113      $COMPOSE['attachments'][$id] = $attachment;
114
115      if (($icon = $COMPOSE['deleteicon']) && is_file($icon)) {
116        $button = html::img(array(
117          'src' => $icon,
118          'alt' => rcube_label('delete')
119        ));
120      }
121      else {
122        $button = Q(rcube_label('delete'));
123      }
124
125      $content = html::a(array(
126        'href' => "#delete",
127        'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id),
128        'title' => rcube_label('delete'),
129      ), $button);
130
131      $content .= Q($attachment['name']);
132
133      $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
134        'html' => $content,
135        'name' => $attachment['name'],
136        'mimetype' => $attachment['mimetype'],
137        'complete' => true), $uploadid);
138    }
139    else {  // upload failed
140      if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
141        $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
142      }
143      else if ($attachment['error']) {
144        $msg = $attachment['error'];
145      }
146      else {
147        $msg = rcube_label('fileuploaderror');
148      }
149
150      $OUTPUT->command('display_message', $msg, 'error');
151      $OUTPUT->command('remove_from_attachment_list', $uploadid);
152    }
153  }
154}
155else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
156  // if filesize exceeds post_max_size then $_FILES array is empty,
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');
163  $OUTPUT->command('remove_from_attachment_list', $uploadid);
164}
165
166// send html page with JS calls as response
167$OUTPUT->command('auto_save_start', false);
168$OUTPUT->send('iframe');
169
Note: See TracBrowser for help on using the repository browser.