AbstractData.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.3.5
  11. */
  12. namespace PrivateBin\Data;
  13. /**
  14. * AbstractData
  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 AbstractData
  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 AbstractData
  55. */
  56. public static function getInstance(array $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, array $paste);
  68. /**
  69. * Read a paste.
  70. *
  71. * @access public
  72. * @param string $pasteid
  73. * @return array|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. */
  82. abstract public function delete($pasteid);
  83. /**
  84. * Test if a paste exists.
  85. *
  86. * @access public
  87. * @param string $pasteid
  88. * @return bool
  89. */
  90. abstract public function exists($pasteid);
  91. /**
  92. * Create a comment in a paste.
  93. *
  94. * @access public
  95. * @param string $pasteid
  96. * @param string $parentid
  97. * @param string $commentid
  98. * @param array $comment
  99. * @return bool
  100. */
  101. abstract public function createComment($pasteid, $parentid, $commentid, array $comment);
  102. /**
  103. * Read all comments of paste.
  104. *
  105. * @access public
  106. * @param string $pasteid
  107. * @return array
  108. */
  109. abstract public function readComments($pasteid);
  110. /**
  111. * Test if a comment exists.
  112. *
  113. * @access public
  114. * @param string $pasteid
  115. * @param string $parentid
  116. * @param string $commentid
  117. * @return bool
  118. */
  119. abstract public function existsComment($pasteid, $parentid, $commentid);
  120. /**
  121. * Returns up to batch size number of paste ids that have expired
  122. *
  123. * @access protected
  124. * @param int $batchsize
  125. * @return array
  126. */
  127. abstract protected function _getExpiredPastes($batchsize);
  128. /**
  129. * Perform a purge of old pastes, at most the given batchsize is deleted.
  130. *
  131. * @access public
  132. * @param int $batchsize
  133. */
  134. public function purge($batchsize)
  135. {
  136. if ($batchsize < 1) {
  137. return;
  138. }
  139. $pastes = $this->_getExpiredPastes($batchsize);
  140. if (count($pastes)) {
  141. foreach ($pastes as $pasteid) {
  142. $this->delete($pasteid);
  143. }
  144. }
  145. }
  146. /**
  147. * Get next free slot for comment from postdate.
  148. *
  149. * @access protected
  150. * @param array $comments
  151. * @param int|string $postdate
  152. * @return int|string
  153. */
  154. protected function getOpenSlot(array &$comments, $postdate)
  155. {
  156. if (array_key_exists($postdate, $comments)) {
  157. $parts = explode('.', $postdate, 2);
  158. if (!array_key_exists(1, $parts)) {
  159. $parts[1] = 0;
  160. }
  161. ++$parts[1];
  162. return $this->getOpenSlot($comments, implode('.', $parts));
  163. }
  164. return $postdate;
  165. }
  166. /**
  167. * Upgrade pre-version 1 pastes with attachment to version 1 format.
  168. *
  169. * @access protected
  170. * @static
  171. * @param array $paste
  172. * @return array
  173. */
  174. protected static function upgradePreV1Format(array $paste)
  175. {
  176. if (array_key_exists('attachment', $paste['meta'])) {
  177. $paste['attachment'] = $paste['meta']['attachment'];
  178. unset($paste['meta']['attachment']);
  179. if (array_key_exists('attachmentname', $paste['meta'])) {
  180. $paste['attachmentname'] = $paste['meta']['attachmentname'];
  181. unset($paste['meta']['attachmentname']);
  182. }
  183. }
  184. return $paste;
  185. }
  186. }