mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-12-27 00:09:34 -05:00
164 lines
3.0 KiB
PHP
164 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* PrivateBin
|
|
*
|
|
* a zero-knowledge paste bin
|
|
*
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
|
* @version 1.2.1
|
|
*/
|
|
|
|
namespace PrivateBin\Model;
|
|
|
|
use Exception;
|
|
use PrivateBin\Configuration;
|
|
use PrivateBin\Data\AbstractData;
|
|
use PrivateBin\FormatV2;
|
|
use stdClass;
|
|
|
|
/**
|
|
* AbstractModel
|
|
*
|
|
* Abstract model for PrivateBin objects.
|
|
*/
|
|
abstract class AbstractModel
|
|
{
|
|
/**
|
|
* 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
|
|
* @var AbstractData
|
|
*/
|
|
protected $_store;
|
|
|
|
/**
|
|
* Instance constructor.
|
|
*
|
|
* @access public
|
|
* @param Configuration $configuration
|
|
* @param AbstractData $storage
|
|
*/
|
|
public function __construct(Configuration $configuration, AbstractData $storage)
|
|
{
|
|
$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
|
|
* @param string $id
|
|
* @throws Exception
|
|
*/
|
|
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
|
|
*/
|
|
public function setData($data)
|
|
{
|
|
if (!FormatV2::isValid($data)) {
|
|
throw new Exception('Invalid data.', 61);
|
|
}
|
|
$this->_data->data = $data;
|
|
|
|
// calculate a 64 bit checksum to avoid collisions
|
|
$this->setId(hash('fnv1a64', $data['ct']));
|
|
}
|
|
|
|
/**
|
|
* Get instance data.
|
|
*
|
|
* @access public
|
|
* @return stdClass
|
|
*/
|
|
abstract public function get();
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public static function isValidId($id)
|
|
{
|
|
return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
|
|
}
|
|
}
|