]> git.mxchange.org Git - friendica.git/blobdiff - src/Moderation/DomainPatternBlocklist.php
Minor l10n issue
[friendica.git] / src / Moderation / DomainPatternBlocklist.php
index 33bd189d6d56fb6b595dc74d19c724abac153a14..923fca88f03740f24c7bb35664b4406031a781c7 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Moderation;
 
 use Exception;
+use Friendica\App\BaseURL;
 use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\L10n;
 use Friendica\Database\Database;
@@ -30,16 +31,28 @@ use Friendica\Util\Emailer;
 
 class DomainPatternBlocklist
 {
-       const DEFAULT_REASON = 'blocked';
-
-       /**
-        * @var IManageConfigValues
-        */
+       /** @var IManageConfigValues */
        private $config;
 
-       public function __construct(IManageConfigValues $config)
+       /** @var Database */
+       private $db;
+
+       /** @var Emailer */
+       private $emailer;
+
+       /** @var L10n */
+       private $l10n;
+
+       /** @var BaseURL */
+       private $baseUrl;
+
+       public function __construct(IManageConfigValues $config, Database $db, Emailer $emailer, L10n $l10n, BaseURL $baseUrl)
        {
-               $this->config = $config;
+               $this->config  = $config;
+               $this->db      = $db;
+               $this->emailer = $emailer;
+               $this->l10n    = $l10n;
+               $this->baseUrl = $baseUrl;
        }
 
        public function get(): array
@@ -49,15 +62,21 @@ class DomainPatternBlocklist
 
        public function set(array $blocklist): bool
        {
-               return $this->config->set('system', 'blocklist', $blocklist);
+               $result = $this->config->set('system', 'blocklist', $blocklist);
+               if ($result) {
+                       $this->notifyAll();
+               }
+
+               return $result;
        }
 
        /**
-        * @param string      $pattern
-        * @param string|null $reason
+        * @param string $pattern
+        * @param string $reason
+        *
         * @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
         */
-       public function addPattern(string $pattern, string $reason = null): int
+       public function addPattern(string $pattern, string $reason): int
        {
                $update = false;
 
@@ -66,7 +85,7 @@ class DomainPatternBlocklist
                        if ($blocked['domain'] === $pattern) {
                                $blocklist[] = [
                                        'domain' => $pattern,
-                                       'reason' => $reason ?? self::DEFAULT_REASON,
+                                       'reason' => $reason,
                                ];
 
                                $update = true;
@@ -78,7 +97,7 @@ class DomainPatternBlocklist
                if (!$update) {
                        $blocklist[] = [
                                'domain' => $pattern,
-                               'reason' => $reason ?? self::DEFAULT_REASON,
+                               'reason' => $reason,
                        ];
                }
 
@@ -87,6 +106,7 @@ class DomainPatternBlocklist
 
        /**
         * @param string $pattern
+        *
         * @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
         */
        public function removePattern(string $pattern): int
@@ -105,6 +125,12 @@ class DomainPatternBlocklist
                return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
        }
 
+       /**
+        * @param string $filename
+        *
+        * @return void
+        * @throws Exception
+        */
        public function exportToFile(string $filename)
        {
                $fp = fopen($filename, 'w');
@@ -121,6 +147,7 @@ class DomainPatternBlocklist
         * Appends to the local block list all the patterns from the provided list that weren't already present.
         *
         * @param array $blocklist
+        *
         * @return int The number of patterns actually added to the block list
         */
        public function append(array $blocklist): int
@@ -147,6 +174,7 @@ class DomainPatternBlocklist
         * Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
         *
         * @param string $filename
+        *
         * @return array
         * @throws Exception
         */
@@ -159,22 +187,61 @@ class DomainPatternBlocklist
 
                $blocklist = [];
                while (($data = fgetcsv($fp, 1000)) !== false) {
-                       $domain = $data[0];
-                       if (count($data) == 0) {
-                               $reason = self::DEFAULT_REASON;
-                       } else {
-                               $reason = $data[1];
-                       }
-
-                       $data = [
-                               'domain' => $domain,
-                               'reason' => $reason
+                       $item = [
+                               'domain' => $data[0],
+                               'reason' => $data[1] ?? '',
                        ];
-                       if (!in_array($data, $blocklist)) {
+                       if (!in_array($item, $blocklist)) {
                                $blocklist[] = $data;
                        }
                }
 
                return $blocklist;
        }
+
+       /**
+        * Sends a system email to all the node users about a change in the block list. Sends a single email to each unique
+        * email address among the valid users.
+        *
+        * @return int The number of recipients that were sent an email
+        * @throws HTTPException\InternalServerErrorException
+        * @throws HTTPException\UnprocessableEntityException
+        */
+       public function notifyAll(): int
+       {
+               // Gathering all non-system parent users who verified their email address and aren't blocked or about to be deleted
+               // We sort on language to minimize the number of actual language switches during the email build loop
+               $recipients = $this->db->selectToArray(
+                       'user',
+                       ['username', 'email', 'language'],
+                       ['`uid` > 0 AND `parent-uid` = 0 AND `verified` AND NOT `account_removed` AND NOT `account_expired` AND NOT `blocked`'],
+                       ['group_by' => ['email'], 'order' => ['language']]
+               );
+               if (!$recipients) {
+                       return 0;
+               }
+
+               foreach ($recipients as $recipient) {
+                       $l10n  = $this->l10n->withLang($recipient['language']);
+                       $email = $this->emailer->newSystemMail()
+                               ->withMessage(
+                                       $l10n->t('[%s] Notice of remote server domain pattern block list update', $this->emailer->getSiteEmailName()),
+                                       $l10n->t(
+                                               'Dear %s,
+
+You are receiving this email because the Friendica node at %s where you are registered as a user updated their remote server domain pattern block list.
+
+Please review the updated list at %s at your earliest convenience.',
+                                               $recipient['username'],
+                                               $this->baseUrl->get(),
+                                               $this->baseUrl . '/friendica'
+                                       )
+                               )
+                               ->withRecipient($recipient['email'])
+                               ->build();
+                       $this->emailer->send($email);
+               }
+
+               return count($recipients);
+       }
 }