Paste.php 7.9 KB

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