]> git.mxchange.org Git - friendica.git/commitdiff
Use centralized functions for tag string handling
authorMichael <heluecht@pirati.ca>
Sun, 28 Jan 2024 06:32:55 +0000 (06:32 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 28 Jan 2024 06:32:55 +0000 (06:32 +0000)
src/Model/Tag.php
src/Module/Admin/Site.php
src/Module/Settings/Channels.php
src/Module/WellKnown/XSocialRelay.php
src/Protocol/Relay.php
src/Util/Strings.php

index 398079e3c6ba1daa7aef35c70fae54a403dcf5be..dac0401b2be988bf39a38c2514900595e27ab175 100644 (file)
@@ -667,12 +667,11 @@ class Tag
         */
        private static function getBlockedSQL(): string
        {
-               $blocked_txt = DI::config()->get('system', 'blocked_tags');
-               if (empty($blocked_txt)) {
+               $blocked = Strings::getTagArrayByString(DI::config()->get('system', 'blocked_tags'));
+               if (empty($blocked)) {
                        return '';
                }
 
-               $blocked = explode(',', $blocked_txt);
                array_walk($blocked, function (&$value) {
                        $value = "'" . DBA::escape(trim($value)) . "'";
                });
index 22309b8eb79cbaa3f84a316cb798d9469798802a..6daa8dddf811301701e1f1fa269e2bd4d17ddc08 100644 (file)
@@ -271,7 +271,7 @@ class Site extends BaseAdmin
                $transactionConfig->set('system', 'explicit_content'       , $explicit_content);
                $transactionConfig->set('system', 'proxify_content'        , $proxify_content);
                $transactionConfig->set('system', 'local_search'           , $local_search);
-               $transactionConfig->set('system', 'blocked_tags'           , $blocked_tags);
+               $transactionConfig->set('system', 'blocked_tags'           , Strings::cleanTags($blocked_tags));
                $transactionConfig->set('system', 'cache_contact_avatar'   , $cache_contact_avatar);
                $transactionConfig->set('system', 'check_new_version_url'  , $check_new_version_url);
 
@@ -332,8 +332,8 @@ class Site extends BaseAdmin
                
                $transactionConfig->set('system', 'relay_directly'                , $relay_directly);
                $transactionConfig->set('system', 'relay_scope'                   , $relay_scope);
-               $transactionConfig->set('system', 'relay_server_tags'             , $relay_server_tags);
-               $transactionConfig->set('system', 'relay_deny_tags'               , $relay_deny_tags);
+               $transactionConfig->set('system', 'relay_server_tags'             , Strings::cleanTags($relay_server_tags));
+               $transactionConfig->set('system', 'relay_deny_tags'               , Strings::cleanTags($relay_deny_tags));
                $transactionConfig->set('system', 'relay_max_tags'                , $relay_max_tags);
                $transactionConfig->set('system', 'relay_user_tags'               , $relay_user_tags);
                $transactionConfig->set('system', 'relay_deny_undetected_language', $relay_deny_undetected_language);
index d846a91be733d085b146f3de48b8aabebd49c73b..34ba0d1d80efe79034b0a576225854399aa01777 100644 (file)
@@ -34,6 +34,7 @@ use Friendica\Module\BaseSettings;
 use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
 use Friendica\Util\Profiler;
+use Friendica\Util\Strings;
 use Psr\Log\LoggerInterface;
 
 class Channels extends BaseSettings
@@ -80,8 +81,8 @@ class Channels extends BaseSettings
                                'access-key'       => substr(mb_strtolower($request['new_access_key']), 0, 1),
                                'uid'              => $uid,
                                'circle'           => (int)$request['new_circle'],
-                               'include-tags'     => $this->cleanTags($request['new_include_tags']),
-                               'exclude-tags'     => $this->cleanTags($request['new_exclude_tags']),
+                               'include-tags'     => Strings::cleanTags($request['new_include_tags']),
+                               'exclude-tags'     => Strings::cleanTags($request['new_exclude_tags']),
                                'full-text-search' => $request['new_text_search'],
                                'media-type'       => ($request['new_image'] ? 1 : 0) | ($request['new_video'] ? 2 : 0) | ($request['new_audio'] ? 4 : 0),
                                'languages'        => $request['new_languages'],
@@ -109,8 +110,8 @@ class Channels extends BaseSettings
                                'access-key'       => substr(mb_strtolower($request['access_key'][$id]), 0, 1),
                                'uid'              => $uid,
                                'circle'           => (int)$request['circle'][$id],
-                               'include-tags'     => $this->cleanTags($request['include_tags'][$id]),
-                               'exclude-tags'     => $this->cleanTags($request['exclude_tags'][$id]),
+                               'include-tags'     => Strings::cleanTags($request['include_tags'][$id]),
+                               'exclude-tags'     => Strings::cleanTags($request['exclude_tags'][$id]),
                                'full-text-search' => $request['text_search'][$id],
                                'media-type'       => ($request['image'][$id] ? 1 : 0) | ($request['video'][$id] ? 2 : 0) | ($request['audio'][$id] ? 4 : 0),
                                'languages'        => $request['languages'][$id],
@@ -222,18 +223,4 @@ class Channels extends BaseSettings
                        '$form_security_token' => self::getFormSecurityToken('settings_channels'),
                ]);
        }
