Mercurial > hg > rc1
comparison plugins/database_attachments/database_attachments.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 15:50:29 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e000243b222 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * Database Attachments | |
5 * | |
6 * This plugin which provides database backed storage for temporary | |
7 * attachment file handling. The primary advantage of this plugin | |
8 * is its compatibility with round-robin dns multi-server roundcube | |
9 * installations. | |
10 * | |
11 * This plugin relies on the core filesystem_attachments plugin | |
12 * | |
13 * @author Ziba Scott <ziba@umich.edu> | |
14 * @author Aleksander Machniak <alec@alec.pl> | |
15 * | |
16 * This program is free software; you can redistribute it and/or modify | |
17 * it under the terms of the GNU General Public License version 2 | |
18 * as published by the Free Software Foundation. | |
19 * | |
20 * This program is distributed in the hope that it will be useful, | |
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 * GNU General Public License for more details. | |
24 * | |
25 * You should have received a copy of the GNU General Public License along | |
26 * with this program; if not, write to the Free Software Foundation, Inc., | |
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
28 */ | |
29 | |
30 if (class_exists('filesystem_attachments', false) && !defined('TESTS_DIR')) { | |
31 die("Configuration issue. There can be only one enabled plugin for attachments handling"); | |
32 } | |
33 | |
34 require_once INSTALL_PATH . 'plugins/filesystem_attachments/filesystem_attachments.php'; | |
35 | |
36 class database_attachments extends filesystem_attachments | |
37 { | |
38 // Cache object | |
39 protected $cache; | |
40 | |
41 // A prefix for the cache key used in the session and in the key field of the cache table | |
42 const PREFIX = "ATTACH"; | |
43 | |
44 /** | |
45 * Save a newly uploaded attachment | |
46 */ | |
47 function upload($args) | |
48 { | |
49 $args['status'] = false; | |
50 | |
51 $cache = $this->get_cache(); | |
52 $key = $this->_key($args); | |
53 $data = file_get_contents($args['path']); | |
54 | |
55 if ($data === false) { | |
56 return $args; | |
57 } | |
58 | |
59 $data = base64_encode($data); | |
60 $status = $cache->write($key, $data); | |
61 | |
62 if ($status) { | |
63 $args['id'] = $key; | |
64 $args['status'] = true; | |
65 $args['path'] = null; | |
66 } | |
67 | |
68 return $args; | |
69 } | |
70 | |
71 /** | |
72 * Save an attachment from a non-upload source (draft or forward) | |
73 */ | |
74 function save($args) | |
75 { | |
76 $args['status'] = false; | |
77 | |
78 $cache = $this->get_cache(); | |
79 $key = $this->_key($args); | |
80 | |
81 if ($args['path']) { | |
82 $args['data'] = file_get_contents($args['path']); | |
83 | |
84 if ($args['data'] === false) { | |
85 return $args; | |
86 } | |
87 | |
88 $args['path'] = null; | |
89 } | |
90 | |
91 $data = base64_encode($args['data']); | |
92 $status = $cache->write($key, $data); | |
93 | |
94 if ($status) { | |
95 $args['id'] = $key; | |
96 $args['status'] = true; | |
97 } | |
98 | |
99 return $args; | |
100 } | |
101 | |
102 /** | |
103 * Remove an attachment from storage | |
104 * This is triggered by the remove attachment button on the compose screen | |
105 */ | |
106 function remove($args) | |
107 { | |
108 $cache = $this->get_cache(); | |
109 $status = $cache->remove($args['id']); | |
110 | |
111 $args['status'] = true; | |
112 | |
113 return $args; | |
114 } | |
115 | |
116 /** | |
117 * When composing an html message, image attachments may be shown | |
118 * For this plugin, $this->get() will check the file and | |
119 * return it's contents | |
120 */ | |
121 function display($args) | |
122 { | |
123 return $this->get($args); | |
124 } | |
125 | |
126 /** | |
127 * When displaying or sending the attachment the file contents are fetched | |
128 * using this method. This is also called by the attachment_display hook. | |
129 */ | |
130 function get($args) | |
131 { | |
132 $cache = $this->get_cache(); | |
133 $data = $cache->read($args['id']); | |
134 | |
135 if ($data !== null && $data !== false) { | |
136 $args['data'] = base64_decode($data); | |
137 $args['status'] = true; | |
138 } | |
139 else { | |
140 $args['status'] = false; | |
141 } | |
142 | |
143 return $args; | |
144 } | |
145 | |
146 /** | |
147 * Delete all temp files associated with this user | |
148 */ | |
149 function cleanup($args) | |
150 { | |
151 // check if cache object exist, it may be empty on session_destroy (#1489726) | |
152 if ($cache = $this->get_cache()) { | |
153 $cache->remove($args['group'], true); | |
154 } | |
155 } | |
156 | |
157 /** | |
158 * Helper method to generate a unique key for the given attachment file | |
159 */ | |
160 protected function _key($args) | |
161 { | |
162 $uname = $args['path'] ?: $args['name']; | |
163 return $args['group'] . md5(time() . $uname . $_SESSION['user_id']); | |
164 } | |
165 | |
166 /** | |
167 * Initialize and return cache object | |
168 */ | |
169 protected function get_cache() | |
170 { | |
171 if (!$this->cache) { | |
172 $this->load_config(); | |
173 | |
174 $rcmail = rcube::get_instance(); | |
175 $ttl = 12 * 60 * 60; // default: 12 hours | |
176 $ttl = $rcmail->config->get('database_attachments_cache_ttl', $ttl); | |
177 $type = $rcmail->config->get('database_attachments_cache', 'db'); | |
178 $prefix = self::PREFIX; | |
179 | |
180 // Add session identifier to the prefix to prevent from removing attachments | |
181 // in other sessions of the same user (#1490542) | |
182 if ($id = session_id()) { | |
183 $prefix .= $id; | |
184 } | |
185 | |
186 // Init SQL cache (disable cache data serialization) | |
187 $this->cache = $rcmail->get_cache($prefix, $type, $ttl, false); | |
188 } | |
189 | |
190 return $this->cache; | |
191 } | |
192 } |