comment.php 4.8 KB

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