Paste.php 8.1 KB

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