commit | author | age
|
48e9c1
|
1 |
<?php |
T |
2 |
/** |
|
3 |
* Filesystem Attachments |
|
4 |
* |
|
5 |
* This is a core plugin which provides basic, filesystem based |
|
6 |
* attachment temporary file handling. This includes storing |
|
7 |
* attachments of messages currently being composed, writing attachments |
|
8 |
* to disk when drafts with attachments are re-opened and writing |
|
9 |
* attachments to disk for inline display in current html compositions. |
|
10 |
* |
|
11 |
* Developers may wish to extend this class when creating attachment |
|
12 |
* handler plugins: |
|
13 |
* require_once('plugins/filesystem_attachments/filesystem_attachments.php'); |
|
14 |
* class myCustom_attachments extends filesystem_attachments |
|
15 |
* |
07c6c6
|
16 |
* @license GNU GPLv3+ |
48e9c1
|
17 |
* @author Ziba Scott <ziba@umich.edu> |
T |
18 |
* @author Thomas Bruederli <roundcube@gmail.com> |
|
19 |
*/ |
|
20 |
class filesystem_attachments extends rcube_plugin |
|
21 |
{ |
|
22 |
public $task = '?(?!login).*'; |
|
23 |
|
|
24 |
function init() |
|
25 |
{ |
|
26 |
// Save a newly uploaded attachment |
|
27 |
$this->add_hook('attachment_upload', array($this, 'upload')); |
|
28 |
|
|
29 |
// Save an attachment from a non-upload source (draft or forward) |
|
30 |
$this->add_hook('attachment_save', array($this, 'save')); |
|
31 |
|
|
32 |
// Remove an attachment from storage |
|
33 |
$this->add_hook('attachment_delete', array($this, 'remove')); |
|
34 |
|
|
35 |
// When composing an html message, image attachments may be shown |
|
36 |
$this->add_hook('attachment_display', array($this, 'display')); |
|
37 |
|
|
38 |
// Get the attachment from storage and place it on disk to be sent |
|
39 |
$this->add_hook('attachment_get', array($this, 'get')); |
|
40 |
|
|
41 |
// Delete all temp files associated with this user |
|
42 |
$this->add_hook('attachments_cleanup', array($this, 'cleanup')); |
|
43 |
$this->add_hook('session_destroy', array($this, 'cleanup')); |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* Save a newly uploaded attachment |
|
48 |
*/ |
|
49 |
function upload($args) |
|
50 |
{ |
|
51 |
$args['status'] = false; |
d3883d
|
52 |
$group = $args['group']; |
1c861d
|
53 |
$rcmail = rcube::get_instance(); |
48e9c1
|
54 |
|
T |
55 |
// use common temp dir for file uploads |
|
56 |
$temp_dir = $rcmail->config->get('temp_dir'); |
|
57 |
$tmpfname = tempnam($temp_dir, 'rcmAttmnt'); |
|
58 |
|
|
59 |
if (move_uploaded_file($args['path'], $tmpfname) && file_exists($tmpfname)) { |
d3883d
|
60 |
$args['id'] = $this->file_id(); |
AM |
61 |
$args['path'] = $tmpfname; |
48e9c1
|
62 |
$args['status'] = true; |
64e1c2
|
63 |
@chmod($tmpfname, 0600); // set correct permissions (#1488996) |
48e9c1
|
64 |
|
T |
65 |
// Note the file for later cleanup |
d3883d
|
66 |
$_SESSION['plugins']['filesystem_attachments'][$group][$args['id']] = $tmpfname; |
48e9c1
|
67 |
} |
T |
68 |
|
|
69 |
return $args; |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* Save an attachment from a non-upload source (draft or forward) |
|
74 |
*/ |
|
75 |
function save($args) |
|
76 |
{ |
|
77 |
$group = $args['group']; |
|
78 |
$args['status'] = false; |
|
79 |
|
|
80 |
if (!$args['path']) { |
1c861d
|
81 |
$rcmail = rcube::get_instance(); |
48e9c1
|
82 |
$temp_dir = $rcmail->config->get('temp_dir'); |
T |
83 |
$tmp_path = tempnam($temp_dir, 'rcmAttmnt'); |
|
84 |
|
|
85 |
if ($fp = fopen($tmp_path, 'w')) { |
|
86 |
fwrite($fp, $args['data']); |
|
87 |
fclose($fp); |
|
88 |
$args['path'] = $tmp_path; |
d3883d
|
89 |
} |
AM |
90 |
else { |
48e9c1
|
91 |
return $args; |
d3883d
|
92 |
} |
48e9c1
|
93 |
} |
T |
94 |
|
d3883d
|
95 |
$args['id'] = $this->file_id(); |
48e9c1
|
96 |
$args['status'] = true; |
T |
97 |
|
|
98 |
// Note the file for later cleanup |
d3883d
|
99 |
$_SESSION['plugins']['filesystem_attachments'][$group][$args['id']] = $args['path']; |
48e9c1
|
100 |
|
T |
101 |
return $args; |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* Remove an attachment from storage |
|
106 |
* This is triggered by the remove attachment button on the compose screen |
|
107 |
*/ |
|
108 |
function remove($args) |
|
109 |
{ |
|
110 |
$args['status'] = @unlink($args['path']); |
|
111 |
return $args; |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* When composing an html message, image attachments may be shown |
|
116 |
* For this plugin, the file is already in place, just check for |
|
117 |
* the existance of the proper metadata |
|
118 |
*/ |
|
119 |
function display($args) |
|
120 |
{ |
|
121 |
$args['status'] = file_exists($args['path']); |
|
122 |
return $args; |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* This attachment plugin doesn't require any steps to put the file |
|
127 |
* on disk for use. This stub function is kept here to make this |
|
128 |
* class handy as a parent class for other plugins which may need it. |
|
129 |
*/ |
|
130 |
function get($args) |
|
131 |
{ |
|
132 |
return $args; |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* Delete all temp files associated with this user |
|
137 |
*/ |
|
138 |
function cleanup($args) |
|
139 |
{ |
|
140 |
// $_SESSION['compose']['attachments'] is not a complete record of |
|
141 |
// temporary files because loading a draft or starting a forward copies |
|
142 |
// the file to disk, but does not make an entry in that array |
d3883d
|
143 |
if (is_array($_SESSION['plugins']['filesystem_attachments'])) { |
48e9c1
|
144 |
foreach ($_SESSION['plugins']['filesystem_attachments'] as $group => $files) { |
d3883d
|
145 |
if ($args['group'] && $args['group'] != $group) { |
48e9c1
|
146 |
continue; |
d3883d
|
147 |
} |
AM |
148 |
|
|
149 |
foreach ((array)$files as $filename) { |
|
150 |
if(file_exists($filename)) { |
48e9c1
|
151 |
unlink($filename); |
T |
152 |
} |
|
153 |
} |
d3883d
|
154 |
|
48e9c1
|
155 |
unset($_SESSION['plugins']['filesystem_attachments'][$group]); |
T |
156 |
} |
|
157 |
} |
|
158 |
return $args; |
|
159 |
} |
|
160 |
|
|
161 |
function file_id() |
|
162 |
{ |
1c861d
|
163 |
$userid = rcube::get_instance()->user->ID; |
d3883d
|
164 |
list($usec, $sec) = explode(' ', microtime()); |
AM |
165 |
$id = preg_replace('/[^0-9]/', '', $userid . $sec . $usec); |
|
166 |
|
|
167 |
// make sure the ID is really unique (#1489546) |
|
168 |
while ($this->find_file_by_id($id)) { |
|
169 |
// increment last four characters |
|
170 |
$x = substr($id, -4) + 1; |
|
171 |
$id = substr($id, 0, -4) . sprintf('%04d', ($x > 9999 ? $x - 9999 : $x)); |
|
172 |
} |
|
173 |
|
|
174 |
return $id; |
|
175 |
} |
|
176 |
|
|
177 |
private function find_file_by_id($id) |
|
178 |
{ |
|
179 |
foreach ((array) $_SESSION['plugins']['filesystem_attachments'] as $group => $files) { |
|
180 |
if (isset($files[$id])) { |
|
181 |
return true; |
|
182 |
} |
|
183 |
} |
48e9c1
|
184 |
} |
T |
185 |
} |