<?
// ------------------ функция вывода текста -------------------------//
function text($text) {
return bb(nl2br(smiles($text)));
}
function esc( $text, $br = NULL ) {
if ( $br != NULL )
for ( $i = 0; $i <= 31; $i++ )
$text = str_replace( chr( $i ), NULL, $text );
else {
for ( $i = 0; $i < 10; $i++ )
$text = str_replace( chr( $i ), NULL, $text );
for ( $i = 11; $i < 20; $i++ )
$text = str_replace( chr( $i ), NULL, $text );
for ( $i = 21; $i <= 31; $i++ )
$text = str_replace( chr( $i ), NULL, $text );
}
return $text;
}

// ------------------ функция вывода смайла -------------------------//
function smiles($str) {
global $dbs;
$querysmiles = DB::$dbs->query("SELECT `name`, `file` FROM `smiles`");
$arrsmiles = $querysmiles->fetch(PDO::FETCH_ASSOC);
do {
$str = str_replace($arrsmiles['name'], '<img src="/assets/smiles/' . $arrsmiles['file'] . '" alt="'.$arrsmiles['name'].'"/>', $str);
}
while($arrsmiles = $querysmiles->fetch(PDO::FETCH_ASSOC));
return $str;
}
// ----------------------- Функция вывода спойлера ------------------------//
function spoiler_text($match) {
$title = (empty($match[1])) ? 'Спойлер' : $match[1];
$text = (empty($match[2])) ? 'Текста нет' : $match[2];
if (!isset($match[2])) {
$title = 'Спойлер';
$text = $match[1];
}
return '<div class="spoiler-wrap">
<div class="spoiler-head open">'.$title.'</div>
<div class="spoiler-body">'.$text.'</div>
</div>';
}
// ----------------------- Функция скрытого текста ------------------------//
function hidden_text($str) {

if ($str[1]=='') $str[1] = 'Текст отсутствует';
if (is_user()) {
$text = '<div class="hide"><b>Скрытый текст:</b> '.$str[1].'</div>';
} else {
$text = '<div class="hide"><b>Скрытый текст.</b> Для просмотра необходимо авторизоваться!</div>';
}

return $text;
}

// ------------------ Функция подсветки кода -------------------------//
function _highlight_code($code) {
if (is_array($code)) $code = $code[1];
$code = strtr($code, array('&lt;'=>'<', '&gt;'=>'>', '&amp;'=>'&', '&quot;'=>'"', '&#36;'=>'$', '&#37;'=>'%', '&#39;'=>"'", '&#92;'=>'\\', '&#94;'=>'^', '&#96;'=>'`', '&#124;' => '|', '<br />'=>"\r\n"));
$code = highlight_string($code, true);
$code = strtr($code, array("\r\n"=>'<br />', '://'=>'&#58//', '$'=>'&#36;', "'"=>'&#39;', '%'=>'&#37;', '\\'=>'&#92;', '`'=>'&#96;', '^'=>'&#94;', '|'=>'&#124;'));
return '<div class="citat">'.$code.'</div>';
}

// ------------------ Функция вставки BB-кода --------------------//

