Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

database_attachments.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $args['path'] = null;
  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 !== null && $data !== false) {
  111. $args['data'] = base64_decode($data);
  112. $args['status'] = true;
  113. }
  114. else {
  115. $args['status'] = false;
  116. }
  117. return $args;
  118. }
  119. /**
  120. * Delete all temp files associated with this user
  121. */
  122. function cleanup($args)
  123. {
  124. // check if cache object exist, it may be empty on session_destroy (#1489726)
  125. if ($cache = $this->get_cache()) {
  126. $cache->remove($args['group'], true);
  127. }
  128. }
  129. /**
  130. * Helper method to generate a unique key for the given attachment file
  131. */
  132. protected function _key($args)
  133. {
  134. $uname = $args['path'] ?: $args['name'];
  135. return $args['group'] . md5(time() . $uname . $_SESSION['user_id']);
  136. }
  137. /**
  138. * Initialize and return cache object
  139. */
  140. protected function get_cache()
  141. {
  142. if (!$this->cache) {
  143. $this->load_config();
  144. $rcmail = rcube::get_instance();
  145. $ttl = 12 * 60 * 60; // default: 12 hours
  146. $ttl = $rcmail->config->get('database_attachments_cache_ttl', $ttl);
  147. $type = $rcmail->config->get('database_attachments_cache', 'db');
  148. $prefix = self::PREFIX;
  149. // Add session identifier to the prefix to prevent from removing attachments
  150. // in other sessions of the same user (#1490542)
  151. if ($id = session_id()) {
  152. $prefix .= $id;
  153. }
  154. // Init SQL cache (disable cache data serialization)
  155. $this->cache = $rcmail->get_cache($prefix, $type, $ttl, false);
  156. }
  157. return $this->cache;
  158. }
  159. }