Paste.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Exception;
  13. use PrivateBin\Controller;
  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 Exception
  45. * @return array
  46. */
  47. public function get()
  48. {
  49. $data = $this->_store->read($this->getId());
  50. if ($data === false) {
  51. throw new Exception(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 Exception(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 (
  69. array_key_exists('adata', $data) &&
  70. $data['adata'][self::ADATA_BURN_AFTER_READING] === 1
  71. ) {
  72. $this->delete();
  73. }
  74. $data['comments'] = array_values($this->getComments());
  75. $data['comment_count'] = count($data['comments']);
  76. $data['comment_offset'] = 0;
  77. $data['@context'] = '?jsonld=paste';
  78. $this->_data = $data;
  79. return $this->_data;
  80. }
  81. /**
  82. * Store the paste's data.
  83. *
  84. * @access public
  85. * @throws Exception
  86. */
  87. public function store()
  88. {
  89. // Check for improbable collision.
  90. if ($this->exists()) {
  91. throw new Exception('You are unlucky. Try again.', 75);
  92. }
  93. $this->_data['meta']['salt'] = ServerSalt::generate();
  94. // store paste
  95. if (
  96. $this->_store->create(
  97. $this->getId(),
  98. $this->_data
  99. ) === false
  100. ) {
  101. throw new Exception('Error saving paste. Sorry.', 76);
  102. }
  103. }
  104. /**
  105. * Delete the paste.
  106. *
  107. * @access public
  108. * @throws Exception
  109. */
  110. public function delete()
  111. {
  112. $this->_store->delete($this->getId());
  113. }
  114. /**
  115. * Test if paste exists in store.
  116. *
  117. * @access public
  118. * @return bool
  119. */
  120. public function exists()
  121. {
  122. return $this->_store->exists($this->getId());
  123. }
  124. /**
  125. * Get a comment, optionally a specific instance.
  126. *
  127. * @access public
  128. * @param string $parentId
  129. * @param string $commentId
  130. * @throws Exception
  131. * @return Comment
  132. */
  133. public function getComment($parentId, $commentId = '')
  134. {
  135. if (!$this->exists()) {
  136. throw new Exception('Invalid data.', 62);
  137. }
  138. $comment = new Comment($this->_conf, $this->_store);
  139. $comment->setPaste($this);
  140. $comment->setParentId($parentId);
  141. if ($commentId !== '') {
  142. $comment->setId($commentId);
  143. }
  144. return $comment;
  145. }
  146. /**
  147. * Get all comments, if any.
  148. *
  149. * @access public
  150. * @return array
  151. */
  152. public function getComments()
  153. {
  154. if ($this->_conf->getKey('discussiondatedisplay')) {
  155. return $this->_store->readComments($this->getId());
  156. }
  157. return array_map(function ($comment) {
  158. if (array_key_exists('created', $comment['meta'])) {
  159. unset($comment['meta']['created']);
  160. }
  161. return $comment;
  162. }, $this->_store->readComments($this->getId()));
  163. }
  164. /**
  165. * Generate the "delete" token.
  166. *
  167. * The token is the hmac of the pastes ID signed with the server salt.
  168. * The paste can be deleted by calling:
  169. * https://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
  170. *
  171. * @access public
  172. * @return string
  173. */
  174. public function getDeleteToken()
  175. {
  176. if (!array_key_exists('salt', $this->_data['meta'])) {
  177. $this->get();
  178. }
  179. return hash_hmac('sha256', $this->getId(), $this->_data['meta']['salt']);
  180. }
  181. /**
  182. * Check if paste has discussions enabled.
  183. *
  184. * @access public
  185. * @throws Exception
  186. * @return bool
  187. */
  188. public function isOpendiscussion()
  189. {
  190. if (!array_key_exists('adata', $this->_data) && !array_key_exists('data', $this->_data)) {
  191. $this->get();
  192. }
  193. return array_key_exists('adata', $this->_data) &&
  194. $this->_data['adata'][self::ADATA_OPEN_DISCUSSION] === 1;
  195. }
  196. /**
  197. * Sanitizes data to conform with current configuration.
  198. *
  199. * @access protected
  200. * @param array $data
  201. */
  202. protected function _sanitize(array &$data)
  203. {
  204. $expiration = $data['meta']['expire'] ?? 0;
  205. unset($data['meta']['expire']);
  206. $expire_options = $this->_conf->getSection('expire_options');
  207. if (array_key_exists($expiration, $expire_options)) {
  208. $expire = $expire_options[$expiration];
  209. } else {
  210. // using getKey() to ensure a default value is present
  211. $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  212. }
  213. if ($expire > 0) {
  214. $data['meta']['expire_date'] = time() + $expire;
  215. }
  216. }
  217. /**
  218. * Validate data.
  219. *
  220. * @access protected
  221. * @param array $data
  222. * @throws Exception
  223. */
  224. protected function _validate(array &$data)
  225. {
  226. // reject invalid or disabled formatters
  227. if (!array_key_exists($data['adata'][self::ADATA_FORMATTER], $this->_conf->getSection('formatter_options'))) {
  228. throw new Exception('Invalid data.', 75);
  229. }
  230. // discussion requested, but disabled in config or burn after reading requested as well, or invalid integer
  231. if (
  232. ($data['adata'][self::ADATA_OPEN_DISCUSSION] === 1 && (
  233. !$this->_conf->getKey('discussion') ||
  234. $data['adata'][self::ADATA_BURN_AFTER_READING] === 1
  235. )) ||
  236. ($data['adata'][self::ADATA_OPEN_DISCUSSION] !== 0 && $data['adata'][self::ADATA_OPEN_DISCUSSION] !== 1)
  237. ) {
  238. throw new Exception('Invalid data.', 74);
  239. }
  240. // reject invalid burn after reading
  241. if (
  242. $data['adata'][self::ADATA_BURN_AFTER_READING] !== 0 &&
  243. $data['adata'][self::ADATA_BURN_AFTER_READING] !== 1
  244. ) {
  245. throw new Exception('Invalid data.', 73);
  246. }
  247. }
  248. }