summaryrefslogtreecommitdiff
path: root/plugins/redundant_attachments/redundant_attachments.php
blob: c0affad3cfc153cb89150e4e551ee09e2ffac214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
 * Redundant attachments
 *
 * This plugin provides a redundant storage for temporary uploaded
 * attachment files. They are stored in both the database backend
 * as well as on the local file system.
 *
 * It provides also memcache store as a fallback (see config file).
 *
 * This plugin relies on the core filesystem_attachments plugin
 * and combines it with the functionality of the database_attachments plugin.
 *
 * @author Thomas Bruederli <roundcube@gmail.com>
 * @author Aleksander Machniak <machniak@kolabsys.com>
 *
 * Copyright (C) 2011, The Roundcube Dev Team
 * Copyright (C) 2011, Kolab Systems AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

require_once(RCUBE_PLUGINS_DIR . 'filesystem_attachments/filesystem_attachments.php');

class redundant_attachments extends filesystem_attachments
{
    // A prefix for the cache key used in the session and in the key field of the cache table
    private $prefix = "ATTACH";

    // rcube_cache instance for SQL DB
    private $cache;

    // rcube_cache instance for memcache
    private $mem_cache;

    private $loaded;

    /**
     * Default constructor
     */
    function init()
    {
        parent::init();
    }

    /**
     * Loads plugin configuration and initializes cache object(s)
     */
    private function _load_drivers()
    {
        if ($this->loaded) {
            return;
        }

        $rcmail = rcmail::get_instance();

        // load configuration
        $this->load_config();

        $ttl = 12 * 60 * 60; // 12 hours
        $ttl = $rcmail->config->get('redundant_attachments_cache_ttl', $ttl);

        // Init SQL cache (disable cache data serialization)
        $this->cache = $rcmail->get_cache($this->prefix, 'db', $ttl, false);

        // Init memcache (fallback) cache
        if ($rcmail->config->get('redundant_attachments_memcache')) {
            $this->mem_cache = $rcmail->get_cache($this->prefix, 'memcache', $ttl, false);
        }

        $this->loaded = true;
    }

    /**
     * Helper method to generate a unique key for the given attachment file
     */
    private function _key($args)
    {
        $uname = $args['path'] ? $args['path'] : $args['name'];
        return $args['group'] . md5(mktime() . $uname . $_SESSION['user_id']);
    }

    /**
     * Save a newly uploaded attachment
     */
    function upload($args)
    {
        $args = parent::upload($args);

        $this->_load_drivers();

        $key  = $this->_key($args);
        $data = base64_encode(file_get_contents($args['path']));

        $status = $this->cache->write($key, $data);

        if (!$status && $this->mem_cache) {
            $status = $this->mem_cache->write($key, $data);
        }

        if ($status) {
            $args['id'] = $key;
            $args['status'] = true;
        }

        return $args;
    }

    /**
     * Save an attachment from a non-upload source (draft or forward)
     */
    function save($args)
    {
        $args = parent::save($args);

        $this->_load_drivers();

        if ($args['path'])
          $args['data'] = file_get_contents($args['path']);

        $key  = $this->_key($args);
        $data = base64_encode($args['data']);

        $status = $this->cache->write($key, $data);

        if (!$status && $this->mem_cache) {
            $status = $this->mem_cache->write($key, $data);
        }

        if ($status) {
            $args['id'] = $key;
            $args['status'] = true;
        }

        return $args;
    }

    /**
     * Remove an attachment from storage
     * This is triggered by the remove attachment button on the compose screen
     */
    function remove($args)
    {
        parent::remove($args);

        $this->_load_drivers();

        $status = $this->cache->remove($args['id']);

        if (!$status && $this->mem_cache) {
            $status = $this->cache->remove($args['id']);
        }

        // we cannot trust the result of any of the methods above
        // assume true, attachments will be removed on cleanup
        $args['status'] = true;

        return $args;
    }

    /**
     * When composing an html message, image attachments may be shown
     * For this plugin, $this->get() will check the file and
     * return it's contents
     */
    function display($args)
    {
        return $this->get($args);
    }

    /**
     * When displaying or sending the attachment the file contents are fetched
     * using this method. This is also called by the attachment_display hook.
     */
    function get($args)
    {
        // attempt to get file from local file system
        $args = parent::get($args);

        if ($args['path'] && ($args['status'] = file_exists($args['path'])))
          return $args;

        $this->_load_drivers();

        // fetch from database if not found on FS
        $data = $this->cache->read($args['id']);

        // fetch from memcache if not found on FS and DB
        if (($data === false || $data === null) && $this->mem_cache) {
            $data = $this->mem_cache->read($args['id']);
        }

        if ($data) {
            $args['data'] = base64_decode($data);
            $args['status'] = true;
        }

        return $args;
    }

    /**
     * Delete all temp files associated with this user
     */
    function cleanup($args)
    {
        $this->_load_drivers();

        if ($this->cache) {
            $this->cache->remove($args['group'], true);
        }

        if ($this->mem_cache) {
            $this->mem_cache->remove($args['group'], true);
        }

        parent::cleanup($args);

        $args['status'] = true;

        return $args;
    }
}