Paste.php 8.7 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 0.22
  11. */
  12. namespace PrivateBin\Model;
  13. use PrivateBin\PrivateBin;
  14. use PrivateBin\Persistence\ServerSalt;
  15. use PrivateBin\Sjcl;
  16. use Exception;
  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. $this->_data = $this->_store->read($this->getId());
  34. if ($this->_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($this->_data->meta, 'expire_date')) {
  39. if ($this->_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. $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. // support < 0.21 syntax highlighting
  49. if (property_exists($this->_data->meta, 'syntaxcoloring') && $this->_data->meta->syntaxcoloring === true) {
  50. $this->_data->meta->formatter = 'syntaxhighlighting';
  51. } else {
  52. $this->_data->meta->formatter = $this->_conf->getKey('defaultformatter');
  53. }
  54. }
  55. // support old paste format with server wide salt
  56. if (!property_exists($this->_data->meta, 'salt')) {
  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. }
  78. $this->_data->meta->postdate = time();
  79. $this->_data->meta->salt = serversalt::generate();
  80. // store paste
  81. if (
  82. $this->_store->create(
  83. $this->getId(),
  84. json_decode(json_encode($this->_data), true)
  85. ) === false
  86. ) {
  87. throw new Exception('Error saving paste. Sorry.', 76);
  88. }
  89. }
  90. /**
  91. * Delete the paste.
  92. *
  93. * @access public
  94. * @throws Exception
  95. * @return void
  96. */
  97. public function delete()
  98. {
  99. $this->_store->delete($this->getId());
  100. }
  101. /**
  102. * Test if paste exists in store.
  103. *
  104. * @access public
  105. * @return bool
  106. */
  107. public function exists()
  108. {
  109. return $this->_store->exists($this->getId());
  110. }
  111. /**
  112. * Get a comment, optionally a specific instance.
  113. *
  114. * @access public
  115. * @param string $parentId
  116. * @param string $commentId
  117. * @throws Exception
  118. * @return Comment
  119. */
  120. public function getComment($parentId, $commentId = null)
  121. {
  122. if (!$this->exists()) {
  123. throw new Exception('Invalid data.', 62);
  124. }
  125. $comment = new Comment($this->_conf, $this->_store);
  126. $comment->setPaste($this);
  127. $comment->setParentId($parentId);
  128. if ($commentId !== null) {
  129. $comment->setId($commentId);
  130. }
  131. return $comment;
  132. }
  133. /**
  134. * Get all comments, if any.
  135. *
  136. * @access public
  137. * @return array
  138. */
  139. public function getComments()
  140. {
  141. return $this->_store->readComments($this->getId());
  142. }
  143. /**
  144. * Generate the "delete" token.
  145. *
  146. * The token is the hmac of the pastes ID signed with the server salt.
  147. * The paste can be deleted by calling:
  148. * http://example.com/privatebin/?pasteid=<pasteid>&deletetoken=<deletetoken>
  149. *
  150. * @access public
  151. * @return string
  152. */
  153. public function getDeleteToken()
  154. {
  155. if (!property_exists($this->_data->meta, 'salt')) {
  156. $this->get();
  157. }
  158. return hash_hmac(
  159. $this->_conf->getKey('zerobincompatibility') ? 'sha1' : 'sha256',
  160. $this->getId(),
  161. $this->_data->meta->salt
  162. );
  163. }
  164. /**
  165. * Set paste's attachment.
  166. *
  167. * @access public
  168. * @param string $attachment
  169. * @throws Exception
  170. * @return void
  171. */
  172. public function setAttachment($attachment)
  173. {
  174. if (!$this->_conf->getKey('fileupload') || !Sjcl::isValid($attachment)) {
  175. throw new Exception('Invalid attachment.', 71);
  176. }
  177. $this->_data->meta->attachment = $attachment;
  178. }
  179. /**
  180. * Set paste's attachment name.
  181. *
  182. * @access public
  183. * @param string $attachmentname
  184. * @throws Exception
  185. * @return void
  186. */
  187. public function setAttachmentName($attachmentname)
  188. {
  189. if (!$this->_conf->getKey('fileupload') || !Sjcl::isValid($attachmentname)) {
  190. throw new Exception('Invalid attachment.', 72);
  191. }
  192. $this->_data->meta->attachmentname = $attachmentname;
  193. }
  194. /**
  195. * Set paste expiration.
  196. *
  197. * @access public
  198. * @param string $expiration
  199. * @return void
  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. * @return void
  221. */
  222. public function setBurnafterreading($burnafterreading = '1')
  223. {
  224. if ($burnafterreading === '0') {
  225. $this->_data->meta->burnafterreading = false;
  226. } else {
  227. if ($burnafterreading !== '1') {
  228. throw new Exception('Invalid data.', 73);
  229. }
  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. $this->_data->meta->opendiscussion = false;
  250. } else {
  251. if ($opendiscussion !== '1') {
  252. throw new Exception('Invalid data.', 74);
  253. }
  254. $this->_data->meta->opendiscussion = true;
  255. }
  256. }
  257. /**
  258. * Set paste's format.
  259. *
  260. * @access public
  261. * @param string $format
  262. * @throws Exception
  263. * @return void
  264. */
  265. public function setFormatter($format)
  266. {
  267. if (!array_key_exists($format, $this->_conf->getSection('formatter_options'))) {
  268. $format = $this->_conf->getKey('defaultformatter');
  269. }
  270. $this->_data->meta->formatter = $format;
  271. }
  272. /**
  273. * Check if paste is of burn-after-reading type.
  274. *
  275. * @access public
  276. * @throws Exception
  277. * @return boolean
  278. */
  279. public function isBurnafterreading()
  280. {
  281. if (!property_exists($this->_data, 'data')) {
  282. $this->get();
  283. }
  284. return property_exists($this->_data->meta, 'burnafterreading') &&
  285. $this->_data->meta->burnafterreading === true;
  286. }
  287. /**
  288. * Check if paste has discussions enabled.
  289. *
  290. * @access public
  291. * @throws Exception
  292. * @return boolean
  293. */
  294. public function isOpendiscussion()
  295. {
  296. if (!property_exists($this->_data, 'data')) {
  297. $this->get();
  298. }
  299. return property_exists($this->_data->meta, 'opendiscussion') &&
  300. $this->_data->meta->opendiscussion === true;
  301. }
  302. }