diff --git a/CHANGELOG.md b/CHANGELOG.md index 914e5eaa8..85b21af0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Added showing releaser stats in profile [\#1568](https://github.com/torrentpier/torrentpier/pull/1568) ([belomaxorka](https://github.com/belomaxorka)) - Improved `filelist.php` [\#1586](https://github.com/torrentpier/torrentpier/pull/1586) ([belomaxorka](https://github.com/belomaxorka)) - Demo mode: Save user language in cookies [\#1584](https://github.com/torrentpier/torrentpier/pull/1584) ([belomaxorka](https://github.com/belomaxorka)) +- BBCode: Fixed relative links working [\#1613](https://github.com/torrentpier/torrentpier/pull/1613) ([belomaxorka](https://github.com/belomaxorka)) - Fixed `md5()` deprecated in PHP 8.4 [\#1561](https://github.com/torrentpier/torrentpier/pull/1561) ([belomaxorka](https://github.com/belomaxorka)) - Increased `USEREMAIL_MAX_LENGTH` [\#1566](https://github.com/torrentpier/torrentpier/pull/1566) ([belomaxorka](https://github.com/belomaxorka)) - Minor improvements [\#1570](https://github.com/torrentpier/torrentpier/pull/1570), [\#1571](https://github.com/torrentpier/torrentpier/pull/1571), [\#1575](https://github.com/torrentpier/torrentpier/pull/1575), [\#1589](https://github.com/torrentpier/torrentpier/pull/1589), [\#1592](https://github.com/torrentpier/torrentpier/pull/1592), [\#1605](https://github.com/torrentpier/torrentpier/pull/1605), [\#1611](https://github.com/torrentpier/torrentpier/pull/1611), [\#1612](https://github.com/torrentpier/torrentpier/pull/1612) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/src/Legacy/BBCode.php b/src/Legacy/BBCode.php index 651a1703a..cbe7dafc8 100644 --- a/src/Legacy/BBCode.php +++ b/src/Legacy/BBCode.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy; +use function in_array; + /** * Class BBCode * @package TorrentPier\Legacy @@ -19,7 +21,7 @@ class BBCode public array $tpl = []; /** @var array $smilies Replacements for smilies */ - public $smilies; + public array $smilies; /** @var array $tidy_cfg Tidy preprocessor configuration */ public array $tidy_cfg = [ @@ -246,22 +248,17 @@ class BBCode */ private function url_callback(array $m): string { - global $bb_cfg; - $url = trim($m[1]); $url_name = isset($m[2]) ? trim($m[2]) : $url; + $url_parse = parse_url($url); - if (!preg_match('#^https?://#iu', $url) && !preg_match('/^#/', $url)) { - $url = 'http://' . $url; + if (!isset($url_parse['scheme']) && isset($url_parse['path'])) { + if (!preg_match('/^([a-zA-Z0-9_\-\.]+\.php)(\?[^#]*)?$/', $url_parse['path'])) { + $url = 'http://' . $url; + } } - if (\in_array(parse_url($url, PHP_URL_HOST), $bb_cfg['nofollow']['allowed_url']) || $bb_cfg['nofollow']['disabled']) { - $link = "$url_name"; - } else { - $link = "$url_name"; - } - - return $link; + return $this->nofollow_url($url, $url_name); } /** @@ -323,19 +320,11 @@ class BBCode */ private function make_url_clickable_callback(array $m): string { - global $bb_cfg; - $max_len = 70; $href = $m[1]; $name = (mb_strlen($href, 'UTF-8') > $max_len) ? mb_substr($href, 0, $max_len - 19) . '...' . mb_substr($href, -16) : $href; - if (\in_array(parse_url($href, PHP_URL_HOST), $bb_cfg['nofollow']['allowed_url']) || $bb_cfg['nofollow']['disabled']) { - $link = "$name"; - } else { - $link = "$name"; - } - - return $link; + return $this->nofollow_url($href, $name); } /** @@ -349,11 +338,9 @@ class BBCode { global $datastore; - if (null === $this->smilies) { - if (!$this->smilies = $datastore->get('smile_replacements') and !$datastore->has('smile_replacements')) { - $datastore->update('smile_replacements'); - $this->smilies = $datastore->get('smile_replacements'); - } + if (!$this->smilies = $datastore->get('smile_replacements') and !$datastore->has('smile_replacements')) { + $datastore->update('smile_replacements'); + $this->smilies = $datastore->get('smile_replacements'); } if ($this->smilies) { @@ -390,4 +377,24 @@ class BBCode { return tidy_repair_string($text, $this->tidy_cfg, 'utf8'); } + + /** + * Nofollow links handling + * + * @param string $href + * @param string $name + * @return string + */ + private function nofollow_url(string $href, string $name): string + { + global $bb_cfg; + + if (in_array(parse_url($href, PHP_URL_HOST), $bb_cfg['nofollow']['allowed_url']) || $bb_cfg['nofollow']['disabled']) { + $link = "$name"; + } else { + $link = "$name"; + } + + return $link; + } }