AbstractModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 0.22
  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. * model_abstract
  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 privatebin_abstract
  51. */
  52. protected $_store;
  53. /**
  54. * Instance constructor.
  55. *
  56. * @access public
  57. * @param configuration $configuration
  58. * @param privatebin_abstract $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)) throw new Exception('Invalid paste ID.', 60);
  89. $this->_id = $id;
  90. }
  91. /**
  92. * Set data and recalculate ID.
  93. *
  94. * @access public
  95. * @param string $data
  96. * @throws Exception
  97. * @return void
  98. */
  99. public function setData($data)
  100. {
  101. if (!sjcl::isValid($data)) throw new Exception('Invalid data.', 61);
  102. $this->_data->data = $data;
  103. // We just want a small hash to avoid collisions:
  104. // Half-MD5 (64 bits) will do the trick
  105. $this->setId(substr(hash('md5', $data), 0, 16));
  106. }
  107. /**
  108. * Get instance data.
  109. *
  110. * @access public
  111. * @return stdClass
  112. */
  113. abstract public function get();
  114. /**
  115. * Store the instance's data.
  116. *
  117. * @access public
  118. * @throws Exception
  119. * @return void
  120. */
  121. abstract public function store();
  122. /**
  123. * Delete the current instance.
  124. *
  125. * @access public
  126. * @throws Exception
  127. * @return void
  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($id)
  146. {
  147. return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
  148. }
  149. }