2015-09-27 03:03:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* PrivateBin
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-27 03:03:55 +02:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2022-04-05 07:29:07 +02:00
|
|
|
* @version 1.4.0
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2016-12-12 18:43:23 +01:00
|
|
|
|
2016-12-12 18:49:08 +01:00
|
|
|
namespace PrivateBin\Model;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2016-10-29 10:24:08 +02:00
|
|
|
use Exception;
|
|
|
|
use Identicon\Identicon;
|
2022-10-26 06:53:56 +02:00
|
|
|
use Jdenticon\Identicon as Jdenticon;
|
2016-08-09 11:54:42 +02:00
|
|
|
use PrivateBin\Persistence\TrafficLimiter;
|
|
|
|
use PrivateBin\Vizhash16x16;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2015-09-27 03:03:55 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* Comment
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* Model of a PrivateBin comment.
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class Comment extends AbstractModel
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Instance's parent.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-08-09 11:54:42 +02:00
|
|
|
* @var Paste
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
private $_paste;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the comment's data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
// Make sure paste exists.
|
|
|
|
$pasteid = $this->getPaste()->getId();
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!$this->getPaste()->exists()) {
|
2015-09-27 03:03:55 +02:00
|
|
|
throw new Exception('Invalid data.', 67);
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
|
|
|
|
// Make sure the discussion is opened in this paste and in configuration.
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) {
|
2015-09-27 03:03:55 +02:00
|
|
|
throw new Exception('Invalid data.', 68);
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
|
|
|
|
// Check for improbable collision.
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($this->exists()) {
|
2015-09-27 03:03:55 +02:00
|
|
|
throw new Exception('You are unlucky. Try again.', 69);
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
|
2019-05-06 22:15:21 +02:00
|
|
|
$this->_data['meta']['created'] = time();
|
2015-09-27 03:03:55 +02:00
|
|
|
|
|
|
|
// store comment
|
|
|
|
if (
|
|
|
|
$this->_store->createComment(
|
|
|
|
$pasteid,
|
|
|
|
$this->getParentId(),
|
|
|
|
$this->getId(),
|
2019-05-06 22:15:21 +02:00
|
|
|
$this->_data
|
2015-09-27 03:03:55 +02:00
|
|
|
) === false
|
2016-07-26 08:19:35 +02:00
|
|
|
) {
|
|
|
|
throw new Exception('Error saving comment. Sorry.', 70);
|
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the comment.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
throw new Exception('To delete a comment, delete its parent paste', 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if comment exists in store.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists()
|
|
|
|
{
|
|
|
|
return $this->_store->existsComment(
|
|
|
|
$this->getPaste()->getId(),
|
|
|
|
$this->getParentId(),
|
|
|
|
$this->getId()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set paste.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-08-09 11:54:42 +02:00
|
|
|
* @param Paste $paste
|
2015-09-27 03:03:55 +02:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
public function setPaste(Paste $paste)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
$this->_paste = $paste;
|
|
|
|
$this->_data['pasteid'] = $paste->getId();
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get paste.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-08-09 11:54:42 +02:00
|
|
|
* @return Paste
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
public function getPaste()
|
|
|
|
{
|
|
|
|
return $this->_paste;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set parent ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $id
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function setParentId($id)
|
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!self::isValidId($id)) {
|
|
|
|
throw new Exception('Invalid paste ID.', 65);
|
|
|
|
}
|
2019-05-06 22:15:21 +02:00
|
|
|
$this->_data['parentid'] = $id;
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parent ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getParentId()
|
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
if (!array_key_exists('parentid', $this->_data)) {
|
2019-05-11 22:18:35 +02:00
|
|
|
$this->_data['parentid'] = $this->getPaste()->getId();
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2019-05-06 22:15:21 +02:00
|
|
|
return $this->_data['parentid'];
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:39:42 +01:00
|
|
|
/**
|
2019-05-06 22:15:21 +02:00
|
|
|
* Sanitizes data to conform with current configuration.
|
2015-11-09 21:39:42 +01:00
|
|
|
*
|
2019-05-06 22:15:21 +02:00
|
|
|
* @access protected
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
2015-11-09 21:39:42 +01:00
|
|
|
*/
|
2019-05-06 22:15:21 +02:00
|
|
|
protected function _sanitize(array $data)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
// we generate an icon based on a SHA512 HMAC of the users IP, if configured
|
2016-08-10 17:41:46 +02:00
|
|
|
$icon = $this->_conf->getKey('icon');
|
|
|
|
if ($icon != 'none') {
|
|
|
|
$pngdata = '';
|
2016-08-15 16:45:47 +02:00
|
|
|
$hmac = TrafficLimiter::getHash();
|
2022-10-30 09:05:29 +01:00
|
|
|
if ($icon == 'identicon') {
|
|
|
|
$identicon = new Identicon();
|
|
|
|
$pngdata = $identicon->getImageDataUri($hmac, 16);
|
|
|
|
} elseif ($icon == 'jdenticon') {
|
2022-10-26 06:53:56 +02:00
|
|
|
$jdenticon = new Jdenticon(array(
|
|
|
|
'hash' => $hmac,
|
|
|
|
'size' => 16,
|
|
|
|
'style' => array(
|
|
|
|
'backgroundColor' => '#fff0', // fully transparent, for dark mode
|
|
|
|
'padding' => 0,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
$pngdata = $jdenticon->getImageDataUri('png');
|
2016-08-10 17:41:46 +02:00
|
|
|
} elseif ($icon == 'vizhash') {
|
2016-08-15 16:45:47 +02:00
|
|
|
$vh = new Vizhash16x16();
|
2016-08-10 17:41:46 +02:00
|
|
|
$pngdata = 'data:image/png;base64,' . base64_encode(
|
|
|
|
$vh->generate($hmac)
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($pngdata != '') {
|
2019-05-06 22:15:21 +02:00
|
|
|
if (!array_key_exists('meta', $data)) {
|
|
|
|
$data['meta'] = array();
|
|
|
|
}
|
|
|
|
$data['meta']['icon'] = $pngdata;
|
2016-07-18 10:14:38 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
2019-05-06 22:15:21 +02:00
|
|
|
return $data;
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
}
|