]> git.mxchange.org Git - friendica.git/blobdiff - include/bbcode.php
Cleanup /format pre-move
[friendica.git] / include / bbcode.php
index 97b1b6c0cc4de578d3c3546b5cce8b152acc7f4f..8ccdfd981c1a2c3658762694b63ec82892ae5c4d 100644 (file)
@@ -1,5 +1,6 @@
 <?php
-use \Friendica\Core\Config;
+
+use Friendica\Core\Config;
 
 require_once 'include/oembed.php';
 require_once 'include/event.php';
@@ -10,7 +11,7 @@ require_once 'include/plaintext.php';
 
 function bb_PictureCacheExt($matches) {
        if (strpos($matches[3], "data:image/") === 0) {
-               return ($matches[0]);
+               return $matches[0];
        }
 
        $matches[3] = proxy_url($matches[3]);
@@ -19,7 +20,7 @@ function bb_PictureCacheExt($matches) {
 
 function bb_PictureCache($matches) {
        if (strpos($matches[1], "data:image/") === 0) {
-               return ($matches[0]);
+               return $matches[0];
        }
 
        $matches[1] = proxy_url($matches[1]);
@@ -28,7 +29,7 @@ function bb_PictureCache($matches) {
 
 function bb_map_coords($match) {
        // the extra space in the following line is intentional
-       return str_replace($match[0], '<div class="map"  >' . generate_map(str_replace('/',' ', $match[1])) . '</div>', $match[0]);
+       return str_replace($match[0], '<div class="map"  >' . generate_map(str_replace('/', ' ', $match[1])) . '</div>', $match[0]);
 }
 function bb_map_location($match) {
        // the extra space in the following line is intentional
@@ -63,7 +64,7 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
                        (similar_text($test1,$test2) / strlen($data["title"])) > 0.9)) {
                        $title2 = $data["url"];
                }
-               $text = sprintf('<a href="%s" title="%s" class="attachment thumbnail" rel="nofollow external">%s</a><br />',
+               $text = sprintf('<a href="%s" title="%s" class="attachment" rel="nofollow external">%s</a><br />',
                                $data["url"], $data["title"], $title2);
        } elseif (($simplehtml != 4) AND ($simplehtml != 0)) {
                $text = sprintf('<a href="%s" target="_blank">%s</a><br>', $data["url"], $data["title"]);
@@ -97,7 +98,7 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
                        }
                }
        }
-       return $data["text"] . $text . $data["after"];
+       return trim($data["text"].' '.$text.' '.$data["after"]);
 }
 
 function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
@@ -140,11 +141,11 @@ function bb_remove_share_information($Text, $plaintext = false, $nolink = false)
 }
 
 function bb_cleanstyle($st) {
-  return "<span style=\"" . cleancss($st[1]) . ";\">" . $st[2] . "</span>";
+       return "<span style=\"" . cleancss($st[1]) . ";\">" . $st[2] . "</span>";
 }
 
 function bb_cleanclass($st) {
-  return "<span class=\"" . cleancss($st[1]) . "\">" . $st[2] . "</span>";
+       return "<span class=\"" . cleancss($st[1]) . "\">" . $st[2] . "</span>";
 }
 
 function cleancss($input) {
@@ -165,7 +166,7 @@ function cleancss($input) {
                }
        }
 
