You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redundant_attachments.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Redundant attachments
  4. *
  5. * This plugin provides a redundant storage for temporary uploaded
  6. * attachment files. They are stored in both the database backend
  7. * as well as on the local file system.
  8. *
  9. * It provides also memcache store as a fallback (see config file).
  10. *
  11. * This plugin relies on the core filesystem_attachments plugin
  12. * and combines it with the functionality of the database_attachments plugin.
  13. *
  14. * @author Thomas Bruederli <roundcube@gmail.com>
  15. * @author Aleksander Machniak <machniak@kolabsys.com>
  16. *
  17. * Copyright (C) 2011, The Roundcube Dev Team
  18. * Copyright (C) 2011, Kolab Systems AG
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 2
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  32. */
  33. if (class_exists('filesystem_attachments', false) && !defined('TESTS_DIR')) {
  34. die("Configuration issue. There can be only one enabled plugin for attachments handling");
  35. }
  36. require_once(RCUBE_PLUGINS_DIR . 'filesystem_attachments/filesystem_attachments.php');
  37. class redundant_attachments extends filesystem_attachments
  38. {
  39. // A prefix for the cache key used in the session and in the key field of the cache table
  40. const PREFIX = "ATTACH";
  41. // rcube_cache instance for SQL DB
  42. private $cache;
  43. // rcube_cache instance for memcache
  44. private $mem_cache;
  45. private $loaded;
  46. /**
  47. * Loads plugin configuration and initializes cache object(s)
  48. */
  49. private function _load_drivers()
  50. {
  51. if ($this->loaded) {
  52. return;
  53. }
  54. $rcmail = rcube::get_instance();
  55. // load configuration
  56. $this->load_config();
  57. $ttl = 12 * 60 * 60; // 12 hours
  58. $ttl = $rcmail->config->get('redundant_attachments_cache_ttl', $ttl);
  59. $prefix = self::PREFIX;
  60. if ($id = session_id()) {
  61. $prefix .= $id;
  62. }
  63. // Init SQL cache (disable cache data serialization)
  64. $this->cache = $rcmail->get_cache($prefix, 'db', $ttl, false);
  65. // Init memcache (fallback) cache
  66. if ($rcmail->config->get('redundant_attachments_memcache')) {
  67. $this->mem_cache = $rcmail->get_cache($prefix, 'memcache', $ttl, false);
  68. }
  69. $this->loaded = true;
  70. }
  71. /**
  72. * Helper method to generate a unique key for the given attachment file
  73. */
  74. private function _key($args)
  75. {
  76. $uname = $args['path'] ?: $args['name'];
  77. return $args['group'] . md5(time() . $uname . $_SESSION['user_id']);
  78. }
  79. /**
  80. * Save a newly uploaded attachment
  81. */
  82. function upload($args)
  83. {
  84. $args = parent::upload($args);
  85. $this->_load_drivers();
  86. $key = $this->_key($args);
  87. $data = base64_encode(file_get_contents($args['path']));
  88. $status = $this->cache->write($key, $data);
  89. if (!$status && $this->mem_cache) {
  90. $status = $this->mem_cache->write($key, $data);
  91. }
  92. if ($status) {
  93. $args['id'] = $key;
  94. $args['status'] = true;
  95. }
  96. return $args;
  97. }
  98. /**
  99. * Save an attachment from a non-upload source (draft or forward)
  100. */
  101. function save($args)
  102. {
  103. $args = parent::save($args);
  104. $this->_load_drivers();
  105. $data = $args['path'] ? file_get_contents($args['path']) : $args['data'];
  106. $args['data'] = null;
  107. $key = $this->_key($args);
  108. $data = base64_encode($data);
  109. $status = $this->cache->write($key, $data);
  110. if (!$status && $this->mem_cache) {
  111. $status = $this->mem_cache->write($key, $data);
  112. }
  113. if ($status) {
  114. $args['id'] = $key;
  115. $args['status'] = true;
  116. }
  117. return $args;
  118. }
  119. /**
  120. * Remove an attachment from storage
  121. * This is triggered by the remove attachment button on the compose screen
  122. */
  123. function remove($args)
  124. {
  125. parent::remove($args);
  126. $this->_load_drivers();
  127. $status = $this->cache->remove($args['id']);
  128. if (!$status && $this->mem_cache) {
  129. $status = $this->cache->remove($args['id']);
  130. }
  131. // we cannot trust the result of any of the methods above
  132. // assume true, attachments will be removed on cleanup
  133. $args['status'] = true;
  134. return $args;
  135. }
  136. /**
  137. * When composing an html message, image attachments may be shown
  138. * For this plugin, $this->get() will check the file and
  139. * return it's contents
  140. */
  141. function display($args)
  142. {
  143. return $this->get($args);
  144. }
  145. /**
  146. * When displaying or sending the attachment the file contents are fetched
  147. * using this method. This is also called by the attachment_display hook.
  148. */
  149. function get($args)
  150. {
  151. // attempt to get file from local file system
  152. $args = parent::get($args);
  153. if ($args['path'] && ($args['status'] = file_exists($args['path'])))
  154. return $args;
  155. $this->_load_drivers();
  156. // fetch from database if not found on FS
  157. $data = $this->cache->read($args['id']);
  158. // fetch from memcache if not found on FS and DB
  159. if (($data === false || $data === null) && $this->mem_cache) {
  160. $data = $this->mem_cache->read($args['id']);
  161. }
  162. if ($data) {
  163. $args['data'] = base64_decode($data);
  164. $args['status'] = true;
  165. }
  166. return $args;
  167. }
  168. /**
  169. * Delete all temp files associated with this user
  170. */
  171. function cleanup($args)
  172. {
  173. $this->_load_drivers();
  174. if ($this->cache) {
  175. $this->cache->remove($args['group'], true);
  176. }
  177. if ($this->mem_cache) {
  178. $this->mem_cache->remove($args['group'], true);
  179. }
  180. parent::cleanup($args);
  181. $args['status'] = true;
  182. return $args;
  183. }
  184. }