paste.php 8.4 KB

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