' . 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]);
}
function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
$data = get_attachment_data($Text);
if (!$data) {
return $Text;
}
if (isset($data["title"])) {
$data["title"] = strip_tags($data["title"]);
$data["title"] = str_replace(array("http://", "https://"), "", $data["title"]);
}
if (((strpos($data["text"], "[img=") !== false) OR (strpos($data["text"], "[img]") !== false)) AND ($data["image"] != "")) {
$data["preview"] = $data["image"];
$data["image"] = "";
}
if ($simplehtml == 7) {
$title2 = $data["title"];
$test1 = trim(html_entity_decode($data["text"],ENT_QUOTES,'UTF-8'));
$test2 = trim(html_entity_decode($data["title"],ENT_QUOTES,'UTF-8'));
// 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"]);
$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 (strstr(strtolower($oembed), "', trim(bbcode($data["description"])));
}
}
}
return trim($data["text"].' '.$text.' '.$data["after"]);
}
function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
$data = get_attachment_data($Text);
if (!$data) {
return $Text;
} elseif ($nolink) {
return $data["text"] . $data["after"];
}
$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 (($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 (($data["url"] != "") AND strpos($data["text"], $data["url"])) {
return $data["text"] . $data["after"];
}
$text = $data["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] . "";
}
function bb_cleanclass($st) {
return "" . $st[2] . "";
}
function cleancss($input) {
$cleaned = "";
$input = strtolower($input);
for ($i = 0; $i < strlen($input); $i++) {
$char = substr($input, $i, 1);
if (($char >= "a") and ($char <= "z")) {
$cleaned .= $char;
}
if (!(strpos(" #;:0123456789-_.%", $char) === false)) {
$cleaned .= $char;
}
}
return $cleaned;
}
/**
* @brief Converts [url] BBCodes in a format that looks fine on Mastodon.
* @param array $match Array with the matching values
* @return string replaced value
*/
function bb_style_url($match) {
$url = $match[1];
if (isset($match[2]) AND ($match[1] != $match[2])) {
return $match[0];
}
$parts = parse_url($url);
if (!isset($parts['scheme'])) {
return $match[0];
}
$styled_url = $url;
$scheme = $parts['scheme'].'://';
$styled_url = str_replace($scheme, '', $styled_url);
$html = ''.
'%s';
if (strlen($styled_url) > 30) {
$html .= '%s'.
'%s';
$ellipsis = substr($styled_url, 0, 30);
$rest = substr($styled_url, 30);
return sprintf($html, $url, $scheme, $ellipsis, $rest);
} else {
$html .= '%s';
return sprintf($html, $url, $scheme, $styled_url);
}
}
function stripcode_br_cb($s) {
return '[code]' . str_replace(' ', '', $s[1]) . '[/code]';
}
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);
$o = oembed_fetch_url($url);
if (!is_object($o)) {
return $match[0];
}
if (isset($match[2])) {
$o->title = $match[2];
}
if ($o->type == "error") {
return $match[0];
}
$html = oembed_format_object($o);
return $html;
}
/*
* [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;
}
/*
* 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;
}
function bb_find_open_close($s, $open, $close, $occurence = 1) {
if ($occurence < 1) {
$occurence = 1;
}
$start_pos = -1;
for ($i = 1; $i <= $occurence; $i++) {
if ($start_pos !== false) {
$start_pos = strpos($s, $open, $start_pos + 1);
}
}
if ($start_pos === false) {
return false;
}
$end_pos = strpos($s, $close, $start_pos);
if ($end_pos === false) {
return false;
}
$res = array( 'start' => $start_pos, 'end' => $end_pos );
return $res;
}
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) {
$start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
}
}
if ($start_open === false) {
return false;
}
$start_equal = strpos($s, '=', $start_open);
$start_close = strpos($s, ']', $start_open);
if ($start_close === false) {
return false;
}
$start_close++;
$end_open = strpos($s, '[/' . $name . ']', $start_close);
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['start']['equal'] = $start_equal + 1;
}
return $res;
}
function bb_tag_preg_replace($pattern, $replace, $name, $s) {
$string = $s;
$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) {
$end = '';
}
$subject = preg_replace($pattern, $replace, $subject);
$string = $start . $subject . $end;
$occurence++;
$pos = get_bb_tag_pos($string, $name, $occurence);
}
return $string;
}
if (! function_exists('bb_extract_images')) {
function bb_extract_images($body) {
$saved_image = array();
$orig_body = $body;
$new_body = '';
$cnt = 0;
$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)) {
$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:')) {
// 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 {
$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
$orig_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);
}
$new_body = $new_body . $orig_body;
return array('body' => $new_body, 'images' => $saved_image);
}}
if (! function_exists('bb_replace_images')) {
function bb_replace_images($body, $images) {
$newbody = $body;
$cnt = 0;
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);
$cnt++;
}
return $newbody;
}}
function bb_ShareAttributes($share, $simplehtml) {
$attributes = $share[2];
$author = "";
preg_match("/author='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
preg_match('/author="(.*?)"/ism', $attributes, $matches);
if ($matches[1] != "")
$author = $matches[1];
$profile = "";
preg_match("/profile='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$profile = $matches[1];
preg_match('/profile="(.*?)"/ism', $attributes, $matches);
if ($matches[1] != "")
$profile = $matches[1];
$avatar = "";
preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$avatar = $matches[1];
preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
if ($matches[1] != "")
$avatar = $matches[1];
$link = "";
preg_match("/link='(.*?)'/ism", $attributes, $matches);
if ($matches[1] != "")
$link = $matches[1];
preg_match('/link="(.*?)"/ism', $attributes, $matches);
if ($matches[1] != "")
$link = $matches[1];
$posted = "";
$itemcache = get_itemcachepath();
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) : '');
// 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]);
if ($preshare != "")
$preshare .= "
', $Text);
$Text = str_replace('[hr]', '', $Text);
// This is actually executed in prepare_body()
$Text = str_replace('[nosmile]', '', $Text);
// Check for font change text
$Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm", "$2", $Text);
// Declare the format for [code] layout
// $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism", 'stripcode_br_cb', $Text);
$CodeLayout = '$1';
// Check for [code] text
$Text = preg_replace("/\[code\](.*?)\[\/code\]/ism", "$CodeLayout", $Text);
// Declare the format for [spoiler] layout
$SpoilerLayout = '
$1
';
// Check for [spoiler] text
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism", "$SpoilerLayout", $Text);
}
// Check for [spoiler=Author] text
$t_wrote = t('$1 wrote:');
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
" " . $t_wrote . "
$2
",
$Text);
}
// Declare the format for [quote] layout
$QuoteLayout = '
$1
';
// Check for [quote] text
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism", "$QuoteLayout", $Text);
}
// Check for [quote=Author] text
$t_wrote = t('$1 wrote:');
// handle nested quotes
$endlessloop = 0;
while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20)) {
$Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
" " . $t_wrote . "