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.2.1 |
||
11 | */ |
||
12 | |||
13 | namespace PrivateBin\Data; |
||
14 | |||
15 | /** |
||
16 | * AbstractData |
||
17 | * |
||
18 | * Abstract model for PrivateBin data access, implemented as a singleton. |
||
19 | */ |
||
20 | abstract class AbstractData |
||
21 | { |
||
22 | /** |
||
23 | * singleton instance |
||
24 | * |
||
25 | * @access protected |
||
26 | * @static |
||
27 | * @var AbstractData |
||
28 | */ |
||
29 | protected static $_instance = null; |
||
30 | |||
31 | /** |
||
32 | * enforce singleton, disable constructor |
||
33 | * |
||
34 | * Instantiate using {@link getInstance()}, privatebin is a singleton object. |
||
35 | * |
||
36 | * @access protected |
||
37 | */ |
||
38 | protected function __construct() |
||
39 | { |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * enforce singleton, disable cloning |
||
44 | * |
||
45 | * Instantiate using {@link getInstance()}, privatebin is a singleton object. |
||
46 | * |
||
47 | * @access private |
||
48 | */ |
||
49 | private function __clone() |
||
50 | { |
||
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) |
||
0 ignored issues
–
show
|
|||
62 | { |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Create a paste. |
||
67 | * |
||
68 | * @access public |
||
69 | * @param string $pasteid |
||
70 | * @param array $paste |
||
71 | * @return bool |
||
72 | */ |
||
73 | abstract public function create($pasteid, array $paste); |
||
74 | |||
75 | /** |
||
76 | * Read a paste. |
||
77 | * |
||
78 | * @access public |
||
79 | * @param string $pasteid |
||
80 | * @return array|false |
||
81 | */ |
||
82 | abstract public function read($pasteid); |
||
83 | |||
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 | /** |
||
93 | * Test if a paste exists. |
||
94 | * |
||
95 | * @access public |
||
96 | * @param string $pasteid |
||
97 | * @return bool |
||
98 | */ |
||
99 | abstract public function exists($pasteid); |
||
100 | |||
101 | /** |
||
102 | * Create a comment in a paste. |
||
103 | * |
||
104 | * @access public |
||
105 | * @param string $pasteid |
||
106 | * @param string $parentid |
||
107 | * @param string $commentid |
||
108 | * @param array $comment |
||
109 | * @return bool |
||
110 | */ |
||
111 | abstract public function createComment($pasteid, $parentid, $commentid, array $comment); |
||
112 | |||
113 | /** |
||
114 | * Read all comments of paste. |
||
115 | * |
||
116 | * @access public |
||
117 | * @param string $pasteid |
||
118 | * @return array |
||
119 | */ |
||
120 | abstract public function readComments($pasteid); |
||
121 | |||
122 | /** |
||
123 | * Test if a comment exists. |
||
124 | * |
||
125 | * @access public |
||
126 | * @param string $pasteid |
||
127 | * @param string $parentid |
||
128 | * @param string $commentid |
||
129 | * @return bool |
||
130 | */ |
||
131 | abstract public function existsComment($pasteid, $parentid, $commentid); |
||
132 | |||
133 | /** |
||
134 | * Returns up to batch size number of paste ids that have expired |
||
135 | * |
||
136 | * @access protected |
||
137 | * @param int $batchsize |
||
138 | * @return array |
||
139 | */ |
||
140 | abstract protected function _getExpiredPastes($batchsize); |
||
141 | |||
142 | /** |
||
143 | * Perform a purge of old pastes, at most the given batchsize is deleted. |
||
144 | * |
||
145 | * @access public |
||
146 | * @param int $batchsize |
||
147 | */ |
||
148 | public function purge($batchsize) |
||
149 | { |
||
150 | if ($batchsize < 1) { |
||
151 | return; |
||
152 | } |
||
153 | $pastes = $this->_getExpiredPastes($batchsize); |
||
154 | if (count($pastes)) { |
||
155 | foreach ($pastes as $pasteid) { |
||
156 | $this->delete($pasteid); |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get next free slot for comment from postdate. |
||
163 | * |
||
164 | * @access protected |
||
165 | * @param array $comments |
||
166 | * @param int|string $postdate |
||
167 | * @return int|string |
||
168 | */ |
||
169 | protected function getOpenSlot(array &$comments, $postdate) |
||
170 | { |
||
171 | if (array_key_exists($postdate, $comments)) { |
||
172 | $parts = explode('.', $postdate, 2); |
||
173 | if (!array_key_exists(1, $parts)) { |
||
174 | $parts[1] = 0; |
||
175 | } |
||
176 | ++$parts[1]; |
||
177 | return $this->getOpenSlot($comments, implode('.', $parts)); |
||
178 | } |
||
179 | return $postdate; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Upgrade pre-version 1 pastes with attachment to version 1 format. |
||
184 | * |
||
185 | * @access protected |
||
186 | * @static |
||
187 | * @param array $paste |
||
188 | * @return array |
||
189 | */ |
||
190 | protected static function upgradePreV1Format(array $paste) |
||
191 | { |
||
192 | if (array_key_exists('attachment', $paste['meta'])) { |
||
193 | $paste['attachment'] = $paste['meta']['attachment']; |
||
194 | unset($paste['meta']['attachment']); |
||
195 | if (array_key_exists('attachmentname', $paste['meta'])) { |
||
196 | $paste['attachmentname'] = $paste['meta']['attachmentname']; |
||
197 | unset($paste['meta']['attachmentname']); |
||
198 | } |
||
199 | } |
||
200 | return $paste; |
||
201 | } |
||
202 | } |
||
203 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.