Paste.php 7.5 KB

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