]> git.mxchange.org Git - friendica.git/commitdiff
Centralize config.admin_email management in Model\User
authorHypolite Petovan <hypolite@mrpetovan.com>
Sat, 12 Nov 2022 17:01:22 +0000 (12:01 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 14 Nov 2022 22:02:42 +0000 (17:02 -0500)
src/App.php
src/Core/Update.php
src/Model/Nodeinfo.php
src/Model/User.php
src/Module/Api/GNUSocial/GNUSocial/Config.php
src/Module/Moderation/BaseUsers.php
src/Module/Register.php
src/Module/Settings/Account.php
src/Module/Settings/RemoveMe.php
src/Object/Api/Mastodon/Instance.php
src/Protocol/ActivityPub/Transmitter.php

index 27eaf100f490e7908abcea41d6e96695320cd060..abad4ee35d1f153ce760c53adbda23ffaec745e7 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\App\BaseURL;
 use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\Core\Config\Factory\Config;
 use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Model\User;
 use Friendica\Module\Maintenance;
 use Friendica\Security\Authentication;
 use Friendica\Core\Config\ValueObject\Cache;
@@ -164,14 +165,16 @@ class App
         * Check if current user has admin role.
         *
         * @return bool true if user is an admin
+        * @throws Exception
         */
        public function isSiteAdmin(): bool
        {
-               $admin_email = $this->config->get('config', 'admin_email');
-
-               $adminlist = explode(',', str_replace(' ', '', $admin_email));
-
-               return $this->session->getLocalUserId() && $admin_email && $this->database->exists('user', ['uid' => $this->getLoggedInUserId(), 'email' => $adminlist]);
+               return
+                       $this->session->getLocalUserId()
+                       && $this->database->exists('user', [
+                               'uid'   => $this->getLoggedInUserId(),
+                               'email' => User::getAdminEmailList()
+                       ]);
        }
 
        /**
@@ -259,8 +262,8 @@ class App
        /**
         * Set workerqueue information
         *
-        * @param array $queue 
-        * @return void 
+        * @param array $queue
+        * @return void
         */
        public function setQueue(array $queue)
        {
index 5f425172099a55cf132e8648f899ef4e349592c6..8ec96dc9893662d68216900712c29ad015e3cd78 100644 (file)
@@ -26,6 +26,7 @@ use Friendica\App\Mode;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Model\User;
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Strings;
@@ -148,7 +149,7 @@ class Update
                                        }
 
                                        DI::config()->set('system', 'maintenance', 1);
-               
+
                                        // run the pre_update_nnnn functions in update.php
                                        for ($version = $stored + 1; $version <= $current; $version++) {
                                                Logger::notice('Execute pre update.', ['version' => $version]);
@@ -289,30 +290,16 @@ class Update
         * @return void
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function updateFailed(int $update_id, string $error_message) {
-               //send the administrators an e-mail
-               $condition = ['email' => explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email'))), 'parent-uid' => 0];
-               $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
-
-               // No valid result?
-               if (!DBA::isResult($adminlist)) {
+       private static function updateFailed(int $update_id, string $error_message)
+       {
+               $adminEmails = User::getAdminListForEmailing(['uid', 'language', 'email']);
+               if (!$adminEmails) {
                        Logger::warning('Cannot notify administrators .', ['update' => $update_id, 'message' => $error_message]);
-
-                       // Don't continue
                        return;
                }
 
-               $sent = [];
-
-               // every admin could had different language
-               while ($admin = DBA::fetch($adminlist)) {
-                       if (in_array($admin['email'], $sent)) {
-                               continue;
-                       }
-                       $sent[] = $admin['email'];
-
-                       $lang = $admin['language'] ?? 'en';
-                       $l10n = DI::l10n()->withLang($lang);
+               foreach($adminEmails as $admin) {
+                       $l10n = DI::l10n()->withLang($admin['language'] ?: 'en');
 
                        $preamble = Strings::deindent($l10n->t("
                                The friendica developers released update %s recently,
@@ -343,35 +330,20 @@ class Update
         */
        private static function updateSuccessful(int $from_build, int $to_build)
        {
-               //send the administrators an e-mail
-               $condition = ['email' => explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email'))), 'parent-uid' => 0];
-               $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
+               foreach(User::getAdminListForEmailing(['uid', 'language', 'email']) as $admin) {
+                       $l10n = DI::l10n()->withLang($admin['language'] ?: 'en');
 
-               if (DBA::isResult($adminlist)) {
-                       $sent = [];
+                       $preamble = Strings::deindent($l10n->t('
+                               The friendica database was successfully updated from %s to %s.',
+                               $from_build, $to_build));
 
-                       // every admin could had different language
-                       while ($admin = DBA::fetch($adminlist)) {
-                               if (in_array($admin['email'], $sent)) {
-                                       continue;
-                               }
-                               $sent[] = $admin['email'];
-
-                               $lang = (($admin['language']) ? $admin['language'] : 'en');
-                               $l10n = DI::l10n()->withLang($lang);
-
-                               $preamble = Strings::deindent($l10n->t('
-                                       The friendica database was successfully updated from %s to %s.',
-                                       $from_build, $to_build));
-
-                               $email = DI::emailer()
-                                       ->newSystemMail()
-                                       ->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble)
-                                       ->forUser($admin)
-                                       ->withRecipient($admin['email'])
-                                       ->build();
-                               DI::emailer()->send($email);
-                       }
+                       $email = DI::emailer()
+                               ->newSystemMail()
+                               ->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble)
+                               ->forUser($admin)
+                               ->withRecipient($admin['email'])
+                               ->build();
+                       DI::emailer()->send($email);
                }
 
                Logger::debug('Database structure update successful.');
index 9a4d2bd60f41b0ed23cfc6a18d8036da67edadcd..b10572612d7d2c7a87b4af48505dafd59b0bd361 100644 (file)
@@ -164,25 +164,16 @@ class Nodeinfo
         *
         * @param IManageConfigValues $config Configuration instance
         * @return array Organization information
+        * @throws \Exception
         */
        public static function getOrganization(IManageConfigValues $config): array
        {
-               $organization = [
-                       'name' => null,
-                       'contact' => null,
-                       'account' => null
-               ];
+               $administrator = User::getFirstAdmin(['username', 'email', 'nickname']);
 
-               if (!empty($config->get('config', 'admin_email'))) {
-                       $adminList = explode(',', str_replace(' ', '', $config->get('config', 'admin_email')));
-                       $organization['contact'] = $adminList[0];
-                       $administrator = User::getByEmail($adminList[0], ['username', 'nickname']);
-                       if (!empty($administrator)) {
-                               $organization['name'] = $administrator['username'];
-                               $organization['account'] = DI::baseUrl()->get() . '/profile/' . $administrator['nickname'];
-                       }
-               }
-
-               return $organization;
+               return [
+                       'name'    => $administrator['username'] ?? null,
+                       'contact' => $administrator['email']    ?? null,
+                       'account' => $administrator['nickname'] ?? '' ? DI::baseUrl()->get() . '/profile/' . $administrator['nickname'] : null,
+               ];
        }
 }
index cc6e11a42d3e031ef60e0e5ea36dda08a66ce98d..73c380561c9c80786e5a98e8f615da7da7dced95 100644 (file)
@@ -381,17 +381,15 @@ class User
         *
         * @param array $fields
         * @return array user
+        * @throws Exception
         */
        public static function getFirstAdmin(array $fields = []) : array
        {
                if (!empty(DI::config()->get('config', 'admin_nickname'))) {
                        return self::getByNickname(DI::config()->get('config', 'admin_nickname'), $fields);
-               } elseif (!empty(DI::config()->get('config', 'admin_email'))) {
-                       $adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
-                       return self::getByEmail($adminList[0], $fields);
-               } else {
-                       return [];
                }
+
+               return self::getAdminList()[0] ?? [];
        }
 
        /**
@@ -1054,11 +1052,8 @@ class User
 
                // Disallow somebody creating an account using openid that uses the admin email address,
                // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
-               if (DI::config()->get('config', 'admin_email') && strlen($openid_url)) {
-                       $adminlist = explode(',', str_replace(' ', '', strtolower(DI::config()->get('config', 'admin_email'))));
-                       if (in_array(strtolower($email), $adminlist)) {
-                               throw new Exception(DI::l10n()->t('Cannot use that email.'));
-                       }
+               if (strlen($openid_url) && in_array(strtolower($email), self::getAdminEmailList())) {
+                       throw new Exception(DI::l10n()->t('Cannot use that email.'));
                }
 
                $nickname = $data['nickname'] = strtolower($nickname);
@@ -1783,4 +1778,64 @@ class User
 
                return DBA::selectToArray('owner-view', [], $condition, $param);
        }
+
+       /**
+        * Returns a list of lowercase admin email addresses from the comma-separated list in the config
+        *
+        * @return array
+        */
+       public static function getAdminEmailList(): array
+       {
+               $adminEmails = strtolower(str_replace(' ', '', DI::config()->get('config', 'admin_email')));
+               if (!$adminEmails) {
+                       return [];
+               }
+
+               return explode(',', $adminEmails);
+       }
+
+       /**
+        * Returns the complete list of admin user accounts
+        *
+        * @param array $fields
+        * @return array
+        * @throws Exception
+        */
+       public static function getAdminList(array $fields = []): array
+       {
+               $condition = [
+                       'email'           => self::getAdminEmailList(),
+                       'parent-uid'      => 0,
+                       'blocked'         => 0,
+                       'verified'        => true,
+                       'account_removed' => false,
+                       'account_expired' => false,
+               ];
+
+               return DBA::selectToArray('user', $fields, $condition, ['order' => ['uid']]);
+       }
+
+       /**
+        * Return a list of admin user accounts where each unique email address appears only once.
+        *
+        * This method is meant for admin notifications that do not need to be sent multiple times to the same email address.
+        *
+        * @param array $fields
+        * @return array
+        * @throws Exception
+        */
+       public static function getAdminListForEmailing(array $fields = []): array
+       {
+               return array_filter(self::getAdminList($fields), function ($user) {
+                       static $emails = [];
+
+                       if (in_array($user['email'], $emails)) {
+                               return false;
+                       }
+
+                       $emails[] = $user['email'];
+
+                       return true;
+               });
+       }
 }
index 8b38ca896716c00a95d41a90c222ec90dd45b2ad..ed27b8ef3a1e8b0bc17d7747630df777ba36c826 100644 (file)
@@ -23,6 +23,7 @@ namespace Friendica\Module\Api\GNUSocial\GNUSocial;
 
 use Friendica\App;
 use Friendica\DI;
+use Friendica\Model\User;
 use Friendica\Module\BaseApi;
 use Friendica\Module\Register;
 
@@ -42,7 +43,7 @@ class Config extends BaseApi
                                'logo'         => DI::baseUrl() . '/images/friendica-64.png',
                                'fancy'        => true,
                                'language'     => DI::config()->get('system', 'language'),
-                               'email'        => DI::config()->get('config', 'admin_email'),
+                               'email'        => implode(',', User::getAdminEmailList()),
                                'broughtby'    => '',
                                'broughtbyurl' => '',
                                'timezone'     => DI::config()->get('system', 'default_timezone'),
index eddfc0909775021c5470a6bbf6f356e0e9fd1fa9..b78027764691a70913db06a8abb2a109d9dcfaa7 100644 (file)
@@ -113,7 +113,7 @@ abstract class BaseUsers extends BaseModeration
 
        protected function setupUserCallback(): \Closure
        {
-               $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
+               $adminlist = User::getAdminEmailList();
                return function ($user) use ($adminlist) {
                        $page_types = [
                                User::PAGE_FLAGS_NORMAL    => $this->t('Normal Account Page'),
index ad3e1feeacce1932d265bd18996ef7ecb7012657..b71fb777c593cc6bc38ec954a4f32a697a78639c 100644 (file)
@@ -352,7 +352,7 @@ class Register extends BaseModule
                                DI::baseUrl()->redirect();
                        }
                } elseif (intval(DI::config()->get('config', 'register_policy')) === self::APPROVE) {
-                       if (!strlen(DI::config()->get('config', 'admin_email'))) {
+                       if (!User::getAdminEmailList()) {
                                DI::sysmsg()->addNotice(DI::l10n()->t('Your registration can not be processed.'));
                                DI::baseUrl()->redirect();
                        }
@@ -387,34 +387,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)
        {
-               // 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)) {
+               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),
+                               '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
                        ]);
                }
-               DBA::close($admins_stmt);               
        }
 }
index c9b140acb3d268efe06167346085773bf7cc00b8..4217b1e88d9af900f8d09fce9cd4cdb6ff9a143e 100644 (file)
@@ -113,12 +113,9 @@ class Account extends BaseSettings
                                        $err .= DI::l10n()->t('Invalid email.');
                                }
                                //  ensure new email is not the admin mail
-                               if (DI::config()->get('config', 'admin_email')) {
-                                       $adminlist = explode(",", str_replace(" ", "", strtolower(DI::config()->get('config', 'admin_email'))));
-                                       if (in_array(strtolower($email), $adminlist)) {
-                                               $err .= DI::l10n()->t('Cannot change to that email.');
-                                               $email = $user['email'];
-                                       }
+                               if (in_array(strtolower($email), User::getAdminEmailList())) {
+                                       $err .= DI::l10n()->t('Cannot change to that email.');
+                                       $email = $user['email'];
                                }
                        }
 
index 87bc6152a5b32fb369c2d0bd3b416a6df71df121..32dffd156f413662c1c2a7700156c551f2f1a310 100644 (file)
@@ -85,14 +85,8 @@ class RemoveMe extends BaseSettings
                }
 
                // send notification to admins so that they can clean up the backups
-               $admin_mails = explode(',', $this->config->get('config', 'admin_email'));
-               foreach ($admin_mails as $mail) {
-                       $admin = $this->database->selectFirst('user', ['uid', 'language', 'email', 'username'], ['email' => trim($mail)]);
-                       if (!$admin) {
-                               continue;
-                       }
-
-                       $l10n = $this->l10n->withLang($admin['language']);
+               foreach (User::getAdminListForEmailing(['uid', 'language', 'email']) as $admin) {
+                       $l10n = $this->l10n->withLang($admin['language'] ?: 'en');
 
                        $email = $this->emailer
                                ->newSystemMail()
index 45fc4bf8951e440fab0d0686eb9b35b6b27ae981..b659dc30636c5e30182a46d5bcd1c047716183d5 100644 (file)
@@ -86,7 +86,7 @@ class Instance extends BaseDataTransferObject
                $this->uri               = $baseUrl->get();
                $this->title             = $config->get('config', 'sitename');
                $this->short_description = $this->description = $config->get('config', 'info');
-               $this->email             = $config->get('config', 'admin_email');
+               $this->email             = implode(',', User::getAdminEmailList());
                $this->version           = '2.8.0 (compatible; Friendica ' . App::VERSION . ')';
                $this->urls              = null; // Not supported
                $this->stats             = new Stats($config, $database);
@@ -98,13 +98,10 @@ class Instance extends BaseDataTransferObject
                $this->invites_enabled   = false;
                $this->contact_account   = [];
 
-               if (!empty($config->get('config', 'admin_email'))) {
-                       $adminList = explode(',', str_replace(' ', '', $config->get('config', 'admin_email')));
-                       $administrator = User::getByEmail($adminList[0], ['nickname']);
-                       if (!empty($administrator)) {
-                               $adminContact = $database->selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
-                               $this->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
-                       }
+               $administrator = User::getFirstAdmin(['nickname']);
+               if ($administrator) {
+                       $adminContact = $database->selectFirst('contact', ['uri-id'], ['nick' => $administrator['nickname'], 'self' => true]);
+                       $this->contact_account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']);
                }
        }
 }
index 95bb0f3121fc6b53538655fdb596e7e52945c83e..ca9a605fc40d0180dc0c2b75963da5fc9f7fc914 100644 (file)
@@ -1710,7 +1710,7 @@ class Transmitter
                }
 
                $data['attachment'] = self::createAttachmentList($item);
-               $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? '');  
+               $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? '');
 
                if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
                        $data['location'] = self::createLocation($item);
@@ -2073,13 +2073,14 @@ class Transmitter
                }
 
                if (empty($uid)) {
-                       // Fetch the list of administrators
-                       $admin_mail = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
-
                        // We need to use some user as a sender. It doesn't care who it will send. We will use an administrator account.
-                       $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'email' => $admin_mail];
-                       $first_user = DBA::selectFirst('user', ['uid'], $condition);
-                       $uid = $first_user['uid'];
+                       $admin = User::getFirstAdmin(['uid']);
+                       if (!$admin) {
+                               Logger::warning('No available admin user for transmission', ['target' => $target]);
+                               return false;
+                       }
+
+                       $uid = $admin['uid'];
                }
 
                $condition = ['verb' => Activity::FOLLOW, 'uid' => 0, 'parent-uri' => $object,