database_attachments.php 5.1 KB

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