Paste.php 8.2 KB

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