3 * Filesystem Attachments
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
10 * This plugin relies on the core filesystem_attachments plugin
12 * @author Ziba Scott <ziba@umich.edu>
15 require_once('plugins/filesystem_attachments/filesystem_attachments.php');
16 class database_attachments extends filesystem_attachments
19 // A prefix for the cache key used in the session and in the key field of the cache table
20 private $cache_prefix = "db_attach";
23 * Helper method to generate a unique key for the given attachment file
25 private function _key($filepath)
27 return $this->cache_prefix.md5(mktime().$filepath.$_SESSION['user_id']);
31 * Save a newly uploaded attachment
33 function upload($args)
35 $args['status'] = false;
36 $rcmail = rcmail::get_instance();
37 $key = $this->_key($args['path']);
38 $data = base64_encode(file_get_contents($args['path']));
40 $status = $rcmail->db->query(
41 "INSERT INTO ".get_table_name('cache')."
42 (created, user_id, cache_key, data)
43 VALUES (".$rcmail->db->now().", ?, ?, ?)",
50 $args['status'] = true;
58 * Save an attachment from a non-upload source (draft or forward)
62 $args['status'] = false;
63 $rcmail = rcmail::get_instance();
65 $key = $this->_key($args['name']);
68 $args['data'] = file_get_contents($args['path']);
70 $data = base64_encode($args['data']);
72 $status = $rcmail->db->query(
73 "INSERT INTO ".get_table_name('cache')."
74 (created, user_id, cache_key, data)
75 VALUES (".$rcmail->db->now().", ?, ?, ?)",
82 $args['status'] = true;
89 * Remove an attachment from storage
90 * This is triggered by the remove attachment button on the compose screen
92 function remove($args)
94 $args['status'] = false;
95 $rcmail = rcmail::get_instance();
96 $status = $rcmail->db->query(
97 "DELETE FROM ".get_table_name('cache')."
100 $_SESSION['user_id'],
104 $args['status'] = true;
111 * When composing an html message, image attachments may be shown
112 * For this plugin, $this->get() will check the file and
113 * return it's contents
115 function display($args)
117 return $this->get($args);
121 * When displaying or sending the attachment the file contents are fetched
122 * using this method. This is also called by the attachment_display hook.
126 $rcmail = rcmail::get_instance();
128 $sql_result = $rcmail->db->query(
129 "SELECT cache_id, data
130 FROM ".get_table_name('cache')."
133 $_SESSION['user_id'],
136 if ($sql_arr = $rcmail->db->fetch_assoc($sql_result)) {
137 $args['data'] = base64_decode($sql_arr['data']);
138 $args['status'] = true;
145 * Delete all temp files associated with this user
147 function cleanup($args)
149 $rcmail = rcmail::get_instance();
151 "DELETE FROM ".get_table_name('cache')."
153 AND cache_key like '{$this->cache_prefix}%'",
154 $_SESSION['user_id']);