From 39607b20e2973fb271544b69b3594d5a56b547c8 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 18 Dec 2022 23:24:59 -0500 Subject: [PATCH] Convert custom profile field URL values to rel="me" links --- src/Content/Text/HTML.php | 37 +++++++++------------------ src/Module/Profile/Profile.php | 25 +++++++++++++++--- src/Util/Strings.php | 46 +++++++++++++++++++++++----------- 3 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/Content/Text/HTML.php b/src/Content/Text/HTML.php index 2e55c71d20..fa5a0a5905 100644 --- a/src/Content/Text/HTML.php +++ b/src/Content/Text/HTML.php @@ -736,22 +736,22 @@ class HTML '[youtube]$2[/youtube]', $s ); - + $s = preg_replace( '#](.*?)https?://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)#ism', '[youtube]$2[/youtube]', $s ); - + $s = preg_replace( '#](.*?)https?://player.vimeo.com/video/([0-9]+)(.*?)#ism', '[vimeo]$2[/vimeo]', $s ); - + return $s; } - + /** * transform link href and img src from relative to absolute * @@ -764,30 +764,30 @@ class HTML if (empty($base)) { return $text; } - + $base = rtrim($base, '/'); - + $base2 = $base . "/"; - + // Replace links $pattern = "/]*) href=\"(?!http|https|\/)([^\"]*)\"/"; $replace = "$1', $s); - $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism", '<$1$2=$3&$4>', $s); - return $s; - } - /** * Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons * diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index bed5ffce7f..864b8a76e8 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -26,7 +26,6 @@ use Friendica\Content\Feature; use Friendica\Content\ForumManager; use Friendica\Content\Nav; use Friendica\Content\Text\BBCode; -use Friendica\Content\Text\HTML; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Hook; use Friendica\Core\L10n; @@ -48,6 +47,7 @@ use Friendica\Profile\ProfileField\Repository\ProfileField; use Friendica\Protocol\ActivityPub; use Friendica\Util\DateTimeFormat; use Friendica\Util\Profiler; +use Friendica\Util\Strings; use Friendica\Util\Temporal; use Psr\Log\LoggerInterface; @@ -204,7 +204,11 @@ class Profile extends BaseProfile } if ($profile['homepage']) { - $basic_fields += self::buildField('homepage', $this->t('Homepage:'), HTML::toLink($profile['homepage'])); + $basic_fields += self::buildField( + 'homepage', + $this->t('Homepage:'), + $this->tryRelMe($profile['homepage']) ?: $profile['homepage'] + ); } if ( @@ -245,7 +249,7 @@ class Profile extends BaseProfile $custom_fields += self::buildField( 'custom_' . $profile_field->order, $profile_field->label, - BBCode::convertForUriId($profile['uri-id'], $profile_field->value), + $this->tryRelMe($profile_field->value) ?: BBCode::convertForUriId($profile['uri-id'], $profile_field->value), 'aprofile custom' ); } @@ -359,4 +363,19 @@ class Profile extends BaseProfile return $htmlhead; } + + /** + * Check if the input is an HTTP(S) link and returns a rel="me" link if yes, empty string if not + * + * @param string $input + * @return string + */ + private function tryRelMe(string $input): string + { + if (preg_match(Strings::onlyLinkRegEx(), trim($input))) { + return '' . trim($input) . ''; + } + + return ''; + } } diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 379f2a2521..c5d5c760a3 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -380,29 +380,47 @@ class Strings * Returns the regular expression string to match URLs in a given text * * @return string - * @see https://daringfireball.net/2010/07/improved_regex_for_matching_urls */ public static function autoLinkRegEx(): string { return '@ -(??«»“”‘’.] # Domain can\'t start with a . - [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’]+ # Domain can\'t end with a . + [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’.] # Domain can\'t start with a . + [^/\s\xA0`!()\[\]{};:\'",<>?«»“”‘’]+ # Domain can\'t end with a . \. [^/\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’]+/? # Followed by a slash ) - (?: # One or more: - [^\s\xA0()<>]+ # Run of non-space, non-()<> - | # or - \(([^\s\xA0()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels - | # or - [^\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars - )* -)@xiu'; + (?: # One or more: + [^\s\xA0()<>]+ # Run of non-space, non-()<> + | # or + \(([^\s\xA0()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels + | # or + [^\s\xA0`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars + )*'; } /** -- 2.39.5