Comment.php 4.8 KB

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