1
0

Paste.php 7.9 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. * 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(
  178. $this->_conf->getKey('zerobincompatibility') ? 'sha1' : 'sha256',
  179. $this->getId(),
  180. $this->_data['meta']['salt']
  181. );
  182. }
  183. /**
  184. * Check if paste has discussions enabled.
  185. *
  186. * @access public
  187. * @throws Exception
  188. * @return bool
  189. */
  190. public function isOpendiscussion()
  191. {
  192. if (!array_key_exists('adata', $this->_data) && !array_key_exists('data', $this->_data)) {
  193. $this->get();
  194. }
  195. return
  196. (array_key_exists('adata', $this->_data) && $this->_data['adata'][2] === 1) ||
  197. (array_key_exists('opendiscussion', $this->_data['meta']) && $this->_data['meta']['opendiscussion']);
  198. }
  199. /**
  200. * Sanitizes data to conform with current configuration.
  201. *
  202. * @access protected
  203. * @param array $data
  204. * @return array
  205. */
  206. protected function _sanitize(array $data)
  207. {
  208. $expiration = $data['meta']['expire'];
  209. unset($data['meta']['expire']);
  210. $expire_options = $this->_conf->getSection('expire_options');
  211. if (array_key_exists($expiration, $expire_options)) {
  212. $expire = $expire_options[$expiration];
  213. } else {
  214. // using getKey() to ensure a default value is present
  215. $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  216. }
  217. if ($expire > 0) {
  218. $data['meta']['expire_date'] = time() + $expire;
  219. }
  220. return $data;
  221. }
  222. /**
  223. * Validate data.
  224. *
  225. * @access protected
  226. * @param array $data
  227. * @throws Exception
  228. */
  229. protected function _validate(array $data)
  230. {
  231. // reject invalid or disabled formatters
  232. if (!array_key_exists($data['adata'][1], $this->_conf->getSection('formatter_options'))) {
  233. throw new Exception('Invalid data.', 75);
  234. }
  235. // discussion requested, but disabled in config or burn after reading requested as well, or invalid integer
  236. if (
  237. ($data['adata'][2] === 1 && ( // open discussion flag
  238. !$this->_conf->getKey('discussion') ||
  239. $data['adata'][3] === 1 // burn after reading flag
  240. )) ||
  241. ($data['adata'][2] !== 0 && $data['adata'][2] !== 1)
  242. ) {
  243. throw new Exception('Invalid data.', 74);
  244. }
  245. // reject invalid burn after reading
  246. if ($data['adata'][3] !== 0 && $data['adata'][3] !== 1) {
  247. throw new Exception('Invalid data.', 73);
  248. }
  249. }
  250. }