<?
// ------------------ функция вывода текста -------------------------//
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'] . '"/>', $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" class="form-control" 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() {
?>

<span class="smiles"><span class="Sopen">Смайлы</span><div class="close">
<img id="sm0" src="/assets/smiles/img_17508.gif" alt=":]]" />
<img id="sm1" src="/assets/smiles/img_16479.gif" alt=":D" />

</div></span>

<span class="smiles"><span class="Sopen">Emoji</span><div class="close">
<img id="sm200" src="/assets/smiles/smile_13848.png" alt="&#9786;" />
<img id="sm201" src="/assets/smiles/smile_35316.png" alt="&#128512;" />
<img id="sm202" src="/assets/smiles/smile_86646.png" alt="&#128513;" />
<img id="sm203" src="/assets/smiles/smile_36026.png" alt="&#128514;" />
<img id="sm204" src="/assets/smiles/smile_50422.png" alt="&#128515;" />
<img id="sm205" src="/assets/smiles/smile_56628.png" alt="&#128516;" />
<img id="sm206" src="/assets/smiles/smile_92955.png" alt="&#128517;" />
<img id="sm207" src="/assets/smiles/smile_48034.png" alt="&#128519;" />
<img id="sm208" src="/assets/smiles/smile_32151.png" alt="&#128520;" />
<img id="sm209" src="/assets/smiles/smile_67263.png" alt="&#128518;" />
<img id="sm211" src="/assets/smiles/smile_35316.png" alt="&#128521;" />
<img id="sm212" src="/assets/smiles/smile_35316.png" alt="&#128523;" />
<img id="sm213" src="/assets/smiles/smile_35316.png" alt="&#128522;" />
<img id="sm214" src="/assets/smiles/smile_35316.png" alt="&#128524;" />
<img id="sm215" src="/assets/smiles/smile_35316.png" alt="&#128525;" />
<img id="sm216" src="/assets/smiles/smile_35316.png" alt="&#128527;" />
<img id="sm217" src="/assets/smiles/smile_35316.png" alt="&#128529;" />
<img id="sm218" src="/assets/smiles/smile_35316.png" alt="&#128526;" />
<img id="sm219" src="/assets/smiles/smile_73611.png" alt="&#128528;" />
<img id="sm220" src="/assets/smiles/smile_80189.png" alt="&#128530;" />
<img id="sm221" src="/assets/smiles/smile_90777.png" alt="&#128531;" />
<img id="sm222" src="/assets/smiles/smile_86202.png" alt="&#128532;" />
<img id="sm223" src="/assets/smiles/smile_64684.png" alt="&#128535;" />
<img id="sm224" src="/assets/smiles/smile_19377.png" alt="&#128533;" />
<img id="sm225" src="/assets/smiles/smile_28267.png" alt="&#128536;" />
<img id="sm226" src="/assets/smiles/smile_56174.png" alt="&#128534;" />
<img id="sm227" src="/assets/smiles/smile_86061.png" alt="&#128537;" />
<img id="sm228" src="/assets/smiles/smile_27315.png" alt="&#128538;" />
<img id="sm229" src="/assets/smiles/smile_84315.png" alt="&#128540;" />



</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>



<span><span class="Sopen">BB коды</span><div class="close"><div class="bb">
<span id="bb0" title="[nosmile]">[без смайлов]</span> 
<span id="bb1" title="[code][/code]">[код]</span> 
<span id="bb2" title="[b][/b]">[<b>жирно</b>]</span> 
<span id="bb3" title="[i][/i]">[<i>курсив</i>]</span> 
<span id="bb4" title="[s][/s]">[<s>перечёркнуто</s>]</span> 
<span id="bb5" title="[red][/red]" class="red">[Красный]</span> 
<span id="bb6" class="green" title="[green][/green]">[Зелень]</span> 
<span id="bb7" title="[q][/q]">[цитата]</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><?
}
?>