1
0

Paste.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.2.3
  11. */
  12. namespace PrivateBin\Model;
  13. use Exception;
  14. use PrivateBin\Controller;
  15. use PrivateBin\Persistence\ServerSalt;
  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(Controller::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(Controller::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) {
  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. // If the paste was meant to be read only once, delete it.
  69. if ($this->isBurnafterreading()) {
  70. $this->delete();
  71. }
  72. return $this->_data;
  73. }
  74. /**
  75. * Store the paste's data.
  76. *
  77. * @access public
  78. * @throws Exception
  79. */
  80. public function store()
  81. {
  82. // Check for improbable collision.
  83. if ($this->exists()) {
  84. throw new Exception('You are unlucky. Try again.', 75);
  85. }
  86. $this->_data->meta->postdate = time();
  87. $this->_data->meta->salt = serversalt::generate();
  88. // store paste
  89. if (
  90. $this->_store->create(
  91. $this->getId(),
  92. json_decode(json_encode($this->_data), true)
  93. ) === false
  94. ) {
  95. throw new Exception('Error saving paste. Sorry.', 76);
  96. }
  97. }
  98. /**
  99. * Delete the paste.
  100. *
  101. * @access public
  102. * @throws Exception
  103. */
  104. public function delete()
  105. {
  106. $this->_store->delete($this->getId());
  107. }
  108. /**
  109. * Test if paste exists in store.
  110. *
  111. * @access public
  112. * @return bool
  113. */
  114. public function exists()
  115. {
  116. return $this->_store->exists($this->getId());
  117. }
  118. /**
  119. * Get a comment, optionally a specific instance.
  120. *
  121. * @access public
  122. * @param string $parentId
  123. * @param string $commentId
  124. * @throws Exception
  125. * @return Comment
  126. */
  127. public function getComment($parentId, $commentId = null)
  128. {
  129. if (!$this->exists()) {
  130. throw new Exception('Invalid data.', 62);
  131. }
  132. $comment = new Comment($this->_conf, $this->_store);
  133. $comment->setPaste($this);
  134. $comment->setParentId($parentId);
  135. if ($commentId !== null) {
  136. $comment->setId($commentId);
  137. }
  138. return $comment;
  139. }
  140. /**
  141. * Get all comments, if any.
  142. *
  143. * @access public
  144. * @return array
  145. */
  146. public function getComments()
  147. {
  148. return $this->_store->readComments($this->getId());
  149. }
  150. /**
  151. * Generate the "delete" token.
  152. *
  153. * The token is the hmac of the pastes ID signed with the server salt.
  154. * The paste can be deleted by calling:
  155. * https://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
  156. *
  157. * @access public
  158. * @return string
  159. */
  160. public function getDeleteToken()
  161. {
  162. if (!property_exists($this->_data->meta, 'salt')) {
  163. $this->get();
  164. }
  165. return hash_hmac(
  166. $this->_conf->getKey('zerobincompatibility') ? 'sha1' : 'sha256',
  167. $this->getId(),
  168. $this->_data->meta->salt
  169. );
  170. }
  171. /**
  172. * Set paste's attachment.
  173. *
  174. * @access public
  175. * @param string $attachment
  176. * @throws Exception
  177. */
  178. public function setAttachment($attachment)
  179. {
  180. if (!$this->_conf->getKey('fileupload') || !Sjcl::isValid($attachment)) {
  181. throw new Exception('Invalid attachment.', 71);
  182. }
  183. $this->_data->meta->attachment = $attachment;
  184. }
  185. /**
  186. * Set paste's attachment name.
  187. *
  188. * @access public
  189. * @param string $attachmentname
  190. * @throws Exception
  191. */
  192. public function setAttachmentName($attachmentname)
  193. {
  194. if (!$this->_conf->getKey('fileupload') || !Sjcl::isValid($attachmentname)) {
  195. throw new Exception('Invalid attachment.', 72);
  196. }
  197. $this->_data->meta->attachmentname = $attachmentname;
  198. }
  199. /**
  200. * Set paste expiration.
  201. *
  202. * @access public
  203. * @param string $expiration
  204. */
  205. public function setExpiration($expiration)
  206. {
  207. $expire_options = $this->_conf->getSection('expire_options');
  208. if (array_key_exists($expiration, $expire_options)) {
  209. $expire = $expire_options[$expiration];
  210. } else {
  211. // using getKey() to ensure a default value is present
  212. $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  213. }
  214. if ($expire > 0) {
  215. $this->_data->meta->expire_date = time() + $expire;
  216. }
  217. }
  218. /**
  219. * Set paste's burn-after-reading type.
  220. *
  221. * @access public
  222. * @param string $burnafterreading
  223. * @throws Exception
  224. */
  225. public function setBurnafterreading($burnafterreading = '1')
  226. {
  227. if ($burnafterreading === '0') {
  228. $this->_data->meta->burnafterreading = false;
  229. } else {
  230. if ($burnafterreading !== '1') {
  231. throw new Exception('Invalid data.', 73);
  232. }
  233. $this->_data->meta->burnafterreading = true;
  234. $this->_data->meta->opendiscussion = false;
  235. }
  236. }
  237. /**
  238. * Set paste's discussion state.
  239. *
  240. * @access public
  241. * @param string $opendiscussion
  242. * @throws Exception
  243. */
  244. public function setOpendiscussion($opendiscussion = '1')
  245. {
  246. if (
  247. !$this->_conf->getKey('discussion') ||
  248. $this->isBurnafterreading() ||
  249. $opendiscussion === '0'
  250. ) {
  251. $this->_data->meta->opendiscussion = false;
  252. } else {
  253. if ($opendiscussion !== '1') {
  254. throw new Exception('Invalid data.', 74);
  255. }
  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. */
  266. public function setFormatter($format)
  267. {
  268. if (!array_key_exists($format, $this->_conf->getSection('formatter_options'))) {
  269. $format = $this->_conf->getKey('defaultformatter');
  270. }
  271. $this->_data->meta->formatter = $format;
  272. }
  273. /**
  274. * Check if paste is of burn-after-reading type.
  275. *
  276. * @access public
  277. * @throws Exception
  278. * @return bool
  279. */
  280. public function isBurnafterreading()
  281. {
  282. if (!property_exists($this->_data, 'data')) {
  283. $this->get();
  284. }
  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 bool
  294. */
  295. public function isOpendiscussion()
  296. {
  297. if (!property_exists($this->_data, 'data')) {
  298. $this->get();
  299. }
  300. return property_exists($this->_data->meta, 'opendiscussion') &&
  301. $this->_data->meta->opendiscussion === true;
  302. }
  303. }