AbstractData.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.2.3
  11. */
  12. namespace PrivateBin\Data;
  13. use stdClass;
  14. /**
  15. * AbstractData
  16. *
  17. * Abstract model for PrivateBin data access, implemented as a singleton.
  18. */
  19. abstract class AbstractData
  20. {
  21. /**
  22. * singleton instance
  23. *
  24. * @access protected
  25. * @static
  26. * @var AbstractData
  27. */
  28. protected static $_instance = null;
  29. /**
  30. * enforce singleton, disable constructor
  31. *
  32. * Instantiate using {@link getInstance()}, privatebin is a singleton object.
  33. *
  34. * @access protected
  35. */
  36. protected function __construct()
  37. {
  38. }
  39. /**
  40. * enforce singleton, disable cloning
  41. *
  42. * Instantiate using {@link getInstance()}, privatebin is a singleton object.
  43. *
  44. * @access private
  45. */
  46. private function __clone()
  47. {
  48. }
  49. /**
  50. * get instance of singleton
  51. *
  52. * @access public
  53. * @static
  54. * @param array $options
  55. * @return AbstractData
  56. */
  57. public static function getInstance($options)
  58. {
  59. }
  60. /**
  61. * Create a paste.
  62. *
  63. * @access public
  64. * @param string $pasteid
  65. * @param array $paste
  66. * @return bool
  67. */
  68. abstract public function create($pasteid, $paste);
  69. /**
  70. * Read a paste.
  71. *
  72. * @access public
  73. * @param string $pasteid
  74. * @return stdClass|false
  75. */
  76. abstract public function read($pasteid);
  77. /**
  78. * Delete a paste and its discussion.
  79. *
  80. * @access public
  81. * @param string $pasteid
  82. */
  83. abstract public function delete($pasteid);
  84. /**
  85. * Test if a paste exists.
  86. *
  87. * @access public
  88. * @param string $pasteid
  89. * @return bool
  90. */
  91. abstract public function exists($pasteid);
  92. /**
  93. * Create a comment in a paste.
  94. *
  95. * @access public
  96. * @param string $pasteid
  97. * @param string $parentid
  98. * @param string $commentid
  99. * @param array $comment
  100. * @return bool
  101. */
  102. abstract public function createComment($pasteid, $parentid, $commentid, $comment);
  103. /**
  104. * Read all comments of paste.
  105. *
  106. * @access public
  107. * @param string $pasteid
  108. * @return array
  109. */
  110. abstract public function readComments($pasteid);
  111. /**
  112. * Test if a comment exists.
  113. *
  114. * @access public
  115. * @param string $pasteid
  116. * @param string $parentid
  117. * @param string $commentid
  118. * @return bool
  119. */
  120. abstract public function existsComment($pasteid, $parentid, $commentid);
  121. /**
  122. * Returns up to batch size number of paste ids that have expired
  123. *
  124. * @access protected
  125. * @param int $batchsize
  126. * @return array
  127. */
  128. abstract protected function _getExpiredPastes($batchsize);
  129. /**
  130. * Perform a purge of old pastes, at most the given batchsize is deleted.
  131. *
  132. * @access public
  133. * @param int $batchsize
  134. */
  135. public function purge($batchsize)
  136. {
  137. if ($batchsize < 1) {
  138. return;
  139. }
  140. $pastes = $this->_getExpiredPastes($batchsize);
  141. if (count($pastes)) {
  142. foreach ($pastes as $pasteid) {
  143. $this->delete($pasteid);
  144. }
  145. }
  146. }
  147. /**
  148. * Get next free slot for comment from postdate.
  149. *
  150. * @access public
  151. * @param array $comments
  152. * @param int|string $postdate
  153. * @return int|string
  154. */
  155. protected function getOpenSlot(&$comments, $postdate)
  156. {
  157. if (array_key_exists($postdate, $comments)) {
  158. $parts = explode('.', $postdate, 2);
  159. if (!array_key_exists(1, $parts)) {
  160. $parts[1] = 0;
  161. }
  162. ++$parts[1];
  163. return $this->getOpenSlot($comments, implode('.', $parts));
  164. }
  165. return $postdate;
  166. }
  167. }