function bb($msg) {
$msg = preg_replace_callback('#\[hide\](.*?)\[/hide\]#i', 'hidden_text', $msg);
$msg = preg_replace_callback('#\[spoiler=(.*?)\](.*?)\[/spoiler\]#si', 'spoiler_text',$msg);
$msg = preg_replace_callback('#\[spoiler\](.*?)\[/spoiler\]#si', 'spoiler_text',$msg);
$msg = preg_replace_callback('~\[url=((https?|ftp)://.+?)\](.+?)\[/url\]|((https?|ftp)://[0-9a-zа-яё/.;?=\(\)\_\-&%#]+)~ui', 'url_replace', $msg);
$msg = preg_replace('#(?<!\])\bhttp://gearnet\.ru[^\s\[<]+#i', '<a href="\0">\0</a>', $msg);
$msg = preg_replace('#(?<!\])\bhttp://skywap\.org[^\s\[<]+#i', '<a href="\0">\0</a>', $msg);
$msg = preg_replace('/<span style="color:#([a-z0-9]{6})">(.*)<\/span>/si', '[color=#\1]\2[/color]', $msg);
$msg = preg_replace('#\[img\](.*?)\[/img\]#si', '<a href="\1"><img src="\1" alt="*" style="max-width:100%; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; border-top-right-radius: 20px; border-top-left-radius: 20px;"/></a>', $msg); 
$msg = preg_replace('#\[big\](.*?)\[/big\]#si', '<big>\1</big>', $msg);
$msg = preg_replace('#\[b\](.*?)\[/b\]#si', '<b>\1</b>', $msg);
$msg = preg_replace('#\[marq\](.*?)\[/marq\]#si', '<marquee>\1</marquee>', $msg);
$msg = preg_replace('#\[i\](.*?)\[/i\]#si', '<i>\1</i>', $msg);
$msg = preg_replace('#\[u\](.*?)\[/u\]#si', '<u>\1</u>', $msg);
$msg = preg_replace('#\[t\](.*?)\[/t\]#si', '<input name="text" type="text" value="\1">', $msg);
$msg = preg_replace('#\[small\](.*?)\[/small\]#si', '<small>\1</small>', $msg);
$msg = preg_replace('#\[red\](.*?)\[/red\]#si', '<span style="color:#ff0000">\1</span>', $msg);
$msg = preg_replace('#\[green\](.*?)\[/green\]#si', '<span style="color:#00cc00">\1</span>', $msg);
$msg = preg_replace('#\[blue\](.*?)\[/blue\]#si', '<span style="color:#0000ff">\1</span>', $msg);
$msg = preg_replace('#\[code\](.*?)\[/code\]#si', '<div class="citat">\1</div>', $msg);
$msg = preg_replace('#\[cit\](.*?)\[/cit\]#si', '<div class="citat">\1</div>', $msg);
$msg = preg_replace('#\[del\](.*?)\[/del\]#si', '<del>\1</del>', $msg);
$msg = preg_replace('#\[br\](.*?)\[br\]#si', '<br>\1</br>', $msg);
return $msg;
}
// Транслит
function retrans($t){
$a = array('_','YA','Ya','ya','yee','YO','yo','Yo','ZH','zh','Zh','Z','z','CH','ch','Ch','SH','sh','Sh','YE','ye','Ye','YU','yu','Yu','JA','ja','Ja','A','a','B','b','V','v','G','g','D','d','E','e','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','R','r','S','s','T','t','U','u','F','f','H','h','W','w','x','q','Y','y','C','c','!');
$b = array(' ','Я','Я','я','ые','Ё','ё','Ё','Ж','ж','Ж','З','з','Ч','ч','Ch','Ш','ш','Ш','Э','э','Э','Ю','ю','Ю','Я','я','Я','А','а','Б','б','В','в','Г','г','Д','д','Е','е','И','и','Й','й','К','к','Л','л','М','м','Н','н','О','о','П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Щ','щ','ъ','ь','Ы','ы','Ц','ц','');
return str_replace($b,$a,$t);
}
// Пригодиться 
function del($text) {
$text = preg_replace("/[^A-z0-9_-]/i", "",$text);
return $text;
}
// Слова
function slv($str,$msg1,$msg2,$msg3) {
$str = (int)$str;
$str1 = abs($str) % 100;
$str2 = $str % 10;
if ($str1 > 10 && $str1 < 20) return $str .' '. $msg3;
if ($str2 > 1 && $str2 < 5) return $str .' '. $msg2;
if ($str2 == 1) return $str .' '. $msg1;
return $str .' '. $msg3;
}
// ------------- Функция вывода пользовательских тегов --------------//
function quickpanel() {


echo'<span class="smiles"><span class="Sopen">Смайлы</span><div class="close">
<img id="sm0" src="/assets/smiles/img_43996.gif" alt=":-p" /> 
<img id="sm1" src="/assets/smiles/img_17508.gif" alt=":]]" /> 
<img id="sm2" src="/assets/smiles/img_16479.gif" alt=":D" /> 
<img id="sm3" src="/assets/smiles/img_46369.gif" alt="=))" /> 
<img id="sm4" src="/assets/smiles/img_98196.gif" alt=":-/" /> 
<img id="sm5" src="/assets/smiles/img_33294.gif" alt=":-(" /> 
<img id="sm6" src="/assets/smiles/img_68669.gif" alt="8-)" /> 
<img id="sm7" src="/assets/smiles/img_63375.gif" alt=";-)" /> 
<img id="sm8" src="/assets/smiles/img_35182.gif" alt=".болен." /> 
<img id="sm9" src="/assets/smiles/img_63942.gif" alt=":twisted:" /> 
<img id="sm10" src="/assets/smiles/img_13796.gif" alt=".ржу." /> 



</div></span>

<span class="smiles"><span class="Sopen">Миньоны </span><div class="close">
<img id="sm200" src="/assets/smiles/img_66796.png" alt=".что." >
<img id="sm201" src="/assets/smiles/img_89713.png" alt=".мдаа." >
<img id="sm202" src="/assets/smiles/img_99622.png" alt=".ура." >
<img id="sm203" src="/assets/smiles/img_22822.png" alt=".привет." >
<img id="sm204" src="/assets/smiles/img_10005.png" alt=".э." >
<img id="sm205" src="/assets/smiles/img_45352.png" alt=".хм." >
<img id="sm206" src="/assets/smiles/img_70545.png" alt=".танц." >
<img id="sm207" src="/assets/smiles/img_61339.png" alt=".кия." >
<img id="sm208" src="/assets/smiles/img_14998.png" alt=".счет." >
<img id="sm209" src="/assets/smiles/img_26116.png" alt=".пжл." >
<img id="sm210" src="/assets/smiles/img_59870.png" alt=".zorro." >
<img id="sm211" src="/assets/smiles/img_94197.png" alt=".xman." >
<img id="sm212" src="/assets/smiles/img_59217.png" alt=".воин." >
<img id="sm213" src="/assets/smiles/img_19081.png" alt=".joker." >


</div></span>';



/*
<span class="smiles"><span class="Sopen">Спотти</span><div class="close">
<img id="sm300" src="/assets/smiles/img_17508.gif" alt=":]]" />
<img id="sm301" src="/assets/smiles/img_16479.gif" alt=":D" />

</div></span>
<span class="smiles"><span class="Sopen">Персик</span><div class="close">
<img id="sm350" src="/assets/smiles/img_17508.gif" alt=":]]" />
<img id="sm351" src="/assets/smiles/img_16479.gif" alt=":D" />

</div></span>

*/

echo' <span><span class="Sopen"> BB коды</span><div class="close"><div class="bb">

<span id="bb0" title="[url=http://][/url]">[Ссылка]</span> 
<span id="bb1" title="[u][/u]">[<u>Подчеркивание</u>]</span> 
<span id="bb2" title="[b][/b]">[<b>Жирно</b>]</span> 
<span id="bb3" title="[i][/i]">[<i>Курсив</i>]</span> 
<span id="bb4" title="[small][/small]">[<small>Малость</small>]</span> 
<span id="bb5" title="[big][/big]">[<big>Большой</big>]</span>
<span id="bb6" title="[marq][/marq]">[Бегущая]</span>
<span id="bb7" title="[t][/t]">[Текстовое]</span>
<span id="bb8" title="[del][/del]">[Зачеркнутый]</span>
<span id="bb9" title="[blue][/blue]" class="red">[Синий]</span> 
<span id="bb10" title="[red][/red]" class="red">[Красный]</span> 
<span id="bb11" class="green" title="[green][/green]">[Зелёный]</span> 
<span id="bb12" title="[cit][/cit]">[Цитата]</span>
</div></div>';

}

