1
0

Comment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 PrivateBin\Sjcl;
  14. use PrivateBin\Persistence\TrafficLimiter;
  15. use PrivateBin\Vizhash16x16;
  16. use Exception;
  17. /**
  18. * 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 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. }
  67. // Make sure the discussion is opened in this paste and in configuration.
  68. if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) {
  69. throw new Exception('Invalid data.', 68);
  70. }
  71. // Check for improbable collision.
  72. if ($this->exists()) {
  73. throw new Exception('You are unlucky. Try again.', 69);
  74. }
  75. $this->_data->meta->postdate = time();
  76. // store comment
  77. if (
  78. $this->_store->createComment(
  79. $pasteid,
  80. $this->getParentId(),
  81. $this->getId(),
  82. json_decode(json_encode($this->_data), true)
  83. ) === false
  84. ) {
  85. throw new Exception('Error saving comment. Sorry.', 70);
  86. }
  87. }
  88. /**
  89. * Delete the comment.
  90. *
  91. * @access public
  92. * @throws Exception
  93. * @return void
  94. */
  95. public function delete()
  96. {
  97. throw new Exception('To delete a comment, delete its parent paste', 64);
  98. }
  99. /**
  100. * Test if comment exists in store.
  101. *
  102. * @access public
  103. * @return bool
  104. */
  105. public function exists()
  106. {
  107. return $this->_store->existsComment(
  108. $this->getPaste()->getId(),
  109. $this->getParentId(),
  110. $this->getId()
  111. );
  112. }
  113. /**
  114. * Set paste.
  115. *
  116. * @access public
  117. * @param Paste $paste
  118. * @throws Exception
  119. * @return void
  120. */
  121. public function setPaste(Paste $paste)
  122. {
  123. $this->_paste = $paste;
  124. $this->_data->meta->pasteid = $paste->getId();
  125. }
  126. /**
  127. * Get paste.
  128. *
  129. * @access public
  130. * @return Paste
  131. */
  132. public function getPaste()
  133. {
  134. return $this->_paste;
  135. }
  136. /**
  137. * Set parent ID.
  138. *
  139. * @access public
  140. * @param string $id
  141. * @throws Exception
  142. * @return void
  143. */
  144. public function setParentId($id)
  145. {
  146. if (!self::isValidId($id)) {
  147. throw new Exception('Invalid paste ID.', 65);
  148. }
  149. $this->_data->meta->parentid = $id;
  150. }
  151. /**
  152. * Get parent ID.
  153. *
  154. * @access public
  155. * @return string
  156. */
  157. public function getParentId()
  158. {
  159. if (!property_exists($this->_data->meta, 'parentid')) {
  160. $this->_data->meta->parentid = '';
  161. }
  162. return $this->_data->meta->parentid;
  163. }
  164. /**
  165. * Set nickname.
  166. *
  167. * @access public
  168. * @param string $nickname
  169. * @throws Exception
  170. * @return void
  171. */
  172. public function setNickname($nickname)
  173. {
  174. if (!Sjcl::isValid($nickname)) {
  175. throw new Exception('Invalid data.', 66);
  176. }
  177. $this->_data->meta->nickname = $nickname;
  178. if ($this->_conf->getKey('vizhash')) {
  179. // Generation of the anonymous avatar (Vizhash):
  180. // If a nickname is provided, we generate a Vizhash.
  181. // (We assume that if the user did not enter a nickname, he/she wants
  182. // to be anonymous and we will not generate the vizhash.)
  183. $vh = new Vizhash16x16();
  184. $pngdata = $vh->generate(TrafficLimiter::getIp());
  185. if ($pngdata != '') {
  186. $this->_data->meta->vizhash = 'data:image/png;base64,' . base64_encode($pngdata);
  187. }
  188. // Once the avatar is generated, we do not keep the IP address, nor its hash.
  189. }
  190. }
  191. }