Passed
Push — master ( 18151e...9ce410 )
by El
02:57
created

lib/Model/Comment.php (2 issues)

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\Sjcl;
19
use PrivateBin\Vizhash16x16;
20
21
/**
22
 * Comment
23
 *
24
 * Model of a PrivateBin comment.
25
 */
26
class Comment extends AbstractModel
27
{
28
    /**
29
     * Instance's parent.
30
     *
31
     * @access private
32
     * @var Paste
33
     */
34
    private $_paste;
35
36
    /**
37
     * Get comment data.
38
     *
39
     * @access public
40
     * @throws Exception
41
     * @return stdClass
0 ignored issues
show
The type PrivateBin\Model\stdClass was not found. Did you mean stdClass? If so, make sure to prefix the type with \.
Loading history...
42
     */
43 5
    public function get()
44
    {
45
        // @todo add support to read specific comment
46 5
        $comments = $this->_store->readComments($this->getPaste()->getId());
47 5
        foreach ($comments as $comment) {
48
            if (
49 5
                $comment->parentid == $this->getParentId() &&
50 5
                $comment->id == $this->getId()
51
            ) {
52 5
                $this->_data = $comment;
53 5
                break;
54
            }
55
        }
56 5
        return $this->_data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_data returns the type stdClass which is incompatible with the documented return type PrivateBin\Model\stdClass.
Loading history...
57
    }
58
59
    /**
60
     * Store the comment's data.
61
     *
62
     * @access public
63
     * @throws Exception
64
     */
65 14
    public function store()
66
    {
67
        // Make sure paste exists.
68 14
        $pasteid = $this->getPaste()->getId();
69 14
        if (!$this->getPaste()->exists()) {
70 1
            throw new Exception('Invalid data.', 67);
71
        }
72
73
        // Make sure the discussion is opened in this paste and in configuration.
74 13
        if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) {
75 3
            throw new Exception('Invalid data.', 68);
76
        }
77
78
        // Check for improbable collision.
79 10
        if ($this->exists()) {
80 3
            throw new Exception('You are unlucky. Try again.', 69);
81
        }
82
83 8
        $this->_data->meta->postdate = time();
84
85
        // store comment
86
        if (
87 8
            $this->_store->createComment(
88 8
                $pasteid,
89 8
                $this->getParentId(),
90 8
                $this->getId(),
91 8
                json_decode(json_encode($this->_data), true)
92 8
            ) === false
93
        ) {
94
            throw new Exception('Error saving comment. Sorry.', 70);
95
        }
96 8
    }
97
98
    /**
99
     * Delete the comment.
100
     *
101
     * @access public
102
     * @throws Exception
103
     */
104 1
    public function delete()
105
    {
106 1
        throw new Exception('To delete a comment, delete its parent paste', 64);
107
    }
108
109
    /**
110
     * Test if comment exists in store.
111
     *
112
     * @access public
113
     * @return bool
114
     */
115 10
    public function exists()
116
    {
117 10
        return $this->_store->existsComment(
118 10
            $this->getPaste()->getId(),
119 10
            $this->getParentId(),
120 10
            $this->getId()
121
        );
122
    }
123
124
    /**
125
     * Set paste.
126
     *
127
     * @access public
128
     * @param Paste $paste
129
     * @throws Exception
130
     */
131 19
    public function setPaste(Paste $paste)
132
    {
133 19
        $this->_paste               = $paste;
134 19
        $this->_data->meta->pasteid = $paste->getId();
135 19
    }
136
137
    /**
138
     * Get paste.
139
     *
140
     * @access public
141
     * @return Paste
142
     */
143 14
    public function getPaste()
144
    {
145 14
        return $this->_paste;
146
    }
147
148
    /**
149
     * Set parent ID.
150
     *
151
     * @access public
152
     * @param string $id
153
     * @throws Exception
154
     */
155 19
    public function setParentId($id)
156
    {
157 19
        if (!self::isValidId($id)) {
158 2
            throw new Exception('Invalid paste ID.', 65);
159
        }
160 17
        $this->_data->meta->parentid = $id;
161 17
    }
162
163
    /**
164
     * Get parent ID.
165
     *
166
     * @access public
167
     * @return string
168
     */
169 10
    public function getParentId()
170
    {
171 10
        if (!property_exists($this->_data->meta, 'parentid')) {
172
            $this->_data->meta->parentid = '';
173
        }
174 10
        return $this->_data->meta->parentid;
175
    }
176
177
    /**
178
     * Set nickname.
179
     *
180
     * @access public
181
     * @param string $nickname
182
     * @throws Exception
183
     */
184 14
    public function setNickname($nickname)
185
    {
186 14
        if (!Sjcl::isValid($nickname)) {
187 2
            throw new Exception('Invalid data.', 66);
188
        }
189 12
        $this->_data->meta->nickname = $nickname;
190
191
        // If a nickname is provided, we generate an icon based on a SHA512 HMAC
192
        // of the users IP. (We assume that if the user did not enter a nickname,
193
        // the user wants to be anonymous and we will not generate an icon.)
194 12
        $icon = $this->_conf->getKey('icon');
195 12
        if ($icon != 'none') {
196 11
            $pngdata = '';
197 11
            $hmac    = TrafficLimiter::getHash();
198 11
            if ($icon == 'identicon') {
199 10
                $identicon = new Identicon();
200 10
                $pngdata   = $identicon->getImageDataUri($hmac, 16);
201 1
            } elseif ($icon == 'vizhash') {
202 1
                $vh      = new Vizhash16x16();
203 1
                $pngdata = 'data:image/png;base64,' . base64_encode(
204 1
                    $vh->generate($hmac)
205
                );
206
            }
207 11
            if ($pngdata != '') {
208 11
                $this->_data->meta->vizhash = $pngdata;
209
            }
210
        }
211
        // Once the icon is generated, we do not keep the IP address hash.
212 12
    }
213
}
214