1
0

Paste.php 8.7 KB

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