1
0

AbstractData.php 4.0 KB

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