<?php
function bbcode( $tmpl )
    {
	global $mysql;
	
	
     $argv = func_get_args();

    $result_sm = $mysql->query("SELECT * FROM `".PREFIX."smiles`");
    while ($s = $result_sm -> fetch_assoc()) {
            $tmpl = str_replace($s['code'], '<img src="'.HOME.'/'.$s['path'].'" alt="'.$s['code'].'" width="20" height="20"/>', $tmpl);
    }
	
	
     $tmpl = preg_replace( "#\[b\](.+?)\[/b\]#is" , "<b>\\1</b>" , $tmpl );
     $tmpl = preg_replace( "#\[i\](.+?)\[/i\]#is" , "<i>\\1</i>" , $tmpl );
     $tmpl = preg_replace( "#\[u\](.+?)\[/u\]#is" , "<u>\\1</u>" , $tmpl );

     $tmpl = preg_replace( "#\[center\](.+?)\[/center\]#is" , "<div align='center'>\\1</div>" , $tmpl );
     $tmpl = preg_replace( "#\[right\](.+?)\[/right\]#is"   , "<div align='right'>\\1</div>"  , $tmpl );
     $tmpl = preg_replace( "#\[left\](.+?)\[/left\]#is"     , "<div align='left'>\\1</div>"   , $tmpl );

     $tmpl = preg_replace( "#<div align='(center|right|left)'>(.+?)</div>[^</p>]#is", "<div align='\\1'>\\2</div><br>" , $tmpl );

     $tmpl = preg_replace( "#\(c\)#i"  , "&copy;" , $tmpl );
     $tmpl = preg_replace( "#\(tm\)#i" , "(tm)" , $tmpl );
     $tmpl = preg_replace( "#\(r\)#i"  , "&reg;"  , $tmpl );

     while ( preg_match( "#\[font=([^\]]+)\](.*?)\[/font\]#ies", $tmpl ) )
      {  $tmpl = preg_replace( "#\[font=([^\]]+)\](.*?)\[/font\]#is"    , "\regex_font_attr(array('s'=>'font','1'=>'\\1','2'=>'\\2'))", $txt );  }

     while( preg_match( "#\[color=([^\]]+)\](.+?)\[/color\]#ies", $tmpl ) )
      {  $tmpl = preg_replace( "#\[color=([^\]]+)\](.+?)\[/color\]#is" , "<span style='color:\\1'>\\2</span>"       , $tmpl );  }

     if( isset( $argv[2] ) && is_array( $argv[2] ) )
      {
       $tmpl = preg_replace( "#\[img=(\d)\]#ie" ,
                               "\bbcode_image( \"\1\" , \"\" , \$argv[1] , \$argv[2] )" ,
                               $tmpl );
       $tmpl = preg_replace( "#\[img=(\d);align=(\w+)\]#ie" ,
                               "\bbcode_image( \"\1\" , \"\\2\" , \$argv[1] , \$argv[2] )" ,
                               $tmpl );
      }

     $tmpl = preg_replace( "#\[img\](.+?)\[/img\]#is" , "<img src=\\1>" , $tmpl );

     $tmpl = preg_replace( "#\[url\](.+?)\[/url\]#ie" ,
                             "\bbcode_link( \"\1\" , \"\" )" ,
                             $tmpl );
     $tmpl = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie" ,
                             "\bbcode_link( \"\1\" , \"\\2\" )" ,
                             $tmpl );
							 
							 

     $tmpl = preg_replace( "#\n?\[list\](.+?)\[/list\]\n?#ies" ,
                             "\bbcode_list( \"\1\" , \"\" )" ,
                             $tmpl );
     $tmpl = preg_replace( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies" ,
                             "\bbcode_list( \"\2\" , \"\\1\" )" ,
                             $tmpl );

     $tmpl = str_replace( "<p></ul>"  , "</ul><p>"      , $tmpl );
     $tmpl = str_replace( "<p><li>"   , "<li>"          , $tmpl );
     $tmpl = str_replace( "</ul><li>" , "</ul><p><li>"  , $tmpl );

	 $tmpl = preg_replace( "/\\n/" , "<br/>" , $tmpl );
	 
	 $tmpl = preg_replace('#\[code\](.+)\[\/code\]#ie', 'highlight_code(\'\\1\')', $tmpl);
	 
     //$tmpl = preg_replace_callback( "#\[code\](.+?)\[/code\]#is", array( $this , "highlight_code(\'\\2\')" ) , $tmpl );

     $tmpl = bbcode_remove( $tmpl );

     return $tmpl;
    }

	/* Функции подсветки PHP кода */
		function highlight_code($tmpl)
		{
			return '<pre class="brush: php;">'.$tmpl.'</pre>';
		}



   function bbcode_remove( $tmpl )
    {
     $tmpl = preg_replace( "#\[b\]|\[/b\]#is"                          , "" , $tmpl );
     $tmpl = preg_replace( "#\[i\]|\[/i\]#is"                          , "" , $tmpl );
     $tmpl = preg_replace( "#\[u\]|\[/u\]#is"                          , "" , $tmpl );
     $tmpl = preg_replace( "#\[center\]|\[/center\]#is"                , "" , $tmpl );
     $tmpl = preg_replace( "#\[right\]|\[/right\]#is"                  , "" , $tmpl );
     $tmpl = preg_replace( "#\[left\]|\[/left\]#is"                    , "" , $tmpl );
     $tmpl = preg_replace( "#\[font\s*=\s*(\S+?)\s*\]\[\/font\]#is"    , "" , $tmpl );
     $tmpl = preg_replace( "#\[color\s*=\s*(\S+?)\s*\]|\[\/color\]#is" , "" , $tmpl );
     $tmpl = preg_replace( "#\[img\]|\[/img\]#is"                      , "" , $tmpl );
     $tmpl = preg_replace( "#\[img=\d\]#is"                            , "" , $tmpl );
     $tmpl = preg_replace( "#\[img=\d;align=(.*?)\]#is"                , "" , $tmpl );
     $tmpl = preg_replace( "#\[url\]|\[/url\]#is"                      , "" , $tmpl );
     $tmpl = preg_replace( "#\[url\s*=\s*(\S+?)\s*\]#is"               , "" , $tmpl );
     $tmpl = preg_replace( "#\[html\]|\[/html\]#is"                    , "" , $tmpl );
     $tmpl = preg_replace( "#\[code\]|\[/code\]#is"                    , "" , $tmpl );

     return $tmpl;
    }

   function bbcode_image( $number , $align , $src , $images = array() )
    {
     $align = ( empty( $align ) ) ? "align='left'" : "align='" . $align . "'" ;

     if( isset( $images[ $number - 1 ] ) )
      {  return "<img src='images/" . $src . $images[ $number - 1 ] . "' border='0' ". $align .">";  }
     else
      {  return "[Image Not Found]";  }
    }

   function bbcode_link( $href , $discription )
    {
     $style = "content";

     if( empty( $discription ) )
      {
       $discription = ( strlen( $discription ) < 80 ) ? $href : substr( $href , 0 , 80 ) . "..." ;
       return "<a href='" . $href . "' class='" . $style . "'>" . $discription . " <img src='/images/blank_link.png' alt='url'/></a>";
      }
     else
      {  return "<a href='" . $href . "' class='" . $style . "'>" . $discription . " <img src='/images/blank_link.png' alt='url'/></a>";  }
    }

   function bbcode_list( $tmpl , $type )
    {
     switch( $type )
      {
       case "1": $list_type = "1"; break;
       case "A": $list_type = "A"; break;
       case "a": $list_type = "a"; break;
       case "I": $list_type = "I"; break;
       case "i": $list_type = "i"; break;
       default:  $list_type = "1"; break;
      }

     $tmpl = preg_replace( "#\[\*\]#" , "<li>" , $tmpl );
     $tmpl = "<ul type='" . $list_type . "'>" . $tmpl . "</ul>";

     return $tmpl;
    }

//print TMPL_Parser_BB( $text );
// Для замены каринок необходимо передать:
// вторым параметром: путь от корня до папки с картинками
// третьим - имена файлов самих картинок (массив) 
?>