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
|
2024-02-11 15:31:11 +01:00
|
|
|
* @version 1.7.1
|
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;
|
2018-07-29 15:17:35 +02:00
|
|
|
use PrivateBin\Controller;
|
2018-07-29 16:15:52 +02:00
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2015-09-27 03:03:55 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* Paste
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* Model of a PrivateBin paste.
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class Paste extends AbstractModel
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get paste data.
|
|
|
|
*
|
|
|
|
* @access public
|
2019-05-06 22:15:21 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @return array
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
2016-08-09 12:45:26 +02:00
|
|
|
$data = $this->_store->read($this->getId());
|
|
|
|
if ($data === false) {
|
2018-07-29 15:17:35 +02:00
|
|
|
throw new Exception(Controller::GENERIC_ERROR, 64);
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2016-07-06 14:58:06 +02:00
|
|
|
|
2024-05-01 20:16:03 +02:00
|
|
|
// check if paste has expired and delete it if necessary.
|
2019-05-06 22:15:21 +02:00
|
|
|
if (array_key_exists('expire_date', $data['meta'])) {
|
|
|
|
if ($data['meta']['expire_date'] < time()) {
|
2015-09-27 03:03:55 +02:00
|
|
|
$this->delete();
|
2018-07-29 15:17:35 +02:00
|
|
|
throw new Exception(Controller::GENERIC_ERROR, 63);
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
// We kindly provide the remaining time before expiration (in seconds)
|
2019-05-06 22:15:21 +02:00
|
|
|
$data['meta']['time_to_live'] = $data['meta']['expire_date'] - time();
|
2019-05-15 07:44:03 +02:00
|
|
|
unset($data['meta']['expire_date']);
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
2024-05-04 14:38:41 +02:00
|
|
|
foreach (array('created', 'postdate') as $key) {
|
|
|
|
if (array_key_exists($key, $data['meta'])) {
|
|
|
|
unset($data['meta'][$key]);
|
|
|
|
}
|
2024-05-01 20:32:39 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
|
2017-04-11 17:23:26 +02:00
|
|
|
// check if non-expired burn after reading paste needs to be deleted
|
2019-05-06 22:15:21 +02:00
|
|
|
if (
|
|
|
|
(array_key_exists('adata', $data) && $data['adata'][3] === 1) ||
|
|
|
|
(array_key_exists('burnafterreading', $data['meta']) && $data['meta']['burnafterreading'])
|
|
|
|
) {
|
2017-04-11 17:23:26 +02:00
|
|
|
$this->delete();
|
|
|
|
}
|
|
|
|
|
2019-05-06 22:15:21 +02:00
|
|
|
// set formatter for the view in version 1 pastes.
|
|
|
|
if (array_key_exists('data', $data) && !array_key_exists('formatter', $data['meta'])) {
|
2015-09-27 03:03:55 +02:00
|
|
|
// support < 0.21 syntax highlighting
|
2019-05-06 22:15:21 +02:00
|
|
|
if (array_key_exists('syntaxcoloring', $data['meta']) && $data['meta']['syntaxcoloring'] === true) {
|
|
|
|
$data['meta']['formatter'] = 'syntaxhighlighting';
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2019-05-06 22:15:21 +02:00
|
|
|
$data['meta']['formatter'] = $this->_conf->getKey('defaultformatter');
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-06 11:37:13 +02:00
|
|
|
|
|
|
|
// support old paste format with server wide salt
|
2019-05-06 22:15:21 +02:00
|
|
|
if (!array_key_exists('salt', $data['meta'])) {
|
|
|
|
$data['meta']['salt'] = ServerSalt::get();
|
2017-04-13 10:52:48 +02:00
|
|
|
}
|
2019-05-06 22:15:21 +02:00
|
|
|
$data['comments'] = array_values($this->getComments());
|
|
|
|
$data['comment_count'] = count($data['comments']);
|
|
|
|
$data['comment_offset'] = 0;
|
2019-06-01 23:49:40 +02:00
|
|
|
$data['@context'] = '?jsonld=paste';
|
2019-05-06 22:15:21 +02:00
|
|
|
$this->_data = $data;
|
2017-04-13 10:52:48 +02:00
|
|
|
|
2015-09-27 03:03:55 +02:00
|
|
|
return $this->_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the paste's data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
// 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.', 75);
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
|
2024-05-01 21:07:24 +02:00
|
|
|
$this->_data['meta']['salt'] = ServerSalt::generate();
|
2015-09-27 03:03:55 +02:00
|
|
|
|
|
|
|
// store paste
|
|
|
|
if (
|
|
|
|
$this->_store->create(
|
|
|
|
$this->getId(),
|
2019-05-13 22:31:52 +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 paste. Sorry.', 76);
|
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the paste.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
$this->_store->delete($this->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if paste exists in store.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists()
|
|
|
|
{
|
|
|
|
return $this->_store->exists($this->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a comment, optionally a specific instance.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $parentId
|
|
|
|
* @param string $commentId
|
|
|
|
* @throws Exception
|
2016-08-09 11:54:42 +02:00
|
|
|
* @return Comment
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2019-05-10 22:13:11 +02:00
|
|
|
public function getComment($parentId, $commentId = '')
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!$this->exists()) {
|
2015-09-27 03:03:55 +02:00
|
|
|
throw new Exception('Invalid data.', 62);
|
|
|
|
}
|
2016-08-09 11:54:42 +02:00
|
|
|
$comment = new Comment($this->_conf, $this->_store);
|
2015-09-27 03:03:55 +02:00
|
|
|
$comment->setPaste($this);
|
|
|
|
$comment->setParentId($parentId);
|
2019-05-06 22:15:21 +02:00
|
|
|
if ($commentId !== '') {
|
2016-07-26 08:19:35 +02:00
|
|
|
$comment->setId($commentId);
|
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all comments, if any.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getComments()
|
|
|
|
{
|
2024-05-04 14:38:41 +02:00
|
|
|
if ($this->_conf->getKey('discussiondatedisplay')) {
|
|
|
|
return $this->_store->readComments($this->getId());
|
|
|
|
}
|
2024-05-05 11:16:17 +02:00
|
|
|
return array_map(function ($comment) {
|
2024-05-04 14:38:41 +02:00
|
|
|
foreach (array('created', 'postdate') as $key) {
|
|
|
|
if (array_key_exists($key, $comment['meta'])) {
|
|
|
|
unset($comment['meta'][$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $comment;
|
|
|
|
}, $this->_store->readComments($this->getId()));
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the "delete" token.
|
|
|
|
*
|
|
|
|
* The token is the hmac of the pastes ID signed with the server salt.
|
|
|
|
* The paste can be deleted by calling:
|
2017-04-11 16:34:13 +02:00
|
|
|
* https://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDeleteToken()
|
|
|
|
{
|
2019-05-08 22:11:21 +02:00
|
|
|
if (!array_key_exists('salt', $this->_data['meta'])) {
|
2016-07-26 08:19:35 +02:00
|
|
|
$this->get();
|
|
|
|
}
|
2016-07-06 11:37:13 +02:00
|
|
|
return hash_hmac(
|
|
|
|
$this->_conf->getKey('zerobincompatibility') ? 'sha1' : 'sha256',
|
|
|
|
$this->getId(),
|
2019-05-08 22:11:21 +02:00
|
|
|
$this->_data['meta']['salt']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:03:55 +02:00
|
|
|
/**
|
2019-05-06 22:15:21 +02:00
|
|
|
* Check if paste has discussions enabled.
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
2019-05-06 22:15:21 +02:00
|
|
|
* @return bool
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2019-05-06 22:15:21 +02:00
|
|
|
public function isOpendiscussion()
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
if (!array_key_exists('adata', $this->_data) && !array_key_exists('data', $this->_data)) {
|
|
|
|
$this->get();
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2019-05-10 21:45:34 +02:00
|
|
|
return
|
2019-05-06 22:15:21 +02:00
|
|
|
(array_key_exists('adata', $this->_data) && $this->_data['adata'][2] === 1) ||
|
2019-05-10 21:45:34 +02:00
|
|
|
(array_key_exists('opendiscussion', $this->_data['meta']) && $this->_data['meta']['opendiscussion']);
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-06 22:15:21 +02:00
|
|
|
* Sanitizes data to conform with current configuration.
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
2019-05-06 22:15:21 +02:00
|
|
|
* @access protected
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
2015-09-27 03:03:55 +02: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
|
|
|
$expiration = $data['meta']['expire'];
|
|
|
|
unset($data['meta']['expire']);
|
2015-09-27 03:03:55 +02:00
|
|
|
$expire_options = $this->_conf->getSection('expire_options');
|
2016-07-26 08:19:35 +02:00
|
|
|
if (array_key_exists($expiration, $expire_options)) {
|
2015-09-27 03:03:55 +02:00
|
|
|
$expire = $expire_options[$expiration];
|
2016-07-26 08:19:35 +02:00
|
|
|
} else {
|
2015-09-27 03:03:55 +02:00
|
|
|
// using getKey() to ensure a default value is present
|
|
|
|
$expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
|
|
|
|
}
|
2016-07-26 08:19:35 +02:00
|
|
|
if ($expire > 0) {
|
2019-05-06 22:15:21 +02:00
|
|
|
$data['meta']['expire_date'] = time() + $expire;
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2019-05-06 22:15:21 +02:00
|
|
|
return $data;
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-06 22:15:21 +02:00
|
|
|
* Validate data.
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
2019-05-06 22:15:21 +02:00
|
|
|
* @access protected
|
|
|
|
* @param array $data
|
2015-09-27 03:03:55 +02:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-05-06 22:15:21 +02:00
|
|
|
protected function _validate(array $data)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
// reject invalid or disabled formatters
|
|
|
|
if (!array_key_exists($data['adata'][1], $this->_conf->getSection('formatter_options'))) {
|
|
|
|
throw new Exception('Invalid data.', 75);
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 22:15:21 +02:00
|
|
|
// discussion requested, but disabled in config or burn after reading requested as well, or invalid integer
|
2015-09-27 03:03:55 +02:00
|
|
|
if (
|
2019-05-06 22:15:21 +02:00
|
|
|
($data['adata'][2] === 1 && ( // open discussion flag
|
|
|
|
!$this->_conf->getKey('discussion') ||
|
|
|
|
$data['adata'][3] === 1 // burn after reading flag
|
|
|
|
)) ||
|
|
|
|
($data['adata'][2] !== 0 && $data['adata'][2] !== 1)
|
2016-07-26 08:19:35 +02:00
|
|
|
) {
|
2019-05-06 22:15:21 +02:00
|
|
|
throw new Exception('Invalid data.', 74);
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 22:15:21 +02:00
|
|
|
// reject invalid burn after reading
|
|
|
|
if ($data['adata'][3] !== 0 && $data['adata'][3] !== 1) {
|
|
|
|
throw new Exception('Invalid data.', 73);
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
}
|