Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.93% covered (success)
83.93%
47 / 56
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Comment
83.93% covered (success)
83.93%
47 / 56
87.50% covered (success)
87.50%
7 / 8
22.83
0.00% covered (danger)
0.00%
0 / 1
 store
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exists
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 setPaste
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPaste
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParentId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getParentId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 _sanitize
65.38% covered (warning)
65.38%
17 / 26
0.00% covered (danger)
0.00%
0 / 1
9.03
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
12namespace PrivateBin\Model;
13
14use Exception;
15use Identicon\Identicon;
16use Jdenticon\Identicon as Jdenticon;
17use PrivateBin\Persistence\TrafficLimiter;
18use PrivateBin\Vizhash16x16;
19
20/**
21 * Comment
22 *
23 * Model of a PrivateBin comment.
24 */
25class 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 in 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     * Delete the comment.
76     *
77     * @access public
78     * @throws Exception
79     */
80    public function delete()
81    {
82        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    public function exists()
92    {
93        return $this->_store->existsComment(
94            $this->getPaste()->getId(),
95            $this->getParentId(),
96            $this->getId()
97        );
98    }
99
100    /**
101     * Set paste.
102     *
103     * @access public
104     * @param Paste $paste
105     * @throws Exception
106     */
107    public function setPaste(Paste &$paste)
108    {
109        $this->_paste           = $paste;
110        $this->_data['pasteid'] = $paste->getId();
111    }
112
113    /**
114     * Get paste.
115     *
116     * @access public
117     * @return Paste
118     */
119    public function getPaste()
120    {
121        return $this->_paste;
122    }
123
124    /**
125     * Set parent ID.
126     *
127     * @access public
128     * @param string $id
129     * @throws Exception
130     */
131    public function setParentId($id)
132    {
133        if (!self::isValidId($id)) {
134            throw new Exception('Invalid paste ID.', 65);
135        }
136        $this->_data['parentid'] = $id;
137    }
138
139    /**
140     * Get parent ID.
141     *
142     * @access public
143     * @return string
144     */
145    public function getParentId()
146    {
147        if (!array_key_exists('parentid', $this->_data)) {
148            $this->_data['parentid'] = $this->getPaste()->getId();
149        }
150        return $this->_data['parentid'];
151    }
152
153    /**
154     * Sanitizes data to conform with current configuration.
155     *
156     * @access protected
157     * @param  array $data
158     */
159    protected function _sanitize(array &$data)
160    {
161        // we generate an icon based on a SHA512 HMAC of the users IP, if configured
162        $icon = $this->_conf->getKey('icon');
163        if ($icon != 'none') {
164            $pngdata = '';
165            $hmac    = TrafficLimiter::getHash();
166            if ($icon == 'identicon') {
167                $identicon = new Identicon();
168                $pngdata   = $identicon->getImageDataUri($hmac, 16);
169            } elseif ($icon == 'jdenticon') {
170                $jdenticon = new Jdenticon(array(
171                    'hash'  => $hmac,
172                    'size'  => 16,
173                    'style' => array(
174                        'backgroundColor'   => '#fff0', // fully transparent, for dark mode
175                        'padding'           => 0,
176                    ),
177                ));
178                $pngdata   = $jdenticon->getImageDataUri('png');
179            } elseif ($icon == 'vizhash') {
180                $vh      = new Vizhash16x16();
181                $pngdata = 'data:image/png;base64,' . base64_encode(
182                    $vh->generate($hmac)
183                );
184            }
185            if ($pngdata != '') {
186                if (!array_key_exists('meta', $data)) {
187                    $data['meta'] = array();
188                }
189                $data['meta']['icon'] = $pngdata;
190            }
191        }
192    }
193}