Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.36% |
53 / 55 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
Comment | |
96.36% |
53 / 55 |
|
85.71% |
6 / 7 |
20 | |
0.00% |
0 / 1 |
store | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
exists | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setPaste | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getPaste | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setParentId | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getParentId | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
_sanitize | |
92.31% |
24 / 26 |
|
0.00% |
0 / 1 |
7.02 |
1 | <?php declare(strict_types=1); |
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 | */ |
11 | |
12 | namespace PrivateBin\Model; |
13 | |
14 | use Exception; |
15 | use Identicon\Identicon; |
16 | use Jdenticon\Identicon as Jdenticon; |
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 | public function store() |
42 | { |
43 | // Make sure paste exists. |
44 | $pasteid = $this->getPaste()->getId(); |
45 | if (!$this->getPaste()->exists()) { |
46 | throw new Exception('Invalid data.', 67); |
47 | } |
48 | |
49 | // Make sure the discussion is opened in this paste and allowed in the configuration. |
50 | if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) { |
51 | throw new Exception('Invalid data.', 68); |
52 | } |
53 | |
54 | // Check for improbable collision. |
55 | if ($this->exists()) { |
56 | throw new Exception('You are unlucky. Try again.', 69); |
57 | } |
58 | |
59 | $this->_data['meta']['created'] = time(); |
60 | |
61 | // store comment |
62 | if ( |
63 | $this->_store->createComment( |
64 | $pasteid, |
65 | $this->getParentId(), |
66 | $this->getId(), |
67 | $this->_data |
68 | ) === false |
69 | ) { |
70 | throw new Exception('Error saving comment. Sorry.', 70); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Test if comment exists in store. |
76 | * |
77 | * @access public |
78 | * @return bool |
79 | */ |
80 | public function exists() |
81 | { |
82 | return $this->_store->existsComment( |
83 | $this->getPaste()->getId(), |
84 | $this->getParentId(), |
85 | $this->getId() |
86 | ); |
87 | } |
88 | |
89 | /** |
90 | * Set paste. |
91 | * |
92 | * @access public |
93 | * @param Paste $paste |
94 | * @throws Exception |
95 | */ |
96 | public function setPaste(Paste &$paste) |
97 | { |
98 | $this->_paste = $paste; |
99 | $this->_data['pasteid'] = $paste->getId(); |
100 | } |
101 | |
102 | /** |
103 | * Get paste. |
104 | * |
105 | * @access public |
106 | * @return Paste |
107 | */ |
108 | public function getPaste() |
109 | { |
110 | return $this->_paste; |
111 | } |
112 | |
113 | /** |
114 | * Set parent ID. |
115 | * |
116 | * @access public |
117 | * @param string $id |
118 | * @throws Exception |
119 | */ |
120 | public function setParentId($id) |
121 | { |
122 | if (!self::isValidId($id)) { |
123 | throw new Exception('Invalid document ID.', 65); |
124 | } |
125 | $this->_data['parentid'] = $id; |
126 | } |
127 | |
128 | /** |
129 | * Get parent ID. |
130 | * |
131 | * @access public |
132 | * @return string |
133 | */ |
134 | public function getParentId() |
135 | { |
136 | if (!array_key_exists('parentid', $this->_data)) { |
137 | $this->_data['parentid'] = $this->getPaste()->getId(); |
138 | } |
139 | return $this->_data['parentid']; |
140 | } |
141 | |
142 | /** |
143 | * Sanitizes data to conform with current configuration. |
144 | * |
145 | * @access protected |
146 | * @param array $data |
147 | */ |
148 | protected function _sanitize(array &$data) |
149 | { |
150 | // we generate an icon based on a SHA512 HMAC of the users IP, if configured |
151 | $icon = $this->_conf->getKey('icon'); |
152 | if ($icon != 'none') { |
153 | $pngdata = ''; |
154 | $hmac = TrafficLimiter::getHash(); |
155 | if ($icon == 'identicon') { |
156 | $identicon = new Identicon(); |
157 | $pngdata = $identicon->getImageDataUri($hmac, 16); |
158 | } elseif ($icon == 'jdenticon') { |
159 | $jdenticon = new Jdenticon(array( |
160 | 'hash' => $hmac, |
161 | 'size' => 16, |
162 | 'style' => array( |
163 | 'backgroundColor' => '#fff0', // fully transparent, for dark mode |
164 | 'padding' => 0, |
165 | ), |
166 | )); |
167 | $pngdata = $jdenticon->getImageDataUri('png'); |
168 | } elseif ($icon == 'vizhash') { |
169 | $vh = new Vizhash16x16(); |
170 | $pngdata = 'data:image/png;base64,' . base64_encode( |
171 | $vh->generate($hmac) |
172 | ); |
173 | } |
174 | if ($pngdata != '') { |
175 | if (!array_key_exists('meta', $data)) { |
176 | $data['meta'] = array(); |
177 | } |
178 | $data['meta']['icon'] = $pngdata; |
179 | } |
180 | } |
181 | } |
182 | } |