AbstractData.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.6.0
  11. */
  12. namespace PrivateBin\Data;
  13. /**
  14. * AbstractData
  15. *
  16. * Abstract model for data access
  17. */
  18. abstract class AbstractData
  19. {
  20. /**
  21. * cache for the traffic limiter
  22. *
  23. * @access protected
  24. * @var array
  25. */
  26. protected $_last_cache = array();
  27. /**
  28. * Create a paste.
  29. *
  30. * @access public
  31. * @param string $pasteid
  32. * @param array $paste
  33. * @return bool
  34. */
  35. abstract public function create($pasteid, array $paste);
  36. /**
  37. * Read a paste.
  38. *
  39. * @access public
  40. * @param string $pasteid
  41. * @return array|false
  42. */
  43. abstract public function read($pasteid);
  44. /**
  45. * Delete a paste and its discussion.
  46. *
  47. * @access public
  48. * @param string $pasteid
  49. */
  50. abstract public function delete($pasteid);
  51. /**
  52. * Test if a paste exists.
  53. *
  54. * @access public
  55. * @param string $pasteid
  56. * @return bool
  57. */
  58. abstract public function exists($pasteid);
  59. /**
  60. * Create a comment in a paste.
  61. *
  62. * @access public
  63. * @param string $pasteid
  64. * @param string $parentid
  65. * @param string $commentid
  66. * @param array $comment
  67. * @return bool
  68. */
  69. abstract public function createComment($pasteid, $parentid, $commentid, array $comment);
  70. /**
  71. * Read all comments of paste.
  72. *
  73. * @access public
  74. * @param string $pasteid
  75. * @return array
  76. */
  77. abstract public function readComments($pasteid);
  78. /**
  79. * Test if a comment exists.
  80. *
  81. * @access public
  82. * @param string $pasteid
  83. * @param string $parentid
  84. * @param string $commentid
  85. * @return bool
  86. */
  87. abstract public function existsComment($pasteid, $parentid, $commentid);
  88. /**
  89. * Purge outdated entries.
  90. *
  91. * @access public
  92. * @param string $namespace
  93. * @param int $time
  94. * @return void
  95. */
  96. public function purgeValues($namespace, $time)
  97. {
  98. if ($namespace === 'traffic_limiter') {
  99. foreach ($this->_last_cache as $key => $last_submission) {
  100. if ($last_submission <= $time) {
  101. unset($this->_last_cache[$key]);
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Save a value.
  108. *
  109. * @access public
  110. * @param string $value
  111. * @param string $namespace
  112. * @param string $key
  113. * @return bool
  114. */
  115. abstract public function setValue($value, $namespace, $key = '');
  116. /**
  117. * Load a value.
  118. *
  119. * @access public
  120. * @param string $namespace
  121. * @param string $key
  122. * @return string
  123. */
  124. abstract public function getValue($namespace, $key = '');
  125. /**
  126. * Returns up to batch size number of paste ids that have expired
  127. *
  128. * @access protected
  129. * @param int $batchsize
  130. * @return array
  131. */
  132. abstract protected function _getExpiredPastes($batchsize);
  133. /**
  134. * Perform a purge of old pastes, at most the given batchsize is deleted.
  135. *
  136. * @access public
  137. * @param int $batchsize
  138. */
  139. public function purge($batchsize)
  140. {
  141. if ($batchsize < 1) {
  142. return;
  143. }
  144. $pastes = $this->_getExpiredPastes($batchsize);
  145. if (count($pastes)) {
  146. foreach ($pastes as $pasteid) {
  147. $this->delete($pasteid);
  148. }
  149. }
  150. }
  151. /**
  152. * Returns all paste ids
  153. *
  154. * @access public
  155. * @return array
  156. */
  157. abstract public function getAllPastes();
  158. /**
  159. * Get next free slot for comment from postdate.
  160. *
  161. * @access protected
  162. * @param array $comments
  163. * @param int|string $postdate
  164. * @return int|string
  165. */
  166. protected function getOpenSlot(array &$comments, $postdate)
  167. {
  168. if (array_key_exists($postdate, $comments)) {
  169. $parts = explode('.', $postdate, 2);
  170. if (!array_key_exists(1, $parts)) {
  171. $parts[1] = 0;
  172. }
  173. ++$parts[1];
  174. return $this->getOpenSlot($comments, implode('.', $parts));
  175. }
  176. return $postdate;
  177. }
  178. /**
  179. * Upgrade pre-version 1 pastes with attachment to version 1 format.
  180. *
  181. * @access protected
  182. * @static
  183. * @param array $paste
  184. * @return array
  185. */
  186. protected static function upgradePreV1Format(array $paste)
  187. {
  188. if (array_key_exists('attachment', $paste['meta'])) {
  189. $paste['attachment'] = $paste['meta']['attachment'];
  190. unset($paste['meta']['attachment']);
  191. if (array_key_exists('attachmentname', $paste['meta'])) {
  192. $paste['attachmentname'] = $paste['meta']['attachmentname'];
  193. unset($paste['meta']['attachmentname']);
  194. }
  195. }
  196. return $paste;
  197. }
  198. }