X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=inline;f=include%2Fhtml2bbcode.php;h=257539b07481d9dc716a778dd2563c2a860afec8;hb=57f114d7e1955eb22167b12a9d2453ab096531cc;hp=650bbdcae8c07d1071a6724463b4ef0f227809df;hpb=1f23d691f377cc8c3c2ea444b785d28016f3515b;p=friendica.git
diff --git a/include/html2bbcode.php b/include/html2bbcode.php
index 650bbdcae8..257539b074 100644
--- a/include/html2bbcode.php
+++ b/include/html2bbcode.php
@@ -1,11 +1,14 @@
0) {
- if ($replace and (@$attr[$attribute] != '')) {
+ if ($replace && (@$attr[$attribute] != '')) {
$startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
@@ -76,29 +79,34 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
return($replace);
}
-if(!function_exists('deletenode')) {
-function deletenode(&$doc, $node)
+function html2bbcode($message, $basepath = '')
{
- $xpath = new DomXPath($doc);
- $list = $xpath->query("//".$node);
- foreach ($list as $child)
- $child->parentNode->removeChild($child);
-}}
-function html2bbcode($message)
-{
+ $message = str_replace("\r", "", $message);
- //$file = tempnam("/tmp/", "html");
- //file_put_contents($file, $message);
+ // Removing code blocks before the whitespace removal processing below
+ $codeblocks = [];
+ $message = preg_replace_callback('#
(.*)
#iUs',
+ function ($matches) use (&$codeblocks) {
+ $return = '[codeblock-' . count($codeblocks) . ']';
- $message = str_replace("\r", "", $message);
+ $prefix = '[code]';
+ if ($matches[1] != '') {
+ $prefix = '[code=' . $matches[1] . ']';
+ }
+ $codeblocks[] = $prefix . $matches[2] . '[/code]';
+ return $return;
+ }
+ , $message);
$message = str_replace(array(
"",
- "
"),
+ "",
+ ),
array(
"",
- ""),
+ "",
+ ),
$message);
// remove namespaces
@@ -112,12 +120,12 @@ function html2bbcode($message)
@$doc->loadHTML($message);
- deletenode($doc, 'style');
- deletenode($doc, 'head');
- deletenode($doc, 'title');
- deletenode($doc, 'meta');
- deletenode($doc, 'xml');
- deletenode($doc, 'removeme');
+ xml::deleteNode($doc, 'style');
+ xml::deleteNode($doc, 'head');
+ xml::deleteNode($doc, 'title');
+ xml::deleteNode($doc, 'meta');
+ xml::deleteNode($doc, 'xml');
+ xml::deleteNode($doc, 'removeme');
$xpath = new DomXPath($doc);
$list = $xpath->query("//pre");
@@ -190,6 +198,7 @@ function html2bbcode($message)
node2bbcode($doc, 'span', array(), "", "");
node2bbcode($doc, 'pre', array(), "", "");
+
node2bbcode($doc, 'div', array(), "\r", "\r");
node2bbcode($doc, 'p', array(), "\n", "\n");
@@ -207,12 +216,19 @@ function html2bbcode($message)
//node2bbcode($doc, 'tr', array(), "[tr]", "[/tr]");
//node2bbcode($doc, 'td', array(), "[td]", "[/td]");
- node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
- node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
+ //node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
+
+ node2bbcode($doc, 'h1', array(), "\n\n[h1]", "[/h1]\n");
+ node2bbcode($doc, 'h2', array(), "\n\n[h2]", "[/h2]\n");
+ node2bbcode($doc, 'h3', array(), "\n\n[h3]", "[/h3]\n");
+ node2bbcode($doc, 'h4', array(), "\n\n[h4]", "[/h4]\n");
+ node2bbcode($doc, 'h5', array(), "\n\n[h5]", "[/h5]\n");
+ node2bbcode($doc, 'h6', array(), "\n\n[h6]", "[/h6]\n");
node2bbcode($doc, 'a', array('href'=>'/mailto:(.+)/'), '[mail=$1]', '[/mail]');
node2bbcode($doc, 'a', array('href'=>'/(.+)/'), '[url=$1]', '[/url]');
@@ -225,7 +241,7 @@ function html2bbcode($message)
node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
- node2bbcode($doc, 'code', array(), '[code]', '[/code]');
+ node2bbcode($doc, 'key', array(), '[code]', '[/code]');
$message = $doc->saveHTML();
@@ -294,6 +310,74 @@ function html2bbcode($message)
// Handling Yahoo style of mails
$message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
- return(trim($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 $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);
+ unset($base['query']);
+ unset($base['fragment']);
+
+ $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;
}
-?>