Aleksander Machniak
2015-05-13 ead2368b716e95e0b62a9a961e2e14ca0ef728fe
commit | author | age
48e9c1 1 <?php
T 2 /**
f0398c 3  * Database Attachments
48e9c1 4  *
T 5  * This plugin which provides database backed storage for temporary
6  * attachment file handling.  The primary advantage of this plugin
7  * is its compatibility with round-robin dns multi-server roundcube
8  * installations.
9  *
10  * This plugin relies on the core filesystem_attachments plugin
11  *
12  * @author Ziba Scott <ziba@umich.edu>
13  * @author Aleksander Machniak <alec@alec.pl>
14  * @version @package_version@
15  */
2feba7 16
AM 17 require_once INSTALL_PATH . 'plugins/filesystem_attachments/filesystem_attachments.php';
18
48e9c1 19 class database_attachments extends filesystem_attachments
T 20 {
f0398c 21     // Cache object
AM 22     protected $cache;
48e9c1 23
T 24     // A prefix for the cache key used in the session and in the key field of the cache table
f0398c 25     protected $prefix = "db_attach";
48e9c1 26
T 27     /**
28      * Save a newly uploaded attachment
29      */
30     function upload($args)
31     {
32         $args['status'] = false;
33
f0398c 34         $cache = $this->get_cache();
AM 35         $key   = $this->_key($args);
36         $data  = file_get_contents($args['path']);
48e9c1 37
f0398c 38         if ($data === false) {
48e9c1 39             return $args;
f0398c 40         }
48e9c1 41
f0398c 42         $data   = base64_encode($data);
AM 43         $status = $cache->write($key, $data);
48e9c1 44
T 45         if ($status) {
46             $args['id'] = $key;
47             $args['status'] = true;
48             unset($args['path']);
49         }
50
51         return $args;
52     }
53
54     /**
55      * Save an attachment from a non-upload source (draft or forward)
56      */
57     function save($args)
58     {
59         $args['status'] = false;
60
f0398c 61         $cache = $this->get_cache();
AM 62         $key   = $this->_key($args);
48e9c1 63
T 64         if ($args['path']) {
65             $args['data'] = file_get_contents($args['path']);
66
f0398c 67             if ($args['data'] === false) {
48e9c1 68                 return $args;
f0398c 69             }
48e9c1 70         }
T 71
f0398c 72         $data   = base64_encode($args['data']);
AM 73         $status = $cache->write($key, $data);
48e9c1 74
T 75         if ($status) {
76             $args['id'] = $key;
77             $args['status'] = true;
78         }
79
80         return $args;
81     }
82
83     /**
84      * Remove an attachment from storage
85      * This is triggered by the remove attachment button on the compose screen
86      */
87     function remove($args)
88     {
f0398c 89         $cache  = $this->get_cache();
AM 90         $status = $cache->remove($args['id']);
48e9c1 91
5d0cb0 92         $args['status'] = true;
48e9c1 93
T 94         return $args;
95     }
96
97     /**
98      * When composing an html message, image attachments may be shown
99      * For this plugin, $this->get() will check the file and
100      * return it's contents
101      */
102     function display($args)
103     {
104         return $this->get($args);
105     }
106
107     /**
108      * When displaying or sending the attachment the file contents are fetched
109      * using this method. This is also called by the attachment_display hook.
110      */
111     function get($args)
112     {
f0398c 113         $cache = $this->get_cache();
AM 114         $data  = $cache->read($args['id']);
48e9c1 115
f0398c 116         if ($data) {
AM 117             $args['data'] = base64_decode($data);
48e9c1 118             $args['status'] = true;
T 119         }
120
121         return $args;
122     }
123
124     /**
125      * Delete all temp files associated with this user
126      */
127     function cleanup($args)
128     {
677fb7 129         // check if cache object exist, it may be empty on session_destroy (#1489726)
AM 130         if ($cache = $this->get_cache()) {
131             $cache->remove($args['group'], true);
132         }
f0398c 133     }
AM 134
135     /**
136      * Helper method to generate a unique key for the given attachment file
137      */
138     protected function _key($args)
139     {
140         $uname = $args['path'] ? $args['path'] : $args['name'];
141         return $args['group'] . md5(mktime() . $uname . $_SESSION['user_id']);
142     }
143
144     /**
145      * Initialize and return cache object
146      */
147     protected function get_cache()
148     {
149         if (!$this->cache) {
150             $this->load_config();
151
152             $rcmail = rcube::get_instance();
153             $ttl    = 12 * 60 * 60; // default: 12 hours
154             $ttl    = $rcmail->config->get('database_attachments_cache_ttl', $ttl);
155             $type   = $rcmail->config->get('database_attachments_cache', 'db');
156
157             // Init SQL cache (disable cache data serialization)
ead236 158             $this->cache = $rcmail->get_cache($this->prefix, $type, $ttl, false);
f0398c 159         }
AM 160
161         return $this->cache;
48e9c1 162     }
T 163 }