paste.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 0.22
  11. */
  12. /**
  13. * model_paste
  14. *
  15. * Model of a PrivateBin paste.
  16. */
  17. class model_paste extends model_abstract
  18. {
  19. /**
  20. * Get paste data.
  21. *
  22. * @access public
  23. * @throws Exception
  24. * @return stdClass
  25. */
  26. public function get()
  27. {
  28. $this->_data = $this->_store->read($this->getId());
  29. if ($this->_data === false) throw new Exception(privatebin::GENERIC_ERROR, 64);
  30. // check if paste has expired and delete it if neccessary.
  31. if (property_exists($this->_data->meta, 'expire_date'))
  32. {
  33. if ($this->_data->meta->expire_date < time())
  34. {
  35. $this->delete();
  36. throw new Exception(privatebin::GENERIC_ERROR, 63);
  37. }
  38. // We kindly provide the remaining time before expiration (in seconds)
  39. $this->_data->meta->remaining_time = $this->_data->meta->expire_date - time();
  40. }
  41. // set formatter for for the view.
  42. if (!property_exists($this->_data->meta, 'formatter'))
  43. {
  44. // support < 0.21 syntax highlighting
  45. if (property_exists($this->_data->meta, 'syntaxcoloring') && $this->_data->meta->syntaxcoloring === true)
  46. {
  47. $this->_data->meta->formatter = 'syntaxhighlighting';
  48. }
  49. else
  50. {
  51. $this->_data->meta->formatter = $this->_conf->getKey('defaultformatter');
  52. }
  53. }
  54. // support old paste format with server wide salt
  55. if (!property_exists($this->_data->meta, 'salt'))
  56. {
  57. $this->_data->meta->salt = serversalt::get();
  58. }
  59. $this->_data->comments = array_values($this->getComments());
  60. $this->_data->comment_count = count($this->_data->comments);
  61. $this->_data->comment_offset = 0;
  62. $this->_data->{'@context'} = 'js/paste.jsonld';
  63. return $this->_data;
  64. }
  65. /**
  66. * Store the paste's data.
  67. *
  68. * @access public
  69. * @throws Exception
  70. * @return void
  71. */
  72. public function store()
  73. {
  74. // Check for improbable collision.
  75. if ($this->exists())
  76. throw new Exception('You are unlucky. Try again.', 75);
  77. $this->_data->meta->postdate = time();
  78. $this->_data->meta->salt = serversalt::generate();
  79. // store paste
  80. if (
  81. $this->_store->create(
  82. $this->getId(),
  83. json_decode(json_encode($this->_data), true)
  84. ) === false
  85. ) throw new Exception('Error saving paste. Sorry.', 76);
  86. }
  87. /**
  88. * Delete the paste.
  89. *
  90. * @access public
  91. * @throws Exception
  92. * @return void
  93. */
  94. public function delete()
  95. {
  96. $this->_store->delete($this->getId());
  97. }
  98. /**
  99. * Test if paste exists in store.
  100. *
  101. * @access public
  102. * @return bool
  103. */
  104. public function exists()
  105. {
  106. return $this->_store->exists($this->getId());
  107. }
  108. /**
  109. * Get a comment, optionally a specific instance.
  110. *
  111. * @access public
  112. * @param string $parentId
  113. * @param string $commentId
  114. * @throws Exception
  115. * @return model_comment
  116. */
  117. public function getComment($parentId, $commentId = null)
  118. {
  119. if (!$this->exists())
  120. {
  121. throw new Exception('Invalid data.', 62);
  122. }
  123. $comment = new model_comment($this->_conf, $this->_store);
  124. $comment->setPaste($this);
  125. $comment->setParentId($parentId);
  126. if ($commentId !== null) $comment->setId($commentId);
  127. return $comment;
  128. }
  129. /**
  130. * Get all comments, if any.
  131. *
  132. * @access public
  133. * @return array
  134. */
  135. public function getComments()
  136. {
  137. return $this->_store->readComments($this->getId());
  138. }
  139. /**
  140. * Generate the "delete" token.
  141. *
  142. * The token is the hmac of the pastes ID signed with the server salt.
  143. * The paste can be deleted by calling:
  144. * http://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
  145. *
  146. * @access public
  147. * @return string
  148. */
  149. public function getDeleteToken()
  150. {
  151. if (!property_exists($this->_data->meta, 'salt')) $this->get();
  152. return hash_hmac(
  153. $this->_conf->getKey('zerobincompatibility') ? 'sha1' : 'sha256',
  154. $this->getId(),
  155. $this->_data->meta->salt
  156. );
  157. }
  158. /**
  159. * Set paste's attachment.
  160. *
  161. * @access public
  162. * @param string $attachment
  163. * @throws Exception
  164. * @return void
  165. */
  166. public function setAttachment($attachment)
  167. {
  168. if (!$this->_conf->getKey('fileupload') || !sjcl::isValid($attachment))
  169. throw new Exception('Invalid attachment.', 71);
  170. $this->_data->meta->attachment = $attachment;
  171. }
  172. /**
  173. * Set paste's attachment name.
  174. *
  175. * @access public
  176. * @param string $attachmentname
  177. * @throws Exception
  178. * @return void
  179. */
  180. public function setAttachmentName($attachmentname)
  181. {
  182. if (!$this->_conf->getKey('fileupload') || !sjcl::isValid($attachmentname))
  183. throw new Exception('Invalid attachment.', 72);
  184. $this->_data->meta->attachmentname = $attachmentname;
  185. }
  186. /**
  187. * Set paste expiration.
  188. *
  189. * @access public
  190. * @param string $expiration
  191. * @return void
  192. */
  193. public function setExpiration($expiration)
  194. {
  195. $expire_options = $this->_conf->getSection('expire_options');
  196. if (array_key_exists($expiration, $expire_options))
  197. {
  198. $expire = $expire_options[$expiration];
  199. }
  200. else
  201. {
  202. // using getKey() to ensure a default value is present
  203. $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  204. }
  205. if ($expire > 0) $this->_data->meta->expire_date = time() + $expire;
  206. }
  207. /**
  208. * Set paste's burn-after-reading type.
  209. *
  210. * @access public
  211. * @param string $burnafterreading
  212. * @throws Exception
  213. * @return void
  214. */
  215. public function setBurnafterreading($burnafterreading = '1')
  216. {
  217. if ($burnafterreading === '0')
  218. {
  219. $this->_data->meta->burnafterreading = false;
  220. }
  221. else
  222. {
  223. if ($burnafterreading !== '1')
  224. throw new Exception('Invalid data.', 73);
  225. $this->_data->meta->burnafterreading = true;
  226. $this->_data->meta->opendiscussion = false;
  227. }
  228. }
  229. /**
  230. * Set paste's discussion state.
  231. *
  232. * @access public
  233. * @param string $opendiscussion
  234. * @throws Exception
  235. * @return void
  236. */
  237. public function setOpendiscussion($opendiscussion = '1')
  238. {
  239. if (
  240. !$this->_conf->getKey('discussion') ||
  241. $this->isBurnafterreading() ||
  242. $opendiscussion === '0'
  243. )
  244. {
  245. $this->_data->meta->opendiscussion = false;
  246. }
  247. else
  248. {
  249. if ($opendiscussion !== '1')
  250. throw new Exception('Invalid data.', 74);
  251. $this->_data->meta->opendiscussion = true;
  252. }
  253. }
  254. /**
  255. * Set paste's format.
  256. *
  257. * @access public
  258. * @param string $format
  259. * @throws Exception
  260. * @return void
  261. */
  262. public function setFormatter($format)
  263. {
  264. if (!array_key_exists($format, $this->_conf->getSection('formatter_options')))
  265. {
  266. $format = $this->_conf->getKey('defaultformatter');
  267. }
  268. $this->_data->meta->formatter = $format;
  269. }
  270. /**
  271. * Check if paste is of burn-after-reading type.
  272. *
  273. * @access public
  274. * @throws Exception
  275. * @return boolean
  276. */
  277. public function isBurnafterreading()
  278. {
  279. if (!property_exists($this->_data, 'data')) $this->get();
  280. return property_exists($this->_data->meta, 'burnafterreading') &&
  281. $this->_data->meta->burnafterreading === true;
  282. }
  283. /**
  284. * Check if paste has discussions enabled.
  285. *
  286. * @access public
  287. * @throws Exception
  288. * @return boolean
  289. */
  290. public function isOpendiscussion()
  291. {
  292. if (!property_exists($this->_data, 'data')) $this->get();
  293. return property_exists($this->_data->meta, 'opendiscussion') &&
  294. $this->_data->meta->opendiscussion === true;
  295. }
  296. }