AbstractData.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * Purge outdated entries.
  122. *
  123. * @access public
  124. * @param string $namespace
  125. * @param int $time
  126. * @return void
  127. */
  128. abstract public function purgeValues($namespace, $time);
  129. /**
  130. * Save a value.
  131. *
  132. * @access public
  133. * @param string $value
  134. * @param string $namespace
  135. * @param string $key
  136. * @return bool
  137. */
  138. abstract public function setValue($value, $namespace, $key = '');
  139. /**
  140. * Load a value.
  141. *
  142. * @access public
  143. * @param string $namespace
  144. * @param string $key
  145. * @return string
  146. */
  147. abstract public function getValue($namespace, $key = '');
  148. /**
  149. * Returns up to batch size number of paste ids that have expired
  150. *
  151. * @access protected
  152. * @param int $batchsize
  153. * @return array
  154. */
  155. abstract protected function _getExpiredPastes($batchsize);
  156. /**
  157. * Perform a purge of old pastes, at most the given batchsize is deleted.
  158. *
  159. * @access public
  160. * @param int $batchsize
  161. */
  162. public function purge($batchsize)
  163. {
  164. if ($batchsize < 1) {
  165. return;
  166. }
  167. $pastes = $this->_getExpiredPastes($batchsize);
  168. if (count($pastes)) {
  169. foreach ($pastes as $pasteid) {
  170. $this->delete($pasteid);
  171. }
  172. }
  173. }
  174. /**
  175. * Get next free slot for comment from postdate.
  176. *
  177. * @access protected
  178. * @param array $comments
  179. * @param int|string $postdate
  180. * @return int|string
  181. */
  182. protected function getOpenSlot(array &$comments, $postdate)
  183. {
  184. if (array_key_exists($postdate, $comments)) {
  185. $parts = explode('.', $postdate, 2);
  186. if (!array_key_exists(1, $parts)) {
  187. $parts[1] = 0;
  188. }
  189. ++$parts[1];
  190. return $this->getOpenSlot($comments, implode('.', $parts));
  191. }
  192. return $postdate;
  193. }
  194. /**
  195. * Upgrade pre-version 1 pastes with attachment to version 1 format.
  196. *
  197. * @access protected
  198. * @static
  199. * @param array $paste
  200. * @return array
  201. */
  202. protected static function upgradePreV1Format(array $paste)
  203. {
  204. if (array_key_exists('attachment', $paste['meta'])) {
  205. $paste['attachment'] = $paste['meta']['attachment'];
  206. unset($paste['meta']['attachment']);
  207. if (array_key_exists('attachmentname', $paste['meta'])) {
  208. $paste['attachmentname'] = $paste['meta']['attachmentname'];
  209. unset($paste['meta']['attachmentname']);
  210. }
  211. }
  212. return $paste;
  213. }
  214. }