Thomas Bruederli
2012-08-06 c41a86e5cc26dc8ae37ed4b3fddcaa195b1616a4
commit | author | age
48e9c1 1 <?php
T 2 /**
3  * Filesystem Attachments
4  *
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  */
16 require_once('plugins/filesystem_attachments/filesystem_attachments.php');
17 class database_attachments extends filesystem_attachments
18 {
19
20     // A prefix for the cache key used in the session and in the key field of the cache table
21     private $cache_prefix = "db_attach";
22
23     /**
24      * Helper method to generate a unique key for the given attachment file
25      */
26     private function _key($args)
27     {
28         $uname = $args['path'] ? $args['path'] : $args['name'];
29         return  $this->cache_prefix . $args['group'] . md5(mktime() . $uname . $_SESSION['user_id']);
30     }
31
32     /**
33      * Save a newly uploaded attachment
34      */
35     function upload($args)
36     {
37         $args['status'] = false;
38         $rcmail = rcmail::get_instance();
39         $key = $this->_key($args);
40
41         $data = file_get_contents($args['path']);
42
43         if ($data === false)
44             return $args;
45
46         $data = base64_encode($data);
47
48         $status = $rcmail->db->query(
49             "INSERT INTO ".get_table_name('cache')."
50              (created, user_id, cache_key, data)
51              VALUES (".$rcmail->db->now().", ?, ?, ?)",
52             $_SESSION['user_id'],
53             $key,
54             $data);
55
56         if ($status) {
57             $args['id'] = $key;
58             $args['status'] = true;
59             unset($args['path']);
60         }
61
62         return $args;
63     }
64
65     /**
66      * Save an attachment from a non-upload source (draft or forward)
67      */
68     function save($args)
69     {
70         $args['status'] = false;
71         $rcmail = rcmail::get_instance();
72
73         $key = $this->_key($args);
74
75         if ($args['path']) {
76             $args['data'] = file_get_contents($args['path']);
77
78             if ($args['data'] === false)
79                 return $args;
80         }
81
82         $data = base64_encode($args['data']);
83
84         $status = $rcmail->db->query(
85             "INSERT INTO ".get_table_name('cache')."
86              (created, user_id, cache_key, data)
87              VALUES (".$rcmail->db->now().", ?, ?, ?)",
88             $_SESSION['user_id'],
89             $key,
90             $data);
91
92         if ($status) {
93             $args['id'] = $key;
94             $args['status'] = true;
95         }
96
97         return $args;
98     }
99
100     /**
101      * Remove an attachment from storage
102      * This is triggered by the remove attachment button on the compose screen
103      */
104     function remove($args)
105     {
106         $args['status'] = false;
107         $rcmail = rcmail::get_instance();
108         $status = $rcmail->db->query(
109             "DELETE FROM ".get_table_name('cache')."
110              WHERE  user_id=?
111              AND    cache_key=?",
112             $_SESSION['user_id'],
113             $args['id']);
114
115         if ($status) {
116             $args['status'] = true;
117         }
118
119         return $args;
120     }
121
122     /**
123      * When composing an html message, image attachments may be shown
124      * For this plugin, $this->get() will check the file and
125      * return it's contents
126      */
127     function display($args)
128     {
129         return $this->get($args);
130     }
131
132     /**
133      * When displaying or sending the attachment the file contents are fetched
134      * using this method. This is also called by the attachment_display hook.
135      */
136     function get($args)
137     {
138         $rcmail = rcmail::get_instance();
139
140         $sql_result = $rcmail->db->query(
141             "SELECT cache_id, data
142              FROM ".get_table_name('cache')."
143              WHERE  user_id=?
144              AND    cache_key=?",
145             $_SESSION['user_id'],
146             $args['id']);
147
148         if ($sql_arr = $rcmail->db->fetch_assoc($sql_result)) {
149             $args['data'] = base64_decode($sql_arr['data']);
150             $args['status'] = true;
151         }
152
153         return $args;
154     }
155
156     /**
157      * Delete all temp files associated with this user
158      */
159     function cleanup($args)
160     {
161         $prefix = $this->cache_prefix . $args['group'];
162         $rcmail = rcmail::get_instance();
163         $rcmail->db->query(
164             "DELETE FROM ".get_table_name('cache')."
165              WHERE  user_id=?
166              AND cache_key like '{$prefix}%'",
167             $_SESSION['user_id']);
168     }
169 }