From 2a28591415e7a667ee4914a31d6a23efdbee3020 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 16 Oct 2017 20:31:13 +0000 Subject: [PATCH 1/4] Complete incomplete URL during feed import --- include/feed.php | 10 +++++- include/html2bbcode.php | 72 +++++++++++++++++++++++++++++++++++------ include/network.php | 31 ++++++++++++++++++ 3 files changed, 103 insertions(+), 10 deletions(-) diff --git a/include/feed.php b/include/feed.php index 5676566c42..1aab26e1df 100644 --- a/include/feed.php +++ b/include/feed.php @@ -27,6 +27,14 @@ function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) { return; } + if (!empty($contact['poll'])) { + $basepath = $contact['poll']; + } elseif (!empty($contact['url'])) { + $basepath = $contact['url']; + } else { + $basepath = ''; + } + $doc = new DOMDocument(); @$doc->loadXML(trim($xml)); $xpath = new DomXPath($doc); @@ -344,7 +352,7 @@ function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) { if (title_is_body($item["title"], $body)) { $item["title"] = ""; } - $item["body"] = html2bbcode($body); + $item["body"] = html2bbcode($body, $basepath); if (($item["body"] == '') && ($item["title"] != '')) { $item["body"] = $item["title"]; diff --git a/include/html2bbcode.php b/include/html2bbcode.php index 435f6b77c1..e7e253e81b 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -79,7 +79,7 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb) return($replace); } -function html2bbcode($message) +function html2bbcode($message, $basepath = '') { $message = str_replace("\r", "", $message); @@ -90,10 +90,10 @@ function html2bbcode($message) function ($matches) use (&$codeblocks) { $return = '[codeblock-' . count($codeblocks) . ']'; - $prefix = '[code]'; - if ($matches[1] != '') { - $prefix = '[code=' . $matches[1] . ']'; - } + $prefix = '[code]'; + if ($matches[1] != '') { + $prefix = '[code=' . $matches[1] . ']'; + } $codeblocks[] = $prefix . $matches[2] . '[/code]'; return $return; } @@ -313,15 +313,69 @@ function html2bbcode($message) // Restore code blocks $message = preg_replace_callback('#\[codeblock-([0-9]+)\]#iU', function ($matches) use ($codeblocks) { - $return = ''; - if (isset($codeblocks[intval($matches[1])])) { - $return = $codeblocks[$matches[1]]; - } + $return = ''; + if (isset($codeblocks[intval($matches[1])])) { + $return = $codeblocks[$matches[1]]; + } return $return; } , $message); $message = trim($message); + if ($basepath != '') { + $message = AddHostname($message, $basepath); + } + return $message; } + +/** + * @brief Sub function to complete incomplete URL + * + * @param array $matches Result of preg_replace_callback + * @param string $basepath Basepath that is used to complete the URL + * + * @return string The expanded URL + */ +function AddHostnameSub($matches, $basepath) { + $base = parse_url($basepath); + + $link = $matches[0]; + $url = $matches[1]; + + $parts = array_merge($base, parse_url($url)); + $url2 = unParseUrl($parts); + + return str_replace($url, $url2, $link); +} + +/** + * @brief Complete incomplete URLs in BBCode + * + * @param string $body Body with URLs + * @param string $basepath Basepath that is used to complete the URL + * + * @return string Body with expanded URLs + */ +function AddHostname($body, $basepath) { + $URLSearchString = "^\[\]"; + + $matches = array("/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism", + "/\[url\]([$URLSearchString]*)\[\/url\]/ism", + "/\[img\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism", + "/\[img\](.*?)\[\/img\]/ism", + "/\[zmg\=[0-9]*x[0-9]*\](.*?)\[\/img\]/ism", + "/\[zmg\](.*?)\[\/zmg\]/ism", + "/\[video\](.*?)\[\/video\]/ism", + "/\[audio\](.*?)\[\/audio\]/ism", + ); + + foreach ($matches AS $match) { + $body = preg_replace_callback($match, + function ($match) use ($basepath) { + return AddHostnameSub($match, $basepath); + }, $body); + } + return $body; +} diff --git a/include/network.php b/include/network.php index c705d4e78c..2a996cd933 100644 --- a/include/network.php +++ b/include/network.php @@ -1003,3 +1003,34 @@ function matching_url($url1, $url2) { return normalise_link($match); } + +/** + * @brief Glue url parts together + * + * @param array $parsed URL parts + * + * @return string The glued URL + */ +function unParseUrl($parsed) { + $get = function ($key) use ($parsed) { + return isset($parsed[$key]) ? $parsed[$key] : null; + }; + + $pass = $get('pass'); + $user = $get('user'); + $userinfo = $pass !== null ? "$user:$pass" : $user; + $port = $get('port'); + $scheme = $get('scheme'); + $query = $get('query'); + $fragment = $get('fragment'); + $authority = + ($userinfo !== null ? $userinfo."@" : '') . + $get('host') . + ($port ? ":$port" : ''); + + return (strlen($scheme) ? $scheme.":" : '') . + (strlen($authority) ? "//".$authority : '') . + $get('path') . + (strlen($query) ? "?".$query : '') . + (strlen($fragment) ? "#".$fragment : ''); +} From f127d65de904773a0a9959c90c27de03593dc5cf Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 16 Oct 2017 20:55:37 +0000 Subject: [PATCH 2/4] Added spaces --- include/html2bbcode.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/html2bbcode.php b/include/html2bbcode.php index e7e253e81b..e90f6c1094 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -90,10 +90,10 @@ function html2bbcode($message, $basepath = '') function ($matches) use (&$codeblocks) { $return = '[codeblock-' . count($codeblocks) . ']'; - $prefix = '[code]'; - if ($matches[1] != '') { - $prefix = '[code=' . $matches[1] . ']'; - } + $prefix = '[code]'; + if ($matches[1] != '') { + $prefix = '[code=' . $matches[1] . ']'; + } $codeblocks[] = $prefix . $matches[2] . '[/code]'; return $return; } @@ -313,10 +313,10 @@ function html2bbcode($message, $basepath = '') // Restore code blocks $message = preg_replace_callback('#\[codeblock-([0-9]+)\]#iU', function ($matches) use ($codeblocks) { - $return = ''; - if (isset($codeblocks[intval($matches[1])])) { - $return = $codeblocks[$matches[1]]; - } + $return = ''; + if (isset($codeblocks[intval($matches[1])])) { + $return = $codeblocks[$matches[1]]; + } return $return; } , $message); From b5212153830602fa79d4931ef7373d6da27b2770 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 16 Oct 2017 20:57:53 +0000 Subject: [PATCH 3/4] Camels aren't studly --- include/html2bbcode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/html2bbcode.php b/include/html2bbcode.php index e90f6c1094..c86d6581c6 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -338,7 +338,7 @@ function html2bbcode($message, $basepath = '') * * @return string The expanded URL */ -function AddHostnameSub($matches, $basepath) { +function addHostnameSub($matches, $basepath) { $base = parse_url($basepath); $link = $matches[0]; @@ -358,7 +358,7 @@ function AddHostnameSub($matches, $basepath) { * * @return string Body with expanded URLs */ -function AddHostname($body, $basepath) { +function addHostname($body, $basepath) { $URLSearchString = "^\[\]"; $matches = array("/\[url\=([$URLSearchString]*)\].*?\[\/url\]/ism", From 3eed3316eabafac5d05c54b23733a53c5d7069b4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 16 Oct 2017 20:59:16 +0000 Subject: [PATCH 4/4] Some more camels --- include/html2bbcode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/html2bbcode.php b/include/html2bbcode.php index c86d6581c6..a95c08cd56 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -324,7 +324,7 @@ function html2bbcode($message, $basepath = '') $message = trim($message); if ($basepath != '') { - $message = AddHostname($message, $basepath); + $message = addHostname($message, $basepath); } return $message; @@ -374,7 +374,7 @@ function addHostname($body, $basepath) { foreach ($matches AS $match) { $body = preg_replace_callback($match, function ($match) use ($basepath) { - return AddHostnameSub($match, $basepath); + return addHostnameSub($match, $basepath); }, $body); } return $body;