2015-09-26 21:03:55 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 05:58:15 -04:00
|
|
|
* PrivateBin
|
2015-09-26 21:03:55 -04:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-26 21:03:55 -04:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 07:56:52 -04:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2016-08-25 03:53:31 -04:00
|
|
|
* @version 1.0
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
2016-08-10 09:03:06 -04:00
|
|
|
namespace PrivateBin\Model;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-10-29 04:24:08 -04:00
|
|
|
use Exception;
|
|
|
|
use Identicon\Identicon;
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Persistence\TrafficLimiter;
|
2016-10-29 04:24:08 -04:00
|
|
|
use PrivateBin\Sjcl;
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Vizhash16x16;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2015-09-26 21:03:55 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* Comment
|
2015-09-26 21:03:55 -04:00
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* Model of a PrivateBin comment.
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class Comment extends AbstractModel
|
2015-09-26 21:03:55 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Instance's parent.
|
|
|
|
*
|
|
|
|
* @access private
|
2016-08-09 05:54:42 -04:00
|
|
|
* @var Paste
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
|
|
|
private $_paste;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get comment data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
2016-07-06 08:58:06 -04:00
|
|
|
* @return stdClass
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
// @todo add support to read specific comment
|
|
|
|
$comments = $this->_store->readComments($this->getPaste()->getId());
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
if (
|
2015-10-18 08:37:58 -04:00
|
|
|
$comment->parentid == $this->getParentId() &&
|
|
|
|
$comment->id == $this->getId()
|
2015-09-26 21:03:55 -04:00
|
|
|
) {
|
|
|
|
$this->_data = $comment;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the comment's data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
// Make sure paste exists.
|
|
|
|
$pasteid = $this->getPaste()->getId();
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!$this->getPaste()->exists()) {
|
2015-09-26 21:03:55 -04:00
|
|
|
throw new Exception('Invalid data.', 67);
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
|
|
|
|
// Make sure the discussion is opened in this paste and in configuration.
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) {
|
2015-09-26 21:03:55 -04:00
|
|
|
throw new Exception('Invalid data.', 68);
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
|
|
|
|
// Check for improbable collision.
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($this->exists()) {
|
2015-09-26 21:03:55 -04:00
|
|
|
throw new Exception('You are unlucky. Try again.', 69);
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
|
|
|
|
$this->_data->meta->postdate = time();
|
|
|
|
|
|
|
|
// store comment
|
|
|
|
if (
|
|
|
|
$this->_store->createComment(
|
|
|
|
$pasteid,
|
|
|
|
$this->getParentId(),
|
|
|
|
$this->getId(),
|
|
|
|
json_decode(json_encode($this->_data), true)
|
|
|
|
) === false
|
2016-07-26 02:19:35 -04:00
|
|
|
) {
|
|
|
|
throw new Exception('Error saving comment. Sorry.', 70);
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the comment.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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 05:54:42 -04:00
|
|
|
* @param Paste $paste
|
2015-09-26 21:03:55 -04:00
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
public function setPaste(Paste $paste)
|
2015-09-26 21:03:55 -04:00
|
|
|
{
|
2016-08-15 10:45:47 -04:00
|
|
|
$this->_paste = $paste;
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->_data->meta->pasteid = $paste->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get paste.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-08-09 05:54:42 -04:00
|
|
|
* @return Paste
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
|
|
|
public function getPaste()
|
|
|
|
{
|
|
|
|
return $this->_paste;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set parent ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $id
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setParentId($id)
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!self::isValidId($id)) {
|
|
|
|
throw new Exception('Invalid paste ID.', 65);
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->_data->meta->parentid = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parent ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getParentId()
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!property_exists($this->_data->meta, 'parentid')) {
|
|
|
|
$this->_data->meta->parentid = '';
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
return $this->_data->meta->parentid;
|
|
|
|
}
|
|
|
|
|
2015-11-09 15:39:42 -05:00
|
|
|
/**
|
|
|
|
* Set nickname.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $nickname
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-09-26 21:03:55 -04:00
|
|
|
public function setNickname($nickname)
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
if (!Sjcl::isValid($nickname)) {
|
2016-07-26 02:19:35 -04:00
|
|
|
throw new Exception('Invalid data.', 66);
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->_data->meta->nickname = $nickname;
|
|
|
|
|
2016-08-10 11:41:46 -04:00
|
|
|
// If a nickname is provided, we generate an icon based on a SHA512 HMAC
|
|
|
|
// of the users IP. (We assume that if the user did not enter a nickname,
|
|
|
|
// the user wants to be anonymous and we will not generate an icon.)
|
|
|
|
$icon = $this->_conf->getKey('icon');
|
|
|
|
if ($icon != 'none') {
|
|
|
|
$pngdata = '';
|
2016-08-15 10:45:47 -04:00
|
|
|
$hmac = TrafficLimiter::getHash();
|
2016-08-10 11:41:46 -04:00
|
|
|
if ($icon == 'identicon') {
|
|
|
|
$identicon = new Identicon();
|
2016-08-15 10:45:47 -04:00
|
|
|
$pngdata = $identicon->getImageDataUri($hmac, 16);
|
2016-08-10 11:41:46 -04:00
|
|
|
} elseif ($icon == 'vizhash') {
|
2016-08-15 10:45:47 -04:00
|
|
|
$vh = new Vizhash16x16();
|
2016-08-10 11:41:46 -04:00
|
|
|
$pngdata = 'data:image/png;base64,' . base64_encode(
|
|
|
|
$vh->generate($hmac)
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($pngdata != '') {
|
2016-08-10 11:41:46 -04:00
|
|
|
$this->_data->meta->vizhash = $pngdata;
|
2016-07-18 04:14:38 -04:00
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
}
|
2016-08-10 11:41:46 -04:00
|
|
|
// Once the icon is generated, we do not keep the IP address hash.
|
2015-09-26 21:03:55 -04:00
|
|
|
}
|
|
|
|
}
|