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($args)
27 $uname = $args['path'] ? $args['path'] : $args['name'];
28 return $this->cache_prefix . $args['group'] . md5(mktime() . $uname . $_SESSION['user_id']);
32 * Save a newly uploaded attachment
34 function upload($args)
36 $args['status'] = false;
37 $rcmail = rcmail::get_instance();
38 $key = $this->_key($args);
40 $data = file_get_contents($args['path']);
45 $data = base64_encode($data);
47 $status = $rcmail->db->query(
48 "INSERT INTO ".get_table_name('cache')."
49 (created, user_id, cache_key, data)
50 VALUES (".$rcmail->db->now().", ?, ?, ?)",
57 $args['status'] = true;
65 * Save an attachment from a non-upload source (draft or forward)
69 $args['status'] = false;
70 $rcmail = rcmail::get_instance();
72 $key = $this->_key($args);
75 $args['data'] = file_get_contents($args['path']);
77 if ($args['data'] === false)
81 $data = base64_encode($args['data']);
83 $status = $rcmail->db->query(
84 "INSERT INTO ".get_table_name('cache')."
85 (created, user_id, cache_key, data)
86 VALUES (".$rcmail->db->now().", ?, ?, ?)",
93 $args['status'] = true;
100 * Remove an attachment from storage
101 * This is triggered by the remove attachment button on the compose screen
103 function remove($args)
105 $args['status'] = false;
106 $rcmail = rcmail::get_instance();
107 $status = $rcmail->db->query(
108 "DELETE FROM ".get_table_name('cache')."
111 $_SESSION['user_id'],
115 $args['status'] = true;
122 * When composing an html message, image attachments may be shown
123 * For this plugin, $this->get() will check the file and
124 * return it's contents
126 function display($args)
128 return $this->get($args);
132 * When displaying or sending the attachment the file contents are fetched
133 * using this method. This is also called by the attachment_display hook.
137 $rcmail = rcmail::get_instance();
139 $sql_result = $rcmail->db->query(
140 "SELECT cache_id, data
141 FROM ".get_table_name('cache')."
144 $_SESSION['user_id'],
147 if ($sql_arr = $rcmail->db->fetch_assoc($sql_result)) {
148 $args['data'] = base64_decode($sql_arr['data']);
149 $args['status'] = true;
156 * Delete all temp files associated with this user
158 function cleanup($args)
160 $prefix = $this->cache_prefix . $args['group'];
161 $rcmail = rcmail::get_instance();
163 "DELETE FROM ".get_table_name('cache')."
165 AND cache_key like '{$prefix}%'",
166 $_SESSION['user_id']);