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-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:49:08 -05:00
|
|
|
namespace PrivateBin\Model;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-10-29 04:24:08 -04:00
|
|
|
use Exception;
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Configuration;
|
|
|
|
use PrivateBin\Data\AbstractData;
|
|
|
|
use PrivateBin\Sjcl;
|
2016-07-21 11:09:48 -04:00
|
|
|
use stdClass;
|
|
|
|
|
2015-09-26 21:03:55 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* AbstractModel
|
2015-09-26 21:03:55 -04:00
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* Abstract model for PrivateBin objects.
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
2016-07-21 11:09:48 -04:00
|
|
|
abstract class AbstractModel
|
2015-09-26 21:03:55 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Instance ID.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_id = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance data.
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @var stdClass
|
|
|
|
*/
|
|
|
|
protected $_data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration.
|
|
|
|
*
|
|
|
|
* @access protected
|
2016-08-09 06:21:32 -04:00
|
|
|
* @var Configuration
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
|
|
|
protected $_conf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data storage.
|
|
|
|
*
|
|
|
|
* @access protected
|
2016-08-09 06:21:32 -04:00
|
|
|
* @var AbstractData
|
2015-09-26 21:03:55 -04:00
|
|
|
*/
|
|
|
|
protected $_store;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance constructor.
|
|
|
|
*
|
|
|
|
* @access public
|
2016-08-09 06:21:32 -04:00
|
|
|
* @param Configuration $configuration
|
|
|
|
* @param AbstractData $storage
|
2015-09-26 21:03:55 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-09 06:21:32 -04:00
|
|
|
public function __construct(Configuration $configuration, AbstractData $storage)
|
2015-09-26 21:03:55 -04:00
|
|
|
{
|
2016-08-15 10:45:47 -04:00
|
|
|
$this->_conf = $configuration;
|
|
|
|
$this->_store = $storage;
|
|
|
|
$this->_data = new stdClass;
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->_data->meta = new stdClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set ID.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-11-09 15:39:42 -05:00
|
|
|
* @param string $id
|
2015-09-26 21:03:55 -04:00
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setId($id)
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!self::isValidId($id)) {
|
|
|
|
throw new Exception('Invalid paste ID.', 60);
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->_id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set data and recalculate ID.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $data
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setData($data)
|
|
|
|
{
|
2016-08-09 07:07:11 -04:00
|
|
|
if (!Sjcl::isValid($data)) {
|
2016-07-26 02:19:35 -04:00
|
|
|
throw new Exception('Invalid data.', 61);
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
$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 08:44:17 -04:00
|
|
|
* @return stdClass
|
2015-09-26 21:03:55 -04: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);
|
|
|
|
}
|
|
|
|
}
|