Paste.php 7.3 KB

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