paste.php 8.6 KB

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