1d403609dSGreg Roach<?php 2d403609dSGreg Roach 3d403609dSGreg Roach/** 4d403609dSGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 6d403609dSGreg Roach * This program is free software: you can redistribute it and/or modify 7d403609dSGreg Roach * it under the terms of the GNU General Public License as published by 8d403609dSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d403609dSGreg Roach * (at your option) any later version. 10d403609dSGreg Roach * This program is distributed in the hope that it will be useful, 11d403609dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d403609dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d403609dSGreg Roach * GNU General Public License for more details. 14d403609dSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16d403609dSGreg Roach */ 17fcfa147eSGreg Roach 18d403609dSGreg Roachdeclare(strict_types=1); 19d403609dSGreg Roach 20d403609dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21d403609dSGreg Roach 22d403609dSGreg Roachuse Exception; 231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 24d403609dSGreg Roachuse Fisharebest\Webtrees\FlashMessages; 2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 2698b7e8b3SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 27d403609dSGreg Roachuse Fisharebest\Webtrees\I18N; 28d403609dSGreg Roachuse Fisharebest\Webtrees\Log; 29d403609dSGreg Roachuse Fisharebest\Webtrees\NoReplyUser; 3070ca9c90SGreg Roachuse Fisharebest\Webtrees\Services\CaptchaService; 31e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService; 328cfb5e7bSGreg Roachuse Fisharebest\Webtrees\Services\MessageService; 339ed332c7SGreg Roachuse Fisharebest\Webtrees\Services\RateLimitService; 34d403609dSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 35f91b88bbSGreg Roachuse Fisharebest\Webtrees\Session; 36d403609dSGreg Roachuse Fisharebest\Webtrees\Site; 37d403609dSGreg Roachuse Fisharebest\Webtrees\SiteUser; 38d403609dSGreg Roachuse Fisharebest\Webtrees\Tree; 39d403609dSGreg Roachuse Fisharebest\Webtrees\TreeUser; 40b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 41d403609dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 42256b18b9SGreg Roachuse Illuminate\Support\Str; 43d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface; 44d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 4598b7e8b3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 46f3874e19SGreg Roach 479fa6ab69SGreg Roachuse function view; 4890a2f718SGreg Roach 49d403609dSGreg Roach/** 50d403609dSGreg Roach * Process a user registration. 51d403609dSGreg Roach */ 5298b7e8b3SGreg Roachclass RegisterAction implements RequestHandlerInterface 53d403609dSGreg Roach{ 5498b7e8b3SGreg Roach use ViewResponseTrait; 5598b7e8b3SGreg Roach 56c4943cffSGreg Roach private CaptchaService $captcha_service; 5770ca9c90SGreg Roach 58c4943cffSGreg Roach private EmailService $email_service; 59d403609dSGreg Roach 609ed332c7SGreg Roach private RateLimitService $rate_limit_service; 619ed332c7SGreg Roach 62c4943cffSGreg Roach private UserService $user_service; 63d403609dSGreg Roach 64d403609dSGreg Roach /** 65d403609dSGreg Roach * RegisterController constructor. 66d403609dSGreg Roach * 6770ca9c90SGreg Roach * @param CaptchaService $captcha_service 68e381f98dSGreg Roach * @param EmailService $email_service 699ed332c7SGreg Roach * @param RateLimitService $rate_limit_service 70d403609dSGreg Roach * @param UserService $user_service 71d403609dSGreg Roach */ 7270ca9c90SGreg Roach public function __construct( 7370ca9c90SGreg Roach CaptchaService $captcha_service, 7470ca9c90SGreg Roach EmailService $email_service, 759ed332c7SGreg Roach RateLimitService $rate_limit_service, 7670ca9c90SGreg Roach UserService $user_service 7770ca9c90SGreg Roach ) { 7870ca9c90SGreg Roach $this->captcha_service = $captcha_service; 79e381f98dSGreg Roach $this->email_service = $email_service; 809ed332c7SGreg Roach $this->rate_limit_service = $rate_limit_service; 81d403609dSGreg Roach $this->user_service = $user_service; 82d403609dSGreg Roach } 83d403609dSGreg Roach 84d403609dSGreg Roach /** 85d403609dSGreg Roach * Perform a registration. 86d403609dSGreg Roach * 87d403609dSGreg Roach * @param ServerRequestInterface $request 88d403609dSGreg Roach * 89d403609dSGreg Roach * @return ResponseInterface 90d403609dSGreg Roach */ 91d403609dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 92d403609dSGreg Roach { 93d403609dSGreg Roach $this->checkRegistrationAllowed(); 94d403609dSGreg Roach 95748dbe15SGreg Roach $tree = Validator::attributes($request)->treeOptional(); 96748dbe15SGreg Roach $comments = Validator::parsedBody($request)->string('comments'); 97748dbe15SGreg Roach $email = Validator::parsedBody($request)->string('email'); 98748dbe15SGreg Roach $password = Validator::parsedBody($request)->string('password'); 99748dbe15SGreg Roach $realname = Validator::parsedBody($request)->string('realname'); 100748dbe15SGreg Roach $username = Validator::parsedBody($request)->string('username'); 101d403609dSGreg Roach 102d403609dSGreg Roach try { 10370ca9c90SGreg Roach if ($this->captcha_service->isRobot($request)) { 10470ca9c90SGreg Roach throw new Exception(I18N::translate('Please try again.')); 10570ca9c90SGreg Roach } 10670ca9c90SGreg Roach 107f91b88bbSGreg Roach $this->doValidateRegistration($request, $username, $email, $realname, $comments, $password); 108f91b88bbSGreg Roach 109f91b88bbSGreg Roach Session::forget('register_comments'); 110f91b88bbSGreg Roach Session::forget('register_email'); 111f91b88bbSGreg Roach Session::forget('register_realname'); 112f91b88bbSGreg Roach Session::forget('register_username'); 113d403609dSGreg Roach } catch (Exception $ex) { 114d403609dSGreg Roach FlashMessages::addMessage($ex->getMessage(), 'danger'); 115d403609dSGreg Roach 116f91b88bbSGreg Roach Session::put('register_comments', $comments); 117f91b88bbSGreg Roach Session::put('register_email', $email); 118f91b88bbSGreg Roach Session::put('register_realname', $realname); 119f91b88bbSGreg Roach Session::put('register_username', $username); 120f91b88bbSGreg Roach 121f91b88bbSGreg Roach return redirect(route(RegisterPage::class)); 122d403609dSGreg Roach } 123d403609dSGreg Roach 1249ed332c7SGreg Roach $this->rate_limit_service->limitRateForSite(5, 300, 'rate-limit-registration'); 1259ed332c7SGreg Roach 126d403609dSGreg Roach Log::addAuthenticationLog('User registration requested for: ' . $username); 127d403609dSGreg Roach 128d403609dSGreg Roach $user = $this->user_service->create($username, $realname, $email, $password); 129256b18b9SGreg Roach $token = Str::random(32); 130256b18b9SGreg Roach 1311fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag()); 1328a07c98eSGreg Roach $user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE')); 1331fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, ''); 1341fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, ''); 1351fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U')); 1361fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_VERIFICATION_TOKEN, $token); 1378cfb5e7bSGreg Roach $user->setPreference(UserInterface::PREF_CONTACT_METHOD, MessageService::CONTACT_METHOD_INTERNAL_AND_EMAIL); 138f91b88bbSGreg Roach $user->setPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT, $comments); 1391fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, '1'); 1401fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS, ''); 1411fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, ''); 1421fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0'); 143d403609dSGreg Roach 144b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 145d403609dSGreg Roach $reply_to = $tree instanceof Tree ? new TreeUser($tree) : new SiteUser(); 146d403609dSGreg Roach 147f917a287SGreg Roach $verify_url = route(VerifyEmail::class, [ 148f917a287SGreg Roach 'username' => $user->userName(), 149256b18b9SGreg Roach 'token' => $token, 150f917a287SGreg Roach 'tree' => $tree instanceof Tree ? $tree->name() : null, 151f917a287SGreg Roach ]); 152f917a287SGreg Roach 153d403609dSGreg Roach // Send a verification message to the user. 154d403609dSGreg Roach /* I18N: %s is a server name/URL */ 155e381f98dSGreg Roach $this->email_service->send( 156b4c5c807SGreg Roach new SiteUser(), 157d403609dSGreg Roach $user, 158d403609dSGreg Roach $reply_to, 159d403609dSGreg Roach I18N::translate('Your registration at %s', $base_url), 160f917a287SGreg Roach view('emails/register-user-text', ['user' => $user, 'base_url' => $base_url, 'verify_url' => $verify_url]), 161f917a287SGreg Roach view('emails/register-user-html', ['user' => $user, 'base_url' => $base_url, 'verify_url' => $verify_url]) 162d403609dSGreg Roach ); 163d403609dSGreg Roach 164d403609dSGreg Roach // Tell the administrators about the registration. 165d403609dSGreg Roach foreach ($this->user_service->administrators() as $administrator) { 1661fe542e9SGreg Roach I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE)); 167d403609dSGreg Roach 168d403609dSGreg Roach /* I18N: %s is a server name/URL */ 169d403609dSGreg Roach $subject = I18N::translate('New registration at %s', $base_url); 170d403609dSGreg Roach 1719fa6ab69SGreg Roach $body_text = view('emails/register-notify-text', [ 1729fa6ab69SGreg Roach 'user' => $user, 173f91b88bbSGreg Roach 'comments' => $comments, 1749fa6ab69SGreg Roach 'base_url' => $base_url, 1759fa6ab69SGreg Roach 'tree' => $tree, 1769fa6ab69SGreg Roach ]); 1779fa6ab69SGreg Roach 1789fa6ab69SGreg Roach $body_html = view('emails/register-notify-html', [ 1799fa6ab69SGreg Roach 'user' => $user, 180f91b88bbSGreg Roach 'comments' => $comments, 1819fa6ab69SGreg Roach 'base_url' => $base_url, 1829fa6ab69SGreg Roach 'tree' => $tree, 1839fa6ab69SGreg Roach ]); 1849fa6ab69SGreg Roach 1859fa6ab69SGreg Roach 186d403609dSGreg Roach /* I18N: %s is a server name/URL */ 187e381f98dSGreg Roach $this->email_service->send( 188d403609dSGreg Roach new SiteUser(), 189d403609dSGreg Roach $administrator, 190d403609dSGreg Roach new NoReplyUser(), 191d403609dSGreg Roach $subject, 1929fa6ab69SGreg Roach $body_text, 1939fa6ab69SGreg Roach $body_html 194d403609dSGreg Roach ); 195d403609dSGreg Roach 1961fe542e9SGreg Roach $mail1_method = $administrator->getPreference(UserInterface::PREF_CONTACT_METHOD); 1978cfb5e7bSGreg Roach if ( 1988cfb5e7bSGreg Roach $mail1_method !== MessageService::CONTACT_METHOD_EMAIL && 1998cfb5e7bSGreg Roach $mail1_method !== MessageService::CONTACT_METHOD_MAILTO && 2008cfb5e7bSGreg Roach $mail1_method !== MessageService::CONTACT_METHOD_NONE 2018cfb5e7bSGreg Roach ) { 202d403609dSGreg Roach DB::table('message')->insert([ 203d403609dSGreg Roach 'sender' => $user->email(), 2044874f72dSGreg Roach 'ip_address' => $request->getAttribute('client-ip'), 205d403609dSGreg Roach 'user_id' => $administrator->id(), 206d403609dSGreg Roach 'subject' => $subject, 2079fa6ab69SGreg Roach 'body' => $body_text, 208d403609dSGreg Roach ]); 209d403609dSGreg Roach } 210d403609dSGreg Roach } 211d403609dSGreg Roach 212d403609dSGreg Roach $title = I18N::translate('Request a new user account'); 213d403609dSGreg Roach 214d403609dSGreg Roach return $this->viewResponse('register-success-page', [ 215d403609dSGreg Roach 'title' => $title, 2169fa6ab69SGreg Roach 'tree' => $tree, 217d403609dSGreg Roach 'user' => $user, 218d403609dSGreg Roach ]); 219d403609dSGreg Roach } 220d403609dSGreg Roach 221d403609dSGreg Roach /** 222d403609dSGreg Roach * Check that visitors are allowed to register on this site. 223d403609dSGreg Roach * 224d403609dSGreg Roach * @return void 225d501c45dSGreg Roach * @throws HttpNotFoundException 226d403609dSGreg Roach */ 227d403609dSGreg Roach private function checkRegistrationAllowed(): void 228d403609dSGreg Roach { 229d403609dSGreg Roach if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') { 230d501c45dSGreg Roach throw new HttpNotFoundException(); 231d403609dSGreg Roach } 232d403609dSGreg Roach } 233d403609dSGreg Roach 234d403609dSGreg Roach /** 235d403609dSGreg Roach * Check the registration details. 236d403609dSGreg Roach * 237d403609dSGreg Roach * @param ServerRequestInterface $request 238d403609dSGreg Roach * @param string $username 239d403609dSGreg Roach * @param string $email 240d403609dSGreg Roach * @param string $realname 241d403609dSGreg Roach * @param string $comments 242d403609dSGreg Roach * @param string $password 243d403609dSGreg Roach * 244d403609dSGreg Roach * @return void 245d403609dSGreg Roach * @throws Exception 246d403609dSGreg Roach */ 247*c5d4d20fSGreg Roach private function doValidateRegistration( 248*c5d4d20fSGreg Roach ServerRequestInterface $request, 249*c5d4d20fSGreg Roach string $username, string $email, 250*c5d4d20fSGreg Roach string $realname, string $comments, 251*c5d4d20fSGreg Roach #[\SensitiveParameter] string $password 252*c5d4d20fSGreg Roach ): void { 253d403609dSGreg Roach // All fields are required 254d403609dSGreg Roach if ($username === '' || $email === '' || $realname === '' || $comments === '' || $password === '') { 255d403609dSGreg Roach throw new Exception(I18N::translate('All fields must be completed.')); 256d403609dSGreg Roach } 257d403609dSGreg Roach 258d403609dSGreg Roach // Username already exists 259d403609dSGreg Roach if ($this->user_service->findByUserName($username) !== null) { 260d403609dSGreg Roach throw new Exception(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); 261d403609dSGreg Roach } 262d403609dSGreg Roach 263d403609dSGreg Roach // Email already exists 264d403609dSGreg Roach if ($this->user_service->findByEmail($email) !== null) { 265d403609dSGreg Roach throw new Exception(I18N::translate('Duplicate email address. A user with that email already exists.')); 266d403609dSGreg Roach } 267d403609dSGreg Roach 268b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 269d403609dSGreg Roach 270d403609dSGreg Roach // No external links 271d403609dSGreg Roach if (preg_match('/(?!' . preg_quote($base_url, '/') . ')(((?:http|https):\/\/)[a-zA-Z0-9.-]+)/', $comments, $match)) { 272d403609dSGreg Roach throw new Exception(I18N::translate('You are not allowed to send messages that contain external links.') . ' ' . I18N::translate('You should delete the “%1$s” from “%2$s” and try again.', e($match[2]), e($match[1]))); 273d403609dSGreg Roach } 274d403609dSGreg Roach } 275d403609dSGreg Roach} 276