]> git.mxchange.org Git - friendica.git/blobdiff - include/bbcode.php
menu entry for "Delegations" moved to the settings.
[friendica.git] / include / bbcode.php
index bede60e23a48ddd08db153c805f6fa70fda22b5e..d1cb1a9110d3484a48f75e4dc66dd9e5ff379d89 100644 (file)
@@ -2,6 +2,35 @@
 require_once("include/oembed.php");
 require_once('include/event.php');
 
+function bb_rearrange_link($shared) {
+       if ($shared[1] != "type-link")
+               return($shared[0]);
+
+       $newshare = trim($shared[2]);
+       $newshare = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $newshare);
+
+       if (!strpos($shared[0], "[bookmark"))
+               $newshare = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $newshare, 1);
+
+       preg_match("/\[img\](.*?)\[\/img\]/ism", $newshare, $matches);
+
+       if ($matches) {
+               $newshare = str_replace($matches[0], '', $newshare);
+               $newshare = "[img]".$matches[1]."[/img]\n".$newshare;
+       }
+
+       $search = array("\n\n", "\n ", " \n");
+       $replace = array("\n", "\n", "\n");
+       do {
+               $oldtext = $newshare;
+               $newshare = str_replace($search, $replace, $newshare);
+       } while ($oldtext != $newshare);
+
+       $newshare = "[class=type-link]".$newshare."[/class]";
+
+       return($newshare);
+}
+
 function bb_remove_share_information($Text) {
         $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanup_share",$Text);
         return($Text);
@@ -26,8 +55,8 @@ function bb_cleanup_share($shared) {
         if (strpos($shared[1],$title) !== false)
                 $title = "";
 
-        if (strpos($shared[1],$link) !== false)
-                $link = "";
+//        if (strpos($shared[1],$link) !== false)
+//                $link = "";
 
         $text = trim($shared[1]);
 
@@ -322,7 +351,7 @@ function bb_ShareAttributes($match) {
        if ($avatar != "")
                $headline .= '<img src="'.$avatar.'" height="32" width="32" >';
 
-       $headline .= sprintf(t('<span><a href="%s" target="external-link">%s</a> wrote the following <a href="%s" target="external-link">post</a>'.$reldate.':</span>'), $profile, $author, $link);
+       $headline .= sprintf(t('<span><a href="%s" target="_blank">%s</a> wrote the following <a href="%s" target="_blank">post</a>'.$reldate.':</span>'), $profile, $author, $link);
 
         $headline .= "</div>";
 
@@ -369,7 +398,7 @@ function bb_ShareAttributesDiaspora($match) {
        $headline .= '<span><b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').$userid.':</b></span>';
        //$headline .= sprintf(t('<span><b>'.
        //              html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').
-       //              '<a href="%s" target="external-link">%s</a>%s:</b></span>'), $profile, $userid, $posted);
+       //              '<a href="%s" target="_blank">%s</a>%s:</b></span>'), $profile, $userid, $posted);
         $headline .= "</div>";
 
        $text = trim($match[1]);
@@ -423,7 +452,7 @@ function bb_ShareAttributesForExport($match) {
        $headline = '<div class="shared_header">';
        $headline .= sprintf(t('<span><b>'.
                        html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').
-                       '<a href="%s" target="external-link">%s</a>%s:</b></span>'), $link, $userid, $posted);
+                       '<a href="%s" target="_blank">%s</a>%s:</b></span>'), $link, $userid, $posted);
         $headline .= "</div>";
 
        $text = trim($match[1]);
@@ -491,7 +520,8 @@ function bb_ShareAttributesSimple2($match) {
 
        $userid = GetProfileUsername($profile,$author);
 
-        $text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />".$match[2];
+        //$text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />".$match[2];
+        $text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid.": <br />".$match[2];
 
         return($text);
 }
@@ -525,14 +555,105 @@ function GetProfileUsername($profile, $username) {
                }
        }
 
-       // To-Do: Better check for pumpio
-       $pumpio = preg_replace("=https?://([^/]*).*/(\w*)=ism", "$2@$1", $profile);
-       if ($pumpio != $profile)
-               return($username." (".$pumpio.")");
+       // pumpio (http://host.name/user)
+       $rest = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$3", $profile);
+       if ($rest == "") {
+               $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "*$2@$1*", $profile);
+               if ($pumpio != $profile)
+                       return($username." (".$pumpio.")");
+       }
 
        return($username);
 }
 