-       return($cleaned);
+       return $cleaned;
 }
 
 function stripcode_br_cb($s) {
@@ -198,10 +199,11 @@ function tryoembed($match) {
        return $html;
 }
 
-// [noparse][i]italic[/i][/noparse] turns into
-// [noparse][ i ]italic[ /i ][/noparse],
-// to hide them from parser.
-
+/*
+ * [noparse][i]italic[/i][/noparse] turns into
+ * [noparse][ i ]italic[ /i ][/noparse],
+ * to hide them from parser.
+ */
 function bb_spacefy($st) {
        $whole_match = $st[0];
        $captured = $st[1];
@@ -210,10 +212,11 @@ function bb_spacefy($st) {
        return $new_str;
 }
 
-// The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
-// now turns back and the [noparse] tags are trimed
-// returning [i]italic[/i]
-
+/*
+ * The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
+ * now turns back and the [noparse] tags are trimed
+ * returning [i]italic[/i]
+ */
 function bb_unspacefy_and_trim($st) {
        $whole_match = $st[0];
        $captured = $st[1];
@@ -222,13 +225,13 @@ function bb_unspacefy_and_trim($st) {
 }
 
 function bb_find_open_close($s, $open, $close, $occurence = 1) {
-  if ($occurence < 1) {
-         $occurence = 1;
-  }
+       if ($occurence < 1) {
+               $occurence = 1;
+       }
 
        $start_pos = -1;
        for ($i = 1; $i <= $occurence; $i++) {
-               if ( $start_pos !== false) {
+               if ($start_pos !== false) {
                        $start_pos = strpos($s, $open, $start_pos + 1);
                }
        }
@@ -251,16 +254,16 @@ function bb_find_open_close($s, $open, $close, $occurence = 1) {
 function get_bb_tag_pos($s, $name, $occurence = 1) {
        if ($occurence < 1) {
                $occurence = 1;
-  }
+       }
 
        $start_open = -1;
        for ($i = 1; $i <= $occurence; $i++) {
-               if ( $start_open !== false) {
+               if ($start_open !== false) {
                        $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
                }
        }
 
-       if ( $start_open === false) {
+       if ($start_open === false) {
                return false;
        }
 
@@ -275,7 +278,7 @@ function get_bb_tag_pos($s, $name, $occurence = 1) {
 
        $end_open = strpos($s, '[/' . $name . ']', $start_close);
 
-       if ( $end_open === false) {
+       if ($end_open === false) {
                return false;
        }
 
@@ -307,8 +310,9 @@ function bb_tag_preg_replace($pattern, $replace, $name, $s) {
                $start = substr($string, 0, $pos['start']['open']);
                $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
                $end = substr($string, $pos['end']['close']);
-               if ($end === false)
+               if ($end === false) {
                        $end = '';
+               }
 
                $subject = preg_replace($pattern, $replace, $subject);
                $string = $start . $subject . $end;
@@ -343,14 +347,16 @@ function bb_extract_images($body) {
                        $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
 
                        $cnt++;
-               }
-               else
+               } else {
                        $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
+               }
 
                $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
 
-               if ($orig_body === false) // in case the body ends on a closing image tag
+               if ($orig_body === false) {
+                       // in case the body ends on a closing image tag
                        $orig_body = '';
+               }
 
                $img_start = strpos($orig_body, '[img');
                $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
@@ -537,85 +543,94 @@ function bb_ShareAttributes($share, $simplehtml) {
                                );
                        break;
        }
-       return($text);
+
+       return $text;
 }
 
 function GetProfileUsername($profile, $username, $compact = false, $getnetwork = false) {
 
        $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1@twitter.com", $profile);
        if ($twitter != $profile) {
-               if ($getnetwork)
-                       return(NETWORK_TWITTER);
-               elseif ($compact)
-                       return($twitter);
-               else
-                       return($username." (".$twitter.")");
+               if ($getnetwork) {
+                       return NETWORK_TWITTER;
+               } elseif ($compact) {
+                       return $twitter;
+               } else {
+                       return ($username . " (" . $twitter . ")");
+               }
        }
 
        $appnet = preg_replace("=https?://alpha.app.net/(.*)=ism", "$1@alpha.app.net", $profile);
        if ($appnet != $profile) {
-               if ($getnetwork)
-                       return(NETWORK_APPNET);
-               elseif ($compact)
-                       return($appnet);
-               else
-                       return($username." (".$appnet.")");
+               if ($getnetwork) {
+                       return NETWORK_APPNET;
+               } elseif ($compact) {
+                       return $appnet;
+               } else {
+                       return ($username . " (" . $appnet . ")");
+               }
        }
 
        $gplus = preg_replace("=https?://plus.google.com/(.*)=ism", "$1@plus.google.com", $profile);
        if ($gplus != $profile) {
-               if ($getnetwork)
-                       return(NETWORK_GPLUS);
-               elseif ($compact)
-                       return($gplususername." (".$username.")");
-               else
-                       return($username." (".$gplus.")");
+               if ($getnetwork) {
+                       return NETWORK_GPLUS;
+               } elseif ($compact) {
+                       return ($gplususername . " (" . $username . ")");
+               } else {
+                       return ($username . " (" . $gplus . ")");
+               }
        }
 
        $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
        if ($friendica != $profile) {
-               if ($getnetwork)
-                       return(NETWORK_DFRN);
-               elseif ($compact)
-                       return($friendica);
-               else
-                       return($username." (".$friendica.")");
+               if ($getnetwork) {
+                       return NETWORK_DFRN;
+               } elseif ($compact) {
+                       return $friendica;
+               } else {
+                       return ($username . " (" . $friendica . ")");
+               }
        }
 
        $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
        if ($diaspora != $profile) {
-               if ($getnetwork)
-                       return(NETWORK_DIASPORA);
-               elseif ($compact)
-                       return($diaspora);
-               else
-                       return($username." (".$diaspora.")");
+               if ($getnetwork) {
+                       return NETWORK_DIASPORA;
+               } elseif ($compact) {
+                       return $diaspora;
+               } else {
+                       return ($username . " (" . $diaspora . ")");
+               }
        }
 
        $red = preg_replace("=https?://(.*)/channel/(.*)=ism", "$2@$1", $profile);
        if ($red != $profile) {
-               if ($getnetwork)
+               if ($getnetwork) {
                        // red is identified as Diaspora - friendica can't connect directly to it
-                       return(NETWORK_DIASPORA);
-               elseif ($compact)
-                       return($red);
-               else
-                       return($username." (".$red.")");
+                       return NETWORK_DIASPORA;
+               } elseif ($compact) {
+                       return $red;
+               } else {
+                       return ($username . " (" . $red . ")");
+               }
        }
 
        $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
        if ($StatusnetHost != $profile) {
                $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
                if ($StatusnetUser != $profile) {
+                       /// @TODO Some hosts run on https, not just http and sometimes http is disabled, let's support both here
                        $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
                        $user = json_decode($UserData);
                        if ($user) {
-                               if ($getnetwork)
-                                       return(NETWORK_STATUSNET);
-                               elseif ($compact)
-                                       return($user->screen_name."@".$StatusnetHost);
-                               else
-                                       return($username." (".$user->screen_name."@".$StatusnetHost.")");
+                               if ($getnetwork) {
+                                       return NETWORK_STATUSNET;
+                               } elseif ($compact) {
+                                       return ($user->screen_name . "@" . $StatusnetHost);
+                               } else {
+                                       return ($username . " (" . $user->screen_name . "@" . $StatusnetHost . ")");
+                               }
                        }
                }
        }
@@ -625,16 +640,17 @@ function GetProfileUsername($profile, $username, $compact = false, $getnetwork =
        if ($rest == "") {
                $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$2@$1", $profile);
                if ($pumpio != $profile) {
-                       if ($getnetwork)
-                               return(NETWORK_PUMPIO);
-                       elseif ($compact)
-                               return($pumpio);
-                       else
-                               return($username." (".$pumpio.")");
+                       if ($getnetwork) {
+                               return NETWORK_PUMPIO;
+                       } elseif ($compact) {
+                               return $pumpio;
+                       } else {
+                               return ($username . " (" . $pumpio . ")");
+                       }
                }
        }
 
-       return($username);
+       return $username;
 }
 
 function bb_DiasporaLinks($match) {
@@ -644,7 +660,7 @@ function bb_DiasporaLinks($match) {
 function bb_RemovePictureLinks($match) {
        $text = Cache::get($match[1]);
 
-       if (is_null($text)){
+       if (is_null($text)) {
                $a = get_app();
 
                $stamp1 = microtime(true);
@@ -685,20 +701,22 @@ function bb_RemovePictureLinks($match) {
                }
                Cache::set($match[1],$text);
        }
-       return($text);
+
+       return $text;
 }
 
 function bb_expand_links($match) {
-       if (($match[3] == "") OR ($match[2] == $match[3]) OR stristr($match[2], $match[3]))
-               return ($match[1]."[url]".$match[2]."[/url]");
-       else
-               return ($match[1].$match[3]." [url]".$match[2]."[/url]");
+       if (($match[3] == "") OR ($match[2] == $match[3]) OR stristr($match[2], $match[3])) {
+               return ($match[1] . "[url]" . $match[2] . "[/url]");
+       } else {
+               return ($match[1] . $match[3] . " [url]" . $match[2] . "[/url]");
+       }
 }
 
 function bb_CleanPictureLinksSub($match) {
        $text = Cache::get($match[1]);
 
-       if (is_null($text)){
+       if (is_null($text)) {
                $a = get_app();
 
                $stamp1 = microtime(true);
@@ -740,18 +758,20 @@ function bb_CleanPictureLinksSub($match) {
                }
                Cache::set($match[1],$text);
        }
-       return($text);
+
+       return $text;
 }
 
 function bb_CleanPictureLinks($text) {
        $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text);
-       return ($text);
+       return $text;
 }
 
 function bb_highlight($match) {
-       if (in_array(strtolower($match[1]),['php','css','mysql','sql','abap','diff','html','perl','ruby',
-               'vbscript','avrc','dtd','java','xml','cpp','python','javascript','js','sh']))
-               return text_highlight($match[2],strtolower($match[1]));
+       if (in_array(strtolower($match[1]), ['php', 'css', 'mysql', 'sql', 'abap', 'diff', 'html', 'perl', 'ruby',
+               'vbscript', 'avrc', 'dtd', 'java', 'xml', 'cpp', 'python', 'javascript', 'js', 'sh'])) {
+               return text_highlight($match[2], strtolower($match[1]));
+       }
        return $match[0];
 }
 
@@ -860,7 +880,7 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa
        // Handle attached links or videos
        $Text = bb_attachment($Text, $simplehtml, $tryoembed);
 
-       $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
+       $Text = str_replace(array("\r","\n"), array('<br />', '<br />'), $Text);
 
        if ($preserve_nl) {
                $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
@@ -1115,15 +1135,14 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa
 
        // Shared content
        $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
-               function ($match) use ($simplehtml){
-                       return(bb_ShareAttributes($match, $simplehtml));
+               function ($match) use ($simplehtml) {
+                       return bb_ShareAttributes($match, $simplehtml);
                }, $Text);
 
        $Text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br/><img src="' .App::get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
        $Text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .App::get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
        //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .App::get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
 
-
        // Try to Oembed
        if ($tryoembed) {
                $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></video>', $Text);
@@ -1193,7 +1212,7 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa
        // Summary (e.g. title) is required, earlier revisions only required description (in addition to
        // start which is always required). Allow desc with a missing summary for compatibility.
 
-       if((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
+       if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
                $sub = format_event_html($ev, $simplehtml);
 
                $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism", '', $Text);