AbstractModel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.2.1
  11. */
  12. namespace PrivateBin\Model;
  13. use Exception;
  14. use PrivateBin\Configuration;
  15. use PrivateBin\Data\AbstractData;
  16. use PrivateBin\FormatV2;
  17. /**
  18. * AbstractModel
  19. *
  20. * Abstract model for PrivateBin objects.
  21. */
  22. abstract class AbstractModel
  23. {
  24. /**
  25. * Instance ID.
  26. *
  27. * @access protected
  28. * @var string
  29. */
  30. protected $_id = '';
  31. /**
  32. * Instance data.
  33. *
  34. * @access protected
  35. * @var array
  36. */
  37. protected $_data = array('meta' => array());
  38. /**
  39. * Configuration.
  40. *
  41. * @access protected
  42. * @var Configuration
  43. */
  44. protected $_conf;
  45. /**
  46. * Data storage.
  47. *
  48. * @access protected
  49. * @var AbstractData
  50. */
  51. protected $_store;
  52. /**
  53. * Instance constructor.
  54. *
  55. * @access public
  56. * @param Configuration $configuration
  57. * @param AbstractData $storage
  58. */
  59. public function __construct(Configuration $configuration, AbstractData $storage)
  60. {
  61. $this->_conf = $configuration;
  62. $this->_store = $storage;
  63. }
  64. /**
  65. * Get ID.
  66. *
  67. * @access public
  68. * @return string
  69. */
  70. public function getId()
  71. {
  72. return $this->_id;
  73. }
  74. /**
  75. * Set ID.
  76. *
  77. * @access public
  78. * @param string $id
  79. * @throws Exception
  80. */
  81. public function setId(string $id)
  82. {
  83. if (!self::isValidId($id)) {
  84. throw new Exception('Invalid paste ID.', 60);
  85. }
  86. $this->_id = $id;
  87. }
  88. /**
  89. * Set data and recalculate ID.
  90. *
  91. * @access public
  92. * @param array $data
  93. * @throws Exception
  94. */
  95. public function setData(array $data)
  96. {
  97. if (!FormatV2::isValid($data, $this instanceof Comment)) {
  98. throw new Exception('Invalid data.', 61);
  99. }
  100. $data = $this->_sanitize($data);
  101. $this->_validate($data);
  102. $this->_data = $data;
  103. // calculate a 64 bit checksum to avoid collisions
  104. $this->setId(hash('fnv1a64', $data['ct']));
  105. }
  106. /**
  107. * Get instance data.
  108. *
  109. * @access public
  110. * @return array
  111. */
  112. public function get()
  113. {
  114. return $this->_data;
  115. }
  116. /**
  117. * Store the instance's data.
  118. *
  119. * @access public
  120. * @throws Exception
  121. */
  122. abstract public function store();
  123. /**
  124. * Delete the current instance.
  125. *
  126. * @access public
  127. * @throws Exception
  128. */
  129. abstract public function delete();
  130. /**
  131. * Test if current instance exists in store.
  132. *
  133. * @access public
  134. * @return bool
  135. */
  136. abstract public function exists();
  137. /**
  138. * Validate ID.
  139. *
  140. * @access public
  141. * @static
  142. * @param string $id
  143. * @return bool
  144. */
  145. public static function isValidId(string $id)
  146. {
  147. return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
  148. }
  149. /**
  150. * Sanitizes data to conform with current configuration.
  151. *
  152. * @access protected
  153. * @param array $data
  154. * @return array
  155. */
  156. abstract protected function _sanitize(array $data);
  157. /**
  158. * Validate data.
  159. *
  160. * @access protected
  161. * @param array $data
  162. * @throws Exception
  163. */
  164. protected function _validate(array $data)
  165. {
  166. return;
  167. }
  168. }