Report.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Exceptions\MailException;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Post\Post;
  13. use ForkBB\Models\Report\Report as ReportModel;
  14. use function \ForkBB\__;
  15. class Report extends Page
  16. {
  17. /**
  18. * Создание нового сигнала (репорта)
  19. */
  20. public function report(array $args, string $method): Page
  21. {
  22. $post = $this->c->posts->load($args['id']);
  23. if (! $post instanceof Post) {
  24. return $this->c->Message->message('Bad request');
  25. }
  26. $topic = $post->parent;
  27. $this->c->Lang->load('validator');
  28. $this->c->Lang->load('misc');
  29. $floodSize = \time() - (int) $this->user->last_report_sent;
  30. $floodSize = $floodSize < $this->user->g_report_flood ? $this->user->g_report_flood - $floodSize : 0;
  31. if ($floodSize > 0) {
  32. $this->fIswev = ['e', ['Flood message', $floodSize]];
  33. }
  34. $data = [];
  35. if ('POST' === $method) {
  36. $v = $this->c->Validator->reset()
  37. ->addValidators([
  38. ])->addRules([
  39. 'token' => 'token:ReportPost',
  40. 'reason' => 'required|string:trim,linebreaks|max:65000 bytes',
  41. 'report' => 'required|string',
  42. ])->addAliases([
  43. 'reason' => 'Reason',
  44. ])->addArguments([
  45. 'token' => $args,
  46. ])->addMessages([
  47. ]);
  48. if (
  49. $v->validation($_POST)
  50. && 0 === $floodSize
  51. ) {
  52. $report = $this->c->reports->create();
  53. $report->author = $this->user;
  54. $report->post = $post;
  55. $report->message = $v->reason;
  56. $result = true;
  57. switch ($this->c->config->i_report_method) {
  58. case 2:
  59. $this->c->reports->insert($report);
  60. case 1:
  61. try {
  62. $result = $this->sendReport($report);
  63. } catch (MailException $e) {
  64. $result = false;
  65. $this->c->Log->error('Report: MailException', [
  66. 'user' => $this->user->fLog(),
  67. 'exception' => $e,
  68. 'headers' => false,
  69. ]);
  70. }
  71. break;
  72. default:
  73. $this->c->reports->insert($report);
  74. break;
  75. }
  76. if ($this->user->g_report_flood > 0) {
  77. $this->user->last_report_sent = \time();
  78. $this->c->users->update($this->user);
  79. }
  80. if (false === $result && 1 === $this->c->config->i_report_method) {
  81. $this->fIswev = ['e', ['Error mail', $this->c->config->o_admin_email]];
  82. } else {
  83. return $this->c->Redirect->page('ViewPost', ['id' => $post->id])->message('Report redirect');
  84. }
  85. }
  86. $this->fIswev = $v->getErrors();
  87. $data = $v->getData();
  88. }
  89. $this->nameTpl = 'report';
  90. // $this->onlinePos = 'forum-' . $forum->id;
  91. // $this->canonical = $this->c->Router->link('NewTopic', ['id' => $forum->id]);
  92. $this->robots = 'noindex';
  93. $this->crumbs = $this->crumbs('Report post', $topic);
  94. $this->form = $this->formReport($args, $data);
  95. return $this;
  96. }
  97. /**
  98. * Создает массив для формирование формы
  99. */
  100. protected function formReport(array $args, array $data): array
  101. {
  102. return [
  103. 'action' => $this->c->Router->link('ReportPost', $args),
  104. 'hidden' => [
  105. 'token' => $this->c->Csrf->create('ReportPost', $args),
  106. ],
  107. 'sets' => [
  108. 'report' => [
  109. 'legend' => 'Reason desc',
  110. 'fields' => [
  111. 'reason' => [
  112. 'type' => 'textarea',
  113. 'caption' => 'Reason',
  114. 'required' => true,
  115. 'value' => $data['reason'] ?? null,
  116. 'autofocus' => true,
  117. ],
  118. ],
  119. ],
  120. ],
  121. 'btns' => [
  122. 'report' => [
  123. 'type' => 'submit',
  124. 'value' => __('Submit'),
  125. ],
  126. 'back' => [
  127. 'type' => 'btn',
  128. 'value' => __('Go back'),
  129. 'link' => $this->c->Router->link('ViewPost', ['id' => $args['id']]),
  130. 'class' => ['f-opacity', 'f-go-back'],
  131. ],
  132. ],
  133. ];
  134. }
  135. /**
  136. * Рассылает email с сигналом (репортом)
  137. */
  138. protected function sendReport(ReportModel $report): bool
  139. {
  140. $tplData = [
  141. 'fMailer' => __(['Mailer', $this->c->config->o_board_title]),
  142. 'username' => $report->author->username,
  143. 'postLink' => $this->c->Router->link(
  144. 'ViewPost',
  145. [
  146. 'id' => $report->post->id,
  147. ]
  148. ),
  149. 'reason' => $report->message,
  150. 'forumId' => $report->post->parent->parent->id,
  151. 'topicSubject' => $report->post->parent->name,
  152. ];
  153. return $this->c->Mail
  154. ->reset()
  155. ->setMaxRecipients((int) $this->c->config->i_email_max_recipients)
  156. ->setFolder($this->c->DIR_LANG)
  157. ->setLanguage($this->c->config->o_default_lang) // ????
  158. ->setTo($this->c->config->o_mailing_list)
  159. ->setFrom($this->c->config->o_webmaster_email, $tplData['fMailer'])
  160. ->setTpl('new_report.tpl', $tplData)
  161. ->send();
  162. }
  163. }