Merge pull request #13858 from annando/tags
Use unified functions for tag string handling
This commit is contained in:
commit
9af5abaf0b
7 changed files with 64 additions and 45 deletions
|
@ -667,12 +667,11 @@ class Tag
|
||||||
*/
|
*/
|
||||||
private static function getBlockedSQL(): string
|
private static function getBlockedSQL(): string
|
||||||
{
|
{
|
||||||
$blocked_txt = DI::config()->get('system', 'blocked_tags');
|
$blocked = Strings::getTagArrayByString(DI::config()->get('system', 'blocked_tags'));
|
||||||
if (empty($blocked_txt)) {
|
if (empty($blocked)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$blocked = explode(',', $blocked_txt);
|
|
||||||
array_walk($blocked, function (&$value) {
|
array_walk($blocked, function (&$value) {
|
||||||
$value = "'" . DBA::escape(trim($value)) . "'";
|
$value = "'" . DBA::escape(trim($value)) . "'";
|
||||||
});
|
});
|
||||||
|
|
|
@ -271,7 +271,7 @@ class Site extends BaseAdmin
|
||||||
$transactionConfig->set('system', 'explicit_content' , $explicit_content);
|
$transactionConfig->set('system', 'explicit_content' , $explicit_content);
|
||||||
$transactionConfig->set('system', 'proxify_content' , $proxify_content);
|
$transactionConfig->set('system', 'proxify_content' , $proxify_content);
|
||||||
$transactionConfig->set('system', 'local_search' , $local_search);
|
$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', 'cache_contact_avatar' , $cache_contact_avatar);
|
||||||
$transactionConfig->set('system', 'check_new_version_url' , $check_new_version_url);
|
$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_directly' , $relay_directly);
|
||||||
$transactionConfig->set('system', 'relay_scope' , $relay_scope);
|
$transactionConfig->set('system', 'relay_scope' , $relay_scope);
|
||||||
$transactionConfig->set('system', 'relay_server_tags' , $relay_server_tags);
|
$transactionConfig->set('system', 'relay_server_tags' , Strings::cleanTags($relay_server_tags));
|
||||||
$transactionConfig->set('system', 'relay_deny_tags' , $relay_deny_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_max_tags' , $relay_max_tags);
|
||||||
$transactionConfig->set('system', 'relay_user_tags' , $relay_user_tags);
|
$transactionConfig->set('system', 'relay_user_tags' , $relay_user_tags);
|
||||||
$transactionConfig->set('system', 'relay_deny_undetected_language', $relay_deny_undetected_language);
|
$transactionConfig->set('system', 'relay_deny_undetected_language', $relay_deny_undetected_language);
|
||||||
|
|
|
@ -34,6 +34,7 @@ use Friendica\Module\BaseSettings;
|
||||||
use Friendica\Module\Response;
|
use Friendica\Module\Response;
|
||||||
use Friendica\Network\HTTPException;
|
use Friendica\Network\HTTPException;
|
||||||
use Friendica\Util\Profiler;
|
use Friendica\Util\Profiler;
|
||||||
|
use Friendica\Util\Strings;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class Channels extends BaseSettings
|
class Channels extends BaseSettings
|
||||||
|
@ -80,8 +81,8 @@ class Channels extends BaseSettings
|
||||||
'access-key' => substr(mb_strtolower($request['new_access_key']), 0, 1),
|
'access-key' => substr(mb_strtolower($request['new_access_key']), 0, 1),
|
||||||
'uid' => $uid,
|
'uid' => $uid,
|
||||||
'circle' => (int)$request['new_circle'],
|
'circle' => (int)$request['new_circle'],
|
||||||
'include-tags' => $this->cleanTags($request['new_include_tags']),
|
'include-tags' => Strings::cleanTags($request['new_include_tags']),
|
||||||
'exclude-tags' => $this->cleanTags($request['new_exclude_tags']),
|
'exclude-tags' => Strings::cleanTags($request['new_exclude_tags']),
|
||||||
'full-text-search' => $request['new_text_search'],
|
'full-text-search' => $request['new_text_search'],
|
||||||
'media-type' => ($request['new_image'] ? 1 : 0) | ($request['new_video'] ? 2 : 0) | ($request['new_audio'] ? 4 : 0),
|
'media-type' => ($request['new_image'] ? 1 : 0) | ($request['new_video'] ? 2 : 0) | ($request['new_audio'] ? 4 : 0),
|
||||||
'languages' => $request['new_languages'],
|
'languages' => $request['new_languages'],
|
||||||
|
@ -109,8 +110,8 @@ class Channels extends BaseSettings
|
||||||
'access-key' => substr(mb_strtolower($request['access_key'][$id]), 0, 1),
|
'access-key' => substr(mb_strtolower($request['access_key'][$id]), 0, 1),
|
||||||
'uid' => $uid,
|
'uid' => $uid,
|
||||||
'circle' => (int)$request['circle'][$id],
|
'circle' => (int)$request['circle'][$id],
|
||||||
'include-tags' => $this->cleanTags($request['include_tags'][$id]),
|
'include-tags' => Strings::cleanTags($request['include_tags'][$id]),
|
||||||
'exclude-tags' => $this->cleanTags($request['exclude_tags'][$id]),
|
'exclude-tags' => Strings::cleanTags($request['exclude_tags'][$id]),
|
||||||
'full-text-search' => $request['text_search'][$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),
|
'media-type' => ($request['image'][$id] ? 1 : 0) | ($request['video'][$id] ? 2 : 0) | ($request['audio'][$id] ? 4 : 0),
|
||||||
'languages' => $request['languages'][$id],
|
'languages' => $request['languages'][$id],
|
||||||
|
@ -222,18 +223,4 @@ class Channels extends BaseSettings
|
||||||
'$form_security_token' => self::getFormSecurityToken('settings_channels'),
|
'$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,10 @@
|
||||||
namespace Friendica\Module\WellKnown;
|
namespace Friendica\Module\WellKnown;
|
||||||
|
|
||||||
use Friendica\BaseModule;
|
use Friendica\BaseModule;
|
||||||
use Friendica\Core\System;
|
|
||||||
use Friendica\DI;
|
use Friendica\DI;
|
||||||
use Friendica\Model\Search;
|
use Friendica\Model\Search;
|
||||||
use Friendica\Protocol\Relay;
|
use Friendica\Protocol\Relay;
|
||||||
|
use Friendica\Util\Strings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Node subscription preferences for social relay systems
|
* Node subscription preferences for social relay systems
|
||||||
|
@ -43,13 +43,7 @@ class XSocialRelay extends BaseModule
|
||||||
$userTags = [];
|
$userTags = [];
|
||||||
|
|
||||||
if ($scope == Relay::SCOPE_TAGS) {
|
if ($scope == Relay::SCOPE_TAGS) {
|
||||||
$server_tags = $config->get('system', 'relay_server_tags');
|
$systemTags = Strings::getTagArrayByString($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, '# ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($config->get('system', 'relay_user_tags')) {
|
if ($config->get('system', 'relay_user_tags')) {
|
||||||
$userTags = Search::getUserTags();
|
$userTags = Search::getUserTags();
|
||||||
|
|
|
@ -97,20 +97,13 @@ class Relay
|
||||||
|
|
||||||
$body = ActivityPub\Processor::normalizeMentionLinks($body);
|
$body = ActivityPub\Processor::normalizeMentionLinks($body);
|
||||||
|
|
||||||
$denyTags = [];
|
|
||||||
|
|
||||||
if ($scope == self::SCOPE_TAGS) {
|
if ($scope == self::SCOPE_TAGS) {
|
||||||
$tagList = self::getSubscribedTags();
|
$tagList = self::getSubscribedTags();
|
||||||
} else {
|
} else {
|
||||||
$tagList = [];
|
$tagList = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$deny_tags = $config->get('system', 'relay_deny_tags');
|
$denyTags = Strings::getTagArrayByString($config->get('system', 'relay_deny_tags'));
|
||||||
$tagitems = explode(',', mb_strtolower($deny_tags));
|
|
||||||
foreach ($tagitems as $tag) {
|
|
||||||
$tag = trim($tag, '# ');
|
|
||||||
$denyTags[] = $tag;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($tagList) || !empty($denyTags)) {
|
if (!empty($tagList) || !empty($denyTags)) {
|
||||||
$content = mb_strtolower(BBCode::toPlaintext($body, false));
|
$content = mb_strtolower(BBCode::toPlaintext($body, false));
|
||||||
|
@ -121,7 +114,7 @@ class Relay
|
||||||
$content_cleaned = mb_strtolower(BBCode::toPlaintext($cleaned, false));
|
$content_cleaned = mb_strtolower(BBCode::toPlaintext($cleaned, false));
|
||||||
|
|
||||||
if (strlen($content_cleaned) < strlen($content) / 2) {
|
if (strlen($content_cleaned) < strlen($content) / 2) {
|
||||||
Logger::info('Possible hashtag spam detected - rejected', ['hashtags' => $tags, 'network' => $network, 'url' => $url, 'causer' => $causer, 'body' => $body]);
|
Logger::info('Possible hashtag spam detected - rejected', ['hashtags' => $tags, 'network' => $network, 'url' => $url, 'causer' => $causer, 'content' => $content]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,10 +161,7 @@ class Relay
|
||||||
*/
|
*/
|
||||||
public static function getSubscribedTags(): array
|
public static function getSubscribedTags(): array
|
||||||
{
|
{
|
||||||
$tags = [];
|
$tags = Strings::getTagArrayByString(DI::config()->get('system', 'relay_server_tags'));
|
||||||
foreach (explode(',', mb_strtolower(DI::config()->get('system', 'relay_server_tags'))) as $tag) {
|
|
||||||
$tags[] = trim($tag, '# ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DI::config()->get('system', 'relay_user_tags')) {
|
if (DI::config()->get('system', 'relay_user_tags')) {
|
||||||
$tags = array_merge($tags, Search::getUserTags());
|
$tags = array_merge($tags, Search::getUserTags());
|
||||||
|
|
|
@ -578,4 +578,36 @@ class Strings
|
||||||
|
|
||||||
return $styled_url;
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,4 +214,21 @@ class StringsTest extends TestCase
|
||||||
|
|
||||||
self::assertEquals($originalText, $text);
|
self::assertEquals($originalText, $text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCleanTags()
|
||||||
|
{
|
||||||
|
$rawTags = 'Open, #Source, Friendica Software; Federation #Fediverse';
|
||||||
|
$cleaned = 'federation,fediverse,friendica,open,software,source';
|
||||||
|
|
||||||
|
self::assertEquals($cleaned, Strings::cleanTags($rawTags));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testgetTagArrayByString()
|
||||||
|
{
|
||||||
|
$list = 'Open, #Source, Friendica Software; Federation #Fediverse';
|
||||||
|
$tags = ['federation', 'fediverse', 'friendica', 'open', 'software', 'source'];
|
||||||
|
|
||||||
|
self::assertEquals($tags, Strings::getTagArrayByString($list));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue