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;
|
2016-08-09 11:54:42 +02:00
|
|
|
use PrivateBin\Configuration;
|
|
|
|
use PrivateBin\Data\AbstractData;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2015-09-27 03:03:55 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* AbstractModel
|
2015-09-27 03:03:55 +02:00
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* Abstract model for PrivateBin objects.
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2016-07-21 17:09:48 +02:00
|
|
|
abstract class AbstractModel
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Instance ID.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_id = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance data.
|
|
|
|
*
|
|
|
|
* @access protected
|
2019-05-06 22:15:21 +02:00
|
|
|
* @var array
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2019-05-06 22:15:21 +02:00
|
|
|
protected $_data = array('meta' => array());
|
2015-09-27 03:03:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration.
|
|
|
|
*
|
|
|
|
* @access protected
|
2016-08-09 12:21:32 +02:00
|
|
|
* @var Configuration
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
protected $_conf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data storage.
|
|
|
|
*
|
|
|
|
* @access protected
|
2016-08-09 12:21:32 +02:00
|
|
|
* @var AbstractData
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
protected $_store;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance constructor.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-08-09 12:21:32 +02:00
|
|
|
* @param Configuration $configuration
|
|
|
|
* @param AbstractData $storage
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2016-08-09 12:21:32 +02:00
|
|
|
public function __construct(Configuration $configuration, AbstractData $storage)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2016-08-15 16:45:47 +02:00
|
|
|
$this->_conf = $configuration;
|
|
|
|
$this->_store = $storage;
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set ID.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-11-09 21:39:42 +01:00
|
|
|
* @param string $id
|
2015-09-27 03:03:55 +02:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-05-10 22:13:11 +02:00
|
|
|
public function setId($id)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!self::isValidId($id)) {
|
|
|
|
throw new Exception('Invalid paste ID.', 60);
|
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
$this->_id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set data and recalculate ID.
|
|
|
|
*
|
|
|
|
* @access public
|
2019-05-06 22:15:21 +02:00
|
|
|
* @param array $data
|
2015-09-27 03:03:55 +02:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-05-06 22:15:21 +02:00
|
|
|
public function setData(array $data)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
$data = $this->_sanitize($data);
|
|
|
|
$this->_validate($data);
|
|
|
|
$this->_data = $data;
|
2015-09-27 03:03:55 +02:00
|
|
|
|
2019-05-03 23:03:57 +02:00
|
|
|
// calculate a 64 bit checksum to avoid collisions
|
2019-05-10 23:27:45 +02:00
|
|
|
$this->setId(hash(version_compare(PHP_VERSION, '5.6', '<') ? 'fnv164' : 'fnv1a64', $data['ct']));
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get instance data.
|
|
|
|
*
|
|
|
|
* @access public
|
2019-05-06 22:15:21 +02:00
|
|
|
* @return array
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
2019-05-06 22:15:21 +02:00
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->_data;
|
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the instance's data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
abstract public function store();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the current instance.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
abstract public function delete();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if current instance exists in store.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
abstract public function exists();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-05-10 22:13:11 +02:00
|
|
|
public static function isValidId($id)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
|
|
|
return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
|
|
|
|
}
|
2019-05-06 22:15:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes data to conform with current configuration.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract protected function _sanitize(array $data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate data.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @param array $data
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function _validate(array $data)
|
|
|
|
{
|
|
|
|
}
|
2015-09-27 03:03:55 +02:00
|
|
|
}
|