Beispiel #1
0
function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false)
{
    $a = get_app();
    // Hide all [noparse] contained bbtags by spacefying them
    // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    // Move all spaces out of the tags
    $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 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.
    $extracted = bb_extract_images($Text);
    $Text = $extracted['body'];
    $saved_image = $extracted['images'];
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // remove some newlines before the general conversion
    $Text = preg_replace("/\\s?\\[share(.*?)\\]\\s?(.*?)\\s?\\[\\/share\\]\\s?/ism", "[share\$1]\$2[/share]", $Text);
    $Text = preg_replace("/\\s?\\[quote(.*?)\\]\\s?(.*?)\\s?\\[\\/quote\\]\\s?/ism", "[quote\$1]\$2[/quote]", $Text);
    $Text = preg_replace("/\n\\[code\\]/ism", "[code]", $Text);
    $Text = preg_replace("/\\[\\/code\\]\n/ism", "[/code]", $Text);
    // Rearrange shares to attachments
    $Text = preg_replace_callback("((.*?)\\[class=(.*?)\\](.*?)\\[\\/class\\])ism", "bb_rearrange_share", $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);
    }
    // Convert new line chars to html <br /> tags
    // nlbr seems to be hopelessly messed up
    //	$Text = nl2br($Text);
    // We'll emulate it.
    $Text = trim($Text);
    $Text = str_replace("\r\n", "\n", $Text);
    // removing multiplicated newlines
    if (get_config("system", "remove_multiplicated_lines")) {
        $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n", "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n");
        $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ", "[/attachment]", "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]");
        do {
            $oldtext = $Text;
            $Text = str_replace($search, $replace, $Text);
        } while ($oldtext != $Text);
    }
    // Handle attached links or videos
    $Text = bb_attachment($Text, $simplehtml, $tryoembed);
    $Text = str_replace(array("\r", "\n"), array('<br />', '<br />'), $Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Remove all hashtag addresses
    if ((!$tryoembed or $simplehtml) and !in_array($simplehtml, array(3, 7))) {
        $Text = preg_replace("/([#@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '$1$3', $Text);
    } elseif ($simplehtml == 3) {
        $Text = preg_replace("/([@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '$1<a href="$2">$3</a>', $Text);
    } elseif ($simplehtml == 7) {
        $Text = preg_replace("/([@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '$1<span class="vcard"><a href="$2" class="url" title="$3"><span class="fn nickname mention">$3</span></a></span>', $Text);
    }
    // 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);
    $Text = preg_replace("/#\\[url\\=[{$URLSearchString}]*\\]\\^\\[\\/url\\]\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/i", "[bookmark=\$1]\$2[/bookmark]", $Text);
    if (in_array($simplehtml, array(2, 6, 7, 8, 9))) {
        $Text = preg_replace_callback("/([^#@])\\[url\\=([^\\]]*)\\](.*?)\\[\\/url\\]/ism", "bb_expand_links", $Text);
        //$Text = preg_replace("/[^#@]\[url\=([^\]]*)\](.*?)\[\/url\]/ism",' $2 [url]$1[/url]',$Text);
        $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", ' $2 [url]$1[/url]', $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);
    }
    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);
    }
    // Handle Diaspora posts
    $Text = preg_replace_callback("&\\[url=/posts/([^\\[\\]]*)\\](.*)\\[\\/url\\]&Usi", 'bb_DiasporaLinks', $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 ($simplehtml != 7) {
    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="_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="_blank">$2</a>', $Text);
    // we may need to restrict this further if it picks up too many strays
    // link acct:user@host to a webfinger profile redirector
    $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "\$1@\$2" . '" target="extlink" >acct:' . "\$1@\$2\$3" . '</a>', $Text);
    // Perform MAIL Search
    $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    // leave open the posibility of [map=something]
    // this is replaced in prepare_body() which has knowledge of the item location
    if (strpos($Text, '[/map]') !== false) {
        $Text = preg_replace_callback("/\\[map\\](.*?)\\[\\/map\\]/ism", 'bb_map_location', $Text);
    }
    if (strpos($Text, '[map=') !== false) {
        $Text = preg_replace_callback("/\\[map=(.*?)\\]/ism", 'bb_map_coords', $Text);
    }
    if (strpos($Text, '[map]') !== false) {
        $Text = preg_replace("/\\[map\\]/", '<div class="map"></div>', $Text);
    }
    // Check for headers
    $Text = preg_replace("(\\[h1\\](.*?)\\[\\/h1\\])ism", '<h1>$1</h1>', $Text);
    $Text = preg_replace("(\\[h2\\](.*?)\\[\\/h2\\])ism", '<h2>$1</h2>', $Text);
    $Text = preg_replace("(\\[h3\\](.*?)\\[\\/h3\\])ism", '<h3>$1</h3>', $Text);
    $Text = preg_replace("(\\[h4\\](.*?)\\[\\/h4\\])ism", '<h4>$1</h4>', $Text);
    $Text = preg_replace("(\\[h5\\](.*?)\\[\\/h5\\])ism", '<h5>$1</h5>', $Text);
    $Text = preg_replace("(\\[h6\\](.*?)\\[\\/h6\\])ism", '<h6>$1</h6>', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    // Check for Underline text
    $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    // Check for strike-through text
    $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    // Check for over-line text
    $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    // Check for colored text
    $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px; line-height: initial;\">\$2</span>", $Text);
    $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1; line-height: initial;\">\$2</span>", $Text);
    // Check for centered text
    $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    // Check for style sheet commands
    $Text = preg_replace_callback("(\\[style=(.*?)\\](.*?)\\[\\/style\\])ism", "bb_cleanstyle", $Text);
    // Check for CSS classes
    $Text = preg_replace_callback("(\\[class=(.*?)\\](.*?)\\[\\/class\\])ism", "bb_cleanclass", $Text);
    // handle nested lists
    $endlessloop = 0;
    while ((strpos($Text, "[/list]") !== false && strpos($Text, "[list") !== false || strpos($Text, "[/ol]") !== false && strpos($Text, "[ol]") !== false || strpos($Text, "[/ul]") !== false && strpos($Text, "[ul]") !== false || strpos($Text, "[/li]") !== false && strpos($Text, "[li]") !== false) && ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
    }
    $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
    $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
    $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    $Text = str_replace('[hr]', '<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", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    // Declare the format for [code] layout
    //	$Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    // Declare the format for [spoiler] layout
    $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
    // 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", "<br /><strong class=" . '"spoiler"' . ">" . $t_wrote . "</strong><blockquote class=" . '"spoiler"' . ">\$2</blockquote>", $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // 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", "<br /><strong class=" . '"author"' . ">" . $t_wrote . "</strong><blockquote>\$2</blockquote>", $Text);
    }
    // [img=widthxheight]image source[/img]
    $Text = preg_replace_callback("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", 'bb_PictureCacheExt', $Text);
    $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
    $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
    // Images
    // [img]pathtoimage[/img]
    $Text = preg_replace_callback("/\\[img\\](.*?)\\[\\/img\\]/ism", 'bb_PictureCache', $Text);
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    $Text = preg_replace("/\\[zmg\\](.*?)\\[\\/zmg\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    // Shared content
    $Text = preg_replace_callback("/(.*?)\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", function ($match) use($simplehtml) {
        return bb_ShareAttributes($match, $simplehtml);
    }, $Text);
    $Text = preg_replace("/\\[crypt\\](.*?)\\[\\/crypt\\]/ism", '<br/><img src="' . $a->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="' . $a->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="' .$a->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);
        $Text = preg_replace("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\\[\\/audio\\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
        $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
    } else {
        $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/", '<a href="$1" target="_blank">$1</a>', $Text);
        $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/", '<a href="$1" target="_blank">$1</a>', $Text);
    }
    // html5 video and audio
    if ($tryoembed) {
        $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
    } else {
        $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<a href="$1">$1</a>', $Text);
    }
    // Youtube extensions
    if ($tryoembed) {
        $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[youtube\\](www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    }
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    if ($tryoembed) {
        $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="_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);
        $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    }
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    if ($tryoembed) {
        $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="_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);
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // Avoid triple linefeeds through oembed
    $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
    // 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')) {
        $sub = format_event_html($ev, $simplehtml);
        $Text = preg_replace("/\\[event\\-summary\\](.*?)\\[\\/event\\-summary\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    //replace oneliner <code> with <key>
    $Text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism", 'bb_onelinecode_cb', $Text);
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    $Text = preg_replace('/\\&\\#039\\;/', '\'', $Text);
    $Text = preg_replace('/\\&quot\\;/', '"', $Text);
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<([^>]*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    $Text = preg_replace("/\\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|gopher|cid)(.*?)\\>/ism", '<$1$2="">', $Text);
    if ($saved_image) {
        $Text = bb_replace_images($Text, $saved_image);
    }
    // Clean up the HTML by loading and saving the HTML with the DOM.
    // Bad structured html can break a whole page.
    // For performance reasons do it only with ativated item cache or at export.
    if (!$tryoembed or get_itemcachepath() != "") {
        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
        $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
        $encoding = '<?xml encoding="UTF-8">';
        @$doc->loadHTML($encoding . $doctype . "<html><body>" . $Text . "</body></html>");
        $doc->encoding = 'UTF-8';
        $Text = $doc->saveHTML();
        $Text = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $Text);
        $Text = str_replace('<br></li>', '</li>', $Text);
        //$Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
    }
    // Clean up some useless linebreaks in lists
    //$Text = str_replace('<br /><ul','<ul ', $Text);
    //$Text = str_replace('</ul><br />','</ul>', $Text);
    //$Text = str_replace('</li><br />','</li>', $Text);
    //$Text = str_replace('<br /><li>','<li>', $Text);
    //	$Text = str_replace('<br /><ul','<ul ', $Text);
    call_hooks('bbcode', $Text);
    return trim($Text);
}
Beispiel #2
0
function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false)
{
    // Hide all [noparse] contained bbtags by spacefying them
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    }
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // process [observer] tags before we do anything else because we might
    // be stripping away stuff that then doesn't need to be worked on anymore
    if ($cache) {
        $observer = false;
    } else {
        $observer = App::get_observer();
    }
    if (strpos($Text, '[/observer]') !== false || strpos($Text, '[/rpost]') !== false) {
        if ($observer) {
            $Text = preg_replace("/\\[observer\\=1\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[observer\\=0\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace_callback("/\\[rpost(=(.*?))?\\](.*?)\\[\\/rpost\\]/ism", 'rpost_callback', $Text);
        } else {
            $Text = preg_replace("/\\[observer\\=1\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace("/\\[observer\\=0\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[rpost(=.*?)?\\](.*?)\\[\\/rpost\\]/ism", '', $Text);
        }
    }
    if ($cache) {
        $channel = false;
    } else {
        $channel = App::get_channel();
    }
    if (strpos($Text, '[/channel]') !== false) {
        if ($channel) {
            $Text = preg_replace("/\\[channel\\=1\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[channel\\=0\\].*?\\[\\/channel\\]/ism", '', $Text);
        } else {
            $Text = preg_replace("/\\[channel\\=1\\].*?\\[\\/channel\\]/ism", '', $Text);
            $Text = preg_replace("/\\[channel\\=0\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
        }
    }
    $x = bb_extract_images($Text);
    $Text = $x['body'];
    $saved_images = $x['images'];
    $Text = str_replace(array('[baseurl]', '[sitename]'), array(z_root(), get_config('system', 'sitename')), $Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    // nlbr seems to be hopelessly messed up
    //	$Text = nl2br($Text);
    // We'll emulate it.
    $Text = str_replace("\r\n", "\n", $Text);
    $Text = str_replace(array("\r", "\n"), array('<br />', '<br />'), $Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    $Text = str_replace(array("\t", "  "), array("&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;"), $Text);
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // replace [observer.baseurl]
    if ($observer) {
        $s1 = '<span class="bb_observer" title="' . t('Different viewers will see this text differently') . '">';
        $s2 = '</span>';
        $obsBaseURL = $observer['xchan_connurl'];
        $obsBaseURL = preg_replace("/\\/poco\\/.*\$/", '', $obsBaseURL);
        $Text = str_replace('[observer.baseurl]', $obsBaseURL, $Text);
        $Text = str_replace('[observer.url]', $observer['xchan_url'], $Text);
        $Text = str_replace('[observer.name]', $s1 . $observer['xchan_name'] . $s2, $Text);
        $Text = str_replace('[observer.address]', $s1 . $observer['xchan_addr'] . $s2, $Text);
        $Text = str_replace('[observer.webname]', substr($observer['xchan_addr'], 0, strpos($observer['xchan_addr'], '@')), $Text);
        $Text = str_replace('[observer.photo]', $s1 . '[zmg]' . $observer['xchan_photo_l'] . '[/zmg]' . $s2, $Text);
    } else {
        $Text = str_replace('[observer.baseurl]', '', $Text);
        $Text = str_replace('[observer.url]', '', $Text);
        $Text = str_replace('[observer.name]', '', $Text);
        $Text = str_replace('[observer.address]', '', $Text);
        $Text = str_replace('[observer.webname]', '', $Text);
        $Text = str_replace('[observer.photo]', '', $Text);
    }
    // Perform URL Search
    $urlchars = '[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\@\\_\\~\\#\\%\\$\\!\\+\\,\\@]';
    if (strpos($Text, 'http') !== false) {
        $Text = preg_replace("/([^\\]\\='" . '"' . "\\/]|^|\\#\\^)(https?\\:\\/\\/{$urlchars}+)/ism", '$1<a href="$2" target="_blank" >$2</a>', $Text);
    }
    if (strpos($Text, '[/share]') !== false) {
        $Text = preg_replace_callback("/\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", 'bb_ShareAttributes', $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/url]') !== false) {
            $Text = preg_replace_callback("/[^\\^]\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
        }
    }
    if (strpos($Text, '[/url]') !== false) {
        $Text = preg_replace("/\\#\\^\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" target="_blank" >$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);
    }
    if (strpos($Text, '[/zrl]') !== false) {
        $Text = preg_replace("/\\#\\^\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" target="_blank" >$2</a>', $Text);
        $Text = preg_replace("/\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" target="_blank" >$2</a>', $Text);
    }
    // Remove bookmarks from UNO
    if (UNO) {
        $Text = str_replace('<span class="bookmark-identifier">#^</span>', '', $Text);
    }
    // Perform MAIL Search
    if (strpos($Text, '[/mail]') !== false) {
        $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1" target="_blank" >$2</a>', $Text);
    }
    // leave open the posibility of [map=something]
    // this is replaced in prepare_body() which has knowledge of the item location
    if (strpos($Text, '[/map]') !== false) {
        $Text = preg_replace_callback("/\\[map\\](.*?)\\[\\/map\\]/ism", 'bb_map_location', $Text);
    }
    if (strpos($Text, '[map=') !== false) {
        $Text = preg_replace_callback("/\\[map=(.*?)\\]/ism", 'bb_map_coords', $Text);
    }
    if (strpos($Text, '[map]') !== false) {
        $Text = preg_replace("/\\[map\\]/", '<div class="map"></div>', $Text);
    }
    // Check for bold text
    if (strpos($Text, '[b]') !== false) {
        $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    }
    // Check for Italics text
    if (strpos($Text, '[i]') !== false) {
        $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    }
    // Check for Underline text
    if (strpos($Text, '[u]') !== false) {
        $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    }
    // Check for strike-through text
    if (strpos($Text, '[s]') !== false) {
        $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    }
    // Check for over-line text
    if (strpos($Text, '[o]') !== false) {
        $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    }
    if (strpos($Text, '[sup]') !== false) {
        $Text = preg_replace("(\\[sup\\](.*?)\\[\\/sup\\])ism", '<sup>$1</sup>', $Text);
    }
    if (strpos($Text, '[sub]') !== false) {
        $Text = preg_replace("(\\[sub\\](.*?)\\[\\/sub\\])ism", '<sub>$1</sub>', $Text);
    }
    // Check for colored text
    if (strpos($Text, '[/color]') !== false) {
        $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    }
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    if (strpos($Text, '[/size]') !== false) {
        $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px;\">\$2</span>", $Text);
        $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    }
    // Check for h1
    if (strpos($Text, '[h1]') !== false) {
        $Text = preg_replace("(\\[h1\\](.*?)\\[\\/h1\\])ism", '<h1>$1</h1>', $Text);
    }
    // Check for h2
    if (strpos($Text, '[h2]') !== false) {
        $Text = preg_replace("(\\[h2\\](.*?)\\[\\/h2\\])ism", '<h2>$1</h2>', $Text);
    }
    // Check for h3
    if (strpos($Text, '[h3]') !== false) {
        $Text = preg_replace("(\\[h3\\](.*?)\\[\\/h3\\])ism", '<h3>$1</h3>', $Text);
    }
    // Check for h4
    if (strpos($Text, '[h4]') !== false) {
        $Text = preg_replace("(\\[h4\\](.*?)\\[\\/h4\\])ism", '<h4>$1</h4>', $Text);
    }
    // Check for h5
    if (strpos($Text, '[h5]') !== false) {
        $Text = preg_replace("(\\[h5\\](.*?)\\[\\/h5\\])ism", '<h5>$1</h5>', $Text);
    }
    // Check for h6
    if (strpos($Text, '[h6]') !== false) {
        $Text = preg_replace("(\\[h6\\](.*?)\\[\\/h6\\])ism", '<h6>$1</h6>', $Text);
    }
    // Check for table of content without params
    if (strpos($Text, '[toc]') !== false) {
        $Text = preg_replace("/\\[toc\\]/ism", '<ul id="toc"></ul>', $Text);
    }
    // Check for table of content with params
    if (strpos($Text, '[toc') !== false) {
        $Text = preg_replace("/\\[toc([^\\]]+?)\\]/ism", '<ul$1></ul>', $Text);
    }
    // Check for centered text
    if (strpos($Text, '[/center]') !== false) {
        $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    }
    // Check for footer
    if (strpos($Text, '[/footer]') !== false) {
        $Text = preg_replace("(\\[footer\\](.*?)\\[\\/footer\\])ism", "<div class=\"wall-item-footer\">\$1</div>", $Text);
    }
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    // handle nested lists
    $endlessloop = 0;
    while ((strpos($Text, "[/list]") !== false && strpos($Text, "[list") !== false || strpos($Text, "[/ol]") !== false && strpos($Text, "[ol]") !== false || strpos($Text, "[/ul]") !== false && strpos($Text, "[ul]") !== false || strpos($Text, "[/dl]") !== false && strpos($Text, "[dl") !== false || strpos($Text, "[/li]") !== false && strpos($Text, "[li]") !== false) && ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
        // [dl] tags have an optional [dl terms="bi"] form where bold/italic/underline/mono/large
        // etc. style may be specified for the "terms" in the definition list. The quotation marks
        // are also optional. The regex looks intimidating, but breaks down as:
        //   "[dl" <optional-whitespace> <optional-termStyles> "]" <matchGroup2> "[/dl]"
        // where optional-termStyles are: "terms=" <optional-quote> <matchGroup1> <optional-quote>
        $Text = preg_replace_callback('/\\[dl[[:space:]]*(?:terms=(?:&quot;|")?([a-zA-Z]+)(?:&quot;|")?)?\\](.*?)\\[\\/dl\\]/ism', 'bb_definitionList', $Text);
    }
    if (strpos($Text, '[th]') !== false) {
        $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    }
    if (strpos($Text, '[td]') !== false) {
        $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    }
    if (strpos($Text, '[tr]') !== false) {
        $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    }
    if (strpos($Text, '[/table]') !== false) {
        $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
        $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
        $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    }
    $Text = str_replace('</tr><br /><tr>', "</tr>\n<tr>", $Text);
    $Text = str_replace('[hr]', '<hr />', $Text);
    // This is actually executed in prepare_body()
    $Text = str_replace('[nosmile]', '', $Text);
    // Check for font change text
    if (strpos($Text, '[/font]') !== false) {
        $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    }
    // Check for [code] text
    if (strpos($Text, '[code]') !== false) {
        $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/ism", 'bb_code', $Text);
    }
    // Check for [spoiler] text
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[spoiler\\](.*?)\\[\\/spoiler\\]/ism", 'bb_spoilertag', $Text);
    }
    // Check for [spoiler=Author] text
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[spoiler=(.*?)\\](.*?)\\[\\/spoiler\\]/ism", 'bb_spoilertag', $Text);
    }
    // Check for [open] text
    $endlessloop = 0;
    while (strpos($Text, "[/open]") !== false and strpos($Text, "[open]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[open\\](.*?)\\[\\/open\\]/ism", 'bb_opentag', $Text);
    }
    // Check for [open=Title] text
    $endlessloop = 0;
    while (strpos($Text, "[/open]") !== false and strpos($Text, "[open=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[open=(.*?)\\](.*?)\\[\\/open\\]/ism", 'bb_opentag', $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // 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", "<span class=" . '"bb-quote"' . ">" . $t_wrote . "</span><blockquote>\$2</blockquote>", $Text);
    }
    // Images
    // [img]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=left\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=right\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=left\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=right\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // style (sanitized)
    if (strpos($Text, '[/style]') !== false) {
        $Text = preg_replace_callback("(\\[style=(.*?)\\](.*?)\\[\\/style\\])ism", "bb_sanitize_style", $Text);
    }
    // crypt
    if (strpos($Text, '[/crypt]') !== false) {
        $x = random_string();
        $Text = preg_replace("/\\[crypt\\](.*?)\\[\\/crypt\\]/ism", '<br /><div id="' . $x . '"><img src="' . z_root() . '/images/lock_icon.gif" onclick="red_decrypt(\'rot13\',\'\',\'$1\',\'#' . $x . '\');" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br /></div>', $Text);
        $Text = preg_replace_callback("/\\[crypt (.*?)\\](.*?)\\[\\/crypt\\]/ism", 'bb_parse_crypt', $Text);
    }
    if (strpos($Text, '[/app]') !== false) {
        $Text = preg_replace_callback("/\\[app\\](.*?)\\[\\/app\\]/ism", 'bb_parse_app', $Text);
    }
    if (strpos($Text, '[/element]') !== false) {
        $Text = preg_replace_callback("/\\[element\\](.*?)\\[\\/element\\]/ism", 'bb_parse_element', $Text);
    }
    // html5 video and audio
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace_callback("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mpeg|mpg))\\[\\/video\\]/ism", 'tryzrlvideo', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace_callback("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3|opus))\\[\\/audio\\]/ism", 'tryzrlaudio', $Text);
    }
    if (strpos($Text, '[/zvideo]') !== false) {
        $Text = preg_replace_callback("/\\[zvideo\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mpeg|mpg))\\[\\/zvideo\\]/ism", 'tryzrlvideo', $Text);
    }
    if (strpos($Text, '[/zaudio]') !== false) {
        $Text = preg_replace_callback("/\\[zaudio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3|opus))\\[\\/zaudio\\]/ism", 'tryzrlaudio', $Text);
    }
    // Try to Oembed
    if ($tryoembed) {
        if (strpos($Text, '[/video]') !== false) {
            $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/audio]') !== false) {
            $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/zvideo]') !== false) {
            $Text = preg_replace_callback("/\\[zvideo\\](.*?)\\[\\/zvideo\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/zaudio]') !== false) {
            $Text = preg_replace_callback("/\\[zaudio\\](.*?)\\[\\/zaudio\\]/ism", 'tryoembed', $Text);
        }
    }
    // if video couldn't be embedded, link to it instead.
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/", '<a href="$1" target="_blank" >$1</a>', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/", '<a href="$1" target="_blank" >$1</a>', $Text);
    }
    if (strpos($Text, '[/zvideo]') !== false) {
        $Text = preg_replace("/\\[zvideo\\](.*?)\\[\\/zvideo\\]/", '<a class="zid" href="$1" target="_blank" >$1</a>', $Text);
    }
    if (strpos($Text, '[/zaudio]') !== false) {
        $Text = preg_replace("/\\[zaudio\\](.*?)\\[\\/zaudio\\]/", '<a class="zid" href="$1" target="_blank" >$1</a>', $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace_callback("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", 'bb_iframe', $Text);
        }
    } else {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<a href="$1" target="_blank" >$1</a>', $Text);
        }
    }
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // Avoid triple linefeeds through oembed
    $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
    // 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')) {
        $sub = format_event_html($ev);
        $sub = str_replace('$', "", $sub);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-summary\\](.*?)\\[\\/event\\-summary\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-id\\](.*?)\\[\\/event\\-id\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
        $Text = str_replace("", '$', $Text);
    }
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    // fix any escaped ampersands that may have been converted into links
    if (strpos($Text, '&amp;') !== false) {
        $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    }
    // This is subtle - it's an XSS filter. It only accepts links with a protocol scheme and where
    // the scheme begins with z (zhttp), h (http(s)), f (ftp), m (mailto), and named anchors.
    $Text = preg_replace("/\\<(.*?)(src|href)=\"[^zhfm#](.*?)\\>/ism", '<$1$2="">', $Text);
    $Text = bb_replace_images($Text, $saved_images);
    call_hooks('bbcode', $Text);
    return $Text;
}
Beispiel #3
0
function bbcode($Text, $preserve_nl = false, $tryoembed = true)
{
    $a = get_app();
    // Move all spaces out of the tags
    // ....Uhm why?
    // This is basically doing a trim() on the stuff in between tags, but it messes up
    // carefully crafted bbcode and especially other pre-formatted code.
    // Commenting out until we come up with a use case where it's needed. Then let's try and
    // special case rather than a heavy-handed approach like this.
    //	$Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
    //	$Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
    // Hide all [noparse] contained bbtags by spacefying them
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    }
    //  Not yet implemented - thinking this should display a map or perhaps be a map directive
    //	if (strpos($Text,'[location]') !== false) {
    //		$Text = preg_replace_callback("/\[location\](.*?)\[\/location\]/ism", 'bb_location',$Text);
    //	}
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // process [observer] tags before we do anything else because we might
    // be stripping away stuff that then doesn't need to be worked on anymore
    $observer = $a->get_observer();
    if (strpos($Text, '[/observer]') !== false || strpos($Text, '[/rpost]') !== false) {
        if ($observer) {
            $Text = preg_replace("/\\[observer\\=1\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[observer\\=0\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace_callback("/\\[rpost(=(.*?))?\\](.*?)\\[\\/rpost\\]/ism", 'rpost_callback', $Text);
        } else {
            $Text = preg_replace("/\\[observer\\=1\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace("/\\[observer\\=0\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[rpost(=.*?)?\\](.*?)\\[\\/rpost\\]/ism", '', $Text);
        }
    }
    $channel = $a->get_channel();
    if (strpos($Text, '[/channel]') !== false) {
        if ($channel) {
            $Text = preg_replace("/\\[channel\\=1\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[channel\\=0\\].*?\\[\\/channel\\]/ism", '', $Text);
        } else {
            $Text = preg_replace("/\\[channel\\=1\\].*?\\[\\/channel\\]/ism", '', $Text);
            $Text = preg_replace("/\\[channel\\=0\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
        }
    }
    $Text = str_replace(array('[baseurl]', '[sitename]'), array(z_root(), get_config('system', 'sitename')), $Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    // nlbr seems to be hopelessly messed up
    //	$Text = nl2br($Text);
    // We'll emulate it.
    $Text = str_replace("\r\n", "\n", $Text);
    $Text = str_replace(array("\r", "\n"), array('<br />', '<br />'), $Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    $Text = str_replace(array("\t", "  "), array("&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;"), $Text);
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // replace [observer.baseurl]
    if ($observer) {
        $obsBaseURL = $observer['xchan_connurl'];
        $obsBaseURL = preg_replace("/\\/poco\\/.*\$/", '', $obsBaseURL);
        $Text = str_replace('[observer.baseurl]', $obsBaseURL, $Text);
        $Text = str_replace('[observer.url]', $observer['xchan_url'], $Text);
        $Text = str_replace('[observer.name]', $observer['xchan_name'], $Text);
        $Text = str_replace('[observer.address]', $observer['xchan_addr'], $Text);
        $Text = str_replace('[observer.webname]', substr($observer['xchan_addr'], 0, strpos($observer['xchan_addr'], '@')), $Text);
        $Text = str_replace('[observer.photo]', '[zmg]' . $observer['xchan_photo_l'] . '[/zmg]', $Text);
    } else {
        $Text = str_replace('[observer.baseurl]', '', $Text);
        $Text = str_replace('[observer.url]', '', $Text);
        $Text = str_replace('[observer.name]', '', $Text);
        $Text = str_replace('[observer.address]', '', $Text);
        $Text = str_replace('[observer.webname]', '', $Text);
        $Text = str_replace('[observer.photo]', '', $Text);
    }
    // Perform URL Search
    $urlchars = '[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\@\\_\\~\\#\\%\\$\\!\\+\\,]';
    if (strpos($Text, 'http') !== false) {
        $Text = preg_replace("/([^\\]\\='" . '"' . "]|^)(https?\\:\\/\\/{$urlchars}+)/ism", '$1<a href="$2" >$2</a>', $Text);
    }
    if (strpos($Text, '[/qr]') !== false) {
        $Text = preg_replace_callback("/\\[qr\\](.*?)\\[\\/qr\\]/ism", "bb_qr", $Text);
    }
    if (strpos($Text, '[/share]') !== false) {
        $Text = preg_replace_callback("/\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", "bb_ShareAttributes", $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/url]') !== false) {
            $Text = preg_replace_callback("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
        }
    }
    if (strpos($Text, '[/url]') !== false) {
        $Text = preg_replace("/\\#\\^\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" >$2</a>', $Text);
        $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<a href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<a href="$1" >$2</a>', $Text);
    }
    if (strpos($Text, '[/zrl]') !== false) {
        $Text = preg_replace("/\\#\\^\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" >$2</a>', $Text);
        $Text = preg_replace("/\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" >$2</a>', $Text);
    }
    // Perform MAIL Search
    if (strpos($Text, '[/mail]') !== false) {
        $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
        $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    }
    // Check for bold text
    if (strpos($Text, '[b]') !== false) {
        $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    }
    // Check for Italics text
    if (strpos($Text, '[i]') !== false) {
        $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    }
    // Check for Underline text
    if (strpos($Text, '[u]') !== false) {
        $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    }
    // Check for strike-through text
    if (strpos($Text, '[s]') !== false) {
        $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    }
    // Check for over-line text
    if (strpos($Text, '[o]') !== false) {
        $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    }
    if (strpos($Text, '[sup]') !== false) {
        $Text = preg_replace("(\\[sup\\](.*?)\\[\\/sup\\])ism", '<sup>$1</sup>', $Text);
    }
    if (strpos($Text, '[sub]') !== false) {
        $Text = preg_replace("(\\[sub\\](.*?)\\[\\/sub\\])ism", '<sub>$1</sub>', $Text);
    }
    // Check for colored text
    if (strpos($Text, '[/color]') !== false) {
        $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    }
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    if (strpos($Text, '[/size]') !== false) {
        $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px;\">\$2</span>", $Text);
        $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    }
    // Check for h1
    if (strpos($Text, '[h1]') !== false) {
        $Text = preg_replace("(\\[h1\\](.*?)\\[\\/h1\\])ism", '<h1>$1</h1>', $Text);
    }
    // Check for h2
    if (strpos($Text, '[h2]') !== false) {
        $Text = preg_replace("(\\[h2\\](.*?)\\[\\/h2\\])ism", '<h2>$1</h2>', $Text);
    }
    // Check for h3
    if (strpos($Text, '[h3]') !== false) {
        $Text = preg_replace("(\\[h3\\](.*?)\\[\\/h3\\])ism", '<h3>$1</h3>', $Text);
    }
    // Check for h4
    if (strpos($Text, '[h4]') !== false) {
        $Text = preg_replace("(\\[h4\\](.*?)\\[\\/h4\\])ism", '<h4>$1</h4>', $Text);
    }
    // Check for h5
    if (strpos($Text, '[h5]') !== false) {
        $Text = preg_replace("(\\[h5\\](.*?)\\[\\/h5\\])ism", '<h5>$1</h5>', $Text);
    }
    // Check for h6
    if (strpos($Text, '[h6]') !== false) {
        $Text = preg_replace("(\\[h6\\](.*?)\\[\\/h6\\])ism", '<h6>$1</h6>', $Text);
    }
    // Check for centered text
    if (strpos($Text, '[/center]') !== false) {
        $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    }
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    // handle nested lists
    $endlessloop = 0;
    while ((strpos($Text, "[/list]") !== false && strpos($Text, "[list") !== false || strpos($Text, "[/ol]") !== false && strpos($Text, "[ol]") !== false || strpos($Text, "[/ul]") !== false && strpos($Text, "[ul]") !== false || strpos($Text, "[/li]") !== false && strpos($Text, "[li]") !== false) && ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
    }
    if (strpos($Text, '[th]') !== false) {
        $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    }
    if (strpos($Text, '[td]') !== false) {
        $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    }
    if (strpos($Text, '[tr]') !== false) {
        $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    }
    if (strpos($Text, '[/table]') !== false) {
        $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
        $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
        $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    }
    $Text = str_replace('[hr]', '<hr />', $Text);
    // This is actually executed in prepare_body()
    $Text = str_replace('[nosmile]', '', $Text);
    // Check for font change text
    if (strpos($Text, '[/font]') !== false) {
        $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    }
    // Declare the format for [code] layout
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    if (strpos($Text, '[code]') !== false) {
        $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    }
    // Declare the format for [spoiler] layout
    $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
    // 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", "<br /><strong class=" . '"spoiler"' . ">" . $t_wrote . "</strong><blockquote class=" . '"spoiler"' . ">\$2</blockquote>", $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // 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", "<br /><strong class=" . '"author"' . ">" . $t_wrote . "</strong><blockquote>\$2</blockquote>", $Text);
    }
    // Images
    // [img]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=left\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=right\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=left\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=right\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // style (sanitized)
    if (strpos($Text, '[/style]') !== false) {
        $Text = preg_replace_callback("(\\[style=(.*?)\\](.*?)\\[\\/style\\])ism", "bb_sanitize_style", $Text);
    }
    // crypt
    if (strpos($Text, '[/crypt]') !== false) {
        $x = random_string();
        $Text = preg_replace("/\\[crypt\\](.*?)\\[\\/crypt\\]/ism", '<br/><div id="' . $x . '"><img src="' . $a->get_baseurl() . '/images/lock_icon.gif" onclick="red_decrypt(\'rot13\',\'\',\'$1\',\'#' . $x . '\');" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br /></div>', $Text);
        $Text = preg_replace_callback("/\\[crypt (.*?)\\](.*?)\\[\\/crypt\\]/ism", 'bb_parse_crypt', $Text);
    }
    if (strpos($Text, '[/app]') !== false) {
        $Text = preg_replace_callback("/\\[app\\](.*?)\\[\\/app\\]/ism", 'bb_parse_app', $Text);
    }
    // html5 video and audio
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace_callback("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4))\\[\\/video\\]/ism", 'tryzrlvideo', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace_callback("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\\[\\/audio\\]/ism", 'tryzrlaudio', $Text);
    }
    // Try to Oembed
    if ($tryoembed) {
        if (strpos($Text, '[/video]') !== false) {
            $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/audio]') !== false) {
            $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
        }
    }
    // if video couldn't be embedded, link to it instead.
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/", '<a href="$1">$1</a>', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/", '<a href="$1">$1</a>', $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace_callback("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", 'bbiframe', $Text);
        }
    } else {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<a href="$1">$1</a>', $Text);
        }
    }
    // Youtube extensions
    if (strpos($Text, '[youtube]') !== false) {
        if ($tryoembed) {
            $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
            $Text = preg_replace_callback("/\\[youtube\\](www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
            $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
        }
        $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
        $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
        $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
        if ($tryoembed) {
            $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
        } else {
            $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", "http://www.youtube.com/watch?v=\$1", $Text);
        }
    }
    if (strpos($Text, '[vimeo]') !== false) {
        if ($tryoembed) {
            $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
            $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
        }
        $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
        $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
        if ($tryoembed) {
            $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
        } else {
            $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", "http://vimeo.com/\$1", $Text);
        }
    }
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // Avoid triple linefeeds through oembed
    $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
    // 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')) {
        $sub = format_event_html($ev);
        $Text = preg_replace("/\\[event\\-summary\\](.*?)\\[\\/event\\-summary\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    $Text = preg_replace("/\\<(.*?)(src|href)=\"[^hfm](.*?)\\>/ism", '<$1$2="">', $Text);
    call_hooks('bbcode', $Text);
    return $Text;
}
Beispiel #4
0
function bbcode($Text, $preserve_nl = false)
{
    // Extract a single private image which uses data url's since preg has issues with
    // large data sizes. Stash it away while we do bbcode conversion, and then put it back
    // in after we've done all the regex matching. We cannot use any preg functions to do this.
    $saved_image = '';
    $img_start = strpos($Text, '[img]data:');
    $img_end = strpos($Text, '[/img]');
    if ($img_start !== false && $img_end !== false && $img_end > $img_start) {
        $start_fragment = substr($Text, 0, $img_start);
        $img_start += strlen('[img]');
        $saved_image = substr($Text, $img_start, $img_end - $img_start);
        $end_fragment = substr($Text, $img_end + strlen('[/img]'));
        //		logger('saved_image: ' . $saved_image,LOGGER_DEBUG);
        $Text = $start_fragment . '[$#saved_image#$]' . $end_fragment;
    }
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    $Text = nl2br($Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Perform URL Search
    $Text = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
    $Text = preg_replace_callback("/\\[bookmark\\=([^\\]]*)\\].*?\\[\\/bookmark\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '[url=$1]$2[/url]', $Text);
    $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]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    // Perform MAIL Search
    $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    // Check for Underline text
    $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    // Check for strike-through text
    $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    // Check for over-line text
    $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    // Check for colored text
    $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    // Check for sized text
    $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    // Check for list text
    if (stristr($Text, '[/list]')) {
        $Text = str_replace("[*]", "<li>", $Text);
    }
    if (stristr($Text, '[/list]')) {
        $Text = str_replace("[*]", "<li>", $Text);
    }
    $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=i\\](.*?)\\[\\/list\\]/sm", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=I\\](.*?)\\[\\/list\\]/sm", '<ul class="listupperroman" style="list-style-type: upper-roman;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=a\\](.*?)\\[\\/list\\]/sm", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=A\\](.*?)\\[\\/list\\]/sm", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$1</ul>', $Text);
    $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/sm", '<li>$1</li>', $Text);
    $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
    $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
    $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    // Check for font change text
    $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    // Declare the format for [code] layout
    $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/ism", 'stripcode_br_cb', $Text);
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", "{$QuoteLayout}", $Text);
    // [img=widthxheight]image source[/img]
    $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
    // Images
    // [img]pathtoimage[/img]
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    // Try to Oembed
    $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
    // html5 video and audio
    $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/ism", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
    $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
    $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
    /*if (get_pconfig(local_user(), 'oembed', 'use_for_youtube' )==1){
    		// use oembed for youtube links
    		$Text = preg_replace("/\[youtube\]/",'[embed]',$Text); 
    		$Text = preg_replace("/\[\/youtube\]/",'[/embed]',$Text); 
    	} else {*/
    // Youtube extensions
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="425" height="350" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
    //}
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="425" height="350" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $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);
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    if (x($ev, 'desc') && x($ev, 'start')) {
        $sub = format_event_html($ev);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    if (strlen($saved_image)) {
        $Text = str_replace('[$#saved_image#$]', '<img src="' . $saved_image . '" alt="' . t('Image/photo') . '" />', $Text);
    }
    call_hooks('bbcode', $Text);
    return $Text;
}
Beispiel #5
0
function bbcode($Text, $preserve_nl = false)
{
    $a = get_app();
    // Hide all [noparse] contained bbtags spacefying them
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    // Extract a single private image which uses data url's since preg has issues with
    // large data sizes. Stash it away while we do bbcode conversion, and then put it back
    // in after we've done all the regex matching. We cannot use any preg functions to do this.
    $saved_image = '';
    $img_start = strpos($Text, '[img]data:');
    $img_end = strpos($Text, '[/img]');
    if ($img_start !== false && $img_end !== false && $img_end > $img_start) {
        $start_fragment = substr($Text, 0, $img_start);
        $img_start += strlen('[img]');
        $saved_image = substr($Text, $img_start, $img_end - $img_start);
        $end_fragment = substr($Text, $img_end + strlen('[/img]'));
        //		logger('saved_image: ' . $saved_image,LOGGER_DEBUG);
        $Text = $start_fragment . '[$#saved_image#$]' . $end_fragment;
    }
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    $Text = nl2br($Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Perform URL Search
    $Text = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
    $Text = preg_replace_callback("/\\[bookmark\\=([^\\]]*)\\].*?\\[\\/bookmark\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '[url=$1]$2[/url]', $Text);
    $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]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    // we may need to restrict this further if it picks up too many strays
    // link acct:user@host to a webfinger profile redirector
    $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "\$1@\$2" . '" target="extlink" >acct:' . "\$1@\$2\$3" . '</a>', $Text);
    // Perform MAIL Search
    $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    // Check for Underline text
    $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    // Check for strike-through text
    $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    // Check for over-line text
    $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    // Check for colored text
    $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px;\">\$2</span>", $Text);
    $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    // Check for centered text
    $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
    // handle nested lists
    $endlessloop = 0;
    while (strpos($Text, "[/list]") !== false and strpos($Text, "[list") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
    }
    $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
    $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
    $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
    $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
    $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    $Text = str_replace('[hr]', '<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", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    // Declare the format for [code] layout
    $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/ism", 'stripcode_br_cb', $Text);
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    // Declare the format for [spoiler] layout
    $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
    // 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", "<br /><strong class=" . '"spoiler"' . ">" . $t_wrote . "</strong><blockquote class=" . '"spoiler"' . ">\$2</blockquote>", $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // 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", "<br /><strong class=" . '"author"' . ">" . $t_wrote . "</strong><blockquote>\$2</blockquote>", $Text);
    }
    // [img=widthxheight]image source[/img]
    //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
    $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
    // Images
    // [img]pathtoimage[/img]
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    $Text = preg_replace("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4))\\[\\/video\\]/ism", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
    $Text = preg_replace("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\\[\\/audio\\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
    // Try to Oembed
    $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
    // html5 video and audio
    $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
    // Youtube extensions
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[youtube\\](www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="425" height="350" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="425" height="350" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $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);
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    if (x($ev, 'desc') && x($ev, 'start')) {
        $sub = format_event_html($ev);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    if (strlen($saved_image)) {
        $Text = str_replace('[$#saved_image#$]', '<img src="' . $saved_image . '" alt="' . t('Image/photo') . '" />', $Text);
    }
    call_hooks('bbcode', $Text);
    return $Text;
}