Paste.php 7.4 KB

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