Email.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\User\User;
  13. use function \ForkBB\__;
  14. class Email extends Page
  15. {
  16. /**
  17. * Получатель
  18. * @var User
  19. */
  20. protected $curUser;
  21. /**
  22. * Подготовка данных для шаблона
  23. */
  24. public function email(array $args, string $method): Page
  25. {
  26. if (! $this->c->Csrf->verify($args['hash'], 'SendEmail', $args)) {
  27. return $this->c->Message->message($this->c->Csrf->getError());
  28. }
  29. $this->curUser = $this->c->users->load($args['id']);
  30. if (
  31. ! $this->curUser instanceof User
  32. || $this->curUser->isGuest
  33. ) {
  34. return $this->c->Message->message('Bad request');
  35. }
  36. $rules = $this->c->ProfileRules->setUser($this->curUser);
  37. if (
  38. ! $rules->viewEmail
  39. || ! $rules->sendEmail
  40. ) {
  41. $message = null === $rules->sendEmail ? 'Form email disabled' : 'Bad request';
  42. return $this->c->Message->message($message);
  43. }
  44. $this->c->Lang->load('validator');
  45. $this->c->Lang->load('misc');
  46. $floodSize = \time() - (int) $this->user->last_email_sent;
  47. $floodSize = $floodSize < $this->user->g_email_flood ? $this->user->g_email_flood - $floodSize : 0;
  48. if ($floodSize > 0) {
  49. $this->fIswev = ['e', ['Flood message', $floodSize]];
  50. }
  51. $data = [
  52. 'redirect' => $this->c->Router->validate(
  53. $this->c->Secury->replInvalidChars($_SERVER['HTTP_REFERER'] ?? ''),
  54. 'Index'
  55. ),
  56. ];
  57. if ('POST' === $method) {
  58. $v = $this->c->Validator->reset()
  59. ->addValidators([
  60. ])->addRules([
  61. 'token' => 'token:SendEmail',
  62. 'redirect' => 'required|referer:Index',
  63. 'subject' => 'required|string:trim|max:70',
  64. 'message' => 'required|string:trim,linebreaks|max:65000 bytes',
  65. 'send' => 'required|string',
  66. ])->addAliases([
  67. 'subject' => 'Email subject',
  68. 'message' => 'Email message',
  69. ])->addArguments([
  70. 'token' => $args,
  71. ])->addMessages([
  72. ]);
  73. if (
  74. $v->validation($_POST)
  75. && 0 === $floodSize
  76. ) {
  77. try {
  78. if ($this->sendEmail($v->getData())) {
  79. if ($this->user->g_email_flood > 0) {
  80. $this->user->last_email_sent = \time();
  81. $this->c->users->update($this->user);
  82. }
  83. $this->c->Log->info('Email send: ok', [
  84. 'user' => $this->user->fLog(),
  85. 'recipient' => $this->curUser->fLog(),
  86. ]);
  87. return $this->c->Redirect->url($v->redirect)->message('Email sent redirect');
  88. }
  89. } catch (MailException $e) {
  90. $this->c->Log->error('Email send: MailException', [
  91. 'user' => $this->user->fLog(),
  92. 'exception' => $e,
  93. 'headers' => false,
  94. ]);
  95. }
  96. return $this->c->Message->message('When sending email there was an error');
  97. }
  98. $this->fIswev = $v->getErrors();
  99. $data = $v->getData();
  100. $this->c->Log->warning('Email send: form, fail', [
  101. 'user' => $this->user->fLog(),
  102. 'recipient' => $this->curUser->fLog(),
  103. ]);
  104. }
  105. $this->nameTpl = 'email';
  106. $this->robots = 'noindex';
  107. $this->crumbs = $this->crumbs([null, ['Send email to %s', $this->curUser->username]]);
  108. $this->form = $this->formEmail($args, $data);
  109. return $this;
  110. }
  111. /**
  112. * Создает массив для формирование формы
  113. */
  114. protected function formEmail(array $args, array $data): array
  115. {
  116. return [
  117. 'action' => $this->c->Router->link('SendEmail', $args),
  118. 'hidden' => [
  119. 'token' => $this->c->Csrf->create('SendEmail', $args),
  120. 'redirect' => $data['redirect'] ?? '',
  121. ],
  122. 'sets' => [
  123. 'send-email' => [
  124. 'legend' => ['Send email to %s', $this->curUser->username],
  125. 'fields' => [
  126. 'subject' => [
  127. 'type' => 'text',
  128. 'maxlength' => '70',
  129. 'caption' => 'Email subject',
  130. 'required' => true,
  131. 'value' => $vars['subject'] ?? null,
  132. 'autofocus' => true,
  133. ],
  134. 'message' => [
  135. 'type' => 'textarea',
  136. 'caption' => 'Email message',
  137. 'required' => true,
  138. 'value' => $data['message'] ?? null,
  139. ],
  140. ],
  141. ],
  142. 'email-info' => [
  143. 'info' => [
  144. [
  145. 'value' => __('Email disclosure note'),
  146. ],
  147. ],
  148. ],
  149. ],
  150. 'btns' => [
  151. 'send' => [
  152. 'type' => 'submit',
  153. 'value' => __('Send email'),
  154. ],
  155. 'back' => [
  156. 'type' => 'btn',
  157. 'value' => __('Go back'),
  158. 'link' => $data['redirect'],
  159. 'class' => ['f-opacity', 'f-go-back'],
  160. ],
  161. ],
  162. ];
  163. }
  164. /**
  165. * Отправляет email
  166. */
  167. protected function sendEmail(array $data): bool
  168. {
  169. $tplData = [
  170. 'fTitle' => $this->c->config->o_board_title,
  171. 'fMailer' => __(['Mailer', $this->c->config->o_board_title]),
  172. 'username' => $this->curUser->username,
  173. 'sender' => $this->user->username,
  174. 'mailSubject' => $data['subject'],
  175. 'mailMessage' => $data['message'],
  176. ];
  177. return $this->c->Mail
  178. ->reset()
  179. ->setMaxRecipients(1)
  180. ->setFolder($this->c->DIR_LANG)
  181. ->setLanguage($this->curUser->language)
  182. ->setTo($this->curUser->email)
  183. ->setFrom($this->c->config->o_webmaster_email, $tplData['fMailer'])
  184. ->setReplyTo($this->user->email, $this->user->username)
  185. ->setTpl('form_email.tpl', $tplData)
  186. ->send();
  187. }
  188. }