1
0

AbstractData.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.0
  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 privatebin_abstract
  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. * @return void
  83. */
  84. abstract public function delete($pasteid);
  85. /**
  86. * Test if a paste exists.
  87. *
  88. * @access public
  89. * @param string $pasteid
  90. * @return bool
  91. */
  92. abstract public function exists($pasteid);
  93. /**
  94. * Create a comment in a paste.
  95. *
  96. * @access public
  97. * @param string $pasteid
  98. * @param string $parentid
  99. * @param string $commentid
  100. * @param array $comment
  101. * @return bool
  102. */
  103. abstract public function createComment($pasteid, $parentid, $commentid, $comment);
  104. /**
  105. * Read all comments of paste.
  106. *
  107. * @access public
  108. * @param string $pasteid
  109. * @return array
  110. */
  111. abstract public function readComments($pasteid);
  112. /**
  113. * Test if a comment exists.
  114. *
  115. * @access public
  116. * @param string $pasteid
  117. * @param string $parentid
  118. * @param string $commentid
  119. * @return bool
  120. */
  121. abstract public function existsComment($pasteid, $parentid, $commentid);
  122. /**
  123. * Returns up to batch size number of paste ids that have expired
  124. *
  125. * @access protected
  126. * @param int $batchsize
  127. * @return array
  128. */
  129. abstract protected function _getExpiredPastes($batchsize);
  130. /**
  131. * Perform a purge of old pastes, at most the given batchsize is deleted.
  132. *
  133. * @access public
  134. * @param int $batchsize
  135. * @return void
  136. */
  137. public function purge($batchsize)
  138. {
  139. if ($batchsize < 1) {
  140. return;
  141. }
  142. $pastes = $this->_getExpiredPastes($batchsize);
  143. if (count($pastes)) {
  144. foreach ($pastes as $pasteid) {
  145. $this->delete($pasteid);
  146. }
  147. }
  148. }
  149. /**
  150. * Get next free slot for comment from postdate.
  151. *
  152. * @access public
  153. * @param array $comments
  154. * @param int|string $postdate
  155. * @return int|string
  156. */
  157. protected function getOpenSlot(&$comments, $postdate)
  158. {
  159. if (array_key_exists($postdate, $comments)) {
  160. $parts = explode('.', $postdate, 2);
  161. if (!array_key_exists(1, $parts)) {
  162. $parts[1] = 0;
  163. }
  164. ++$parts[1];
  165. return $this->getOpenSlot($comments, implode('.', $parts));
  166. }
  167. return $postdate;
  168. }
  169. }