AbstractData.php 5.7 KB

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