abstract.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  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.21.1
  11. */
  12. /**
  13. * model_abstract
  14. *
  15. * Abstract model for ZeroBin 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 zerobin_abstract
  45. */
  46. protected $_store;
  47. /**
  48. * Instance constructor.
  49. *
  50. * @access public
  51. * @param configuration $configuration
  52. * @param zerobin_abstract $storage
  53. * @return void
  54. */
  55. public function __construct(configuration $configuration, zerobin_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. * @throws Exception
  77. * @return void
  78. */
  79. public function setId($id)
  80. {
  81. if (!self::isValidId($id)) throw new Exception('Invalid paste ID.', 60);
  82. $this->_id = $id;
  83. }
  84. /**
  85. * Set data and recalculate ID.
  86. *
  87. * @access public
  88. * @param string $data
  89. * @throws Exception
  90. * @return void
  91. */
  92. public function setData($data)
  93. {
  94. if (!sjcl::isValid($data)) throw new Exception('Invalid data.', 61);
  95. $this->_data->data = $data;
  96. // We just want a small hash to avoid collisions:
  97. // Half-MD5 (64 bits) will do the trick
  98. $this->setId(substr(hash('md5', $data), 0, 16));
  99. }
  100. /**
  101. * Get instance data.
  102. *
  103. * @access public
  104. * @return stdObject
  105. */
  106. abstract public function get();
  107. /**
  108. * Store the instance's data.
  109. *
  110. * @access public
  111. * @throws Exception
  112. * @return void
  113. */
  114. abstract public function store();
  115. /**
  116. * Delete the current instance.
  117. *
  118. * @access public
  119. * @throws Exception
  120. * @return void
  121. */
  122. abstract public function delete();
  123. /**
  124. * Test if current instance exists in store.
  125. *
  126. * @access public
  127. * @return bool
  128. */
  129. abstract public function exists();
  130. /**
  131. * Validate ID.
  132. *
  133. * @access public
  134. * @static
  135. * @param string $id
  136. * @return bool
  137. */
  138. public static function isValidId($id)
  139. {
  140. return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
  141. }
  142. }