abstract.php 3.0 KB

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