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.7.1 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PrivateBin\Data; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AbstractData |
17
|
|
|
* |
18
|
|
|
* Abstract model for data access |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractData |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* cache for the traffic limiter |
24
|
|
|
* |
25
|
|
|
* @access protected |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $_last_cache = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a paste. |
32
|
|
|
* |
33
|
|
|
* @access public |
34
|
|
|
* @param string $pasteid |
35
|
|
|
* @param array $paste |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
abstract public function create($pasteid, array $paste); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Read a paste. |
42
|
|
|
* |
43
|
|
|
* @access public |
44
|
|
|
* @param string $pasteid |
45
|
|
|
* @return array|false |
46
|
|
|
*/ |
47
|
|
|
abstract public function read($pasteid); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Delete a paste and its discussion. |
51
|
|
|
* |
52
|
|
|
* @access public |
53
|
|
|
* @param string $pasteid |
54
|
|
|
*/ |
55
|
|
|
abstract public function delete($pasteid); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test if a paste exists. |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* @param string $pasteid |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
abstract public function exists($pasteid); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a comment in a paste. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* @param string $pasteid |
71
|
|
|
* @param string $parentid |
72
|
|
|
* @param string $commentid |
73
|
|
|
* @param array $comment |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
abstract public function createComment($pasteid, $parentid, $commentid, array $comment); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Read all comments of paste. |
80
|
|
|
* |
81
|
|
|
* @access public |
82
|
|
|
* @param string $pasteid |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
abstract public function readComments($pasteid); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test if a comment exists. |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
* @param string $pasteid |
92
|
|
|
* @param string $parentid |
93
|
|
|
* @param string $commentid |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
abstract public function existsComment($pasteid, $parentid, $commentid); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Purge outdated entries. |
100
|
|
|
* |
101
|
|
|
* @access public |
102
|
|
|
* @param string $namespace |
103
|
|
|
* @param int $time |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function purgeValues($namespace, $time) |
107
|
|
|
{ |
108
|
|
|
if ($namespace === 'traffic_limiter') { |
109
|
|
|
foreach ($this->_last_cache as $key => $last_submission) { |
110
|
|
|
if ($last_submission <= $time) { |
111
|
|
|
unset($this->_last_cache[$key]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Save a value. |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
* @param string $value |
122
|
|
|
* @param string $namespace |
123
|
|
|
* @param string $key |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
abstract public function setValue($value, $namespace, $key = ''); |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Load a value. |
130
|
|
|
* |
131
|
|
|
* @access public |
132
|
|
|
* @param string $namespace |
133
|
|
|
* @param string $key |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
abstract public function getValue($namespace, $key = ''); |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns up to batch size number of paste ids that have expired |
140
|
|
|
* |
141
|
|
|
* @access protected |
142
|
|
|
* @param int $batchsize |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
abstract protected function _getExpiredPastes($batchsize); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Perform a purge of old pastes, at most the given batchsize is deleted. |
149
|
|
|
* |
150
|
|
|
* @access public |
151
|
|
|
* @param int $batchsize |
152
|
|
|
*/ |
153
|
|
|
public function purge($batchsize) |
154
|
|
|
{ |
155
|
|
|
if ($batchsize < 1) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
$pastes = $this->_getExpiredPastes($batchsize); |
159
|
|
|
if (count($pastes)) { |
160
|
|
|
foreach ($pastes as $pasteid) { |
161
|
|
|
$this->delete($pasteid); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns all paste ids |
168
|
|
|
* |
169
|
|
|
* @access public |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
abstract public function getAllPastes(); |
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
|
|
|
/** |
196
|
|
|
* Upgrade pre-version 1 pastes with attachment to version 1 format. |
197
|
|
|
* |
198
|
|
|
* @access protected |
199
|
|
|
* @static |
200
|
|
|
* @param array $paste |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
protected static function upgradePreV1Format(array $paste) |
204
|
|
|
{ |
205
|
|
|
if (array_key_exists('attachment', $paste['meta'])) { |
206
|
|
|
$paste['attachment'] = $paste['meta']['attachment']; |
207
|
|
|
unset($paste['meta']['attachment']); |
208
|
|
|
if (array_key_exists('attachmentname', $paste['meta'])) { |
209
|
|
|
$paste['attachmentname'] = $paste['meta']['attachmentname']; |
210
|
|
|
unset($paste['meta']['attachmentname']); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
return $paste; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|