]> git.mxchange.org Git - friendica.git/blobdiff - include/html2bbcode.php
Merge pull request #3868 from rabuzarus/20171104_-_use_best_link_url_for_events
[friendica.git] / include / html2bbcode.php
index 189ba91f19c847e673844367c10c9a02054da19b..257539b07481d9dc716a778dd2563c2a860afec8 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * @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
@@ -44,7 +44,7 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
 
                        if (strpos('*'.$startbb, '$1') > 0) {
 
-                               if ($replace and (@$attr[$attribute] != '')) {
+                               if ($replace && (@$attr[$attribute] != '')) {
 
                                        $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
 
@@ -79,16 +79,25 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
        return($replace);
 }
 
-function _replace_code_cb($m){
-       return "<code>".str_replace("\n","<br>\n",$m[1]). "</code>";
-}
-
-function html2bbcode($message)
+function html2bbcode($message, $basepath = '')
 {
 
        $message = str_replace("\r", "", $message);
 
-       $message = preg_replace_callback("|<pre><code>([^<]*)</code></pre>|ism", "_replace_code_cb", $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) . ']';
+
+                       $prefix = '[code]';
+                       if ($matches[1] != '') {
+                               $prefix = '[code=' . $matches[1] . ']';
+                       }
+                       $codeblocks[] = $prefix . $matches[2] . '[/code]';
+                       return $return;
+               }
+       , $message);
 
        $message = str_replace(array(
                                        "<li><p>",
@@ -232,7 +241,6 @@ 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();
@@ -302,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;
 }
-?>