Link.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Link;
  20. use App\Core\DB;
  21. use App\Core\Event;
  22. use App\Core\Modules\Component;
  23. use App\Entity\Actor;
  24. use App\Entity\Note;
  25. use App\Util\Common;
  26. use App\Util\HTML;
  27. use Component\Link\Entity\NoteToLink;
  28. use EventResult;
  29. use InvalidArgumentException;
  30. class Link extends Component
  31. {
  32. /**
  33. * Note that this persists both a Link and a NoteToLink
  34. *
  35. * @return array{ link: ?Entity\Link, note_to_link: ?NoteToLink }
  36. */
  37. public static function maybeCreateLink(string $url, int $note_id): array
  38. {
  39. try {
  40. $link = Entity\Link::getOrCreate($url);
  41. DB::persist($note_link = NoteToLink::create(['link_id' => $link->getId(), 'note_id' => $note_id]));
  42. return ['link' => $link, 'note_to_link' => $note_link];
  43. } catch (InvalidArgumentException) {
  44. return ['link' => null, 'note_to_link' => null];
  45. }
  46. }
  47. /**
  48. * Extract URLs from $content and create the appropriate Link and NoteToLink entities
  49. *
  50. * @param array{ignoreLinks?: string[]} $process_note_content_extra_args
  51. */
  52. public function onProcessNoteContent(Note $note, string $content, string $content_type, array $process_note_content_extra_args = []): EventResult
  53. {
  54. $ignore = $process_note_content_extra_args['ignoreLinks'] ?? [];
  55. if (Common::config('attachments', 'process_links')) {
  56. $matched_urls = [];
  57. preg_match_all($this->getURLRegex(), $content, $matched_urls);
  58. $matched_urls = array_unique($matched_urls[1]);
  59. foreach ($matched_urls as $match) {
  60. if (\in_array($match, $ignore)) {
  61. continue;
  62. }
  63. self::maybeCreateLink($match, $note->getId());
  64. }
  65. }
  66. return Event::next;
  67. }
  68. public function onRenderPlainTextNoteContent(string &$text): EventResult
  69. {
  70. $text = $this->replaceURLs($text);
  71. return Event::next;
  72. }
  73. public function getURLRegex(): string
  74. {
  75. $geouri_labeltext_regex = '\pN\pL\-';
  76. $geouri_mark_regex = '\-\_\.\!\~\*\\\'\(\)'; // the \\\' is really pretty
  77. $geouri_unreserved_regex = '\pN\pL' . $geouri_mark_regex;
  78. $geouri_punreserved_regex = '\[\]\:\&\+\$';
  79. $geouri_pctencoded_regex = '(?:\%[0-9a-fA-F][0-9a-fA-F])';
  80. $geouri_paramchar_regex = $geouri_unreserved_regex . $geouri_punreserved_regex; //FIXME: add $geouri_pctencoded_regex here so it works
  81. return '#'
  82. . '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'
  83. . '('
  84. . '(?:'
  85. . '(?:' //Known protocols
  86. . '(?:'
  87. . '(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_COLON_DOUBLE_SLASH)) . ')://)'
  88. . '|'
  89. . '(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_SINGLE_COLON)) . '):)'
  90. . ')'
  91. . '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?' //user:pass@
  92. . '(?:'
  93. . '(?:'
  94. . '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]' //[dns]
  95. . ')|(?:'
  96. . '[\pN\pL\-\_\:\.]+(?<![\.\:])' //dns
  97. . ')'
  98. . ')'
  99. . ')'
  100. . '|(?:'
  101. . '(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_COLON_COORDINATES)) . '):'
  102. // There's an order that must be followed here too, if ;crs= is used, it must precede ;u=
  103. // Also 'crsp' (;crs=$crsp) must match $geouri_labeltext_regex
  104. // Also 'uval' (;u=$uval) must be a pnum: \-?[0-9]+
  105. . '(?:'
  106. . '(?:[0-9]+(?:\.[0-9]+)?(?:\,[0-9]+(?:\.[0-9]+)?){1,2})' // 1(.23)?(,4(.56)){1,2}
  107. . '(?:\;(?:[' . $geouri_labeltext_regex . ']+)(?:\=[' . $geouri_paramchar_regex . ']+)*)*'
  108. . ')'
  109. . ')'
  110. // URLs without domain name, like magnet:?xt=...
  111. . '|(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_NO_DOMAIN)) . '):(?=\?))' // zero-length lookahead requires ? after :
  112. . (Common::config('linkify', 'ipv4') // Convert IPv4 addresses to hyperlinks
  113. ? '|(?:(?: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]?)'
  114. : '')
  115. . (Common::config('linkify', 'ipv6') // Convert IPv6 addresses to hyperlinks
  116. ? '|(?:' //IPv6
  117. . '\[?(?:(?:(?:[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})))\]?(?<!:)'
  118. . ')'
  119. : '')
  120. . (Common::config('linkify', 'bare_domains')
  121. ? '|(?:' //DNS
  122. . '(?:[\pN\pL\-\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KHBYKNJ4F|испытание|XN--9T4B11YI5A|테스트|XN--DEBA0AD|טעסט|XN--G6W251D|測試|XN--HGBK6AJ7F53BBA|آزمایشی|XN--HLCJ6AYA9ESC7A|பரிட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZONE|ZW|local|loc|onion)'
  126. . ')(?![\pN\pL\-\_])'
  127. : '') // if common_config('linkify', 'bare_domains') is false, don't add anything here
  128. . ')'
  129. . '(?:'
  130. . '(?:\:\d+)?' //:port
  131. . '(?:/[' . URL_REGEX_VALID_PATH_CHARS . ']*)?' // path
  132. . '(?:\?[' . URL_REGEX_VALID_QSTRING_CHARS . ']*)?' // ?query string
  133. . '(?:\#[' . URL_REGEX_VALID_FRAGMENT_CHARS . ']*)?' // #fragment
  134. . ')(?<![' . URL_REGEX_EXCLUDED_END_CHARS . '])'
  135. . ')'
  136. . '#ixu';
  137. }
  138. public const URL_SCHEME_COLON_DOUBLE_SLASH = 1;
  139. public const URL_SCHEME_SINGLE_COLON = 2;
  140. public const URL_SCHEME_NO_DOMAIN = 4;
  141. public const URL_SCHEME_COLON_COORDINATES = 8;
  142. /**
  143. * @param self::URL_SCHEME_COLON_COORDINATES|self::URL_SCHEME_COLON_DOUBLE_SLASH|self::URL_SCHEME_NO_DOMAIN|self::URL_SCHEME_SINGLE_COLON $filter
  144. *
  145. * @return string[]
  146. */
  147. public function URLSchemes(?int $filter = null): array
  148. {
  149. // TODO: move these to config
  150. $schemes = [
  151. 'http' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  152. 'https' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  153. 'ftp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  154. 'ftps' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  155. 'mms' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  156. 'rtsp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  157. 'gopher' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  158. 'news' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  159. 'nntp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  160. 'telnet' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  161. 'wais' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  162. 'file' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  163. 'prospero' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  164. 'webcal' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  165. 'irc' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  166. 'ircs' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  167. 'aim' => self::URL_SCHEME_SINGLE_COLON,
  168. 'bitcoin' => self::URL_SCHEME_SINGLE_COLON,
  169. 'fax' => self::URL_SCHEME_SINGLE_COLON,
  170. 'jabber' => self::URL_SCHEME_SINGLE_COLON,
  171. 'mailto' => self::URL_SCHEME_SINGLE_COLON,
  172. 'tel' => self::URL_SCHEME_SINGLE_COLON,
  173. 'xmpp' => self::URL_SCHEME_SINGLE_COLON,
  174. 'magnet' => self::URL_SCHEME_NO_DOMAIN,
  175. 'geo' => self::URL_SCHEME_COLON_COORDINATES,
  176. ];
  177. return array_keys(array_filter($schemes, fn ($scheme) => \is_null($filter) || ($scheme & $filter)));
  178. }
  179. /**
  180. * Find links in the given text and pass them to the given callback function.
  181. */
  182. public function replaceURLs(string $text): string
  183. {
  184. $regex = $this->getURLRegex();
  185. return preg_replace_callback($regex, fn ($matches) => $this->callbackHelper($matches, [$this, 'linkify']), $text);
  186. }
  187. /**
  188. * Intermediate callback for `replaceURLs()`, which helps resolve some
  189. * ambiguous link forms before passing on to the final callback.
  190. *
  191. * @param string[] $matches
  192. * @param callable(string $text): string $callback: return replacement text
  193. */
  194. private function callbackHelper(array $matches, callable $callback): string
  195. {
  196. $url = $matches[1];
  197. $left = mb_strpos($matches[0], $url);
  198. $right = $left + mb_strlen($url);
  199. $groupSymbolSets = [
  200. [
  201. 'left' => '(',
  202. 'right' => ')',
  203. ],
  204. [
  205. 'left' => '[',
  206. 'right' => ']',
  207. ],
  208. [
  209. 'left' => '{',
  210. 'right' => '}',
  211. ],
  212. [
  213. 'left' => '<',
  214. 'right' => '>',
  215. ],
  216. ];
  217. $cannotEndWith = ['.', '?', ',', '#'];
  218. do {
  219. $original_url = $url;
  220. foreach ($groupSymbolSets as $groupSymbolSet) {
  221. if (mb_substr($url, -1) == $groupSymbolSet['right']) {
  222. $group_left_count = mb_substr_count($url, $groupSymbolSet['left']);
  223. $group_right_count = mb_substr_count($url, $groupSymbolSet['right']);
  224. if ($group_left_count < $group_right_count) {
  225. --$right;
  226. $url = mb_substr($url, 0, -1);
  227. }
  228. }
  229. }
  230. if (\in_array(mb_substr($url, -1), $cannotEndWith)) {
  231. --$right;
  232. $url = mb_substr($url, 0, -1);
  233. }
  234. } while ($original_url != $url);
  235. $result = $callback($url);
  236. return mb_substr($matches[0], 0, $left) . $result . mb_substr($matches[0], $right);
  237. }
  238. /**
  239. * Convert a plain text $url to HTML <a>
  240. */
  241. public function linkify(string $url): string
  242. {
  243. // It comes in special'd, so we unspecial it before passing to the stringifying
  244. // functions
  245. $url = htmlspecialchars_decode($url);
  246. if (str_contains($url, '@') && !str_contains($url, ':') && ($email = filter_var($url, \FILTER_VALIDATE_EMAIL)) !== false) {
  247. //url is an email address without the mailto: protocol
  248. $url = "mailto:{$email}";
  249. }
  250. $attrs = ['href' => $url, 'title' => $url];
  251. // TODO Check to see whether this is a known "attachment" URL.
  252. // Whether to nofollow
  253. $nf = Common::config('nofollow', 'external');
  254. if ($nf == 'never') {
  255. $attrs['rel'] = 'external';
  256. } else {
  257. $attrs['rel'] = 'noopener nofollow external noreferrer';
  258. }
  259. return HTML::html(['a' => ['attrs' => $attrs, $url]], options: ['indent' => false]);
  260. }
  261. public function onNoteDeleteRelated(Note &$note, Actor $actor): EventResult
  262. {
  263. DB::wrapInTransaction(fn () => NoteToLink::removeWhereNoteId($note->getId()));
  264. return Event::next;
  265. }
  266. }