paste.php 8.0 KB

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