2021-09-07 05:01:20 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-09-07 05:01:20 +09:00
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Component\Link;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
2021-09-14 21:40:50 +09:00
|
|
|
use App\Core\Event;
|
2021-09-07 05:01:20 +09:00
|
|
|
use App\Core\Modules\Component;
|
2021-12-28 15:16:32 +09:00
|
|
|
use App\Entity\Actor;
|
2021-09-21 00:16:42 +09:00
|
|
|
use App\Entity\Note;
|
2021-09-07 05:01:20 +09:00
|
|
|
use App\Util\Common;
|
2021-09-20 21:08:17 +09:00
|
|
|
use App\Util\HTML;
|
2021-12-10 11:38:47 +09:00
|
|
|
use Component\Link\Entity\NoteToLink;
|
2021-09-07 05:01:20 +09:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
class Link extends Component
|
|
|
|
{
|
|
|
|
/**
|
2021-09-14 21:40:50 +09:00
|
|
|
* Extract URLs from $content and create the appropriate Link and NoteToLink entities
|
2021-09-07 05:01:20 +09:00
|
|
|
*/
|
2022-02-14 07:55:54 +09:00
|
|
|
public function onProcessNoteContent(Note $note, string $content, string $content_type, array $process_note_content_extra_args = []): bool
|
2021-09-07 05:01:20 +09:00
|
|
|
{
|
2022-02-14 07:55:54 +09:00
|
|
|
$ignore = $process_note_content_extra_args['ignoreLinks'] ?? [];
|
2021-09-07 05:01:20 +09:00
|
|
|
if (Common::config('attachments', 'process_links')) {
|
2021-09-14 21:40:50 +09:00
|
|
|
$matched_urls = [];
|
2022-02-14 07:55:54 +09:00
|
|
|
preg_match_all($this->getURLRegex(), $content, $matched_urls);
|
2021-10-25 05:30:47 +09:00
|
|
|
$matched_urls = array_unique($matched_urls[1]);
|
2021-09-07 05:01:20 +09:00
|
|
|
foreach ($matched_urls as $match) {
|
2022-02-16 12:01:25 +09:00
|
|
|
if (\in_array($match, $ignore)) {
|
2022-02-14 07:55:54 +09:00
|
|
|
continue;
|
|
|
|
}
|
2021-09-07 05:01:20 +09:00
|
|
|
try {
|
2021-09-20 21:08:17 +09:00
|
|
|
$link_id = Entity\Link::getOrCreate($match)->getId();
|
2021-09-21 00:16:42 +09:00
|
|
|
DB::persist(NoteToLink::create(['link_id' => $link_id, 'note_id' => $note->getId()]));
|
2021-09-07 05:01:20 +09:00
|
|
|
} catch (InvalidArgumentException) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-14 21:40:50 +09:00
|
|
|
return Event::next;
|
2021-09-07 05:01:20 +09:00
|
|
|
}
|
2021-09-20 21:08:17 +09:00
|
|
|
|
2021-12-03 09:44:45 +09:00
|
|
|
public function onRenderPlainTextNoteContent(string &$text): bool
|
2021-09-20 21:08:17 +09:00
|
|
|
{
|
|
|
|
$text = $this->replaceURLs($text);
|
2021-11-28 00:06:46 +09:00
|
|
|
return Event::next;
|
2021-09-20 21:08:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getURLRegex(): string
|
|
|
|
{
|
|
|
|
$geouri_labeltext_regex = '\pN\pL\-';
|
|
|
|
$geouri_mark_regex = '\-\_\.\!\~\*\\\'\(\)'; // the \\\' is really pretty
|
|
|
|
$geouri_unreserved_regex = '\pN\pL' . $geouri_mark_regex;
|
|
|
|
$geouri_punreserved_regex = '\[\]\:\&\+\$';
|
|
|
|
$geouri_pctencoded_regex = '(?:\%[0-9a-fA-F][0-9a-fA-F])';
|
|
|
|
$geouri_paramchar_regex = $geouri_unreserved_regex . $geouri_punreserved_regex; //FIXME: add $geouri_pctencoded_regex here so it works
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
return '#'
|
|
|
|
. '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'
|
|
|
|
. '('
|
|
|
|
. '(?:'
|
|
|
|
. '(?:' //Known protocols
|
|
|
|
. '(?:'
|
|
|
|
. '(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_COLON_DOUBLE_SLASH)) . ')://)'
|
|
|
|
. '|'
|
|
|
|
. '(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_SINGLE_COLON)) . '):)'
|
|
|
|
. ')'
|
|
|
|
. '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?' //user:pass@
|
|
|
|
. '(?:'
|
|
|
|
. '(?:'
|
|
|
|
. '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]' //[dns]
|
|
|
|
. ')|(?:'
|
|
|
|
. '[\pN\pL\-\_\:\.]+(?<![\.\:])' //dns
|
|
|
|
. ')'
|
|
|
|
. ')'
|
|
|
|
. ')'
|
|
|
|
. '|(?:'
|
|
|
|
. '(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_COLON_COORDINATES)) . '):'
|
2021-09-20 21:08:17 +09:00
|
|
|
// There's an order that must be followed here too, if ;crs= is used, it must precede ;u=
|
|
|
|
// Also 'crsp' (;crs=$crsp) must match $geouri_labeltext_regex
|
|
|
|
// Also 'uval' (;u=$uval) must be a pnum: \-?[0-9]+
|
2021-10-10 17:26:18 +09:00
|
|
|
. '(?:'
|
|
|
|
. '(?:[0-9]+(?:\.[0-9]+)?(?:\,[0-9]+(?:\.[0-9]+)?){1,2})' // 1(.23)?(,4(.56)){1,2}
|
|
|
|
. '(?:\;(?:[' . $geouri_labeltext_regex . ']+)(?:\=[' . $geouri_paramchar_regex . ']+)*)*'
|
|
|
|
. ')'
|
|
|
|
. ')'
|
2021-09-20 21:08:17 +09:00
|
|
|
// URLs without domain name, like magnet:?xt=...
|
2021-10-10 17:26:18 +09:00
|
|
|
. '|(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_NO_DOMAIN)) . '):(?=\?))' // zero-length lookahead requires ? after :
|
|
|
|
. (Common::config('linkify', 'ipv4') // Convert IPv4 addresses to hyperlinks
|
2021-09-20 21:08:17 +09:00
|
|
|
? '|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
|
2021-10-10 17:26:18 +09:00
|
|
|
: '')
|
|
|
|
. (Common::config('linkify', 'ipv6') // Convert IPv6 addresses to hyperlinks
|
|
|
|
? '|(?:' //IPv6
|
|
|
|
. '\[?(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:(?:[0-9A-Fa-f]{1,4})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::|(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(?::[0-9A-Fa-f]{1,4})))|(?:(?:[0-9A-Fa-f]{1,4}:){5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){4}(?::[0-9A-Fa-f]{1,4}){0,1}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){3}(?::[0-9A-Fa-f]{1,4}){0,2}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){2}(?::[0-9A-Fa-f]{1,4}){0,3}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:)(?::[0-9A-Fa-f]{1,4}){0,4}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?::(?::[0-9A-Fa-f]{1,4}){0,5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))\]?(?<!:)'
|
|
|
|
. ')'
|
|
|
|
: '')
|
|
|
|
. (Common::config('linkify', 'bare_domains')
|
|
|
|
? '|(?:' //DNS
|
|
|
|
. '(?:[\pN\pL\-\_\+\%\~]+(?:\:[\pN\pL\-\_\+\%\~]+)?\@)?' //user:pass@
|
|
|
|
. '[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'
|
2021-09-20 21:08:17 +09:00
|
|
|
//tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local,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ட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZONE|ZW|local|loc|onion)'
|
|
|
|
. ')(?![\pN\pL\-\_])'
|
|
|
|
: '') // if common_config('linkify', 'bare_domains') is false, don't add anything here
|
|
|
|
. ')'
|
|
|
|
. '(?:'
|
|
|
|
. '(?:\:\d+)?' //:port
|
|
|
|
. '(?:/[' . URL_REGEX_VALID_PATH_CHARS . ']*)?' // path
|
|
|
|
. '(?:\?[' . URL_REGEX_VALID_QSTRING_CHARS . ']*)?' // ?query string
|
|
|
|
. '(?:\#[' . URL_REGEX_VALID_FRAGMENT_CHARS . ']*)?' // #fragment
|
|
|
|
. ')(?<![' . URL_REGEX_EXCLUDED_END_CHARS . '])'
|
|
|
|
. ')'
|
|
|
|
. '#ixu';
|
2021-09-20 21:08:17 +09:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
public const URL_SCHEME_COLON_DOUBLE_SLASH = 1;
|
|
|
|
public const URL_SCHEME_SINGLE_COLON = 2;
|
|
|
|
public const URL_SCHEME_NO_DOMAIN = 4;
|
|
|
|
public const URL_SCHEME_COLON_COORDINATES = 8;
|
2021-09-20 21:08:17 +09:00
|
|
|
|
|
|
|
public function URLSchemes($filter = null)
|
|
|
|
{
|
|
|
|
// TODO: move these to config
|
|
|
|
$schemes = [
|
|
|
|
'http' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'https' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'ftp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'ftps' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'mms' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'rtsp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'gopher' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'news' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'nntp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'telnet' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'wais' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'file' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'prospero' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'webcal' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'irc' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'ircs' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
|
|
|
|
'aim' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'bitcoin' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'fax' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'jabber' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'mailto' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'tel' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'xmpp' => self::URL_SCHEME_SINGLE_COLON,
|
|
|
|
'magnet' => self::URL_SCHEME_NO_DOMAIN,
|
|
|
|
'geo' => self::URL_SCHEME_COLON_COORDINATES,
|
|
|
|
];
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
return array_keys(array_filter($schemes, fn ($scheme) => \is_null($filter) || ($scheme & $filter)));
|
2021-09-20 21:08:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find links in the given text and pass them to the given callback function.
|
|
|
|
*/
|
|
|
|
public function replaceURLs(string $text): string
|
|
|
|
{
|
|
|
|
$regex = $this->getURLRegex();
|
|
|
|
return preg_replace_callback($regex, fn ($matches) => $this->callbackHelper($matches, [$this, 'linkify']), $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intermediate callback for `replaceURLs()`, which helps resolve some
|
|
|
|
* ambiguous link forms before passing on to the final callback.
|
|
|
|
*
|
|
|
|
* @param callable(string $text): string $callback: return replacement text
|
|
|
|
*/
|
|
|
|
private function callbackHelper(array $matches, callable $callback): string
|
|
|
|
{
|
|
|
|
$url = $matches[1];
|
2021-10-10 17:26:18 +09:00
|
|
|
$left = mb_strpos($matches[0], $url);
|
|
|
|
$right = $left + mb_strlen($url);
|
2021-09-20 21:08:17 +09:00
|
|
|
|
|
|
|
$groupSymbolSets = [
|
|
|
|
[
|
|
|
|
'left' => '(',
|
|
|
|
'right' => ')',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'left' => '[',
|
|
|
|
'right' => ']',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'left' => '{',
|
|
|
|
'right' => '}',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'left' => '<',
|
|
|
|
'right' => '>',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$cannotEndWith = ['.', '?', ',', '#'];
|
|
|
|
do {
|
|
|
|
$original_url = $url;
|
|
|
|
foreach ($groupSymbolSets as $groupSymbolSet) {
|
2021-10-10 17:26:18 +09:00
|
|
|
if (mb_substr($url, -1) == $groupSymbolSet['right']) {
|
|
|
|
$group_left_count = mb_substr_count($url, $groupSymbolSet['left']);
|
|
|
|
$group_right_count = mb_substr_count($url, $groupSymbolSet['right']);
|
2021-09-20 21:08:17 +09:00
|
|
|
if ($group_left_count < $group_right_count) {
|
|
|
|
--$right;
|
2021-10-10 17:26:18 +09:00
|
|
|
$url = mb_substr($url, 0, -1);
|
2021-09-20 21:08:17 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:26:18 +09:00
|
|
|
if (\in_array(mb_substr($url, -1), $cannotEndWith)) {
|
2021-09-20 21:08:17 +09:00
|
|
|
--$right;
|
2021-10-10 17:26:18 +09:00
|
|
|
$url = mb_substr($url, 0, -1);
|
2021-09-20 21:08:17 +09:00
|
|
|
}
|
|
|
|
} while ($original_url != $url);
|
|
|
|
|
|
|
|
$result = $callback($url);
|
2021-10-10 17:26:18 +09:00
|
|
|
return mb_substr($matches[0], 0, $left) . $result . mb_substr($matches[0], $right);
|
2021-09-20 21:08:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a plain text $url to HTML <a>
|
|
|
|
*/
|
|
|
|
public function linkify(string $url): string
|
|
|
|
{
|
|
|
|
// It comes in special'd, so we unspecial it before passing to the stringifying
|
|
|
|
// functions
|
|
|
|
$url = htmlspecialchars_decode($url);
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
if (str_contains($url, '@') && !str_contains($url, ':') && ($email = filter_var($url, \FILTER_VALIDATE_EMAIL)) !== false) {
|
2021-09-20 21:08:17 +09:00
|
|
|
//url is an email address without the mailto: protocol
|
|
|
|
$url = "mailto:{$email}";
|
|
|
|
}
|
|
|
|
|
|
|
|
$attrs = ['href' => $url, 'title' => $url];
|
|
|
|
|
|
|
|
// TODO Check to see whether this is a known "attachment" URL.
|
|
|
|
|
|
|
|
// Whether to nofollow
|
|
|
|
$nf = Common::config('nofollow', 'external');
|
|
|
|
if ($nf == 'never') {
|
|
|
|
$attrs['rel'] = 'external';
|
|
|
|
} else {
|
|
|
|
$attrs['rel'] = 'noopener nofollow external noreferrer';
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTML::html(['a' => ['attrs' => $attrs, $url]], options: ['indent' => false]);
|
|
|
|
}
|
2021-12-10 11:38:47 +09:00
|
|
|
|
2021-12-28 15:16:32 +09:00
|
|
|
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
|
2021-12-10 11:38:47 +09:00
|
|
|
{
|
2021-12-28 15:16:32 +09:00
|
|
|
DB::wrapInTransaction(fn () => NoteToLink::removeWhereNoteId($note->getId()));
|
2021-12-10 11:38:47 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2021-09-07 05:01:20 +09:00
|
|
|
}
|