X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fbbcode.php;h=2715334e46b0712f39aa5e2f514eedc81599c81c;hb=4aac140749c4d9244da424b597bc0a7144c1ba6e;hp=c2ebf35e6308b7464359a8af2b3efe36d13628f7;hpb=5c0b550f38dba890f9d79b5356661bf1bf2f82b6;p=friendica.git
diff --git a/include/bbcode.php b/include/bbcode.php
index c2ebf35e63..2715334e46 100644
--- a/include/bbcode.php
+++ b/include/bbcode.php
@@ -1,225 +1,150 @@
' . generate_map(str_replace('/', ' ', $match[1])) . '', $match[0]);
+}
+function bb_map_location($match) {
+ // the extra space in the following line is intentional
+ return str_replace($match[0], '
' . generate_named_map($match[1]) . '
', $match[0]);
+}
- $url = "";
- preg_match("/url='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $url = $matches[1];
+function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
- preg_match('/url="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $url = $matches[1];
+ $data = get_attachment_data($Text);
+ if (!$data) {
+ return $Text;
+ }
- $title = "";
- preg_match("/title='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $title = $matches[1];
+ if (isset($data["title"])) {
+ $data["title"] = strip_tags($data["title"]);
+ $data["title"] = str_replace(array("http://", "https://"), "", $data["title"]);
+ }
- preg_match('/title="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $title = $matches[1];
+ if (((strpos($data["text"], "[img=") !== false) OR (strpos($data["text"], "[img]") !== false)) AND ($data["image"] != "")) {
+ $data["preview"] = $data["image"];
+ $data["image"] = "";
+ }
- //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
- $title = bbcode(html_entity_decode($title), false, false, true);
- $title = str_replace(array("[", "]"), array("[", "]"), $title);
+ if ($simplehtml == 7) {
+ $title2 = $data["title"];
- $image = "";
- if ($type != "video") {
- preg_match("/image='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $image = $matches[1];
+ $test1 = trim(html_entity_decode($data["text"],ENT_QUOTES,'UTF-8'));
+ $test2 = trim(html_entity_decode($data["title"],ENT_QUOTES,'UTF-8'));
- preg_match('/image="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $image = $matches[1];
- }
-
- $preview = "";
- if ($type != "video") {
- preg_match("/preview='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $preview = $matches[1];
+ // If the link description is similar to the text above then don't add the link description
+ if (($data["title"] != "") AND ((strpos($test1,$test2) !== false) OR
+ (similar_text($test1,$test2) / strlen($data["title"])) > 0.9)) {
+ $title2 = $data["url"];
+ }
+ $text = sprintf('%s ',
+ $data["url"], $data["title"], $title2);
+ } elseif (($simplehtml != 4) AND ($simplehtml != 0)) {
+ $text = sprintf('%s ', $data["url"], $data["title"]);
+ } else {
+ $text = sprintf('', $data["type"]);
- preg_match('/preview="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $preview = $matches[1];
- }
+ $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $data["url"], $data["title"]), $data["url"], $data["title"]);
+ if ($tryoembed) {
+ $oembed = tryoembed($bookmark);
+ } else {
+ $oembed = $bookmark[0];
+ }
- if (((strpos($match[1], "[img=") !== false) OR (strpos($match[1], "[img]") !== false)) AND ($image != "")) {
- $preview = $image;
- $image = "";
+ if (strstr(strtolower($oembed), "', trim(bbcode($data["description"])));
+ }
+ }
}
- $Text .= "]".$description."[/attachment]";
-
- return($Text);
+ return trim($data["text"].' '.$text.' '.$data["after"]);
}
function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
- $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism",
- function ($match) use ($plaintext, $nolink){
- return(bb_cleanup_share($match, $plaintext, $nolink));
- },$Text);
- return($Text);
-}
-function bb_cleanup_share($shared, $plaintext, $nolink) {
- if (!in_array($shared[2], array("type-link", "type-video")))
- return($shared[0]);
-
- if ($plaintext)
- $shared[3] = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism","[bookmark=$1]$1[/bookmark]", $shared[3]);
-
- if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark))
- return($shared[0]);
-
- if ($nolink)
- return(trim($shared[1]));
-
- $title = "";
- $link = "";
+ $data = get_attachment_data($Text);
- if (isset($bookmark[2][0]))
- $title = $bookmark[2][0];
-
- if (isset($bookmark[1][0]))
- $link = $bookmark[1][0];
-
- if (($title != "") AND ((strpos($shared[1],$title) !== false) OR
- (similar_text($shared[1],$title) / strlen($title)) > 0.9))
- $title = "";
-
-// if (strpos($shared[1],$link) !== false)
-// $link = "";
+ if (!$data) {
+ return $Text;
+ } elseif ($nolink) {
+ return $data["text"] . $data["after"];
+ }
- $text = trim($shared[1]);
+ $title = htmlentities($data["title"], ENT_QUOTES, 'UTF-8', false);
+ $text = htmlentities($data["text"], ENT_QUOTES, 'UTF-8', false);
+ if ($plaintext OR (($title != "") AND strstr($text, $title))) {
+ $data["title"] = $data["url"];
+ } elseif (($text != "") AND strstr($title, $text)) {
+ $data["text"] = $data["title"];
+ $data["title"] = $data["url"];
+ }
- if (($text == "") AND ($title != "") AND ($link == ""))
- $text .= "\n\n".trim($title);
+ if (($data["text"] == "") AND ($data["title"] != "") AND ($data["url"] == "")) {
+ return $data["title"] . $data["after"];
+ }
// If the link already is included in the post, don't add it again
- if (($link != "") AND strpos($text, $link))
- return(trim($text));
+ if (($data["url"] != "") AND strpos($data["text"], $data["url"])) {
+ return $data["text"] . $data["after"];
+ }
- if (($link != "") AND ($title != ""))
- $text .= "\n[url=".trim($link)."]".trim($title)."[/url]";
- elseif (($link != ""))
- $text .= "\n".trim($link);
+ $text = $data["text"];
- return(trim($text));
-}
+ if (($data["url"] != "") AND ($data["title"] != "")) {
+ $text .= "\n[url=" . $data["url"] . "]" . $data["title"] . "[/url]";
+ } elseif (($data["url"] != "")) {
+ $text .= "\n" . $data["url"];
+ }
+ return $text . "\n" . $data["after"];
+}
function bb_cleanstyle($st) {
- return "".$st[2]."";
+ return "" . $st[2] . "";
}
function bb_cleanclass($st) {
- return "".$st[2]."";
+ return "" . $st[2] . "";
}
function cleancss($input) {
@@ -231,120 +156,145 @@ function cleancss($input) {
for ($i = 0; $i < strlen($input); $i++) {
$char = substr($input, $i, 1);
- if (($char >= "a") and ($char <= "z"))
+ if (($char >= "a") and ($char <= "z")) {
$cleaned .= $char;
+ }
- if (!(strpos(" #;:0123456789-_", $char) === false))
+ if (!(strpos(" #;:0123456789-_.%", $char) === false)) {
$cleaned .= $char;
+ }
}
- return($cleaned);
+ return $cleaned;
}
function stripcode_br_cb($s) {
return '[code]' . str_replace(' ', '', $s[1]) . '[/code]';
}
-function tryoembed($match){
- //$url = ((count($match)==2)?$match[1]:$match[2]);
+function tryoembed($match) {
$url = $match[1];
// Always embed the SSL version
$url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
- //logger("tryoembed: $url");
-
$o = oembed_fetch_url($url);
- if (isset($match[2]))
+ if (!is_object($o)) {
+ return $match[0];
+ }
+
+ if (isset($match[2])) {
$o->title = $match[2];
+ }
- if ($o->type=="error") return $match[0];
+ if ($o->type == "error") {
+ return $match[0];
+ }
$html = oembed_format_object($o);
- return $html; //oembed_iframe($html,$o->width,$o->height);
+ 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];
- $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
- $new_str = str_replace($captured, $spacefied, $whole_match);
- return $new_str;
+ $whole_match = $st[0];
+ $captured = $st[1];
+ $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
+ $new_str = str_replace($captured, $spacefied, $whole_match);
+ 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];
- $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
- return $unspacefied;
+ $whole_match = $st[0];
+ $captured = $st[1];
+ $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
+ return $unspacefied;
}
-function bb_find_open_close($s, $open, $close, $occurance = 1) {
-
- if($occurance < 1)
- $occurance = 1;
+function bb_find_open_close($s, $open, $close, $occurence = 1) {
+ if ($occurence < 1) {
+ $occurence = 1;
+ }
$start_pos = -1;
- for($i = 1; $i <= $occurance; $i++) {
- if( $start_pos !== false)
+ for ($i = 1; $i <= $occurence; $i++) {
+ if ($start_pos !== false) {
$start_pos = strpos($s, $open, $start_pos + 1);
+ }
}
- if( $start_pos === false)
+ if ($start_pos === false) {
return false;
+ }
$end_pos = strpos($s, $close, $start_pos);
- if( $end_pos === false)
+ if ($end_pos === false) {
return false;
+ }
$res = array( 'start' => $start_pos, 'end' => $end_pos );
return $res;
}
-function get_bb_tag_pos($s, $name, $occurance = 1) {
-
- if($occurance < 1)
- $occurance = 1;
+function get_bb_tag_pos($s, $name, $occurence = 1) {
+ if ($occurence < 1) {
+ $occurence = 1;
+ }
$start_open = -1;
- for($i = 1; $i <= $occurance; $i++) {
- if( $start_open !== false)
+ for ($i = 1; $i <= $occurence; $i++) {
+ 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;
+ }
$start_equal = strpos($s, '=', $start_open);
$start_close = strpos($s, ']', $start_open);
- if( $start_close === false)
+ if ($start_close === false) {
return false;
+ }
$start_close++;
$end_open = strpos($s, '[/' . $name . ']', $start_close);
- if( $end_open === false)
+ if ($end_open === false) {
return false;
+ }
- $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
- 'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
- if( $start_equal !== false)
+ $res = array(
+ 'start' => array(
+ 'open' => $start_open,
+ 'close' => $start_close
+ ),
+ 'end' => array(
+ 'open' => $end_open,
+ 'close' => $end_open + strlen('[/' . $name . ']')
+ ),
+ );
+
+ if ($start_equal !== false) {
$res['start']['equal'] = $start_equal + 1;
+ }
return $res;
}
@@ -353,27 +303,27 @@ function bb_tag_preg_replace($pattern, $replace, $name, $s) {
$string = $s;
- $occurance = 1;
- $pos = get_bb_tag_pos($string, $name, $occurance);
- while($pos !== false && $occurance < 1000) {
-
+ $occurence = 1;
+ $pos = get_bb_tag_pos($string, $name, $occurence);
+ while ($pos !== false && $occurence < 1000) {
$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;
- $occurance++;
- $pos = get_bb_tag_pos($string, $name, $occurance);
+ $occurence++;
+ $pos = get_bb_tag_pos($string, $name, $occurence);
}
return $string;
}
-if(! function_exists('bb_extract_images')) {
+if (! function_exists('bb_extract_images')) {
function bb_extract_images($body) {
$saved_image = array();
@@ -384,26 +334,28 @@ function bb_extract_images($body) {
$img_start = strpos($orig_body, '[img');
$img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
$img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
- while(($img_st_close !== false) && ($img_end !== false)) {
+ while (($img_st_close !== false) && ($img_end !== false)) {
$img_st_close++; // make it point to AFTER the closing bracket
$img_end += $img_start;
- if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
+ if (! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
// This is an embedded image
$saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
$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);
@@ -415,17 +367,17 @@ function bb_extract_images($body) {
return array('body' => $new_body, 'images' => $saved_image);
}}
-if(! function_exists('bb_replace_images')) {
+if (! function_exists('bb_replace_images')) {
function bb_replace_images($body, $images) {
$newbody = $body;
$cnt = 0;
- foreach($images as $image) {
+ foreach ($images as $image) {
// We're depending on the property of 'foreach' (specified on the PHP website) that
// it loops over the array starting from the first element and going sequentially
// to the last element
- $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '', $newbody);
+ $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '', $newbody);
$cnt++;
}
@@ -475,21 +427,40 @@ function bb_ShareAttributes($share, $simplehtml) {
$itemcache = get_itemcachepath();
- // relative dates only make sense when they aren't cached
- if ($itemcache == "") {
- preg_match("/posted='(.*?)'/ism", $attributes, $matches);
- if ($matches[1] != "")
- $posted = $matches[1];
+ preg_match("/posted='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $posted = $matches[1];
- preg_match('/posted="(.*?)"/ism', $attributes, $matches);
- if ($matches[1] != "")
- $posted = $matches[1];
+ preg_match('/posted="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $posted = $matches[1];
+ // relative dates only make sense when they aren't cached
+ if ($itemcache == "")
$reldate = (($posted) ? " " . relative_date($posted) : '');
- }
- $userid = GetProfileUsername($profile,$author, false);
- $userid_compact = GetProfileUsername($profile,$author, true);
+ // We only call this so that a previously unknown contact can be added.
+ // This is important for the function "get_contact_details_by_url".
+ // This function then can fetch an entry from the contact table.
+ get_contact($profile, 0);
+
+ $data = get_contact_details_by_url($profile);
+
+ if (isset($data["name"]) AND ($data["name"] != "") AND isset($data["addr"]) AND ($data["addr"] != ""))
+ $userid_compact = $data["name"]." (".$data["addr"].")";
+ else
+ $userid_compact = GetProfileUsername($profile,$author, true);
+
+ if (isset($data["addr"]) AND ($data["addr"] != ""))
+ $userid = $data["addr"];
+ else
+ $userid = GetProfileUsername($profile,$author, false);
+
+ if (isset($data["name"]) AND ($data["name"] != ""))
+ $author = $data["name"];
+
+ if (isset($data["micro"]) AND ($data["micro"] != ""))
+ $avatar = $data["micro"];
$preshare = trim($share[1]);
@@ -504,9 +475,7 @@ function bb_ShareAttributes($share, $simplehtml) {
$text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": ".$share[3];
break;
case 3: // Diaspora
- $headline = '