' . Map::byCoordinates(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], '
' . Map::byLocation($match[1]) . '
', $match[0]);
}
/**
* Processes [attachment] tags
*
* Note: Can produce a [bookmark] tag in the returned string
*
* @brief Processes [attachment] tags
* @param string $Text
* @param bool|int $simplehtml
* @param bool $tryoembed
* @return string
*/
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) || (strpos($data["text"], "[img]") !== false) || Config::get('system', 'always_show_preview')) && ($data["image"] != "")) {
$data["preview"] = $data["image"];
$data["image"] = "";
}
if ($simplehtml == 7) {
$text = style_url_for_mastodon($data["url"]);
} elseif (($simplehtml != 4) && ($simplehtml != 0)) {
$text = sprintf('%s ', $data["url"], $data["title"]);
} else {
if ($simplehtml != 4) {
$text = sprintf('', $data["type"]);
}
$oembed = sprintf('[bookmark=%s]%s[/bookmark]', $data['url'], $data['title']);
if ($tryoembed) {
try {
$oembed = OEmbed::getHTML($data['url'], $data['title']);
} catch (Exception $e) {
// $oembed isn't modified
}
}
if (stripos($oembed, "';
}
}
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 || (($title != "") && strstr($text, $title))) {
$data["title"] = $data["url"];
} elseif (($text != "") && strstr($title, $text)) {
$data["text"] = $data["title"];
$data["title"] = $data["url"];
}
if (($data["text"] == "") && ($data["title"] != "") && ($data["url"] == "")) {
return $data["title"] . $data["after"];
}
// If the link already is included in the post, don't add it again
if (($data["url"] != "") && strpos($data["text"], $data["url"])) {
return $data["text"] . $data["after"];
}
$text = $data["text"];
if (($data["url"] != "") && ($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") && ($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. (callback function)
* @param array $match Array with the matching values
* @return string reformatted link including HTML codes
*/
function bb_style_url($match) {
$url = $match[1];
if (isset($match[2]) && ($match[1] != $match[2])) {
return $match[0];
}
$parts = parse_url($url);
if (!isset($parts['scheme'])) {
return $match[0];
}
return style_url_for_mastodon($url);
}
/**
* @brief Converts [url] BBCodes in a format that looks fine on Mastodon and GNU Social.
* @param string $url URL that is about to be reformatted
* @return string reformatted link including HTML codes
*/
function style_url_for_mastodon($url) {
$styled_url = $url;
$parts = parse_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]';
}
/*
* [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) {
$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;
}
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);
}
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;
}
/**
* Processes [share] tags
*
* Note: Can produce a [bookmark] tag in the output
*
* @brief Processes [share] tags
* @param array $share preg_match_callback result array
* @param bool|int $simplehtml
* @return string
*/
function bb_ShareAttributes($share, $simplehtml)
{
$attributes = $share[2];
$author = "";
preg_match("/author='(.*?)'/ism", $attributes, $matches);
if (x($matches, 1)) {
$author = html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
preg_match('/author="(.*?)"/ism', $attributes, $matches);
if (x($matches, 1)) {
$author = $matches[1];
}
$profile = "";
preg_match("/profile='(.*?)'/ism", $attributes, $matches);
if (x($matches, 1)) {
$profile = $matches[1];
}
preg_match('/profile="(.*?)"/ism', $attributes, $matches);
if (x($matches, 1)) {
$profile = $matches[1];
}
$avatar = "";
preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
if (x($matches, 1)) {
$avatar = $matches[1];
}
preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
if (x($matches, 1)) {
$avatar = $matches[1];
}
$link = "";
preg_match("/link='(.*?)'/ism", $attributes, $matches);
if (x($matches, 1)) {
$link = $matches[1];
}
preg_match('/link="(.*?)"/ism', $attributes, $matches);
if (x($matches, 1)) {
$link = $matches[1];
}
$posted = "";
preg_match("/posted='(.*?)'/ism", $attributes, $matches);
if (x($matches, 1)) {
$posted = $matches[1];
}
preg_match('/posted="(.*?)"/ism', $attributes, $matches);
if (x($matches, 1)) {
$posted = $matches[1];
}
// 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.
Contact::getIdForURL($profile, 0);
$data = Contact::getDetailsByURL($profile);
if (x($data, "name") && x($data, "addr")) {
$userid_compact = $data["name"] . " (" . $data["addr"] . ")";
} else {
$userid_compact = GetProfileUsername($profile, $author, true);
}
if (x($data, "addr")) {
$userid = $data["addr"];
} else {
$userid = GetProfileUsername($profile, $author, false);
}
if (x($data, "name")) {
$author = $data["name"];
}
if (x($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 = '