-
-       private function cleanTags(string $tag_list): string
-       {
-               $tags = [];
-
-               $tagitems = explode(',', mb_strtolower($tag_list));
-               foreach ($tagitems as $tag) {
-                       $tag = trim($tag, '# ');
-                       if (!empty($tag)) {
-                               $tags[] = preg_replace('#\s#u', '', $tag);
-                       }
-               }
-               return implode(',', $tags);
-       }
 }
index 50cfb8aa826b6e5dac0c86b4bc0bc863156d351c..39b67de846f20a4d57e9e86c2c9b123382e823e3 100644 (file)
 namespace Friendica\Module\WellKnown;
 
 use Friendica\BaseModule;
-use Friendica\Core\System;
 use Friendica\DI;
 use Friendica\Model\Search;
 use Friendica\Protocol\Relay;
+use Friendica\Util\Strings;
 
 /**
  * Node subscription preferences for social relay systems
@@ -43,13 +43,7 @@ class XSocialRelay extends BaseModule
                $userTags = [];
 
                if ($scope == Relay::SCOPE_TAGS) {
-                       $server_tags = $config->get('system', 'relay_server_tags');
-                       $tagitems = explode(',', $server_tags);
-
-                       /// @todo Check if it was better to use "strtolower" on the tags
-                       foreach ($tagitems as $tag) {
-                               $systemTags[] = trim($tag, '# ');
-                       }
+                       $systemTags = Strings::getTagArrayByString($config->get('system', 'relay_server_tags'));
 
                        if ($config->get('system', 'relay_user_tags')) {
                                $userTags = Search::getUserTags();
index db61e34791f55db17243d211f1262bb0b91a48b7..3c655f8cb7e627439988970a9010a225a9ed3a09 100644 (file)
@@ -97,20 +97,13 @@ class Relay
 
                $body = ActivityPub\Processor::normalizeMentionLinks($body);
 
-               $denyTags = [];
-
                if ($scope == self::SCOPE_TAGS) {
                        $tagList = self::getSubscribedTags();
                } else {
                        $tagList  = [];
                }
 
-               $deny_tags = $config->get('system', 'relay_deny_tags');
-               $tagitems = explode(',', mb_strtolower($deny_tags));
-               foreach ($tagitems as $tag) {
-                       $tag = trim($tag, '# ');
-                       $denyTags[] = $tag;
-               }
+               $denyTags = Strings::getTagArrayByString($config->get('system', 'relay_deny_tags'));
 
                if (!empty($tagList) || !empty($denyTags)) {
                        $content = mb_strtolower(BBCode::toPlaintext($body, false));
@@ -168,10 +161,7 @@ class Relay
         */
        public static function getSubscribedTags(): array
        {
-               $tags  = [];
-               foreach (explode(',', mb_strtolower(DI::config()->get('system', 'relay_server_tags'))) as $tag) {
-                       $tags[] = trim($tag, '# ');
-               }
+               $tags = Strings::getTagArrayByString(DI::config()->get('system', 'relay_server_tags'));
 
                if (DI::config()->get('system', 'relay_user_tags')) {
                        $tags = array_merge($tags, Search::getUserTags());
index fff218e46602ee63e8a9d202297df3c7df30396a..34029648ee4f82c582696715977200f64bf266fc 100644 (file)
@@ -578,4 +578,36 @@ class Strings
 
                return $styled_url;
        }
+
+       /**
+        * Sort a comma separated list of hashtags, convert them to lowercase and remove duplicates
+        *
+        * @param string $tag_list
+        * @return string
+        */
+       public static function cleanTags(string $tag_list): string
+       {
+               $tags = [];
+
+               $tagitems = explode(',', str_replace([' ', ';', '#'], ',', mb_strtolower($tag_list)));
+               foreach ($tagitems as $tag) {
+                       if (!empty($tag)) {
+                               $tags[] = preg_replace('#\s#u', '', $tag);
+                       }
+               }
+               $tags = array_unique($tags);
+               asort($tags);
+               return implode(',', $tags);
+       }
+
+       /**
+        * Get a tag array out of a comma separated list of tags
+        *
+        * @param string $tag_list
+        * @return array
+        */
+       public static function getTagArrayByString(string $tag_list): array
+       {
+               return explode(',', self::cleanTags($tag_list));
+       }
 }