Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.36% covered (success)
96.36%
53 / 55
85.71% covered (success)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Comment
96.36% covered (success)
96.36%
53 / 55
85.71% covered (success)
85.71%
6 / 7
20
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
 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
92.31% covered (success)
92.31%
24 / 26
0.00% covered (danger)
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
12namespace PrivateBin\Model;
13
14use Identicon\Identicon;
15use Jdenticon\Identicon as Jdenticon;
16use PrivateBin\Exception\TranslatedException;
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 TranslatedException
40     */
41    public function store()
42    {
43        // Make sure paste exists.
44        $pasteid = $this->getPaste()->getId();
45        if (!$this->getPaste()->exists()) {
46            throw new TranslatedException(self::INVALID_DATA_ERROR, 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 TranslatedException(self::INVALID_DATA_ERROR, 68);
52        }
53
54        // Check for improbable collision.
55        if ($this->exists()) {
56            throw new TranslatedException(self::COLLISION_ERROR, 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 TranslatedException('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     */
95    public function setPaste(Paste &$paste)
96    {
97        $this->_paste           = $paste;
98        $this->_data['pasteid'] = $paste->getId();
99    }
100
101    /**
102     * Get paste.
103     *
104     * @access public
105     * @return Paste
106     */
107    public function getPaste()
108    {
109        return $this->_paste;
110    }
111
112    /**
113     * Set parent ID.
114     *
115     * @access public
116     * @param string $id
117     * @throws TranslatedException
118     */
119    public function setParentId($id)
120    {
121        if (!self::isValidId($id)) {
122            throw new TranslatedException('Invalid document ID.', 65);
123        }
124        $this->_data['parentid'] = $id;
125    }
126
127    /**
128     * Get parent ID.
129     *
130     * @access public
131     * @return string
132     */
133    public function getParentId()
134    {
135        if (!array_key_exists('parentid', $this->_data)) {
136            $this->_data['parentid'] = $this->getPaste()->getId();
137        }
138        return $this->_data['parentid'];
139    }
140
141    /**
142     * Sanitizes data to conform with current configuration.
143     *
144     * @access protected
145     * @param  array $data
146     */
147    protected function _sanitize(array &$data)
148    {
149        // we generate an icon based on a SHA512 HMAC of the users IP, if configured
150        $icon = $this->_conf->getKey('icon');
151        if ($icon !== 'none') {
152            $pngdata = '';
153            $hmac    = TrafficLimiter::getHash();
154            if ($icon === 'identicon') {
155                $identicon = new Identicon();
156                $pngdata   = $identicon->getImageDataUri($hmac, 16);
157            } elseif ($icon === 'jdenticon') {
158                $jdenticon = new Jdenticon(array(
159                    'hash'  => $hmac,
160                    'size'  => 16,
161                    'style' => array(
162                        'backgroundColor'   => '#fff0', // fully transparent, for dark mode
163                        'padding'           => 0,
164                    ),
165                ));
166                $pngdata   = $jdenticon->getImageDataUri('png');
167            } elseif ($icon === 'vizhash') {
168                $vh      = new Vizhash16x16();
169                $pngdata = 'data:image/png;base64,' . base64_encode(
170                    $vh->generate($hmac)
171                );
172            }
173            if (!empty($pngdata)) {
174                if (!array_key_exists('meta', $data)) {
175                    $data['meta'] = array();
176                }
177                $data['meta']['icon'] = $pngdata;
178            }
179        }
180    }
181}