.
// }}}
namespace Component\Link;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Modules\Component;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Common;
use App\Util\HTML;
use Component\Link\Entity\NoteToLink;
use InvalidArgumentException;
class Link extends Component
{
    /**
     * Extract URLs from $content and create the appropriate Link and NoteToLink entities
     */
    public function onProcessNoteContent(Note $note, string $content): bool
    {
        if (Common::config('attachments', 'process_links')) {
            $matched_urls = [];
            // TODO: This solution to ignore mentions when content is in html is far from ideal
            preg_match_all($this->getURLRegex(), preg_replace('##', '', $content), $matched_urls);
            $matched_urls = array_unique($matched_urls[1]);
            foreach ($matched_urls as $match) {
                try {
                    $link_id = Entity\Link::getOrCreate($match)->getId();
                    DB::persist(NoteToLink::create(['link_id' => $link_id, 'note_id' => $note->getId()]));
                } catch (InvalidArgumentException) {
                    continue;
                }
            }
        }
        return Event::next;
    }
    public function onRenderPlainTextNoteContent(string &$text): bool
    {
        $text = $this->replaceURLs($text);
        return Event::next;
    }
    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
        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\-\_\:\.]+(?URLSchemes(self::URL_SCHEME_COLON_COORDINATES)) . '):'
                   // 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]+
                   . '(?:'
                   . '(?:[0-9]+(?:\.[0-9]+)?(?:\,[0-9]+(?:\.[0-9]+)?){1,2})'    // 1(.23)?(,4(.56)){1,2}
                   . '(?:\;(?:[' . $geouri_labeltext_regex . ']+)(?:\=[' . $geouri_paramchar_regex . ']+)*)*'
                   . ')'
                   . ')'
                   // URLs without domain name, like magnet:?xt=...
                   . '|(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_NO_DOMAIN)) . '):(?=\?))'  // zero-length lookahead requires ? after :
                   . (Common::config('linkify', 'ipv4')   // Convert IPv4 addresses to hyperlinks
                    ? '|(?:(?: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]?)'
                    : '')
                   . (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})))\]?(? 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,
        ];
        return array_keys(array_filter($schemes, fn ($scheme) => \is_null($filter) || ($scheme & $filter)));
    }
    /**
     * 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];
        $left  = mb_strpos($matches[0], $url);
        $right = $left + mb_strlen($url);
        $groupSymbolSets = [
            [
                'left'  => '(',
                'right' => ')',
            ],
            [
                'left'  => '[',
                'right' => ']',
            ],
            [
                'left'  => '{',
                'right' => '}',
            ],
            [
                'left'  => '<',
                'right' => '>',
            ],
        ];
        $cannotEndWith = ['.', '?', ',', '#'];
        do {
            $original_url = $url;
            foreach ($groupSymbolSets as $groupSymbolSet) {
                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']);
                    if ($group_left_count < $group_right_count) {
                        --$right;
                        $url = mb_substr($url, 0, -1);
                    }
                }
            }
            if (\in_array(mb_substr($url, -1), $cannotEndWith)) {
                --$right;
                $url = mb_substr($url, 0, -1);
            }
        } while ($original_url != $url);
        $result = $callback($url);
        return mb_substr($matches[0], 0, $left) . $result . mb_substr($matches[0], $right);
    }
    /**
     * Convert a plain text $url to HTML 
     */
    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);
        if (str_contains($url, '@') && !str_contains($url, ':') && ($email = filter_var($url, \FILTER_VALIDATE_EMAIL)) !== false) {
            //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]);
    }
    public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
    {
        DB::wrapInTransaction(fn () => NoteToLink::removeWhereNoteId($note->getId()));
        return Event::next;
    }
}