3 * Filesystem Attachments
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.
11 * Developers may wish to extend this class when creating attachment
13 * require_once('plugins/filesystem_attachments/filesystem_attachments.php');
14 * class myCustom_attachments extends filesystem_attachments
16 * @author Ziba Scott <ziba@umich.edu>
17 * @author Thomas Bruederli <roundcube@gmail.com>
20 class filesystem_attachments extends rcube_plugin
22 public $task = 'mail|addressbook';
26 // Save a newly uploaded attachment
27 $this->add_hook('attachment_upload', array($this, 'upload'));
29 // Save an attachment from a non-upload source (draft or forward)
30 $this->add_hook('attachment_save', array($this, 'save'));
32 // Remove an attachment from storage
33 $this->add_hook('attachment_delete', array($this, 'remove'));
35 // When composing an html message, image attachments may be shown
36 $this->add_hook('attachment_display', array($this, 'display'));
38 // Get the attachment from storage and place it on disk to be sent
39 $this->add_hook('attachment_get', array($this, 'get'));
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'));
47 * Save a newly uploaded attachment
49 function upload($args)
51 $args['status'] = false;
52 $rcmail = rcmail::get_instance();
54 // use common temp dir for file uploads
55 $temp_dir = $rcmail->config->get('temp_dir');
56 $tmpfname = tempnam($temp_dir, 'rcmAttmnt');
58 if (move_uploaded_file($args['path'], $tmpfname) && file_exists($tmpfname)) {
59 $args['id'] = $this->file_id();
60 $args['path'] = $tmpfname;
61 $args['status'] = true;
63 // Note the file for later cleanup
64 $_SESSION['plugins']['filesystem_attachments']['tmp_files'][] = $tmpfname;
71 * Save an attachment from a non-upload source (draft or forward)
75 $args['status'] = false;
78 $rcmail = rcmail::get_instance();
79 $temp_dir = $rcmail->config->get('temp_dir');
80 $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
82 if ($fp = fopen($tmp_path, 'w')) {
83 fwrite($fp, $args['data']);
85 $args['path'] = $tmp_path;
90 $args['id'] = $this->file_id();
91 $args['status'] = true;
93 // Note the file for later cleanup
94 $_SESSION['plugins']['filesystem_attachments']['tmp_files'][] = $args['path'];
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'] = @unlink($args['path']);
110 * When composing an html message, image attachments may be shown
111 * For this plugin, the file is already in place, just check for
112 * the existance of the proper metadata
114 function display($args)
116 $args['status'] = file_exists($args['path']);
121 * This attachment plugin doesn't require any steps to put the file
122 * on disk for use. This stub function is kept here to make this
123 * class handy as a parent class for other plugins which may need it.
131 * Delete all temp files associated with this user
133 function cleanup($args)
135 // $_SESSION['compose']['attachments'] is not a complete record of
136 // temporary files because loading a draft or starting a forward copies
137 // the file to disk, but does not make an entry in that array
138 if (is_array($_SESSION['plugins']['filesystem_attachments']['tmp_files'])){
139 foreach ($_SESSION['plugins']['filesystem_attachments']['tmp_files'] as $filename){
140 if(file_exists($filename)){
144 unset($_SESSION['plugins']['filesystem_attachments']['tmp_files']);
151 $userid = rcmail::get_instance()->user->ID;
152 list($usec, $sec) = explode(' ', microtime());
153 return preg_replace('/[^0-9]/', '', $userid . $sec . $usec);