Comment.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php declare(strict_types=1);
  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. */
  11. namespace PrivateBin\Model;
  12. use Exception;
  13. use Identicon\Identicon;
  14. use Jdenticon\Identicon as Jdenticon;
  15. use PrivateBin\Persistence\TrafficLimiter;
  16. use PrivateBin\Vizhash16x16;
  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. * Store the comment's data.
  33. *
  34. * @access public
  35. * @throws Exception
  36. */
  37. public function store()
  38. {
  39. // Make sure paste exists.
  40. $pasteid = $this->getPaste()->getId();
  41. if (!$this->getPaste()->exists()) {
  42. throw new Exception('Invalid data.', 67);
  43. }
  44. // Make sure the discussion is opened in this paste and allowed in the configuration.
  45. if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) {
  46. throw new Exception('Invalid data.', 68);
  47. }
  48. // Check for improbable collision.
  49. if ($this->exists()) {
  50. throw new Exception('You are unlucky. Try again.', 69);
  51. }
  52. $this->_data['meta']['created'] = time();
  53. // store comment
  54. if (
  55. $this->_store->createComment(
  56. $pasteid,
  57. $this->getParentId(),
  58. $this->getId(),
  59. $this->_data
  60. ) === false
  61. ) {
  62. throw new Exception('Error saving comment. Sorry.', 70);
  63. }
  64. }
  65. /**
  66. * Test if comment exists in store.
  67. *
  68. * @access public
  69. * @return bool
  70. */
  71. public function exists()
  72. {
  73. return $this->_store->existsComment(
  74. $this->getPaste()->getId(),
  75. $this->getParentId(),
  76. $this->getId()
  77. );
  78. }
  79. /**
  80. * Set paste.
  81. *
  82. * @access public
  83. * @param Paste $paste
  84. * @throws Exception
  85. */
  86. public function setPaste(Paste &$paste)
  87. {
  88. $this->_paste = $paste;
  89. $this->_data['pasteid'] = $paste->getId();
  90. }
  91. /**
  92. * Get paste.
  93. *
  94. * @access public
  95. * @return Paste
  96. */
  97. public function getPaste()
  98. {
  99. return $this->_paste;
  100. }
  101. /**
  102. * Set parent ID.
  103. *
  104. * @access public
  105. * @param string $id
  106. * @throws Exception
  107. */
  108. public function setParentId($id)
  109. {
  110. if (!self::isValidId($id)) {
  111. throw new Exception('Invalid document ID.', 65);
  112. }
  113. $this->_data['parentid'] = $id;
  114. }
  115. /**
  116. * Get parent ID.
  117. *
  118. * @access public
  119. * @return string
  120. */
  121. public function getParentId()
  122. {
  123. if (!array_key_exists('parentid', $this->_data)) {
  124. $this->_data['parentid'] = $this->getPaste()->getId();
  125. }
  126. return $this->_data['parentid'];
  127. }
  128. /**
  129. * Sanitizes data to conform with current configuration.
  130. *
  131. * @access protected
  132. * @param array $data
  133. */
  134. protected function _sanitize(array &$data)
  135. {
  136. // we generate an icon based on a SHA512 HMAC of the users IP, if configured
  137. $icon = $this->_conf->getKey('icon');
  138. if ($icon != 'none') {
  139. $pngdata = '';
  140. $hmac = TrafficLimiter::getHash();
  141. if ($icon == 'identicon') {
  142. $identicon = new Identicon();
  143. $pngdata = $identicon->getImageDataUri($hmac, 16);
  144. } elseif ($icon == 'jdenticon') {
  145. $jdenticon = new Jdenticon(array(
  146. 'hash' => $hmac,
  147. 'size' => 16,
  148. 'style' => array(
  149. 'backgroundColor' => '#fff0', // fully transparent, for dark mode
  150. 'padding' => 0,
  151. ),
  152. ));
  153. $pngdata = $jdenticon->getImageDataUri('png');
  154. } elseif ($icon == 'vizhash') {
  155. $vh = new Vizhash16x16();
  156. $pngdata = 'data:image/png;base64,' . base64_encode(
  157. $vh->generate($hmac)
  158. );
  159. }
  160. if ($pngdata != '') {
  161. if (!array_key_exists('meta', $data)) {
  162. $data['meta'] = array();
  163. }
  164. $data['meta']['icon'] = $pngdata;
  165. }
  166. }
  167. }
  168. }