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);
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"];
return($replace);
}
-function html2bbcode($message)
+function html2bbcode($message, $basepath = '')
{
$message = str_replace("\r", "", $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;
}
// 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;
+}
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 : '');
+}