]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Register.php
spelling: names
[friendica.git] / src / Module / Register.php
index 0e6ff18f2d35b22681c07037388fa00f25a6ddd6..e5c5840d22dc2a30dfc1bf01b74d383d44d765fe 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -29,7 +29,6 @@ use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
-use Friendica\Core\Session;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
@@ -74,20 +73,20 @@ class Register extends BaseModule
                // 'block_extended_register' blocks all registrations, period.
                $block = DI::config()->get('system', 'block_extended_register');
 
-               if (Session::getLocalUser() && $block) {
+               if (DI::userSession()->getLocalUserId() && $block) {
                        DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
                        return '';
                }
 
-               if (Session::getLocalUser()) {
-                       $user = DBA::selectFirst('user', ['parent-uid'], ['uid' => Session::getLocalUser()]);
+               if (DI::userSession()->getLocalUserId()) {
+                       $user = DBA::selectFirst('user', ['parent-uid'], ['uid' => DI::userSession()->getLocalUserId()]);
                        if (!empty($user['parent-uid'])) {
                                DI::sysmsg()->addNotice(DI::l10n()->t('Only parent users can create additional accounts.'));
                                return '';
                        }
                }
 
-               if (!Session::getLocalUser() && (intval(DI::config()->get('config', 'register_policy')) === self::CLOSED)) {
+               if (!DI::userSession()->getLocalUserId() && (intval(DI::config()->get('config', 'register_policy')) === self::CLOSED)) {
                        DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
                        return '';
                }
@@ -109,7 +108,7 @@ class Register extends BaseModule
                $photo      = $_REQUEST['photo']      ?? '';
                $invite_id  = $_REQUEST['invite_id']  ?? '';
 
-               if (Session::getLocalUser() || DI::config()->get('system', 'no_openid')) {
+               if (DI::userSession()->getLocalUserId() || DI::config()->get('system', 'no_openid')) {
                        $fillwith = '';
                        $fillext  = '';
                        $oidlabel = '';
@@ -162,7 +161,7 @@ class Register extends BaseModule
                        '$ask_password' => $ask_password,
                        '$password1'    => ['password1', DI::l10n()->t('New Password:'), '', DI::l10n()->t('Leave empty for an auto generated password.')],
                        '$password2'    => ['confirm', DI::l10n()->t('Confirm:'), '', ''],
-                       '$nickdesc'     => DI::l10n()->t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be "<strong>nickname@%s</strong>".', DI::baseUrl()->getHostname()),
+                       '$nickdesc'     => DI::l10n()->t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be "<strong>nickname@%s</strong>".', DI::baseUrl()->getHost()),
                        '$nicklabel'    => DI::l10n()->t('Choose a nickname: '),
                        '$photo'        => $photo,
                        '$publish'      => $profile_publish,
@@ -170,7 +169,7 @@ class Register extends BaseModule
                        '$username'     => $username,
                        '$email'        => $email,
                        '$nickname'     => $nickname,
-                       '$sitename'     => DI::baseUrl()->getHostname(),
+                       '$sitename'     => DI::baseUrl()->getHost(),
                        '$importh'      => DI::l10n()->t('Import'),
                        '$importt'      => DI::l10n()->t('Import your profile to this friendica instance'),
                        '$showtoslink'  => DI::config()->get('system', 'tosdisplay'),
@@ -180,7 +179,7 @@ class Register extends BaseModule
                        '$form_security_token' => BaseModule::getFormSecurityToken('register'),
                        '$explicit_content' => DI::config()->get('system', 'explicit_content', false),
                        '$explicit_content_note' => DI::l10n()->t('Note: This node explicitly contains adult content'),
-                       '$additional'   => !empty(Session::getLocalUser()),
+                       '$additional'   => !empty(DI::userSession()->getLocalUserId()),
                        '$parent_password' => ['parent_password', DI::l10n()->t('Parent Password:'), '', DI::l10n()->t('Please enter the password of the parent account to legitimize your request.')]
 
                ]);
@@ -203,19 +202,19 @@ class Register extends BaseModule
 
                $additional_account = false;
 
-               if (!Session::getLocalUser() && !empty($arr['post']['parent_password'])) {
+               if (!DI::userSession()->getLocalUserId() && !empty($arr['post']['parent_password'])) {
                        DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
                        return;
-               } elseif (Session::getLocalUser() && !empty($arr['post']['parent_password'])) {
+               } elseif (DI::userSession()->getLocalUserId() && !empty($arr['post']['parent_password'])) {
                        try {
-                               Model\User::getIdFromPasswordAuthentication(Session::getLocalUser(), $arr['post']['parent_password']);
+                               Model\User::getIdFromPasswordAuthentication(DI::userSession()->getLocalUserId(), $arr['post']['parent_password']);
                        } catch (\Exception $ex) {
                                DI::sysmsg()->addNotice(DI::l10n()->t("Password doesn't match."));
                                $regdata = ['nickname' => $arr['post']['nickname'], 'username' => $arr['post']['username']];
                                DI::baseUrl()->redirect('register?' . http_build_query($regdata));
                        }
                        $additional_account = true;
-               } elseif (Session::getLocalUser()) {
+               } elseif (DI::userSession()->getLocalUserId()) {
                        DI::sysmsg()->addNotice(DI::l10n()->t('Please enter your password.'));
                        $regdata = ['nickname' => $arr['post']['nickname'], 'username' => $arr['post']['username']];
                        DI::baseUrl()->redirect('register?' . http_build_query($regdata));
@@ -263,7 +262,7 @@ class Register extends BaseModule
                }
 
                if ($additional_account) {
-                       $user = DBA::selectFirst('user', ['email'], ['uid' => Session::getLocalUser()]);
+                       $user = DBA::selectFirst('user', ['email'], ['uid' => DI::userSession()->getLocalUserId()]);
                        if (!DBA::isResult($user)) {
                                DI::sysmsg()->addNotice(DI::l10n()->t('User not found.'));
                                DI::baseUrl()->redirect('register');
@@ -299,7 +298,7 @@ class Register extends BaseModule
 
                $user = $result['user'];
 
-               $base_url = DI::baseUrl()->get();
+               $base_url = DI::baseUrl();
 
                if ($netpublish && intval(DI::config()->get('config', 'register_policy')) !== self::APPROVE) {
                        $url = $base_url . '/profile/' . $user['nickname'];
@@ -307,7 +306,7 @@ class Register extends BaseModule
                }
 
                if ($additional_account) {
-                       DBA::update('user', ['parent-uid' => Session::getLocalUser()], ['uid' => $user['uid']]);
+                       DBA::update('user', ['parent-uid' => DI::userSession()->getLocalUserId()], ['uid' => $user['uid']]);
                        DI::sysmsg()->addInfo(DI::l10n()->t('The additional account was created.'));
                        DI::baseUrl()->redirect('delegation');
                }
@@ -334,6 +333,9 @@ class Register extends BaseModule
 
                                if ($res) {
                                        DI::sysmsg()->addInfo(DI::l10n()->t('Registration successful. Please check your email for further instructions.'));
+                                       if (DI::config()->get('system', 'register_notification')) {
+                                               $this->sendNotification($user, 'SYSTEM_REGISTER_NEW');
+                                       }
                                        DI::baseUrl()->redirect();
                                } else {
                                        DI::sysmsg()->addNotice(
@@ -344,10 +346,14 @@ class Register extends BaseModule
                                }
                        } else {
                                DI::sysmsg()->addInfo(DI::l10n()->t('Registration successful.'));
+                               if (DI::config()->get('system', 'register_notification')) {
+                                       $this->sendNotification($user, 'SYSTEM_REGISTER_NEW');
+                               }
                                DI::baseUrl()->redirect();
                        }
                } elseif (intval(DI::config()->get('config', 'register_policy')) === self::APPROVE) {
-                       if (!strlen(DI::config()->get('config', 'admin_email'))) {
+                       if (!User::getAdminEmailList()) {
+                               $this->logger->critical('Registration policy is set to APPROVE but no admin email address has been set in config.admin_email');
                                DI::sysmsg()->addNotice(DI::l10n()->t('Your registration can not be processed.'));
                                DI::baseUrl()->redirect();
                        }
@@ -357,10 +363,17 @@ class Register extends BaseModule
                                DI::sysmsg()->addNotice(DI::l10n()->t('You have to leave a request note for the admin.')
                                        . DI::l10n()->t('Your registration can not be processed.'));
 
-                               DI::baseUrl()->redirect('register/');
+                               $this->baseUrl->redirect('register');
                        }
 
-                       Model\Register::createForApproval($user['uid'], DI::config()->get('system', 'language'), $_POST['permonlybox']);
+                       try {
+                               Model\Register::createForApproval($user['uid'], DI::config()->get('system', 'language'), $_POST['permonlybox']);
+                       } catch (\Throwable $e) {
+                               $this->logger->error('Unable to create a `register` record.', ['user' => $user]);
+                               DI::sysmsg()->addNotice(DI::l10n()->t('An internal error occured.')
+                                       . DI::l10n()->t('Your registration can not be processed.'));
+                               $this->baseUrl->redirect('register');
+                       }
 
                        // invite system
                        if ($using_invites && $invite_id) {
@@ -368,29 +381,8 @@ class Register extends BaseModule
                                DI::pConfig()->set($user['uid'], 'system', 'invites_remaining', $num_invites);
                        }
 
-                       // send email to admins
-                       $admins_stmt = DBA::select(
-                               'user',
-                               ['uid', 'language', 'email'],
-                               ['email' => explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')))]
-                       );
-
-                       // send notification to admins
-                       while ($admin = DBA::fetch($admins_stmt)) {
-                               DI::notify()->createFromArray([
-                                       'type'         => Model\Notification\Type::SYSTEM,
-                                       'event'        => 'SYSTEM_REGISTER_REQUEST',
-                                       'uid'          => $admin['uid'],
-                                       'link'         => DI::baseUrl()->get(true) . '/admin/users/',
-                                       'source_name'  => $user['username'],
-                                       'source_mail'  => $user['email'],
-                                       'source_nick'  => $user['nickname'],
-                                       'source_link'  => DI::baseUrl()->get(true) . '/admin/users/',
-                                       'source_photo' => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
-                                       'show_in_notification_page' => false
-                               ]);
-                       }
-                       DBA::close($admins_stmt);
+                       // send notification to the admin
+                       $this->sendNotification($user, 'SYSTEM_REGISTER_REQUEST');
 
                        // send notification to the user, that the registration is pending
                        Model\User::sendRegisterPendingEmail(
@@ -403,7 +395,23 @@ class Register extends BaseModule
                        DI::sysmsg()->addInfo(DI::l10n()->t('Your registration is pending approval by the site owner.'));
                        DI::baseUrl()->redirect();
                }
+       }
 
-               return;
+       private function sendNotification(array $user, string $event)
+       {
+               foreach (User::getAdminListForEmailing(['uid', 'language', 'email']) as $admin) {
+                       DI::notify()->createFromArray([
+                               'type'                      => Model\Notification\Type::SYSTEM,
+                               'event'                     => $event,
+                               'uid'                       => $admin['uid'],
+                               'link'                      => DI::baseUrl() . '/moderation/users/',
+                               'source_name'               => $user['username'],
+                               'source_mail'               => $user['email'],
+                               'source_nick'               => $user['nickname'],
+                               'source_link'               => DI::baseUrl() . '/moderation/users/',
+                               'source_photo'              => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
+                               'show_in_notification_page' => false
+                       ]);
+               }
        }
 }