Post.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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\Pages;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Model;
  12. use ForkBB\Models\Page;
  13. use ForkBB\Models\Topic\Topic;
  14. use function \ForkBB\__;
  15. class Post extends Page
  16. {
  17. use PostFormTrait;
  18. use PostValidatorTrait;
  19. /**
  20. * Создание новой темы
  21. */
  22. public function newTopic(array $args, string $method): Page
  23. {
  24. $forum = $this->c->forums->get($args['id']);
  25. if (
  26. empty($forum)
  27. || $forum->redirect_url
  28. || ! $forum->canCreateTopic
  29. ) {
  30. return $this->c->Message->message('Bad request');
  31. }
  32. $this->c->Lang->load('post');
  33. if (1 === $this->c->config->b_poll_enabled) {
  34. $this->c->Lang->load('poll');
  35. }
  36. $this->onlinePos = 'forum-' . $forum->id;
  37. if ('POST' === $method) {
  38. $v = $this->messageValidator($forum, 'NewTopic', $args, false, true);
  39. if ($this->user->isGuest) {
  40. $v = $this->c->Test->beforeValidation($v);
  41. }
  42. if (
  43. $v->validation($_POST, $this->user->isGuest) //????
  44. && null === $v->preview
  45. && null !== $v->submit
  46. ) {
  47. return $this->endPost($forum, $v);
  48. }
  49. $this->fIswev = $v->getErrors();
  50. $args['_vars'] = $v->getData();
  51. if (
  52. null !== $v->preview
  53. && ! $v->getErrors()
  54. ) {
  55. $this->previewHtml = $this->c->censorship->censor(
  56. $this->c->Parser->parseMessage(null, (bool) $v->hide_smilies)
  57. );
  58. if (
  59. $this->user->usePoll
  60. && $v->poll_enable
  61. ) {
  62. $this->poll = $this->c->polls->create($v->poll);
  63. $this->c->polls->revision($this->poll, true);
  64. }
  65. }
  66. }
  67. $this->nameTpl = 'post';
  68. $this->canonical = $this->c->Router->link(
  69. 'NewTopic',
  70. [
  71. 'id' => $forum->id,
  72. ]
  73. );
  74. $this->robots = 'noindex';
  75. $this->formTitle = 'Post new topic';
  76. $this->crumbs = $this->crumbs($this->formTitle, $forum);
  77. $this->form = $this->messageForm($forum, 'NewTopic', $args, false, true, false);
  78. return $this;
  79. }
  80. /**
  81. * Подготовка данных для шаблона создания сообщения
  82. */
  83. public function newReply(array $args, string $method): Page
  84. {
  85. $topic = $this->c->topics->load($args['id']);
  86. if (
  87. ! $topic instanceof Topic
  88. || ! $topic->canReply
  89. ) {
  90. return $this->c->Message->message('Bad request');
  91. }
  92. $this->c->Lang->load('post');
  93. $this->onlinePos = 'topic-' . $topic->id;
  94. if ('POST' === $method) {
  95. $v = $this->messageValidator($topic, 'NewReply', $args, false, false);
  96. if ($this->user->isGuest) {
  97. $v = $this->c->Test->beforeValidation($v);
  98. }
  99. if (
  100. $v->validation($_POST, $this->user->isGuest) //????
  101. && null === $v->preview
  102. && null !== $v->submit
  103. ) {
  104. return $this->endPost($topic, $v);
  105. }
  106. $this->fIswev = $v->getErrors();
  107. $args['_vars'] = $v->getData(); //????
  108. if (
  109. null !== $v->preview
  110. && ! $v->getErrors()
  111. ) {
  112. $this->previewHtml = $this->c->Parser->parseMessage(null, (bool) $v->hide_smilies);
  113. }
  114. } elseif (isset($args['quote'])) {
  115. $post = $this->c->posts->load($args['quote'], $topic->id);
  116. if (empty($post)) {
  117. return $this->c->Message->message('Bad request');
  118. }
  119. $args['_vars'] = [
  120. 'message' => "[quote=\"{$post->poster}\"]{$post->message}[/quote]",
  121. ];
  122. unset($args['quote']);
  123. }
  124. $this->nameTpl = 'post';
  125. $this->canonical = $this->c->Router->link(
  126. 'NewReply',
  127. [
  128. 'id' => $topic->id,
  129. ]
  130. );
  131. $this->robots = 'noindex';
  132. $this->formTitle = 'Post a reply';
  133. $this->crumbs = $this->crumbs($this->formTitle, $topic);
  134. $this->form = $this->messageForm($topic, 'NewReply', $args, false, false, false);
  135. $this->postsTitle = 'Topic review';
  136. $this->posts = $topic->review();
  137. return $this;
  138. }
  139. /**
  140. * Создание темы/сообщения
  141. */
  142. protected function endPost(Model $model, Validator $v): Page
  143. {
  144. $this->c->Online->calc($this); // для подписок
  145. $now = \time();
  146. $username = $this->user->isGuest ? $v->username : $this->user->username;
  147. $merge = false;
  148. $executive = $this->user->isAdmin || $this->user->isModerator($model);
  149. // подготовка к объединению/сохранению сообщения
  150. if (null === $v->subject) {
  151. $createTopic = false;
  152. $forum = $model->parent;
  153. $topic = $model;
  154. if (
  155. ! $this->user->isGuest
  156. && $topic->last_poster_id === $this->user->id
  157. ) {
  158. if ($executive) {
  159. if ($v->merge_post) {
  160. $merge = true;
  161. }
  162. } else {
  163. $merge = true;
  164. }
  165. }
  166. // создание темы
  167. } else {
  168. $createTopic = true;
  169. $forum = $model;
  170. $topic = $this->c->topics->create();
  171. $topic->subject = $v->subject;
  172. $topic->poster = $username;
  173. $topic->poster_id = $this->user->id;
  174. $topic->last_poster = $username;
  175. $topic->last_poster_id = $this->user->id;
  176. $topic->posted = $now;
  177. $topic->last_post = $now;
  178. $topic->sticky = $v->stick_topic ? 1 : 0;
  179. $topic->stick_fp = $v->stick_fp ? 1 : 0;
  180. $this->c->topics->insert($topic);
  181. }
  182. // попытка объеденить новое сообщение с крайним в теме
  183. if ($merge) {
  184. $lastPost = $this->c->posts->load($topic->last_post_id, $topic->id);
  185. $newLength = \mb_strlen($lastPost->message . $v->message, 'UTF-8');
  186. if ($newLength < $this->c->MAX_POST_SIZE - 100) {
  187. $lastPost->message = $lastPost->message . "\n[after=" . ($now - $topic->last_post) . "]\n" . $v->message; //????
  188. $lastPost->edited = $now;
  189. $lastPost->editor = $username; // ???? может копировать из poster
  190. $lastPost->editor_id = $this->user->id; // ???? может копировать из poster_id
  191. $this->c->posts->update($lastPost);
  192. } else {
  193. $merge = false;
  194. }
  195. }
  196. // создание нового сообщения
  197. if (! $merge) {
  198. $post = $this->c->posts->create();
  199. $post->poster = $username;
  200. $post->poster_id = $this->user->id;
  201. $post->poster_ip = $this->user->ip;
  202. $post->poster_email = (string) $v->email;
  203. $post->message = $v->message; //?????
  204. $post->hide_smilies = $v->hide_smilies ? 1 : 0;
  205. # $post->edit_post =
  206. $post->posted = $now;
  207. # $post->edited =
  208. # $post->editor =
  209. # $post->editor_id =
  210. $post->user_agent = \mb_substr($this->user->userAgent, 0, 255, 'UTF-8');
  211. $post->topic_id = $topic->id;
  212. $this->c->posts->insert($post);
  213. }
  214. if ($createTopic) {
  215. $topic->forum_id = $forum->id;
  216. $topic->first_post_id = $post->id;
  217. if (
  218. $this->user->usePoll
  219. && $v->poll_enable
  220. ) {
  221. $topic->poll_type = $v->poll['duration'] > 0 ? 1000 + $v->poll['duration'] : 1; // ???? перенести в модель poll?
  222. $topic->poll_time = $now;
  223. $topic->poll_term = $v->poll['hide_result'] ? $this->c->config->i_poll_term : 0;
  224. $poll = $this->c->polls->create([
  225. 'tid' => $topic->id,
  226. 'question' => $v->poll['question'],
  227. 'answer' => $v->poll['answer'],
  228. 'type' => $v->poll['type'],
  229. ]);
  230. $this->c->polls->insert($poll);
  231. }
  232. }
  233. // обновление данных в теме и разделе
  234. $this->c->topics->update($topic->calcStat());
  235. $this->c->forums->update($forum->calcStat());
  236. // обновление данных текущего пользователя
  237. if (
  238. ! $merge
  239. && ! $this->user->isGuest
  240. ) {
  241. if (0 == $forum->no_sum_mess) {
  242. $this->user->num_posts = $this->user->num_posts + 1;
  243. if (
  244. $this->user->g_promote_next_group > 0
  245. && $this->user->num_posts >= $this->user->g_promote_min_posts
  246. ) {
  247. $this->user->group_id = $this->user->g_promote_next_group;
  248. }
  249. }
  250. if ($createTopic) {
  251. $this->user->num_topics = $this->user->num_topics + 1;
  252. }
  253. }
  254. $this->user->last_post = $now;
  255. $this->c->users->update($this->user);
  256. if (1 === $this->c->config->b_topic_subscriptions) { // ????
  257. if (
  258. $v->subscribe
  259. && ! $topic->is_subscribed
  260. ) {
  261. $this->c->subscriptions->subscribe($this->user, $topic);
  262. } elseif (
  263. ! $v->subscribe
  264. && $topic->is_subscribed
  265. ) {
  266. $this->c->subscriptions->unsubscribe($this->user, $topic);
  267. }
  268. }
  269. if ($merge) {
  270. $this->c->search->index($lastPost, 'merge');
  271. } else {
  272. $this->c->search->index($post);
  273. if ($createTopic) {
  274. if (1 === $this->c->config->b_forum_subscriptions) { // ????
  275. $this->c->subscriptions->send($post, $topic);
  276. }
  277. } else {
  278. if (1 === $this->c->config->b_topic_subscriptions) { // ????
  279. $this->c->subscriptions->send($post);
  280. }
  281. }
  282. }
  283. return $this->c->Redirect->url($merge ? $lastPost->link : $post->link)->message('Post redirect');
  284. }
  285. }