]> git.donarmstrong.com Git - roundcube.git/blob - plugins/filesystem_attachments/filesystem_attachments.php
Merge commit 'upstream/0.7'
[roundcube.git] / plugins / filesystem_attachments / filesystem_attachments.php
1 <?php
2 /**
3  * Filesystem Attachments
4  * 
5  * This is a core plugin which provides basic, filesystem based
6  * attachment temporary file handling.  This includes storing
7  * attachments of messages currently being composed, writing attachments
8  * to disk when drafts with attachments are re-opened and writing
9  * attachments to disk for inline display in current html compositions.
10  *
11  * Developers may wish to extend this class when creating attachment
12  * handler plugins:
13  *   require_once('plugins/filesystem_attachments/filesystem_attachments.php');
14  *   class myCustom_attachments extends filesystem_attachments
15  *
16  * @author Ziba Scott <ziba@umich.edu>
17  * @author Thomas Bruederli <roundcube@gmail.com>
18  * 
19  */
20 class filesystem_attachments extends rcube_plugin
21 {
22     public $task = '?(?!login).*';
23
24     function init()
25     {
26         // Save a newly uploaded attachment
27         $this->add_hook('attachment_upload', array($this, 'upload'));
28
29         // Save an attachment from a non-upload source (draft or forward)
30         $this->add_hook('attachment_save', array($this, 'save'));
31
32         // Remove an attachment from storage
33         $this->add_hook('attachment_delete', array($this, 'remove'));
34
35         // When composing an html message, image attachments may be shown
36         $this->add_hook('attachment_display', array($this, 'display'));
37
38         // Get the attachment from storage and place it on disk to be sent
39         $this->add_hook('attachment_get', array($this, 'get'));
40
41         // Delete all temp files associated with this user
42         $this->add_hook('attachments_cleanup', array($this, 'cleanup'));
43         $this->add_hook('session_destroy', array($this, 'cleanup'));
44     }
45
46     /**
47      * Save a newly uploaded attachment
48      */
49     function upload($args)
50     {
51         $args['status'] = false;
52         $group = $args['group'];
53         $rcmail = rcmail::get_instance();
54
55         // use common temp dir for file uploads
56         $temp_dir = $rcmail->config->get('temp_dir');
57         $tmpfname = tempnam($temp_dir, 'rcmAttmnt');
58
59         if (move_uploaded_file($args['path'], $tmpfname) && file_exists($tmpfname)) {
60             $args['id'] = $this->file_id();
61             $args['path'] = $tmpfname;
62             $args['status'] = true;
63
64             // Note the file for later cleanup
65             $_SESSION['plugins']['filesystem_attachments'][$group][] = $tmpfname;
66         }
67
68         return $args;
69     }
70
71     /**
72      * Save an attachment from a non-upload source (draft or forward)
73      */
74     function save($args)
75     {
76         $group = $args['group'];
77         $args['status'] = false;
78
79         if (!$args['path']) {
80             $rcmail = rcmail::get_instance();
81             $temp_dir = $rcmail->config->get('temp_dir');
82             $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
83
84             if ($fp = fopen($tmp_path, 'w')) {
85                 fwrite($fp, $args['data']);
86                 fclose($fp);
87                 $args['path'] = $tmp_path;
88             } else
89                 return $args;
90         }
91
92         $args['id'] = $this->file_id();
93         $args['status'] = true;
94
95         // Note the file for later cleanup
96         $_SESSION['plugins']['filesystem_attachments'][$group][] = $args['path'];
97
98         return $args;
99     }
100
101     /**
102      * Remove an attachment from storage
103      * This is triggered by the remove attachment button on the compose screen
104      */
105     function remove($args)
106     {
107         $args['status'] = @unlink($args['path']);
108         return $args;
109     }
110
111     /**
112      * When composing an html message, image attachments may be shown
113      * For this plugin, the file is already in place, just check for
114      * the existance of the proper metadata
115      */
116     function display($args)
117     {
118         $args['status'] = file_exists($args['path']);
119         return $args;
120     }
121
122     /**
123      * This attachment plugin doesn't require any steps to put the file
124      * on disk for use.  This stub function is kept here to make this 
125      * class handy as a parent class for other plugins which may need it.
126      */
127     function get($args)
128     {
129         return $args;
130     }
131
132     /**
133      * Delete all temp files associated with this user
134      */
135     function cleanup($args)
136     {
137         // $_SESSION['compose']['attachments'] is not a complete record of
138         // temporary files because loading a draft or starting a forward copies
139         // the file to disk, but does not make an entry in that array
140         if (is_array($_SESSION['plugins']['filesystem_attachments'])){
141             foreach ($_SESSION['plugins']['filesystem_attachments'] as $group => $files) {
142                 if ($args['group'] && $args['group'] != $group)
143                     continue;
144                 foreach ((array)$files as $filename){
145                     if(file_exists($filename)){
146                         unlink($filename);
147                     }
148                 }
149                 unset($_SESSION['plugins']['filesystem_attachments'][$group]);
150             }
151         }
152         return $args;
153     }
154
155     function file_id()
156     {
157         $userid = rcmail::get_instance()->user->ID;
158             list($usec, $sec) = explode(' ', microtime()); 
159         return preg_replace('/[^0-9]/', '', $userid . $sec . $usec);
160     }
161 }