Post.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Post;
  10. use ForkBB\Models\DataModel;
  11. use ForkBB\Models\Forum\Forum;
  12. use ForkBB\Models\Topic\Topic;
  13. use ForkBB\Models\User\User;
  14. use RuntimeException;
  15. class Post extends DataModel
  16. {
  17. /**
  18. * Ключ модели для контейнера
  19. * @var string
  20. */
  21. protected $cKey = 'Post';
  22. /**
  23. * Получение родительской темы
  24. */
  25. protected function getparent(): ?Topic
  26. {
  27. if ($this->topic_id < 1) {
  28. throw new RuntimeException('Parent is not defined');
  29. }
  30. $topic = $this->c->topics->load($this->topic_id);
  31. if (
  32. ! $topic instanceof Topic
  33. || $topic->moved_to
  34. || ! $topic->parent instanceof Forum
  35. ) {
  36. return null;
  37. } else {
  38. return $topic;
  39. }
  40. }
  41. /**
  42. * Ссылка на сообщение
  43. */
  44. protected function getlink(): string
  45. {
  46. return $this->c->Router->link(
  47. 'ViewPost',
  48. [
  49. 'id' => $this->id,
  50. ]
  51. );
  52. }
  53. /**
  54. * Автор сообщения
  55. */
  56. protected function getuser(): User
  57. {
  58. if (
  59. $this->poster_id < 1
  60. || ! ($user = $this->c->users->load($this->poster_id)) instanceof User
  61. ) {
  62. $user = $this->c->users->guest([
  63. 'username' => $this->poster,
  64. 'email' => $this->poster_email,
  65. ]);
  66. }
  67. if (! $user instanceof User) {
  68. throw new RuntimeException("No user data in post number {$this->id}");
  69. }
  70. return $user;
  71. }
  72. /**
  73. * Статус возможности сигналить на сообщение
  74. */
  75. protected function getcanReport(): bool
  76. {
  77. return ! $this->c->user->isAdmin && ! $this->c->user->isGuest;
  78. }
  79. /**
  80. * Ссылка на страницу отправки сигнала
  81. */
  82. protected function getlinkReport(): string
  83. {
  84. return $this->c->Router->link(
  85. 'ReportPost',
  86. [
  87. 'id' => $this->id,
  88. ]
  89. );
  90. }
  91. /**
  92. * Статус возможности удаления
  93. */
  94. protected function getcanDelete(): bool
  95. {
  96. if ($this->c->user->isAdmin) {
  97. return true;
  98. } elseif (
  99. $this->c->user->isGuest
  100. || isset($this->c->admins->list[$this->poster_id]) // ???? или юзера проверять?
  101. ) {
  102. return false;
  103. } elseif ($this->c->user->isModerator($this)) {
  104. return true;
  105. } elseif ('1' == $this->parent->closed) {
  106. return false;
  107. }
  108. return $this->user->id === $this->c->user->id
  109. && (
  110. (
  111. $this->id == $this->parent->first_post_id
  112. && 1 === $this->c->user->g_delete_topics
  113. )
  114. || (
  115. $this->id != $this->parent->first_post_id
  116. && 1 === $this->c->user->g_delete_posts
  117. )
  118. )
  119. && (
  120. '0' == $this->c->user->g_deledit_interval
  121. || '1' == $this->edit_post
  122. || \time() - $this->posted < $this->c->user->g_deledit_interval
  123. );
  124. }
  125. /**
  126. * Ссылка на страницу удаления
  127. */
  128. protected function getlinkDelete(): string
  129. {
  130. return $this->c->Router->link(
  131. 'DeletePost',
  132. [
  133. 'id' => $this->id,
  134. ]
  135. );
  136. }
  137. /**
  138. * Статус возможности редактирования
  139. */
  140. protected function getcanEdit(): bool
  141. {
  142. if ($this->c->user->isAdmin) {
  143. return true;
  144. } elseif (
  145. $this->c->user->isGuest
  146. || isset($this->c->admins->list[$this->poster_id]) // ???? или юзера проверять?
  147. ) {
  148. return false;
  149. } elseif ($this->c->user->isModerator($this)) {
  150. return true;
  151. } elseif ('1' == $this->parent->closed) {
  152. return false;
  153. }
  154. return $this->user->id === $this->c->user->id
  155. && 1 === $this->c->user->g_edit_posts
  156. && (
  157. '0' == $this->c->user->g_deledit_interval
  158. || '1' == $this->edit_post
  159. || \time() - $this->posted < $this->c->user->g_deledit_interval
  160. || (
  161. $this->user->id === $this->editor_id
  162. && \time() - $this->edited < $this->c->user->g_deledit_interval
  163. )
  164. );
  165. }
  166. /**
  167. * Ссылка на страницу редактирования
  168. */
  169. protected function getlinkEdit(): string
  170. {
  171. return $this->c->Router->link(
  172. 'EditPost',
  173. [
  174. 'id' => $this->id,
  175. ]
  176. );
  177. }
  178. /**
  179. * Статус возможности ответа с цитированием
  180. */
  181. protected function getcanQuote(): bool
  182. {
  183. return $this->parent->canReply;
  184. }
  185. /**
  186. * Ссылка на страницу ответа с цитированием
  187. */
  188. protected function getlinkQuote(): string
  189. {
  190. return $this->c->Router->link(
  191. 'NewReply',
  192. [
  193. 'id' => $this->parent->id,
  194. 'quote' => $this->id,
  195. ]
  196. );
  197. }
  198. /**
  199. * HTML код сообщения
  200. */
  201. public function html(): string
  202. {
  203. return $this->c->censorship->censor($this->c->Parser->parseMessage($this->message, (bool) $this->hide_smilies));
  204. }
  205. }