AbstractModel.php 3.2 KB

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