+function bb_RemovePictureLinks($match) {
+       $text = Cache::get($match[1]);
+
+       if(is_null($text)){
+               $ch = @curl_init($match[1]);
+               @curl_setopt($ch, CURLOPT_NOBODY, true);
+               @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+               @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
+               @curl_exec($ch);
+               $curl_info = @curl_getinfo($ch);
+
+               if (substr($curl_info["content_type"], 0, 6) == "image/")
+                       $text = "[url=".$match[1]."]".$match[1]."[/url]";
+               else {
+                       $text = "[url=".$match[2]."]".$match[2]."[/url]";
+
+                       // if its not a picture then look if its a page that contains a picture link
+                       require_once("include/network.php");
+
+                       $body = fetch_url($match[1]);
+
+                       $doc = new DOMDocument();
+                       @$doc->loadHTML($body);
+                       $xpath = new DomXPath($doc);
+                       $list = $xpath->query("//meta[@name]");
+                       foreach ($list as $node) {
+                               $attr = array();
+
+                               if ($node->attributes->length)
+                                       foreach ($node->attributes as $attribute)
+                                               $attr[$attribute->name] = $attribute->value;
+
+                               if (strtolower($attr["name"]) == "twitter:image")
+                                       $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
+                       }
+               }
+               Cache::set($match[1],$text);
+       }
+       return($text);
+}
+
+function bb_CleanPictureLinksSub($match) {
+       $text = Cache::get($match[1]);
+
+       if(is_null($text)){
+               $ch = @curl_init($match[1]);
+               @curl_setopt($ch, CURLOPT_NOBODY, true);
+               @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+               @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
+               @curl_exec($ch);
+               $curl_info = @curl_getinfo($ch);
+
+               // if its a link to a picture then embed this picture
+               if (substr($curl_info["content_type"], 0, 6) == "image/")
+                       $text = "[img]".$match[1]."[/img]";
+               else {
+                       $text = "[img]".$match[2]."[/img]";
+
+                       // if its not a picture then look if its a page that contains a picture link
+                       require_once("include/network.php");
+
+                       $body = fetch_url($match[1]);
+
+                       $doc = new DOMDocument();
+                       @$doc->loadHTML($body);
+                       $xpath = new DomXPath($doc);
+                       $list = $xpath->query("//meta[@name]");
+                       foreach ($list as $node) {
+                               $attr = array();
+
+                               if ($node->attributes->length)
+                                       foreach ($node->attributes as $attribute)
+                                               $attr[$attribute->name] = $attribute->value;
+
+                               if (strtolower($attr["name"]) == "twitter:image")
+                                       $text = "[img]".$attr["content"]."[/img]";
+                       }
+               }
+               Cache::set($match[1],$text);
+       }
+       return($text);
+}
+
+function bb_CleanPictureLinks($text) {
+       $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text);
+       return ($text);
+}
+
        // BBcode 2 HTML was written by WAY2WEB.net
        // extended to work with Mistpark/Friendica - Mike Macgirvin
 
@@ -554,7 +675,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
        $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
        $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
 
-       // Extract the private images which use data url's since preg has issues with
+       // Extract the private images which use data urls since preg has issues with
        // large data sizes. Stash them away while we do bbcode conversion, and then put them back
        // in after we've done all the regex matching. We cannot use any preg functions to do this.
 
@@ -582,6 +703,10 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
        $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
        $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
 
+       // Rearrange shared links
+       if (get_config("system", "rearrange_shared_links") AND (!$simplehtml OR $tryoembed))
+               $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_rearrange_link",$Text);
+
        // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
        if (!$tryoembed)
                $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
@@ -618,28 +743,39 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
        // Set up the parameters for a MAIL search string
        $MAILSearchString = $URLSearchString;
 
+       // Bookmarks in red - will be converted to bookmarks in friendica
+       $Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
+       $Text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $Text);
 
-       // Perform URL Search
-       // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
-       if (!$forplaintext)
-               $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
-       else
-               $Text = preg_replace("(\[url\](.*?)\[\/url\])ism","$1",$Text);
+       if ($simplehtml == 5)
+               $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text);
 
+       // Perform URL Search
        if ($tryoembed)
                $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
 
-       $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
+       if ($simplehtml == 5)
+               $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url]$1[/url]',$Text);
+       else
+               $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
+
+       // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
+       if (!$forplaintext)
+               $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
+       else {
+               $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism"," $1 ",$Text);
+               $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text);
+       }
 
        if ($tryoembed)
                $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
 
-       $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
-       $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
+       $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$1</a>', $Text);
+       $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
        //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
 
        // Red compatibility, though the link can't be authenticated on Friendica
-       $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
+       $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
 
 
        // we may need to restrict this further if it picks up too many strays
@@ -786,7 +922,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
                $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributes",$Text);
        elseif ($simplehtml == 1)
                $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesSimple",$Text);
-       elseif ($simplehtml == 2)
+       elseif (($simplehtml == 2) OR ($simplehtml == 5))
                $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesSimple2",$Text);
        elseif ($simplehtml == 3)
                $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesDiaspora",$Text);
@@ -807,9 +943,9 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
                $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
        } else {
                $Text = preg_replace("/\[video\](.*?)\[\/video\]/",
-                                       '<a href="$1" target="external-link">$1</a>', $Text);
+                                       '<a href="$1" target="_blank">$1</a>', $Text);
                $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/",
-                                       '<a href="$1" target="external-link">$1</a>', $Text);
+                                       '<a href="$1" target="_blank">$1</a>', $Text);
        }
 
        // html5 video and audio
@@ -835,7 +971,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
                $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
        else
                $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
-                                       '<a href="https://www.youtube.com/watch?v=$1" target="external-link">https://www.youtube.com/watch?v=$1</a>', $Text);
+                                       '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
 
        if ($tryoembed) {
                $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
@@ -849,7 +985,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
                $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
        else
                $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
-                                       '<a href="https://vimeo.com/$1" target="external-link">https://vimeo.com/$1</a>', $Text);
+                                       '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
 
 //     $Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);