Paste.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.0
  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. // set formatter for for the view.
  47. if (!property_exists($data->meta, 'formatter')) {
  48. // support < 0.21 syntax highlighting
  49. if (property_exists($data->meta, 'syntaxcoloring') && $data->meta->syntaxcoloring === true) {
  50. $data->meta->formatter = 'syntaxhighlighting';
  51. } else {
  52. $data->meta->formatter = $this->_conf->getKey('defaultformatter');
  53. }
  54. }
  55. // support old paste format with server wide salt
  56. if (!property_exists($data->meta, 'salt')) {
  57. $data->meta->salt = ServerSalt::get();
  58. }
  59. $data->comments = array_values($this->getComments());
  60. $data->comment_count = count($data->comments);
  61. $data->comment_offset = 0;
  62. $data->{'@context'} = 'js/paste.jsonld';
  63. $this->_data = $data;
  64. return $this->_data;
  65. }
  66. /**
  67. * Store the paste's data.
  68. *
  69. * @access public
  70. * @throws Exception
  71. * @return void
  72. */
  73. public function store()
  74. {
  75. // Check for improbable collision.
  76. if ($this->exists()) {
  77. throw new Exception('You are unlucky. Try again.', 75);
  78. }
  79. $this->_data->meta->postdate = time();
  80. $this->_data->meta->salt = serversalt::generate();
  81. // store paste
  82. if (
  83. $this->_store->create(
  84. $this->getId(),
  85. json_decode(json_encode($this->_data), true)
  86. ) === false
  87. ) {
  88. throw new Exception('Error saving paste. Sorry.', 76);
  89. }
  90. }
  91. /**
  92. * Delete the paste.
  93. *
  94. * @access public
  95. * @throws Exception
  96. * @return void
  97. */
  98. public function delete()
  99. {
  100. $this->_store->delete($this->getId());
  101. }
  102. /**
  103. * Test if paste exists in store.
  104. *
  105. * @access public
  106. * @return bool
  107. */
  108. public function exists()
  109. {
  110. return $this->_store->exists($this->getId());
  111. }
  112. /**
  113. * Get a comment, optionally a specific instance.
  114. *
  115. * @access public
  116. * @param string $parentId
  117. * @param string $commentId
  118. * @throws Exception
  119. * @return Comment
  120. */
  121. public function getComment($parentId, $commentId = null)
  122. {
  123. if (!$this->exists()) {
  124. throw new Exception('Invalid data.', 62);
  125. }
  126. $comment = new Comment($this->_conf, $this->_store);
  127. $comment->setPaste($this);
  128. $comment->setParentId($parentId);
  129. if ($commentId !== null) {
  130. $comment->setId($commentId);
  131. }
  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')) {
  157. $this->get();
  158. }
  159. return hash_hmac(
  160. $this->_conf->getKey('zerobincompatibility') ? 'sha1' : 'sha256',
  161. $this->getId(),
  162. $this->_data->meta->salt
  163. );
  164. }
  165. /**
  166. * Set paste's attachment.
  167. *
  168. * @access public
  169. * @param string $attachment
  170. * @throws Exception
  171. * @return void
  172. */
  173. public function setAttachment($attachment)
  174. {
  175. if (!$this->_conf->getKey('fileupload') || !Sjcl::isValid($attachment)) {
  176. throw new Exception('Invalid attachment.', 71);
  177. }
  178. $this->_data->meta->attachment = $attachment;
  179. }
  180. /**
  181. * Set paste's attachment name.
  182. *
  183. * @access public
  184. * @param string $attachmentname
  185. * @throws Exception
  186. * @return void
  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. * @return void
  201. */
  202. public function setExpiration($expiration)
  203. {
  204. $expire_options = $this->_conf->getSection('expire_options');
  205. if (array_key_exists($expiration, $expire_options)) {
  206. $expire = $expire_options[$expiration];
  207. } else {
  208. // using getKey() to ensure a default value is present
  209. $expire = $this->_conf->getKey($this->_conf->getKey('default', 'expire'), 'expire_options');
  210. }
  211. if ($expire > 0) {
  212. $this->_data->meta->expire_date = time() + $expire;
  213. }
  214. }
  215. /**
  216. * Set paste's burn-after-reading type.
  217. *
  218. * @access public
  219. * @param string $burnafterreading
  220. * @throws Exception
  221. * @return void
  222. */
  223. public function setBurnafterreading($burnafterreading = '1')
  224. {
  225. if ($burnafterreading === '0') {
  226. $this->_data->meta->burnafterreading = false;
  227. } else {
  228. if ($burnafterreading !== '1') {
  229. throw new Exception('Invalid data.', 73);
  230. }
  231. $this->_data->meta->burnafterreading = true;
  232. $this->_data->meta->opendiscussion = false;
  233. }
  234. }
  235. /**
  236. * Set paste's discussion state.
  237. *
  238. * @access public
  239. * @param string $opendiscussion
  240. * @throws Exception
  241. * @return void
  242. */
  243. public function setOpendiscussion($opendiscussion = '1')
  244. {
  245. if (
  246. !$this->_conf->getKey('discussion') ||
  247. $this->isBurnafterreading() ||
  248. $opendiscussion === '0'
  249. ) {
  250. $this->_data->meta->opendiscussion = false;
  251. } else {
  252. if ($opendiscussion !== '1') {
  253. throw new Exception('Invalid data.', 74);
  254. }
  255. $this->_data->meta->opendiscussion = true;
  256. }
  257. }
  258. /**
  259. * Set paste's format.
  260. *
  261. * @access public
  262. * @param string $format
  263. * @throws Exception
  264. * @return void
  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. }