1
0

AbstractModel.php 3.4 KB

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