abstract.php 3.9 KB

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