comment.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_comment
  14. *
  15. * Model of a ZeroBin comment.
  16. */
  17. class model_comment extends model_abstract
  18. {
  19. /**
  20. * Instance's parent.
  21. *
  22. * @access private
  23. * @var model_paste
  24. */
  25. private $_paste;
  26. /**
  27. * Get comment data.
  28. *
  29. * @access public
  30. * @throws Exception
  31. * @return stdObject
  32. */
  33. public function get()
  34. {
  35. // @todo add support to read specific comment
  36. $comments = $this->_store->readComments($this->getPaste()->getId());
  37. foreach ($comments as $comment) {
  38. if (
  39. $comment->parentid == $this->getParentId() &&
  40. $comment->id == $this->getId()
  41. ) {
  42. $this->_data = $comment;
  43. break;
  44. }
  45. }
  46. return $this->_data;
  47. }
  48. /**
  49. * Store the comment's data.
  50. *
  51. * @access public
  52. * @throws Exception
  53. * @return void
  54. */
  55. public function store()
  56. {
  57. // Make sure paste exists.
  58. $pasteid = $this->getPaste()->getId();
  59. if (!$this->getPaste()->exists())
  60. throw new Exception('Invalid data.', 67);
  61. // Make sure the discussion is opened in this paste and in configuration.
  62. if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion'))
  63. throw new Exception('Invalid data.', 68);
  64. // Check for improbable collision.
  65. if ($this->exists())
  66. throw new Exception('You are unlucky. Try again.', 69);
  67. $this->_data->meta->postdate = time();
  68. // store comment
  69. if (
  70. $this->_store->createComment(
  71. $pasteid,
  72. $this->getParentId(),
  73. $this->getId(),
  74. json_decode(json_encode($this->_data), true)
  75. ) === false
  76. ) throw new Exception('Error saving comment. Sorry.', 70);
  77. }
  78. /**
  79. * Delete the comment.
  80. *
  81. * @access public
  82. * @throws Exception
  83. * @return void
  84. */
  85. public function delete()
  86. {
  87. throw new Exception('To delete a comment, delete its parent paste', 64);
  88. }
  89. /**
  90. * Test if comment exists in store.
  91. *
  92. * @access public
  93. * @return bool
  94. */
  95. public function exists()
  96. {
  97. return $this->_store->existsComment(
  98. $this->getPaste()->getId(),
  99. $this->getParentId(),
  100. $this->getId()
  101. );
  102. }
  103. /**
  104. * Set paste.
  105. *
  106. * @access public
  107. * @param model_paste $paste
  108. * @throws Exception
  109. * @return void
  110. */
  111. public function setPaste(model_paste $paste)
  112. {
  113. $this->_paste = $paste;
  114. $this->_data->meta->pasteid = $paste->getId();
  115. }
  116. /**
  117. * Get paste.
  118. *
  119. * @access public
  120. * @return model_paste
  121. */
  122. public function getPaste()
  123. {
  124. return $this->_paste;
  125. }
  126. /**
  127. * Set parent ID.
  128. *
  129. * @access public
  130. * @param string $id
  131. * @throws Exception
  132. * @return void
  133. */
  134. public function setParentId($id)
  135. {
  136. if (!self::isValidId($id)) throw new Exception('Invalid paste ID.', 65);
  137. $this->_data->meta->parentid = $id;
  138. }
  139. /**
  140. * Get parent ID.
  141. *
  142. * @access public
  143. * @return string
  144. */
  145. public function getParentId()
  146. {
  147. if (!property_exists($this->_data->meta, 'parentid')) $this->_data->meta->parentid = '';
  148. return $this->_data->meta->parentid;
  149. }
  150. /**
  151. * Set nickname.
  152. *
  153. * @access public
  154. * @param string $nickname
  155. * @throws Exception
  156. * @return void
  157. */
  158. public function setNickname($nickname)
  159. {
  160. if (!sjcl::isValid($nickname)) throw new Exception('Invalid data.', 66);
  161. $this->_data->meta->nickname = $nickname;
  162. // Generation of the anonymous avatar (Vizhash):
  163. // If a nickname is provided, we generate a Vizhash.
  164. // (We assume that if the user did not enter a nickname, he/she wants
  165. // to be anonymous and we will not generate the vizhash.)
  166. $vh = new vizhash16x16();
  167. $pngdata = $vh->generate(trafficlimiter::getIp());
  168. if ($pngdata != '')
  169. {
  170. $this->_data->meta->vizhash = 'data:image/png;base64,' . base64_encode($pngdata);
  171. }
  172. // Once the avatar is generated, we do not keep the IP address, nor its hash.
  173. }
  174. }