Email.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\Profile;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Core\Exceptions\MailException;
  12. use ForkBB\Models\Page;
  13. use ForkBB\Models\Pages\Profile;
  14. use ForkBB\Models\User\User;
  15. use function \ForkBB\__;
  16. class Email extends Profile
  17. {
  18. /**
  19. * Изменяет почтовый адрес пользователя по ссылке активации
  20. */
  21. public function setEmail(array $args, string $method): Page
  22. {
  23. if (
  24. $this->user->id !== $args['id']
  25. || ! $this->c->Csrf->verify($args['hash'], 'SetNewEmail', $args)
  26. || ! \hash_equals($this->user->activate_string, $args['key'])
  27. ) {
  28. return $this->c->Message->message('Bad request', false);
  29. }
  30. $this->c->Lang->load('profile');
  31. $this->user->email = $args['email'];
  32. $this->user->email_confirmed = 1;
  33. $this->user->activate_string = '';
  34. $change = $this->user->isModified('email');
  35. $this->c->users->update($this->user);
  36. return $this->c->Redirect
  37. ->url($this->user->link)
  38. ->message($change ? 'Email changed redirect' : 'Email confirmed redirect');
  39. }
  40. /**
  41. * Подготавливает данные для шаблона смены почтового адреса
  42. */
  43. public function email(array $args, string $method): Page
  44. {
  45. if (
  46. false === $this->initProfile($args['id'])
  47. || ! $this->rules->editEmail
  48. ) {
  49. return $this->c->Message->message('Bad request');
  50. }
  51. $this->c->Lang->load('validator');
  52. if ('POST' === $method) {
  53. $isSent = null;
  54. $key = null;
  55. $v = $this->c->Validator->reset()
  56. ->addValidators([
  57. 'check_password' => [$this, 'vCheckPassword'],
  58. ])->addRules([
  59. 'token' => 'token:EditUserEmail',
  60. 'password' => 'required|string:trim|max:100000|check_password',
  61. 'new_email' => 'required|string:trim|email',
  62. 'submit' => 'required|string',
  63. ])->addAliases([
  64. 'new_email' => 'New email',
  65. 'password' => 'Your passphrase',
  66. ])->addArguments([
  67. 'token' => $args,
  68. ])->addMessages([
  69. ]);
  70. $isValid = $v->validation($_POST);
  71. if ($isValid) {
  72. if (
  73. $v->new_email === $this->curUser->email
  74. && ! $this->rules->confirmEmail
  75. ) {
  76. return $this->c->Redirect
  77. ->page('EditUserProfile', $args)
  78. ->message('Email is old redirect');
  79. }
  80. $v = $v->reset()
  81. ->addRules([
  82. 'new_email' => 'required|string:trim|email:noban,flood',
  83. ])->addAliases([
  84. 'new_email' => 'New email',
  85. ])->addArguments([
  86. 'new_email.email' => $this->curUser,
  87. ]);
  88. $isValid = $v->validation($_POST);
  89. }
  90. if ($isValid) {
  91. $v = $this->c->Validator->reset()
  92. ->addRules([
  93. 'new_email' => 'required|string:trim|email:unique',
  94. ])->addAliases([
  95. 'new_email' => 'New email',
  96. ])->addArguments([
  97. 'new_email.email' => $this->curUser,
  98. ]);
  99. $isValid = $v->validation($_POST);
  100. if ($isValid) {
  101. if (
  102. ! $this->rules->my
  103. && $this->rules->admin
  104. ) {
  105. $this->curUser->email = $v->new_email;
  106. $this->curUser->email_confirmed = 0;
  107. $this->c->users->update($this->curUser);
  108. return $this->c->Redirect
  109. ->page('EditUserProfile', $args)
  110. ->message('Email changed redirect');
  111. } else {
  112. $this->c->Csrf->setHashExpiration(259200); // ???? хэш действует 72 часа
  113. $key = $this->c->Secury->randomPass(33);
  114. $link = $this->c->Router->link(
  115. 'SetNewEmail',
  116. [
  117. 'id' => $this->curUser->id,
  118. 'email' => $v->new_email,
  119. 'key' => $key,
  120. ]
  121. );
  122. $tplData = [
  123. 'fRootLink' => $this->c->Router->link('Index'),
  124. 'fMailer' => __(['Mailer', $this->c->config->o_board_title]),
  125. 'username' => $this->curUser->username,
  126. 'link' => $link,
  127. ];
  128. try {
  129. $isSent = $this->c->Mail
  130. ->reset()
  131. ->setMaxRecipients(1)
  132. ->setFolder($this->c->DIR_LANG)
  133. ->setLanguage($this->curUser->language)
  134. ->setTo($v->new_email, $this->curUser->username)
  135. ->setFrom($this->c->config->o_webmaster_email, $tplData['fMailer'])
  136. ->setTpl('activate_email.tpl', $tplData)
  137. ->send();
  138. } catch (MailException $e) {
  139. $isSent = false;
  140. $this->c->Log->error('Email activation: MailException', [
  141. 'exception' => $e,
  142. 'headers' => false,
  143. ]);
  144. }
  145. }
  146. } elseif (! $this->user->isAdmin) {
  147. // обманка
  148. $isSent = true;
  149. }
  150. if (null !== $isSent) {
  151. if ($isSent) {
  152. if (\is_string($key)) {
  153. $this->curUser->activate_string = $key;
  154. }
  155. $this->curUser->last_email_sent = \time();
  156. $this->c->users->update($this->curUser);
  157. return $this->c->Message
  158. ->message(['Activate email sent', $this->c->config->o_admin_email], false, 200);
  159. } else {
  160. return $this->c->Message
  161. ->message(['Error mail', $this->c->config->o_admin_email], true, 200);
  162. }
  163. }
  164. }
  165. $this->curUser->__email = $v->new_email;
  166. $this->fIswev = $v->getErrors();
  167. }
  168. $this->crumbs = $this->crumbs(
  169. [
  170. $this->c->Router->link('EditUserEmail', $args),
  171. 'Change email',
  172. ],
  173. [
  174. $this->c->Router->link('EditUserProfile', $args),
  175. 'Editing profile',
  176. ]
  177. );
  178. $this->form = $this->form($args);
  179. $this->actionBtns = $this->btns('edit');
  180. return $this;
  181. }
  182. /**
  183. * Создает массив данных для формы
  184. */
  185. protected function form(array $args): array
  186. {
  187. $form = [
  188. 'action' => $this->c->Router->link('EditUserEmail', $args),
  189. 'hidden' => [
  190. 'token' => $this->c->Csrf->create('EditUserEmail', $args),
  191. ],
  192. 'sets' => [
  193. 'new-email' => [
  194. 'class' => ['data-edit'],
  195. 'fields' => [
  196. 'new_email' => [
  197. 'autofocus' => true,
  198. 'type' => 'text',
  199. 'maxlength' => '80',
  200. 'caption' => $this->rules->confirmEmail ? 'New or old email' : 'New email',
  201. 'required' => true,
  202. 'pattern' => '.+@.+',
  203. 'value' => $this->curUser->email,
  204. 'help' => $this->rules->my ? 'Email instructions' : null,
  205. ],
  206. 'password' => [
  207. 'type' => 'password',
  208. 'caption' => 'Your passphrase',
  209. 'required' => true,
  210. ],
  211. ],
  212. ],
  213. ],
  214. 'btns' => [
  215. 'submit' => [
  216. 'type' => 'submit',
  217. 'value' => __('Submit'),
  218. ],
  219. ],
  220. ];
  221. return $form;
  222. }
  223. }