]> git.donarmstrong.com Git - roundcube.git/blob - plugins/database_attachments/database_attachments.php
Imported Upstream version 0.6+dfsg
[roundcube.git] / plugins / database_attachments / database_attachments.php
1 <?php
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  * 
14  */
15 require_once('plugins/filesystem_attachments/filesystem_attachments.php');
16 class database_attachments extends filesystem_attachments
17 {
18
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";
21
22     /**
23      * Helper method to generate a unique key for the given attachment file
24      */
25     private function _key($args)
26     {
27         $uname = $args['path'] ? $args['path'] : $args['name'];
28         return  $this->cache_prefix . $args['group'] . md5(mktime() . $uname . $_SESSION['user_id']);
29     }
30
31     /**
32      * Save a newly uploaded attachment
33      */
34     function upload($args)
35     {
36         $args['status'] = false;
37         $rcmail = rcmail::get_instance();
38         $key = $this->_key($args);
39
40         $data = file_get_contents($args['path']);
41
42         if ($data === false)
43             return $args;
44
45         $data = base64_encode($data);
46
47         $status = $rcmail->db->query(
48             "INSERT INTO ".get_table_name('cache')."
49              (created, user_id, cache_key, data)
50              VALUES (".$rcmail->db->now().", ?, ?, ?)",
51             $_SESSION['user_id'],
52             $key,
53             $data);
54
55         if ($status) {
56             $args['id'] = $key;
57             $args['status'] = true;
58             unset($args['path']);
59         }
60
61         return $args;
62     }
63
64     /**
65      * Save an attachment from a non-upload source (draft or forward)
66      */
67     function save($args)
68     {
69         $args['status'] = false;
70         $rcmail = rcmail::get_instance();
71
72         $key = $this->_key($args);
73
74         if ($args['path']) {
75             $args['data'] = file_get_contents($args['path']);
76
77             if ($args['data'] === false)
78                 return $args;
79         }
80
81         $data = base64_encode($args['data']);
82
83         $status = $rcmail->db->query(
84             "INSERT INTO ".get_table_name('cache')."
85              (created, user_id, cache_key, data)
86              VALUES (".$rcmail->db->now().", ?, ?, ?)",
87             $_SESSION['user_id'],
88             $key,
89             $data);
90
91         if ($status) {
92             $args['id'] = $key;
93             $args['status'] = true;
94         }
95
96         return $args;
97     }
98
99     /**
100      * Remove an attachment from storage
101      * This is triggered by the remove attachment button on the compose screen
102      */
103     function remove($args)
104     {
105         $args['status'] = false;
106         $rcmail = rcmail::get_instance();
107         $status = $rcmail->db->query(
108             "DELETE FROM ".get_table_name('cache')."
109              WHERE  user_id=?
110              AND    cache_key=?",
111             $_SESSION['user_id'],
112             $args['id']);
113
114         if ($status) {
115             $args['status'] = true;
116         }
117
118         return $args;
119     }
120
121     /**
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
125      */
126     function display($args)
127     {
128         return $this->get($args);
129     }
130
131     /**
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.
134      */
135     function get($args)
136     {
137         $rcmail = rcmail::get_instance();
138
139         $sql_result = $rcmail->db->query(
140             "SELECT cache_id, data
141              FROM ".get_table_name('cache')."
142              WHERE  user_id=?
143              AND    cache_key=?",
144             $_SESSION['user_id'],
145             $args['id']);
146
147         if ($sql_arr = $rcmail->db->fetch_assoc($sql_result)) {
148             $args['data'] = base64_decode($sql_arr['data']);
149             $args['status'] = true;
150         }
151
152         return $args;
153     }
154
155     /**
156      * Delete all temp files associated with this user
157      */
158     function cleanup($args)
159     {
160         $prefix = $this->cache_prefix . $args['group'];
161         $rcmail = rcmail::get_instance();
162         $rcmail->db->query(
163             "DELETE FROM ".get_table_name('cache')."
164              WHERE  user_id=?
165              AND cache_key like '{$prefix}%'",
166             $_SESSION['user_id']);
167     }
168 }