+
+/**
+ * @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);
+ unset($base['query']);
+ unset($base['fragment']);
+
+ $link = $matches[0];
+ $url = $matches[1];
+
+ $parts = array_merge($base, parse_url($url));
+ $url2 = Network::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 = ["/\[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;
+}