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
|
2015-11-09 21:39:42 +01:00
|
|
|
* @version 0.22
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-21 17:09:48 +02:00
|
|
|
namespace PrivateBin\Model;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use PrivateBin\configuration;
|
|
|
|
use PrivateBin\data\AbstractData;
|
|
|
|
use PrivateBin\sjcl;
|
|
|
|
use stdClass;
|
|
|
|
|
2015-09-27 03:03:55 +02:00
|
|
|
/**
|
|
|
|
* model_abstract
|
|
|
|
*
|
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
|
|
|
|
* @var stdClass
|
|
|
|
*/
|
|
|
|
protected $_data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var configuration
|
|
|
|
*/
|
|
|
|
protected $_conf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data storage.
|
|
|
|
*
|
|
|
|
* @access protected
|
2016-07-11 11:58:15 +02:00
|
|
|
* @var privatebin_abstract
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
protected $_store;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance constructor.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param configuration $configuration
|
2016-07-11 11:58:15 +02:00
|
|
|
* @param privatebin_abstract $storage
|
2015-09-27 03:03:55 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-21 17:09:48 +02:00
|
|
|
public function __construct(configuration $configuration, AbstractData $storage)
|
2015-09-27 03:03:55 +02:00
|
|
|
{
|
|
|
|
$this->_conf = $configuration;
|
|
|
|
$this->_store = $storage;
|
|
|
|
$this->_data = new stdClass;
|
|
|
|
$this->_data->meta = new stdClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setId($id)
|
|
|
|
{
|
|
|
|
if (!self::isValidId($id)) throw new Exception('Invalid paste ID.', 60);
|
|
|
|
$this->_id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set data and recalculate ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $data
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setData($data)
|
|
|
|
{
|
|
|
|
if (!sjcl::isValid($data)) throw new Exception('Invalid data.', 61);
|
|
|
|
$this->_data->data = $data;
|
|
|
|
|
|
|
|
// We just want a small hash to avoid collisions:
|
|
|
|
// Half-MD5 (64 bits) will do the trick
|
|
|
|
$this->setId(substr(hash('md5', $data), 0, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get instance data.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-07-19 14:44:17 +02:00
|
|
|
* @return stdClass
|
2015-09-27 03:03:55 +02:00
|
|
|
*/
|
|
|
|
abstract public function get();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the instance's data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
abstract public function store();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the current instance.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static function isValidId($id)
|
|
|
|
{
|
|
|
|
return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
|
|
|
|
}
|
|
|
|
}
|