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\Model; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
use Identicon\Identicon; |
17
|
|
|
use PrivateBin\Persistence\TrafficLimiter; |
18
|
|
|
use PrivateBin\Vizhash16x16; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Comment |
22
|
|
|
* |
23
|
|
|
* Model of a PrivateBin comment. |
24
|
|
|
*/ |
25
|
|
|
class Comment extends AbstractModel |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Instance's parent. |
29
|
|
|
* |
30
|
|
|
* @access private |
31
|
|
|
* @var Paste |
32
|
|
|
*/ |
33
|
|
|
private $_paste; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Store the comment's data. |
37
|
|
|
* |
38
|
|
|
* @access public |
39
|
|
|
* @throws Exception |
40
|
|
|
*/ |
41
|
13 |
|
public function store() |
42
|
|
|
{ |
43
|
|
|
// Make sure paste exists. |
44
|
13 |
|
$pasteid = $this->getPaste()->getId(); |
45
|
13 |
|
if (!$this->getPaste()->exists()) { |
46
|
1 |
|
throw new Exception('Invalid data.', 67); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Make sure the discussion is opened in this paste and in configuration. |
50
|
12 |
|
if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) { |
51
|
3 |
|
throw new Exception('Invalid data.', 68); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Check for improbable collision. |
55
|
9 |
|
if ($this->exists()) { |
56
|
3 |
|
throw new Exception('You are unlucky. Try again.', 69); |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
$this->_data['meta']['created'] = time(); |
60
|
|
|
|
61
|
|
|
// store comment |
62
|
|
|
if ( |
63
|
7 |
|
$this->_store->createComment( |
64
|
7 |
|
$pasteid, |
65
|
7 |
|
$this->getParentId(), |
66
|
7 |
|
$this->getId(), |
67
|
7 |
|
$this->_data |
68
|
7 |
|
) === false |
69
|
|
|
) { |
70
|
|
|
throw new Exception('Error saving comment. Sorry.', 70); |
71
|
|
|
} |
72
|
7 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Delete the comment. |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
* @throws Exception |
79
|
|
|
*/ |
80
|
1 |
|
public function delete() |
81
|
|
|
{ |
82
|
1 |
|
throw new Exception('To delete a comment, delete its parent paste', 64); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Test if comment exists in store. |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
9 |
|
public function exists() |
92
|
|
|
{ |
93
|
9 |
|
return $this->_store->existsComment( |
94
|
9 |
|
$this->getPaste()->getId(), |
95
|
9 |
|
$this->getParentId(), |
96
|
9 |
|
$this->getId() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set paste. |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
* @param Paste $paste |
105
|
|
|
* @throws Exception |
106
|
|
|
*/ |
107
|
18 |
|
public function setPaste(Paste $paste) |
108
|
|
|
{ |
109
|
18 |
|
$this->_paste = $paste; |
110
|
18 |
|
$this->_data['pasteid'] = $paste->getId(); |
111
|
18 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get paste. |
115
|
|
|
* |
116
|
|
|
* @access public |
117
|
|
|
* @return Paste |
118
|
|
|
*/ |
119
|
14 |
|
public function getPaste() |
120
|
|
|
{ |
121
|
14 |
|
return $this->_paste; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set parent ID. |
126
|
|
|
* |
127
|
|
|
* @access public |
128
|
|
|
* @param string $id |
129
|
|
|
* @throws Exception |
130
|
|
|
*/ |
131
|
17 |
|
public function setParentId($id) |
132
|
|
|
{ |
133
|
17 |
|
if (!self::isValidId($id)) { |
134
|
3 |
|
throw new Exception('Invalid paste ID.', 65); |
135
|
|
|
} |
136
|
14 |
|
$this->_data['parentid'] = $id; |
137
|
14 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get parent ID. |
141
|
|
|
* |
142
|
|
|
* @access public |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
10 |
|
public function getParentId() |
146
|
|
|
{ |
147
|
10 |
|
if (!array_key_exists('parentid', $this->_data)) { |
148
|
1 |
|
$this->_data['parentid'] = $this->getPaste()->getId(); |
149
|
|
|
} |
150
|
10 |
|
return $this->_data['parentid']; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Sanitizes data to conform with current configuration. |
155
|
|
|
* |
156
|
|
|
* @access protected |
157
|
|
|
* @param array $data |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
11 |
|
protected function _sanitize(array $data) |
161
|
|
|
{ |
162
|
|
|
// we generate an icon based on a SHA512 HMAC of the users IP, if configured |
163
|
11 |
|
$icon = $this->_conf->getKey('icon'); |
164
|
11 |
|
if ($icon != 'none') { |
165
|
10 |
|
$pngdata = ''; |
166
|
10 |
|
$hmac = TrafficLimiter::getHash(); |
167
|
10 |
|
if ($icon == 'identicon') { |
168
|
9 |
|
$identicon = new Identicon(); |
169
|
9 |
|
$pngdata = $identicon->getImageDataUri($hmac, 16); |
170
|
1 |
|
} elseif ($icon == 'vizhash') { |
171
|
1 |
|
$vh = new Vizhash16x16(); |
172
|
1 |
|
$pngdata = 'data:image/png;base64,' . base64_encode( |
173
|
1 |
|
$vh->generate($hmac) |
174
|
|
|
); |
175
|
|
|
} |
176
|
10 |
|
if ($pngdata != '') { |
177
|
10 |
|
if (!array_key_exists('meta', $data)) { |
178
|
10 |
|
$data['meta'] = array(); |
179
|
|
|
} |
180
|
10 |
|
$data['meta']['icon'] = $pngdata; |
181
|
|
|
} |
182
|
|
|
} |
183
|
11 |
|
return $data; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|