]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Register.php
Apply suggestions from code review
[friendica.git] / src / Module / Register.php
index 9e79dabbe6f2080a8c4ca11666063676d3b9c68a..6b21a0c7e784d7d688165abc547ec0ff2047802d 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
  *
@@ -333,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(
@@ -343,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();
                        }
@@ -356,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) {
@@ -367,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(
@@ -402,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()->get(true) . '/moderation/users/',
+                               'source_name'               => $user['username'],
+                               'source_mail'               => $user['email'],
+                               'source_nick'               => $user['nickname'],
+                               'source_link'               => DI::baseUrl()->get(true) . '/moderation/users/',
+                               'source_photo'              => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
+                               'show_in_notification_page' => false
+                       ]);
+               }
        }
 }