Paste.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php declare(strict_types=1);
  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. */
  11. namespace PrivateBin\Model;
  12. use PrivateBin\Controller;
  13. use PrivateBin\Exception\TranslatedException;
  14. use PrivateBin\Persistence\ServerSalt;
  15. /**
  16. * Paste
  17. *
  18. * Model of a PrivateBin paste.
  19. */
  20. class Paste extends AbstractModel
  21. {
  22. /**
  23. * authenticated data index of paste formatter (plaintext/syntaxhighlighting/markdown)
  24. *
  25. * @const int
  26. */
  27. const ADATA_FORMATTER = 1;
  28. /**
  29. * authenticated data index of open-discussion flag (0/1)
  30. *
  31. * @const int
  32. */
  33. const ADATA_OPEN_DISCUSSION = 2;
  34. /**
  35. * authenticated data index of burn-after-reading flag (0/1)
  36. *
  37. * @const int
  38. */
  39. const ADATA_BURN_AFTER_READING = 3;
  40. /**
  41. * Get paste data.
  42. *
  43. * @access public
  44. * @throws TranslatedException
  45. * @return array
  46. */
  47. public function get()
  48. {
  49. $data = $this->_store->read($this->getId());
  50. if ($data === false) {
  51. throw new TranslatedException(Controller::GENERIC_ERROR, 64);
  52. }
  53. // check if paste has expired and delete it if necessary.
  54. if (array_key_exists('expire_date', $data['meta'])) {
  55. $now = time();
  56. if ($data['meta']['expire_date'] < $now) {
  57. $this->delete();
  58. throw new TranslatedException(Controller::GENERIC_ERROR, 63);
  59. }
  60. // We kindly provide the remaining time before expiration (in seconds)
  61. $data['meta']['time_to_live'] = $data['meta']['expire_date'] - $now;
  62. unset($data['meta']['expire_date']);
  63. }
  64. if (array_key_exists('created', $data['meta'])) {
  65. unset($data['meta']['created']);
  66. }
  67. // check if non-expired burn after reading paste needs to be deleted
  68. if (($data['adata'][self::ADATA_BURN_AFTER_READING] ?? 0) === 1) {
  69. $this->delete();
  70. }
  71. $data['comments'] = array_values($this->getComments());
  72. $data['comment_count'] = count($data['comments']);
  73. $data['comment_offset'] = 0;
  74. $data['@context'] = '?jsonld=paste';
  75. $this->_data = $data;
  76. return $this->_data;
  77. }
  78. /**
  79. * Store the paste's data.
  80. *
  81. * @access public
  82. * @throws TranslatedException
  83. */
  84. public function store()
  85. {
  86. // Check for improbable collision.
  87. if ($this->exists()) {
  88. throw new TranslatedException(self::COLLISION_ERROR, 75);
  89. }
  90. $this->_data['meta']['salt'] = ServerSalt::generate();
  91. // store paste
  92. if (
  93. $this->_store->create(
  94. $this->getId(),
  95. $this->_data
  96. ) === false
  97. ) {
  98. throw new TranslatedException('Error saving document. Sorry.', 76);
  99. }
  100. }
  101. /**
  102. * Delete the paste.
  103. *
  104. * @access public
  105. */
  106. public function delete()
  107. {
  108. $this->_store->delete($this->getId());
  109. }
  110. /**
  111. * Test if paste exists in store.
  112. *
  113. * @access public
  114. * @return bool
  115. */
  116. public function exists()
  117. {
  118. return $this->_store->exists($this->getId());
  119. }
  120. /**
  121. * Get a comment, optionally a specific instance.
  122. *
  123. * @access public
  124. * @param string $parentId
  125. * @param string $commentId
  126. * @throws TranslatedException
  127. * @return Comment
  128. */
  129. public function getComment($parentId, $commentId = '')
  130. {
  131. if (!$this->exists()) {
  132. throw new TranslatedException(self::INVALID_DATA_ERROR, 62);
  133. }
  134. $comment = new Comment($this->_conf, $this->_store);
  135. $comment->setPaste($this);
  136. $comment->setParentId($parentId);
  137. if (!empty($commentId)) {
  138. $comment->setId($commentId);
  139. }
  140. return $comment;
  141. }
  142. /**
  143. * Get all comments, if any.
  144. *
  145. * @access public
  146. * @return array
  147. */
  148. public function getComments()
  149. {
  150. if ($this->_conf->getKey('discussiondatedisplay')) {
  151. return $this->_store->readComments($this->getId());
  152. }
  153. return array_map(function ($comment) {
  154. if (array_key_exists('created', $comment['meta'])) {
  155. unset($comment['meta']['created']);
  156. }
  157. return $comment;
  158. }, $this->_store->readComments($this->getId()));
  159. }
  160. /**
  161. * Generate the "delete" token.
  162. *
  163. * The token is the hmac of the pastes ID signed with the server salt.
  164. * The paste can be deleted by calling:
  165. * https://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
  166. *
  167. * @access public
  168. * @return string
  169. */
  170. public function getDeleteToken()
  171. {
  172. if (!array_key_exists('salt', $this->_data['meta'])) {
  173. $this->get();
  174. }
  175. return hash_hmac('sha256', $this->getId(), $this->_data['meta']['salt']);
  176. }
  177. /**
  178. * Check if paste has discussions enabled.
  179. *
  180. * @access public
  181. * @return bool
  182. */
  183. public function isOpendiscussion()
  184. {
  185. if (!array_key_exists('adata', $this->_data) && !array_key_exists('data', $this->_data)) {
  186. $this->get();
  187. }
  188. return ($this->_data['adata'][self::ADATA_OPEN_DISCUSSION] ?? 0) === 1;
  189. }
  190. /**
  191. * Sanitizes data to conform with current configuration.
  192. *
  193. * @access protected
  194. * @param array $data
  195. */
  196. protected function _sanitize(array &$data)
  197. {
  198. $expiration = $data['meta']['expire'] ?? 0;
  199. unset($data['meta']['expire']);
  200. $expire_options = $this->_conf->getSection('expire_options');
  201. // using getKey() to ensure a default value is present
  202. $expire = $expire_options[$expiration] ??
  203. $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  204. if ($expire > 0) {
  205. $data['meta']['expire_date'] = time() + $expire;
  206. }
  207. }
  208. /**
  209. * Validate data.
  210. *
  211. * @access protected
  212. * @param array $data
  213. * @throws TranslatedException
  214. */
  215. protected function _validate(array &$data)
  216. {
  217. // reject invalid or disabled formatters
  218. if (!array_key_exists($data['adata'][self::ADATA_FORMATTER], $this->_conf->getSection('formatter_options'))) {
  219. throw new TranslatedException(self::INVALID_DATA_ERROR, 75);
  220. }
  221. // discussion requested, but disabled in config or burn after reading requested as well, or invalid integer
  222. if (
  223. ($data['adata'][self::ADATA_OPEN_DISCUSSION] === 1 && (
  224. !$this->_conf->getKey('discussion') ||
  225. $data['adata'][self::ADATA_BURN_AFTER_READING] === 1
  226. )) ||
  227. ($data['adata'][self::ADATA_OPEN_DISCUSSION] !== 0 && $data['adata'][self::ADATA_OPEN_DISCUSSION] !== 1)
  228. ) {
  229. throw new TranslatedException(self::INVALID_DATA_ERROR, 74);
  230. }
  231. // reject invalid burn after reading
  232. if (
  233. $data['adata'][self::ADATA_BURN_AFTER_READING] !== 0 &&
  234. $data['adata'][self::ADATA_BURN_AFTER_READING] !== 1
  235. ) {
  236. throw new TranslatedException(self::INVALID_DATA_ERROR, 73);
  237. }
  238. }
  239. }