Paste.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * Get paste data.
  24. *
  25. * @access public
  26. * @throws Exception
  27. * @return array
  28. */
  29. public function get()
  30. {
  31. $data = $this->_store->read($this->getId());
  32. if ($data === false) {
  33. throw new Exception(Controller::GENERIC_ERROR, 64);
  34. }
  35. // check if paste has expired and delete it if necessary.
  36. if (array_key_exists('expire_date', $data['meta'])) {
  37. if ($data['meta']['expire_date'] < time()) {
  38. $this->delete();
  39. throw new Exception(Controller::GENERIC_ERROR, 63);
  40. }
  41. // We kindly provide the remaining time before expiration (in seconds)
  42. $data['meta']['time_to_live'] = $data['meta']['expire_date'] - time();
  43. unset($data['meta']['expire_date']);
  44. }
  45. foreach (array('created', 'postdate') as $key) {
  46. if (array_key_exists($key, $data['meta'])) {
  47. unset($data['meta'][$key]);
  48. }
  49. }
  50. // check if non-expired burn after reading paste needs to be deleted
  51. if (
  52. (array_key_exists('adata', $data) && $data['adata'][3] === 1) ||
  53. (array_key_exists('burnafterreading', $data['meta']) && $data['meta']['burnafterreading'])
  54. ) {
  55. $this->delete();
  56. }
  57. // set formatter for the view in version 1 pastes.
  58. if (array_key_exists('data', $data) && !array_key_exists('formatter', $data['meta'])) {
  59. // support < 0.21 syntax highlighting
  60. if (array_key_exists('syntaxcoloring', $data['meta']) && $data['meta']['syntaxcoloring'] === true) {
  61. $data['meta']['formatter'] = 'syntaxhighlighting';
  62. } else {
  63. $data['meta']['formatter'] = $this->_conf->getKey('defaultformatter');
  64. }
  65. }
  66. // support old paste format with server wide salt
  67. if (!array_key_exists('salt', $data['meta'])) {
  68. $data['meta']['salt'] = ServerSalt::get();
  69. }
  70. $data['comments'] = array_values($this->getComments());
  71. $data['comment_count'] = count($data['comments']);
  72. $data['comment_offset'] = 0;
  73. $data['@context'] = '?jsonld=paste';
  74. $this->_data = $data;
  75. return $this->_data;
  76. }
  77. /**
  78. * Store the paste's data.
  79. *
  80. * @access public
  81. * @throws Exception
  82. */
  83. public function store()
  84. {
  85. // Check for improbable collision.
  86. if ($this->exists()) {
  87. throw new Exception('You are unlucky. Try again.', 75);
  88. }
  89. $this->_data['meta']['salt'] = ServerSalt::generate();
  90. // store paste
  91. if (
  92. $this->_store->create(
  93. $this->getId(),
  94. $this->_data
  95. ) === false
  96. ) {
  97. throw new Exception('Error saving paste. Sorry.', 76);
  98. }
  99. }
  100. /**
  101. * Delete the paste.
  102. *
  103. * @access public
  104. * @throws Exception
  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 Exception
  127. * @return Comment
  128. */
  129. public function getComment($parentId, $commentId = '')
  130. {
  131. if (!$this->exists()) {
  132. throw new Exception('Invalid data.', 62);
  133. }
  134. $comment = new Comment($this->_conf, $this->_store);
  135. $comment->setPaste($this);
  136. $comment->setParentId($parentId);
  137. if ($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. foreach (array('created', 'postdate') as $key) {
  155. if (array_key_exists($key, $comment['meta'])) {
  156. unset($comment['meta'][$key]);
  157. }
  158. }
  159. return $comment;
  160. }, $this->_store->readComments($this->getId()));
  161. }
  162. /**
  163. * Generate the "delete" token.
  164. *
  165. * The token is the hmac of the pastes ID signed with the server salt.
  166. * The paste can be deleted by calling:
  167. * https://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
  168. *
  169. * @access public
  170. * @return string
  171. */
  172. public function getDeleteToken()
  173. {
  174. if (!array_key_exists('salt', $this->_data['meta'])) {
  175. $this->get();
  176. }
  177. return hash_hmac('sha256', $this->getId(), $this->_data['meta']['salt']);
  178. }
  179. /**
  180. * Check if paste has discussions enabled.
  181. *
  182. * @access public
  183. * @throws Exception
  184. * @return bool
  185. */
  186. public function isOpendiscussion()
  187. {
  188. if (!array_key_exists('adata', $this->_data) && !array_key_exists('data', $this->_data)) {
  189. $this->get();
  190. }
  191. return
  192. (array_key_exists('adata', $this->_data) && $this->_data['adata'][2] === 1) ||
  193. (array_key_exists('opendiscussion', $this->_data['meta']) && $this->_data['meta']['opendiscussion']);
  194. }
  195. /**
  196. * Sanitizes data to conform with current configuration.
  197. *
  198. * @access protected
  199. * @param array $data
  200. */
  201. protected function _sanitize(array &$data)
  202. {
  203. $expiration = $data['meta']['expire'] ?? 0;
  204. unset($data['meta']['expire']);
  205. $expire_options = $this->_conf->getSection('expire_options');
  206. if (array_key_exists($expiration, $expire_options)) {
  207. $expire = $expire_options[$expiration];
  208. } else {
  209. // using getKey() to ensure a default value is present
  210. $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  211. }
  212. if ($expire > 0) {
  213. $data['meta']['expire_date'] = time() + $expire;
  214. }
  215. }
  216. /**
  217. * Validate data.
  218. *
  219. * @access protected
  220. * @param array $data
  221. * @throws Exception
  222. */
  223. protected function _validate(array &$data)
  224. {
  225. // reject invalid or disabled formatters
  226. if (!array_key_exists($data['adata'][1], $this->_conf->getSection('formatter_options'))) {
  227. throw new Exception('Invalid data.', 75);
  228. }
  229. // discussion requested, but disabled in config or burn after reading requested as well, or invalid integer
  230. if (
  231. ($data['adata'][2] === 1 && ( // open discussion flag
  232. !$this->_conf->getKey('discussion') ||
  233. $data['adata'][3] === 1 // burn after reading flag
  234. )) ||
  235. ($data['adata'][2] !== 0 && $data['adata'][2] !== 1)
  236. ) {
  237. throw new Exception('Invalid data.', 74);
  238. }
  239. // reject invalid burn after reading
  240. if ($data['adata'][3] !== 0 && $data['adata'][3] !== 1) {
  241. throw new Exception('Invalid data.', 73);
  242. }
  243. }
  244. }