// -------------- Вспомогательная функция быстрой вставки  -------------//
function quickpaste($form) {
?><script language="JavaScript" type="text/javascript">
jQuery(function() {
    $('form[class!=ajax]').on('keypress',function(e){
        if((e.which == 13||e.which == 10) && e.ctrlKey){
            this.submit();
        }
    }); 
    $('.smiles img').on('click',function(){
        var alt = $('#'+this.id).attr('alt');
        var text = $("textarea")[0].value+' '+alt;
        $("textarea")[0].value = text;
    });
    $('.bb span').on('click',function(){
        var alt;
        if($('#'+this.id).attr('title')==null){
            alt = $('#'+this.id).attr('tooltip');
        }else{
            alt = $('#'+this.id).attr('title');
        }
        var text = $("textarea")[0].value+' '+alt;
        $("textarea")[0].value = text;
    });
    $(".Sopen").on('click',function(){
        if(this.id==''){
            $(this).attr('id', 'openbutton'+Math.floor(Math.random()*1001));
        }     
        var parId;
        var openid = this.id; 
        $("*:has(#"+openid+")").each(function(){
            if(this.id==''){
                $(this).attr('id', 'randomid'+Math.floor(Math.random()*1001));
            }
            parId = this.id; 
        });
        $("#"+parId+">.close").toggle("fast");
    });
});
function remove_message(id){
    $("#"+id).remove();
}
    
function hide_message(id){
    $("#"+id).hide("slow");
    setTimeout('remove_message("'+id+'")', 1000);
}
function message(message, liveTime){
    var id = 'js_message_'+parseInt(Math.random()*1000);
    $("body").append('<div id="'+id+'" class="js_message"><a class="close" href="">[X]</a>'+message+'</div>');
    $("#"+id).show("slow");
    $("#"+id+" a.close").click(function(){
        hide_message(id);
        return false;
    });
        
    if(liveTime!=null){
        if(liveTime ==-1){
            liveTime = (5+parseInt(message.length/10))*500;
        }
        setTimeout('hide_message("'+id+'")', liveTime);
    }
}
</script>
<style type="text/css">
.Sopen{border:solid 1px #cccccc;padding:5px;margin-top:20px;border-radius: 6px; cursor: pointer;}
.Sopen:hover{background-color: #B7D6F0;}
.open:hover{background-color: #141414;}
.close{display: none;border:solid 1px #cccccc;padding:5px;margin:20px;border-radius: 6px;}
.smiles img{cursor: pointer;}
.bb span{cursor:pointer;}
</style><?
}
?>