<?php

function bbcode ($str, $filter=false, $pre_url="", $embeds=true) {
    $str = preg_replace ('/\r/', '', $str);
    //echo $str;
    $search1 = array (
        '/(\[center\].*?\[\/center\])\n/is',
        '/\n(\[center\].*?\[\/center\])/is',
        '/(\[quote\].*?\[\/quote\])\n/is',
        '/\n(\[quote\].*?\[\/quote\])/is'
    );
    $replace1 = array (
        '$1',
        '$1'
    );
    $str = preg_replace ($search1, '$1', $str);
    $str= vanish($str);
    $str= mywrap($str);
    $str= preg_replace_callback ('/\[code\](.+?)\[\/code\]/is',"bb_code",$str);
    $str= preg_replace_callback ('/\[php\](.+?)\[\/php\]/is', "php_callback", $str);
    $str= bb_quote($str);
    $search2 = array(
    '/\[b\](.*?)\[\/b\]/is',
    '/\[i\](.*?)\[\/i\]/is',
    '/\[u\](.*?)\[\/u\]/is',
    '/\[center\](.*?)\[\/center\]/is',
    '/\[font\=&quot;(.*?)&quot;\](.*?)\[\/font\]/is',
    '/\[size\=&quot;(.*?)&quot;\](.*?)\[\/size\]/is',
    '/\[youtube\](.+?)\[\/youtube\]/is',
    '/:\)/is',
    '/:D/s',
    '/:eek:/s',
    '/(<br>|\n)/',
    '/\{MYWRAP_BREAK\}/'
    );
    global $urlval;
    $urlval="";
    if($filter) {
        if(stripos($filter, "nofollow") !== false) $urlval.=" rel=\"nofollow\"";
        if(stripos($filter, "blank") !== false) $urlval.=" target=\"_blank\"";
    }
    $replace2 = array(
    '<b>$1</b>',
    '<i>$1</i>',
    '<u>$1</u>',
    '<div style="text-align:center">$1</div>',
    '<font face="$1">$2</font>',
    '<font size="$1">$2</font>',
    (($embeds==true)?('<iframe width="640" height="390" src="https://www.youtube.com/embed/$1" allowfullscreen frameborder="0"></iframe>'):'<a href="https://www.youtube.com/watch?v=$1"'.$urlval.'>https://www.youtube.com/watch?v=$1</a>'),
    '<img src="'.$pre_url.'images/smiles/smile.gif" title="Smile" alt=":)">',
    '<img src="'.$pre_url.'images/smiles/biggrin.gif" title="Big Grin" alt=":D">',
    '<img src="'.$pre_url.'images/smiles/eek.gif" title="EEK!" alt=":eek:">',
    "<br>\n",
    ' '
    );

    $search3=array(
        '/\[img\](.+?)\[\/img\]/i',
        '/\[url\](.*?)\[\/url\]/i',
        '/\[url\=&quot;(.*?)&quot;\](.*?)\[\/url\]/i',
        '/\[url\=(.*?)\](.*?)\[\/url\]/i'
    );

    $str= preg_replace_callback ($search3, "replace_callback", $str);
    $str = preg_replace ($search2, $replace2, $str);
    //wordwrap(string str, [int width], [string break], [boolean cut])

    return $str;
}
function php_callback ($input) {
    $search = array (
        "/&nbsp;/",
        "/\<br \/\>/",
        "/\n/"
    );
    $replace = array (
        " ",
        "<br>",
        ""
    );
    return preg_replace($search,$replace,highlight_string(html_entity_decode($input[1]), true));
}
function replace_callback ($input){
    global $urlval;
    if (isset($input[2])){
        $replace=urlcode($input[1],$urlval,$input[2]);
    } else if (preg_match ('/\[url\](.*?)\[\/url\]/is', $input[0])) {
        $replace=urlcode($input[1],$urlval,$input[1]);
    } else {
        $replace=imgcode($input[1]);
    }
    $out=$replace;
    return $out;
}
function bb_code($matches){
    $search[] = "{MYWRAP_BREAK}";
    $search[] = "[";
    $search[] = "]";
    $search[] = ":";
    $replace[] = "";
    $replace[] = "&#91;";
    $replace[] = "&#93;";
    $replace[] = "&#58;";
    $str = str_replace($search, $replace, $matches[1]);
    return $str;
}
function vanish($str){
    $str = htmlspecialchars($str);
    $search[] ='/\[(.*?javascript\:)/is';
    $replace[]= '&#91;$1';
    $str = preg_replace ($search, $replace, $str);
    return $str;
}
function bb_quote( $str )
{
    $search[]='/\[quote\](.*?)\[\/quote\]/is';
    $search[]='/\[quote\=(.*?)](.*?)\[\/quote\]/is';
    $replace[]='<center><table class="quote"><tr><th>Quote:</tr><tr><td>$1</tr></table></center>';
    $replace[]='<center><table class="quote"><tr><th>$1 wrote:</tr><tr><td>$2</tr></table></center>';
    $str = preg_replace( $search, $replace, $str );
    if ( preg_match('/(\[quote\]|\[quote\=.*?\])(.*?)\[\/quote\]/is', $str)){
        $str = bb_quote( $str );
    }
    return $str;
}

function mywrap($str, $length=72, $break="{MYWRAP_BREAK}", $cut=true) {
    $words=explode(" ", $str);
    foreach ($words as $word){
        if((strpos($word, 'http://') === false)
        && (strpos($word, 'https://') === false)
        && (strpos($word, 'www.') === false)
        && (strpos($word, '[center') === false)
        && (strpos($word, '[img') === false)
        && (strpos($word, '[url') === false)) {
            $word=wordwrap($word, $length, $break, $cut);
        }
        $tmp[]=$word;
    }
    $str=implode(" ", $tmp);
    return $str;
}
function imgcode ($imgurl){
    global $prefix;
    $external = isexternal($imgurl);
    $arr=explode('?', $imgurl);
    $fname=(strpos($imgurl,"/") === 0) ? $prefix.$arr[0]: $arr[0];
    if ($external !== false || file_exists($fname)) return "<img src=\"$imgurl\">";
    else return "&lt;Missing file: <b>".$arr[0]."</b>".(($arr[0] !== $imgurl)?" (Url: <b>$imgurl</b>)":"")."&gt;";
}
function urlcode ($url,$urlval,$text){
    global $prefix;
    $external = isexternal($url);
    $url = getPath($url);
    $arr=explode('?', $url);
    $fname=(strpos($url,"/") === 0) ? $prefix.$arr[0]: $arr[0];
    if ($external !== false || file_exists($fname)) return "<a href=\"$url\"$urlval>$text</a>";
    else {
        return "&lt;Missing file: <b>".$arr[0]."</b>".(($arr[0] !== $url)?" (Url: <b>$url</b>)":"")."&gt;";
    }
}

function isexternal($url) {
    $components = parse_url($url);
    if ( empty($components['host']) ) return false;
    if ( strcasecmp($components['host'], 'juozas24.lt') === 0 ) return false;
    return strrpos(strtolower($components['host']), '.juozas24.lt') !== strlen($components['host']) - strlen('.juozas24.lt');
}
function getPath($url) {
    $components = parse_url($url);
    if (isexternal($url) || empty($components['host']) || empty($components['path'])) return $url;
    else return $components['path'].(!empty($components['query'])?("?".$components['query']):"");
}

?>