1
0

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 0.22
  11. */
  12. namespace PrivateBin\Data;
  13. /**
  14. * privatebin_abstract
  15. *
  16. * Abstract model for PrivateBin data access, implemented as a singleton.
  17. */
  18. abstract class AbstractData
  19. {
  20. /**
  21. * singleton instance
  22. *
  23. * @access protected
  24. * @static
  25. * @var privatebin_abstract
  26. */
  27. protected static $_instance = null;
  28. /**
  29. * enforce singleton, disable constructor
  30. *
  31. * Instantiate using {@link getInstance()}, privatebin is a singleton object.
  32. *
  33. * @access protected
  34. */
  35. protected function __construct()
  36. {
  37. }
  38. /**
  39. * enforce singleton, disable cloning
  40. *
  41. * Instantiate using {@link getInstance()}, privatebin is a singleton object.
  42. *
  43. * @access private
  44. */
  45. private function __clone()
  46. {
  47. }
  48. /**
  49. * get instance of singleton
  50. *
  51. * @access public
  52. * @static
  53. * @param array $options
  54. * @return privatebin_abstract
  55. */
  56. public static function getInstance($options)
  57. {
  58. }
  59. /**
  60. * Create a paste.
  61. *
  62. * @access public
  63. * @param string $pasteid
  64. * @param array $paste
  65. * @return bool
  66. */
  67. abstract public function create($pasteid, $paste);
  68. /**
  69. * Read a paste.
  70. *
  71. * @access public
  72. * @param string $pasteid
  73. * @return stdClass|false
  74. */
  75. abstract public function read($pasteid);
  76. /**
  77. * Delete a paste and its discussion.
  78. *
  79. * @access public
  80. * @param string $pasteid
  81. * @return void
  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 void
  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. * @return void
  135. */
  136. public function purge($batchsize)
  137. {
  138. if ($batchsize < 1) {
  139. return;
  140. }
  141. $pastes = $this->_getExpiredPastes($batchsize);
  142. if (count($pastes)) {
  143. foreach ($pastes as $pasteid) {
  144. $this->delete($pasteid);
  145. }
  146. }
  147. }
  148. /**
  149. * Get next free slot for comment from postdate.
  150. *
  151. * @access public
  152. * @param array $comments
  153. * @param int|string $postdate
  154. * @return int|string
  155. */
  156. protected function getOpenSlot(&$comments, $postdate)
  157. {
  158. if (array_key_exists($postdate, $comments)) {
  159. $parts = explode('.', $postdate, 2);
  160. if (!array_key_exists(1, $parts)) {
  161. $parts[1] = 0;
  162. }
  163. ++$parts[1];
  164. return $this->getOpenSlot($comments, implode('.', $parts));
  165. }
  166. return $postdate;
  167. }
  168. }