paste.php 7.8 KB

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