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.

database_attachments.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Database 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. * @author Aleksander Machniak <alec@alec.pl>
  14. * @version @package_version@
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. if (class_exists('filesystem_attachments', false) && !defined('TESTS_DIR')) {
  30. die("Configuration issue. There can be only one enabled plugin for attachments handling");
  31. }
  32. require_once INSTALL_PATH . 'plugins/filesystem_attachments/filesystem_attachments.php';
  33. class database_attachments extends filesystem_attachments
  34. {
  35. // Cache object
  36. protected $cache;
  37. // A prefix for the cache key used in the session and in the key field of the cache table
  38. const PREFIX = "ATTACH";
  39. /**
  40. * Save a newly uploaded attachment
  41. */
  42. function upload($args)
  43. {
  44. $args['status'] = false;
  45. $cache = $this->get_cache();
  46. $key = $this->_key($args);
  47. $data = file_get_contents($args['path']);
  48. if ($data === false) {
  49. return $args;
  50. }
  51. $data = base64_encode($data);
  52. $status = $cache->write($key, $data);
  53. if ($status) {
  54. $args['id'] = $key;
  55. $args['status'] = true;
  56. $args['path'] = null;
  57. }
  58. return $args;
  59. }
  60. /**
  61. * Save an attachment from a non-upload source (draft or forward)
  62. */
  63. function save($args)
  64. {
  65. $args['status'] = false;
  66. $cache = $this->get_cache();
  67. $key = $this->_key($args);
  68. if ($args['path']) {
  69. $args['data'] = file_get_contents($args['path']);
  70. if ($args['data'] === false) {
  71. return $args;
  72. }
  73. }
  74. $data = base64_encode($args['data']);
  75. $status = $cache->write($key, $data);
  76. if ($status) {
  77. $args['id'] = $key;
  78. $args['status'] = true;
  79. }
  80. return $args;
  81. }
  82. /**
  83. * Remove an attachment from storage
  84. * This is triggered by the remove attachment button on the compose screen
  85. */
  86. function remove($args)
  87. {
  88. $cache = $this->get_cache();
  89. $status = $cache->remove($args['id']);
  90. $args['status'] = true;
  91. return $args;
  92. }
  93. /**
  94. * When composing an html message, image attachments may be shown
  95. * For this plugin, $this->get() will check the file and
  96. * return it's contents
  97. */
  98. function display($args)
  99. {
  100. return $this->get($args);
  101. }
  102. /**
  103. * When displaying or sending the attachment the file contents are fetched
  104. * using this method. This is also called by the attachment_display hook.
  105. */
  106. function get($args)
  107. {
  108. $cache = $this->get_cache();
  109. $data = $cache->read($args['id']);
  110. if ($data) {
  111. $args['data'] = base64_decode($data);
  112. $args['status'] = true;
  113. }
  114. return $args;
  115. }
  116. /**
  117. * Delete all temp files associated with this user
  118. */
  119. function cleanup($args)
  120. {
  121. // check if cache object exist, it may be empty on session_destroy (#1489726)
  122. if ($cache = $this->get_cache()) {
  123. $cache->remove($args['group'], true);
  124. }
  125. }
  126. /**
  127. * Helper method to generate a unique key for the given attachment file
  128. */
  129. protected function _key($args)
  130. {
  131. $uname = $args['path'] ?: $args['name'];
  132. return $args['group'] . md5(time() . $uname . $_SESSION['user_id']);
  133. }
  134. /**
  135. * Initialize and return cache object
  136. */
  137. protected function get_cache()
  138. {
  139. if (!$this->cache) {
  140. $this->load_config();
  141. $rcmail = rcube::get_instance();
  142. $ttl = 12 * 60 * 60; // default: 12 hours
  143. $ttl = $rcmail->config->get('database_attachments_cache_ttl', $ttl);
  144. $type = $rcmail->config->get('database_attachments_cache', 'db');
  145. $prefix = self::PREFIX;
  146. // Add session identifier to the prefix to prevent from removing attachments
  147. // in other sessions of the same user (#1490542)
  148. if ($id = session_id()) {
  149. $prefix .= $id;
  150. }
  151. // Init SQL cache (disable cache data serialization)
  152. $this->cache = $rcmail->get_cache($prefix, $type, $ttl, false);
  153. }
  154. return $this->cache;
  155. }
  156. }