Link.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\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 InvalidArgumentException;
  29. class Link extends Component
  30. {
  31. /**
  32. * Extract URLs from $content and create the appropriate Link and NoteToLink entities
  33. */
  34. public function onProcessNoteContent(Note $note, string $content): bool
  35. {
  36. if (Common::config('attachments', 'process_links')) {
  37. $matched_urls = [];
  38. // TODO: This solution to ignore mentions when content is in html is far from ideal
  39. preg_match_all($this->getURLRegex(), preg_replace('#<a href="(.*?)" class="u-url mention">#', '', $content), $matched_urls);
  40. $matched_urls = array_unique($matched_urls[1]);
  41. foreach ($matched_urls as $match) {
  42. try {
  43. $link_id = Entity\Link::getOrCreate($match)->getId();
  44. DB::persist(NoteToLink::create(['link_id' => $link_id, 'note_id' => $note->getId()]));
  45. } catch (InvalidArgumentException) {
  46. continue;
  47. }
  48. }
  49. }
  50. return Event::next;
  51. }
  52. public function onRenderPlainTextNoteContent(string &$text): bool
  53. {
  54. $text = $this->replaceURLs($text);
  55. return Event::next;
  56. }
  57. public function getURLRegex(): string
  58. {
  59. $geouri_labeltext_regex = '\pN\pL\-';
  60. $geouri_mark_regex = '\-\_\.\!\~\*\\\'\(\)'; // the \\\' is really pretty
  61. $geouri_unreserved_regex = '\pN\pL' . $geouri_mark_regex;
  62. $geouri_punreserved_regex = '\[\]\:\&\+\$';
  63. $geouri_pctencoded_regex = '(?:\%[0-9a-fA-F][0-9a-fA-F])';
  64. $geouri_paramchar_regex = $geouri_unreserved_regex . $geouri_punreserved_regex; //FIXME: add $geouri_pctencoded_regex here so it works
  65. return '#'
  66. . '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'
  67. . '('
  68. . '(?:'
  69. . '(?:' //Known protocols
  70. . '(?:'
  71. . '(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_COLON_DOUBLE_SLASH)) . ')://)'
  72. . '|'
  73. . '(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_SINGLE_COLON)) . '):)'
  74. . ')'
  75. . '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?' //user:pass@
  76. . '(?:'
  77. . '(?:'
  78. . '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]' //[dns]
  79. . ')|(?:'
  80. . '[\pN\pL\-\_\:\.]+(?<![\.\:])' //dns
  81. . ')'
  82. . ')'
  83. . ')'
  84. . '|(?:'
  85. . '(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_COLON_COORDINATES)) . '):'
  86. // There's an order that must be followed here too, if ;crs= is used, it must precede ;u=
  87. // Also 'crsp' (;crs=$crsp) must match $geouri_labeltext_regex
  88. // Also 'uval' (;u=$uval) must be a pnum: \-?[0-9]+
  89. . '(?:'
  90. . '(?:[0-9]+(?:\.[0-9]+)?(?:\,[0-9]+(?:\.[0-9]+)?){1,2})' // 1(.23)?(,4(.56)){1,2}
  91. . '(?:\;(?:[' . $geouri_labeltext_regex . ']+)(?:\=[' . $geouri_paramchar_regex . ']+)*)*'
  92. . ')'
  93. . ')'
  94. // URLs without domain name, like magnet:?xt=...
  95. . '|(?:(?:' . implode('|', $this->URLSchemes(self::URL_SCHEME_NO_DOMAIN)) . '):(?=\?))' // zero-length lookahead requires ? after :
  96. . (Common::config('linkify', 'ipv4') // Convert IPv4 addresses to hyperlinks
  97. ? '|(?:(?: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]?)'
  98. : '')
  99. . (Common::config('linkify', 'ipv6') // Convert IPv6 addresses to hyperlinks
  100. ? '|(?:' //IPv6
  101. . '\[?(?:(?:(?:[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})))\]?(?<!:)'
  102. . ')'
  103. : '')
  104. . (Common::config('linkify', 'bare_domains')
  105. ? '|(?:' //DNS
  106. . '(?:[\pN\pL\-\_\+\%\~]+(?:\:[\pN\pL\-\_\+\%\~]+)?\@)?' //user:pass@
  107. . '[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'
  108. //tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
  109. . '(?:AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XN--0ZWM56D|测试|XN--11B5BS3A9AJ6G|परीक्षा|XN--80AKHBYKNJ4F|испытание|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)'
  110. . ')(?![\pN\pL\-\_])'
  111. : '') // if common_config('linkify', 'bare_domains') is false, don't add anything here
  112. . ')'
  113. . '(?:'
  114. . '(?:\:\d+)?' //:port
  115. . '(?:/[' . URL_REGEX_VALID_PATH_CHARS . ']*)?' // path
  116. . '(?:\?[' . URL_REGEX_VALID_QSTRING_CHARS . ']*)?' // ?query string
  117. . '(?:\#[' . URL_REGEX_VALID_FRAGMENT_CHARS . ']*)?' // #fragment
  118. . ')(?<![' . URL_REGEX_EXCLUDED_END_CHARS . '])'
  119. . ')'
  120. . '#ixu';
  121. }
  122. public const URL_SCHEME_COLON_DOUBLE_SLASH = 1;
  123. public const URL_SCHEME_SINGLE_COLON = 2;
  124. public const URL_SCHEME_NO_DOMAIN = 4;
  125. public const URL_SCHEME_COLON_COORDINATES = 8;
  126. public function URLSchemes($filter = null)
  127. {
  128. // TODO: move these to config
  129. $schemes = [
  130. 'http' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  131. 'https' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  132. 'ftp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  133. 'ftps' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  134. 'mms' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  135. 'rtsp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  136. 'gopher' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  137. 'news' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  138. 'nntp' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  139. 'telnet' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  140. 'wais' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  141. 'file' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  142. 'prospero' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  143. 'webcal' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  144. 'irc' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  145. 'ircs' => self::URL_SCHEME_COLON_DOUBLE_SLASH,
  146. 'aim' => self::URL_SCHEME_SINGLE_COLON,
  147. 'bitcoin' => self::URL_SCHEME_SINGLE_COLON,
  148. 'fax' => self::URL_SCHEME_SINGLE_COLON,
  149. 'jabber' => self::URL_SCHEME_SINGLE_COLON,
  150. 'mailto' => self::URL_SCHEME_SINGLE_COLON,
  151. 'tel' => self::URL_SCHEME_SINGLE_COLON,
  152. 'xmpp' => self::URL_SCHEME_SINGLE_COLON,
  153. 'magnet' => self::URL_SCHEME_NO_DOMAIN,
  154. 'geo' => self::URL_SCHEME_COLON_COORDINATES,
  155. ];
  156. return array_keys(array_filter($schemes, fn ($scheme) => \is_null($filter) || ($scheme & $filter)));
  157. }
  158. /**
  159. * Find links in the given text and pass them to the given callback function.
  160. */
  161. public function replaceURLs(string $text): string
  162. {
  163. $regex = $this->getURLRegex();
  164. return preg_replace_callback($regex, fn ($matches) => $this->callbackHelper($matches, [$this, 'linkify']), $text);
  165. }
  166. /**
  167. * Intermediate callback for `replaceURLs()`, which helps resolve some
  168. * ambiguous link forms before passing on to the final callback.
  169. *
  170. * @param callable(string $text): string $callback: return replacement text
  171. */
  172. private function callbackHelper(array $matches, callable $callback): string
  173. {
  174. $url = $matches[1];
  175. $left = mb_strpos($matches[0], $url);
  176. $right = $left + mb_strlen($url);
  177. $groupSymbolSets = [
  178. [
  179. 'left' => '(',
  180. 'right' => ')',
  181. ],
  182. [
  183. 'left' => '[',
  184. 'right' => ']',
  185. ],
  186. [
  187. 'left' => '{',
  188. 'right' => '}',
  189. ],
  190. [
  191. 'left' => '<',
  192. 'right' => '>',
  193. ],
  194. ];
  195. $cannotEndWith = ['.', '?', ',', '#'];
  196. do {
  197. $original_url = $url;
  198. foreach ($groupSymbolSets as $groupSymbolSet) {
  199. if (mb_substr($url, -1) == $groupSymbolSet['right']) {
  200. $group_left_count = mb_substr_count($url, $groupSymbolSet['left']);
  201. $group_right_count = mb_substr_count($url, $groupSymbolSet['right']);
  202. if ($group_left_count < $group_right_count) {
  203. --$right;
  204. $url = mb_substr($url, 0, -1);
  205. }
  206. }
  207. }
  208. if (\in_array(mb_substr($url, -1), $cannotEndWith)) {
  209. --$right;
  210. $url = mb_substr($url, 0, -1);
  211. }
  212. } while ($original_url != $url);
  213. $result = $callback($url);
  214. return mb_substr($matches[0], 0, $left) . $result . mb_substr($matches[0], $right);
  215. }
  216. /**
  217. * Convert a plain text $url to HTML <a>
  218. */
  219. public function linkify(string $url): string
  220. {
  221. // It comes in special'd, so we unspecial it before passing to the stringifying
  222. // functions
  223. $url = htmlspecialchars_decode($url);
  224. if (str_contains($url, '@') && !str_contains($url, ':') && ($email = filter_var($url, \FILTER_VALIDATE_EMAIL)) !== false) {
  225. //url is an email address without the mailto: protocol
  226. $url = "mailto:{$email}";
  227. }
  228. $attrs = ['href' => $url, 'title' => $url];
  229. // TODO Check to see whether this is a known "attachment" URL.
  230. // Whether to nofollow
  231. $nf = Common::config('nofollow', 'external');
  232. if ($nf == 'never') {
  233. $attrs['rel'] = 'external';
  234. } else {
  235. $attrs['rel'] = 'noopener nofollow external noreferrer';
  236. }
  237. return HTML::html(['a' => ['attrs' => $attrs, $url]], options: ['indent' => false]);
  238. }
  239. public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
  240. {
  241. DB::wrapInTransaction(fn () => NoteToLink::removeWhereNoteId($note->getId()));
  242. return Event::next;
  243. }
  244. }