Post.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. */
  20. protected string $cKey = 'Post';
  21. /**
  22. * Получение родительской темы
  23. */
  24. protected function getparent(): ?Topic
  25. {
  26. if ($this->topic_id < 1) {
  27. throw new RuntimeException('Parent is not defined');
  28. }
  29. $topic = $this->c->topics->load($this->topic_id);
  30. if (
  31. ! $topic instanceof Topic
  32. || $topic->moved_to
  33. || ! $topic->parent instanceof Forum
  34. ) {
  35. return null;
  36. } else {
  37. return $topic;
  38. }
  39. }
  40. /**
  41. * Ссылка на сообщение
  42. */
  43. protected function getlink(): string
  44. {
  45. return $this->c->Router->link(
  46. 'ViewPost',
  47. [
  48. 'id' => $this->id,
  49. ]
  50. );
  51. }
  52. /**
  53. * Автор сообщения
  54. */
  55. protected function getuser(): User
  56. {
  57. if (
  58. $this->poster_id < 1
  59. || ! ($user = $this->c->users->load($this->poster_id)) instanceof User
  60. ) {
  61. $user = $this->c->users->guest([
  62. 'username' => $this->poster,
  63. 'email' => $this->poster_email,
  64. ]);
  65. }
  66. if (! $user instanceof User) {
  67. throw new RuntimeException("No user data in post number {$this->id}");
  68. }
  69. return $user;
  70. }
  71. /**
  72. * Статус возможности сигналить на сообщение
  73. */
  74. protected function getcanReport(): bool
  75. {
  76. return ! $this->c->user->isAdmin && ! $this->c->user->isGuest;
  77. }
  78. /**
  79. * Ссылка на страницу отправки сигнала
  80. */
  81. protected function getlinkReport(): string
  82. {
  83. return $this->c->Router->link(
  84. 'ReportPost',
  85. [
  86. 'id' => $this->id,
  87. ]
  88. );
  89. }
  90. /**
  91. * Статус возможности удаления
  92. */
  93. protected function getcanDelete(): bool
  94. {
  95. if ($this->c->user->isAdmin) {
  96. return true;
  97. } elseif (
  98. $this->c->user->isGuest
  99. || isset($this->c->admins->list[$this->poster_id]) // ???? или юзера проверять?
  100. ) {
  101. return false;
  102. } elseif ($this->c->user->isModerator($this)) {
  103. return true;
  104. } elseif (0 !== $this->parent->closed) {
  105. return false;
  106. }
  107. return $this->user->id === $this->c->user->id
  108. && (
  109. (
  110. $this->id == $this->parent->first_post_id
  111. && 1 === $this->c->user->g_delete_topics
  112. )
  113. || (
  114. $this->id != $this->parent->first_post_id
  115. && 1 === $this->c->user->g_delete_posts
  116. )
  117. )
  118. && (
  119. 0 === $this->c->user->g_deledit_interval
  120. || 1 === $this->edit_post
  121. || \time() - $this->posted < $this->c->user->g_deledit_interval
  122. );
  123. }
  124. /**
  125. * Ссылка на страницу удаления
  126. */
  127. protected function getlinkDelete(): string
  128. {
  129. return $this->c->Router->link(
  130. 'DeletePost',
  131. [
  132. 'id' => $this->id,
  133. ]
  134. );
  135. }
  136. /**
  137. * Статус возможности редактирования
  138. */
  139. protected function getcanEdit(): bool
  140. {
  141. if ($this->c->user->isAdmin) {
  142. return true;
  143. } elseif (
  144. $this->c->user->isGuest
  145. || isset($this->c->admins->list[$this->poster_id]) // ???? или юзера проверять?
  146. ) {
  147. return false;
  148. } elseif ($this->c->user->isModerator($this)) {
  149. return true;
  150. } elseif (0 !== $this->parent->closed) {
  151. return false;
  152. }
  153. return $this->user->id === $this->c->user->id
  154. && 1 === $this->c->user->g_edit_posts
  155. && (
  156. 0 === $this->c->user->g_deledit_interval
  157. || 1 === $this->edit_post
  158. || \time() - $this->posted < $this->c->user->g_deledit_interval
  159. || (
  160. $this->user->id === $this->editor_id
  161. && \time() - $this->edited < $this->c->user->g_deledit_interval
  162. )
  163. );
  164. }
  165. /**
  166. * Ссылка на страницу редактирования
  167. */
  168. protected function getlinkEdit(): string
  169. {
  170. return $this->c->Router->link(
  171. 'EditPost',
  172. [
  173. 'id' => $this->id,
  174. ]
  175. );
  176. }
  177. /**
  178. * Ссылка на страницу редактирования автора и даты
  179. */
  180. protected function getlinkAnD(): string
  181. {
  182. return $this->c->Router->link(
  183. 'ChangeAnD',
  184. [
  185. 'id' => $this->id,
  186. ]
  187. );
  188. }
  189. /**
  190. * Статус возможности ответа с цитированием
  191. */
  192. protected function getcanQuote(): bool
  193. {
  194. return $this->parent->canReply;
  195. }
  196. /**
  197. * Ссылка на страницу ответа с цитированием
  198. */
  199. protected function getlinkQuote(): string
  200. {
  201. return $this->c->Router->link(
  202. 'NewReply',
  203. [
  204. 'id' => $this->parent->id,
  205. 'quote' => $this->id,
  206. ]
  207. );
  208. }
  209. /**
  210. * Ссылка на страницу данных об ip
  211. */
  212. protected function getlinkGetHost(): string
  213. {
  214. return $this->c->Router->link(
  215. 'AdminHost',
  216. [
  217. 'ip' => $this->poster_ip,
  218. ]
  219. );
  220. }
  221. /**
  222. * HTML код сообщения
  223. */
  224. public function html(): string
  225. {
  226. return $this->c->censorship->censor($this->c->Parser->parseMessage($this->message, (bool) $this->hide_smilies));
  227. }
  228. }