1
0

AbstractData.php 5.0 KB

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