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