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