alecpl
2008-12-05 133bb07f78b9486f0a524390253f08a0b446c584
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
133bb0 5  | program/steps/mail/attachments.inc                                    |
4e17e6 6  |                                                                       |
T 7  | This file is part of the RoundCube Webmail client                     |
133bb0 8  | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                 |
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
133bb0 18  $Id: compose.inc 2081 2008-11-23 12:38:44Z thomasb $
4e17e6 19
T 20 */
21
22
7ffc08 23 if (!$_SESSION['compose']) {
T 24   die("Invalid session var!");
25 }
4e17e6 26
T 27
133bb0 28 // remove an attachment
A 29 if ($RCMAIL->action=='remove-attachment')
30 {
31   if (preg_match('/^rcmfile([0-9]+)$/', $_POST['_file'], $regs))
32   {
33     $id = $regs[1];
34     if (is_array($_SESSION['compose']['attachments'][$id]))
35     {
36       @unlink($_SESSION['compose']['attachments'][$id]['path']);
37       unset($_SESSION['compose']['attachments'][$id]);
38       $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
39       $OUTPUT->send();
40     }
41   }
42   exit;
43 }
44
45 if ($RCMAIL->action=='display-attachment')
46 {
47   if (preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs))
48   {
49     $id = $regs[1];
50     if (is_array($_SESSION['compose']['attachments'][$id]))
51     {
52       $apath = $_SESSION['compose']['attachments'][$id]['path'];
53       header('Content-Type: ' . $_SESSION['compose']['attachments'][$id]['mimetype']);
54       header('Content-Length: ' . filesize($apath));
55       readfile($apath);
56     }
57   }
58   exit;
59 }
60
61 // attachment upload action
62
70d4b9 63 // use common temp dir for file uploads
T 64 $temp_dir = unslashify($CONFIG['temp_dir']);
4e17e6 65
6d5dba 66 if (!is_array($_SESSION['compose']['attachments'])) {
4e17e6 67   $_SESSION['compose']['attachments'] = array();
6d5dba 68 }
4e17e6 69
7ffc08 70 // clear all stored output properties (like scripts and env vars)
T 71 $OUTPUT->reset();
4e17e6 72
6d5dba 73 if (is_array($_FILES['_attachments']['tmp_name'])) {
T 74   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
599843 75     $tmpfname = tempnam($temp_dir, 'rcmAttmnt');
6d5dba 76     if (move_uploaded_file($filepath, $tmpfname)) {
599843 77       $id = count($_SESSION['compose']['attachments']);
6d5dba 78       $_SESSION['compose']['attachments'][] = array(
T 79         'name' => $_FILES['_attachments']['name'][$i],
80         'mimetype' => rc_mime_content_type($tmpfname, $_FILES['_attachments']['type'][$i]),
81         'path' => $tmpfname,
82       );
4e17e6 83
6d5dba 84       if (is_file($icon = $CONFIG['skin_path'] . '/images/icons/remove-attachment.png')) {
T 85         $button = html::img(array(
86           'src' => $icon,
87           'alt' => rcube_label('delete'),
88           'style' => "padding-right:2px;vertical-align:middle",
89         ));
599843 90       }
6d5dba 91       else {
T 92         $button = Q(rcube_label('delete'));
93       }
94
95       $content = html::a(array(
96         'href' => "#delete",
97         'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%d', this)", JS_OBJECT_NAME, $id),
98         'title' => rcube_label('delete'),
99       ), $button);
100       
101       $content .= Q($_FILES['_attachments']['name'][$i]);
102       
103       $OUTPUT->command('add2attachment_list', "rcmfile$id", $content);
104     }
105     else {  // upload failed
599843 106       $err = $_FILES['_attachments']['error'][$i];
6d5dba 107       if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
599843 108         $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
6d5dba 109       }
T 110       else {
599843 111         $msg = rcube_label('fileuploaderror');
6d5dba 112       }
86df15 113     
599843 114       $OUTPUT->command('display_message', $msg, 'error');
86df15 115     }
4e17e6 116   }
6d5dba 117 }
T 118 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
e34545 119   $OUTPUT->command('display_message', rcube_label('fileuploaderror'), 'error');
6d5dba 120 }
4e17e6 121
T 122 // send html page with JS calls as response
f11541 123 $OUTPUT->command('show_attachment_form', false);
T 124 $OUTPUT->command('auto_save_start', false);
125 $OUTPUT->send('iframe');
4e17e6 126
133bb0 127 ?>