1
0

AbstractModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.3
  11. */
  12. namespace PrivateBin\Model;
  13. use Exception;
  14. use PrivateBin\Configuration;
  15. use PrivateBin\Data\AbstractData;
  16. use PrivateBin\Sjcl;
  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. */
  60. public function __construct(Configuration $configuration, AbstractData $storage)
  61. {
  62. $this->_conf = $configuration;
  63. $this->_store = $storage;
  64. $this->_data = new stdClass;
  65. $this->_data->meta = new stdClass;
  66. }
  67. /**
  68. * Get ID.
  69. *
  70. * @access public
  71. * @return string
  72. */
  73. public function getId()
  74. {
  75. return $this->_id;
  76. }
  77. /**
  78. * Set ID.
  79. *
  80. * @access public
  81. * @param string $id
  82. * @throws Exception
  83. */
  84. public function setId($id)
  85. {
  86. if (!self::isValidId($id)) {
  87. throw new Exception('Invalid paste ID.', 60);
  88. }
  89. $this->_id = $id;
  90. }
  91. /**
  92. * Set data and recalculate ID.
  93. *
  94. * @access public
  95. * @param string $data
  96. * @throws Exception
  97. */
  98. public function setData($data)
  99. {
  100. if (!Sjcl::isValid($data)) {
  101. throw new Exception('Invalid data.', 61);
  102. }
  103. $this->_data->data = $data;
  104. // We just want a small hash to avoid collisions:
  105. // Half-MD5 (64 bits) will do the trick
  106. $this->setId(substr(hash('md5', $data), 0, 16));
  107. }
  108. /**
  109. * Get instance data.
  110. *
  111. * @access public
  112. * @return stdClass
  113. */
  114. abstract public function get();
  115. /**
  116. * Store the instance's data.
  117. *
  118. * @access public
  119. * @throws Exception
  120. */
  121. abstract public function store();
  122. /**
  123. * Delete the current instance.
  124. *
  125. * @access public
  126. * @throws Exception
  127. */
  128. abstract public function delete();
  129. /**
  130. * Test if current instance exists in store.
  131. *
  132. * @access public
  133. * @return bool
  134. */
  135. abstract public function exists();
  136. /**
  137. * Validate ID.
  138. *
  139. * @access public
  140. * @static
  141. * @param string $id
  142. * @return bool
  143. */
  144. public static function isValidId($id)
  145. {
  146. return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
  147. }
  148. }