AbstractData.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. protected static $_namespaces = array(
  29. 'purge_limiter',
  30. 'salt',
  31. 'traffic_limiter',
  32. );
  33. /**
  34. * Enforce singleton, disable constructor
  35. *
  36. * Instantiate using {@link getInstance()}, privatebin is a singleton object.
  37. *
  38. * @access protected
  39. */
  40. protected function __construct()
  41. {
  42. }
  43. /**
  44. * Enforce singleton, disable cloning
  45. *
  46. * Instantiate using {@link getInstance()}, privatebin is a singleton object.
  47. *
  48. * @access private
  49. */
  50. private function __clone()
  51. {
  52. }
  53. /**
  54. * Get instance of singleton
  55. *
  56. * @access public
  57. * @static
  58. * @param array $options
  59. * @return AbstractData
  60. */
  61. public static function getInstance(array $options)
  62. {
  63. }
  64. /**
  65. * Create a paste.
  66. *
  67. * @access public
  68. * @param string $pasteid
  69. * @param array $paste
  70. * @return bool
  71. */
  72. abstract public function create($pasteid, array $paste);
  73. /**
  74. * Read a paste.
  75. *
  76. * @access public
  77. * @param string $pasteid
  78. * @return array|false
  79. */
  80. abstract public function read($pasteid);
  81. /**
  82. * Delete a paste and its discussion.
  83. *
  84. * @access public
  85. * @param string $pasteid
  86. */
  87. abstract public function delete($pasteid);
  88. /**
  89. * Test if a paste exists.
  90. *
  91. * @access public
  92. * @param string $pasteid
  93. * @return bool
  94. */
  95. abstract public function exists($pasteid);
  96. /**
  97. * Create a comment in a paste.
  98. *
  99. * @access public
  100. * @param string $pasteid
  101. * @param string $parentid
  102. * @param string $commentid
  103. * @param array $comment
  104. * @return bool
  105. */
  106. abstract public function createComment($pasteid, $parentid, $commentid, array $comment);
  107. /**
  108. * Read all comments of paste.
  109. *
  110. * @access public
  111. * @param string $pasteid
  112. * @return array
  113. */
  114. abstract public function readComments($pasteid);
  115. /**
  116. * Test if a comment exists.
  117. *
  118. * @access public
  119. * @param string $pasteid
  120. * @param string $parentid
  121. * @param string $commentid
  122. * @return bool
  123. */
  124. abstract public function existsComment($pasteid, $parentid, $commentid);
  125. /**
  126. * Save a value.
  127. *
  128. * @access public
  129. * @param string $value
  130. * @param string $namespace
  131. * @param string $key
  132. * @return bool
  133. */
  134. abstract public function setValue($value, $namespace, $key = '');
  135. /**
  136. * Load a value.
  137. *
  138. * @access public
  139. * @param string $namespace
  140. * @param string $key
  141. * @return string
  142. */
  143. abstract public function getValue($namespace, $key = '');
  144. /**
  145. * Returns up to batch size number of paste ids that have expired
  146. *
  147. * @access protected
  148. * @param int $batchsize
  149. * @return array
  150. */
  151. abstract protected function _getExpiredPastes($batchsize);
  152. /**
  153. * Perform a purge of old pastes, at most the given batchsize is deleted.
  154. *
  155. * @access public
  156. * @param int $batchsize
  157. */
  158. public function purge($batchsize)
  159. {
  160. if ($batchsize < 1) {
  161. return;
  162. }
  163. $pastes = $this->_getExpiredPastes($batchsize);
  164. if (count($pastes)) {
  165. foreach ($pastes as $pasteid) {
  166. $this->delete($pasteid);
  167. }
  168. }
  169. }
  170. /**
  171. * Get next free slot for comment from postdate.
  172. *
  173. * @access protected
  174. * @param array $comments
  175. * @param int|string $postdate
  176. * @return int|string
  177. */
  178. protected function getOpenSlot(array &$comments, $postdate)
  179. {
  180. if (array_key_exists($postdate, $comments)) {
  181. $parts = explode('.', $postdate, 2);
  182. if (!array_key_exists(1, $parts)) {
  183. $parts[1] = 0;
  184. }
  185. ++$parts[1];
  186. return $this->getOpenSlot($comments, implode('.', $parts));
  187. }
  188. return $postdate;
  189. }
  190. /**
  191. * Upgrade pre-version 1 pastes with attachment to version 1 format.
  192. *
  193. * @access protected
  194. * @static
  195. * @param array $paste
  196. * @return array
  197. */
  198. protected static function upgradePreV1Format(array $paste)
  199. {
  200. if (array_key_exists('attachment', $paste['meta'])) {
  201. $paste['attachment'] = $paste['meta']['attachment'];
  202. unset($paste['meta']['attachment']);
  203. if (array_key_exists('attachmentname', $paste['meta'])) {
  204. $paste['attachmentname'] = $paste['meta']['attachmentname'];
  205. unset($paste['meta']['attachmentname']);
  206. }
  207. }
  208. return $paste;
  209. }
  210. }