]> git.mxchange.org Git - friendica.git/blobdiff - include/html2bbcode.php
Code standards in api_get_user
[friendica.git] / include / html2bbcode.php
index 10a2fd13e5e24eb6be995d5fb7fbc92965e234da..763351e802d44179075969b4876b8066c7c3b0f6 100644 (file)
@@ -1,11 +1,14 @@
 <?php
-/*
-html2bbcode.php
-Converter for HTML to BBCode
-Made by: ike@piratenpartei.de
-Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
-                                       https://github.com/annando/Syncom
-*/
+/**
+ * @file include/html2bbcode.php
+ * @brief Converter for HTML to BBCode
+ *
+ * Made by: ike@piratenpartei.de
+ * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
+ *                                     https://github.com/annando/Syncom
+ */
+
+use Friendica\Util\XML;
 
 function node2bbcode(&$doc, $oldnode, $attributes, $startbb, $endbb)
 {
@@ -23,11 +26,12 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
 
        $list = $xpath->query("//".$oldnode);
        foreach ($list as $oldNode) {
-
                $attr = array();
-               if ($oldNode->attributes->length)
-                       foreach ($oldNode->attributes as $attribute)
+               if ($oldNode->attributes->length) {
+                       foreach ($oldNode->attributes as $attribute) {
                                $attr[$attribute->name] = $attribute->value;
+                       }
+               }
 
                $replace = true;
 
@@ -36,23 +40,22 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
                $i = 0;
 
                foreach ($attributes as $attribute => $value) {
-
                        $startbb = str_replace('\x01'.++$i, '$1', $startbb);
-
                        if (strpos('*'.$startbb, '$1') > 0) {
-
-                               if ($replace and (@$attr[$attribute] != '')) {
-
+                               if ($replace && (@$attr[$attribute] != '')) {
                                        $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
 
                                        // If nothing could be changed
-                                       if ($count == 0)
+                                       if ($count == 0) {
                                                $replace = false;
-                               } else
+                                       }
+                               } else {
                                        $replace = false;
+                               }
                        } else {
-                               if (@$attr[$attribute] != $value)
+                               if (@$attr[$attribute] != $value) {
                                        $replace = false;
+                               }
                        }
                }
 
@@ -76,33 +79,39 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
        return($replace);
 }
 
-if(!function_exists('deletenode')) {
-function deletenode(&$doc, $node)
-{
-       $xpath = new DomXPath($doc);
-       $list = $xpath->query("//".$node);
-       foreach ($list as $child)
-               $child->parentNode->removeChild($child);
-}}
-
-function html2bbcode($message)
+function html2bbcode($message, $basepath = '')
 {
 
        $message = str_replace("\r", "", $message);
 
-       $message = preg_replace_callback("|<pre><code>([^<]*)</code></pre>|ism", function($m) {
-               return "<code>".str_replace("\n","<br>\n",$m[1]). "</code>";
-       }, $message);
+       // Removing code blocks before the whitespace removal processing below
+       $codeblocks = [];
+       $message = preg_replace_callback(
+               '#<pre><code(?: class="([^"]*)")?>(.*)</code></pre>#iUs',
+               function ($matches) use (&$codeblocks) {
+                       $return = '[codeblock-' . count($codeblocks) . ']';
 
-       $message = str_replace(array(
-                                       "<li><p>",
-                                       "</p></li>",
-                               ),
-                               array(
-                                       "<li>",
-                                       "</li>",
-                               ),
-                               $message);
+                       $prefix = '[code]';
+                       if ($matches[1] != '') {
+                               $prefix = '[code=' . $matches[1] . ']';
+                       }
+                       $codeblocks[] = $prefix . $matches[2] . '[/code]';
+                       return $return;
+               },
+               $message
+       );
+
+       $message = str_replace(
+               array(
+                       "<li><p>",
+                       "</p></li>",
+               ),
+               array(
+                       "<li>",
+                       "</li>",
+               ),
+               $message
+       );
 
        // remove namespaces
        $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
@@ -115,17 +124,18 @@ 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");
-       foreach ($list as $node)
+       foreach ($list as $node) {
                $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
+       }
 
        $message = $doc->saveHTML();
        $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br />", " ", ""), $message);
@@ -153,7 +163,7 @@ function html2bbcode($message)
        node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
        node2bbcode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
        node2bbcode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
-*/
+       */
        // Untested
        //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
        //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
@@ -236,8 +246,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]');
+       node2bbcode($doc, 'key', array(), '[code]', '[/code]');
 
        $message = $doc->saveHTML();
 
@@ -277,35 +286,115 @@ function html2bbcode($message)
 
        do {
                $oldmessage = $message;
-               $message = str_replace(array(
-                                       "[/size]\n\n",
-                                       "\n[hr]",
-                                       "[hr]\n",
-                                       "\n[list",
-                                       "[/list]\n",
-                                       "\n[/",
-                                       "[list]\n",
-                                       "[list=1]\n",
-                                       "\n[*]"),
-                               array(
-                                       "[/size]\n",
-                                       "[hr]",
-                                       "[hr]",
-                                       "[list",
-                                       "[/list]",
-                                       "[/",
-                                       "[list]",
-                                       "[list=1]",
-                                       "[*]"),
-                               $message);
+               $message = str_replace(
+                       array(
+                               "[/size]\n\n",
+                               "\n[hr]",
+                               "[hr]\n",
+                               "\n[list",
+                               "[/list]\n",
+                               "\n[/",
+                               "[list]\n",
+                               "[list=1]\n",
+                               "\n[*]"),
+                       array(
+                               "[/size]\n",
+                               "[hr]",
+                               "[hr]",
+                               "[list",
+                               "[/list]",
+                               "[/",
+                               "[list]",
+                               "[list=1]",
+                               "[*]"),
+                       $message
+               );
        } while ($message != $oldmessage);
 
-       $message = str_replace(array('[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'),
-               array('[b]', '[/b]', '[i]', '[/i]'), $message);
+       $message = str_replace(
+               array('[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'),
+               array('[b]', '[/b]', '[i]', '[/i]'),
+               $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;
 }
-?>