<?php
$HOME = 'http://'.$_SERVER['HTTP_HOST'];

session_start();
ob_start();
include_once 'dbconnect.php';
function strong($msg){
$msg = trim($msg);
$msg = htmlspecialchars($msg);
$msg = mysql_escape_string($msg);
return $msg;
}
require_once ('config.php'); //Подключаем конфиг с параметрами
$mysql_connect = mysql_connect(dbhost, dbuser, dbpass) or die('Сайт не доступен ,возможно Вы не прописали БД!');
mysql_query('SET NAMES `utf8`', $mysql_connect);
mysql_select_db(dbname, $mysql_connect) or die('Нету подключения к БД');
function seter($str){ 
$strold=array("Q", "W", "E", "R","T", "Y", "U", "I", "O", "P", "A","S", "D", "F", "G", "H", "J", "K","L", "Z", "X", "C", "V", "B", "N", "M");
$strnew=array("q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s","d", "f", "g", "h", "j", "k", "l", "z", "x","c", "v", "b", "n", "m"); 
$str=str_replace($strold, $strnew, $str);
return $str;
}
foreach ($_GET as $links) {
if (!is_string($links) || !preg_match('#^(?:[a-z0-9_\-/]+|\.+(?!/))*$#i', $links)) {
header ('Location: '.$HOME.'');
exit;
} 
} 
unset($links);
$blok_text = array('javascript','alert','javascript:alert','meta');
foreach($_POST as $key=>
$set){
$fotr = seter($_POST[$key]);
if(in_array($fotr, $blok_text)){unset($_POST[$key]);}}
###############################
if (isset($_COOKIE['uslog']) and isset($_COOKIE['uspass'])) {
$uslog = strong($_COOKIE['uslog']);
$uspass = strong($_COOKIE['uspass']);
$_SESSION['login'] = $uslog;
$_SESSION['pass'] = $uspass;
$dbs = $pdo->query("SELECT * FROM `users` WHERE `login` = '".$uslog."' and `pass` = '".$uspass."' LIMIT 1");
$user = $dbs->fetch();
if (isset($user['id'])) {
if ($user['login'] != $uslog or $user['pass'] != $uspass) {
setcookie('uslog', '', time() - 86400*31);
setcookie('uspass', '', time() - 86400*31);
}
}
 $config = mysql_fetch_assoc(mysql_query("SELECT * FROM `config` WHERE `id` = '1'"));
$users = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `login` = '".$uslog."' and `pass`='".$uspass."' LIMIT 1"));           
mysql_query("UPDATE `users` SET `viz`='".time()."', `ip`='".$_SERVER['REMOTE_ADDR']."',`browser`='".$_SERVER['HTTP_USER_AGENT']."',`gde`='".$_SERVER['REQUEST_URI']."' WHERE `id`='".$users['id']."'");
$vremja = time() - $users['viz'];
if($vremja < 120) {
$newtime = $user['online'] + $vremja;
mysql_query("UPDATE `users` SET `online` ='".$newtime."'  WHERE `id`='".$users['id']."'");
}

if(isset($user['id']) && $users['login']!=$uslog or $users['pass']!=$uspass) {
setcookie('uslog', '', time() - 86400*31);
setcookie('uspass', '', time() - 86400*31); 
}
}
###############################
########### BB Коды ###########
###############################
function ipr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){      $ip=$_SERVER['HTTP_CLIENT_IP'];}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{      $ip=$_SERVER['REMOTE_ADDR'];   }return $ip; }
function bb($mes){
$mes = stripslashes($mes);
$mes = preg_replace('#\[cit\](.*?)\[/cit\]#si', '<div class="cit">\1</div>', $mes);
$mes = preg_replace('#\[b\](.*?)\[/b\]#si', '<span style="font-weight: bold;"> \1 </span>', $mes);
$mes = preg_replace('/\[url\s?=\s?([\'"]?)(?:http:\/\/)?(.*?)\1\](.*?)\[\/url\]/', ' <a href="http://$2"> $3 </a> ', $mes);
$mes = preg_replace('#\[black\](.*?)\[\/black\]#si', '<span style="color:#000000;">\1</span>', $mes);
$mes = preg_replace('#\[i\](.*?)\[\/i\]#si', '<i>\1</i>', $mes);
$mes = preg_replace('#\[u\](.*?)\[\/u\]#si', '<u>\1</u>', $mes);
$mes = preg_replace('#\[red\](.*?)\[\/red\]#si', '<span style="color: red">\1</span>', $mes);
$mes = preg_replace('#\[green\](.*?)\[\/green\]#si', '<span style="color: green">\1</span>', $mes);
return $mes;
}
###############################
######### Функция ника ########
###############################
function dol($id){
$users = mysql_fetch_assoc(mysql_query('select * from `users`
where `id` = "'.$id.'" limit 1'));
if($users != 0){
if($users['level'] >= 1){
if($users['level'] == 1) $access ='Мод';
elseif($users['level'] == 2) $access = 'Админ';
elseif($users['level'] == 3) $access
= 'Ст.Адм';
return '<font color="green">['.
$access.']</font>';}}}
function nick($id){
$users = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$id."' LIMIT 1"));


//---Х-статус---//
if($users['sex'] == 1)$pol = 'man_on';
else
if($users['sex'] == 2)$pol = 'j_on';

if($users['xstatus'] == 0)$xst = '<img src="'.$HOME.'/images/onl/'.$pol.'.png" alt="*" title="Онлайн" />';
elseif($users['xstatus'] == 1)$xst = '<img src="'.$HOME.'/images/x-status/1.png" alt="*" />';
elseif($users['xstatus'] == 2)$xst = '<img src="'.$HOME.'/images/x-status/2.png" alt="*" />';
elseif($users['xstatus'] == 3)$xst = '<img src="'.$HOME.'/images/x-status/3.png" alt="*" />';
elseif($users['xstatus'] == 4)$xst = '<img src="'.$HOME.'/images/x-status/4.png" alt="*" />';
elseif($users['xstatus'] == 5)$xst = '<img src="'.$HOME.'/images/x-status/5.png" alt="*" />';
elseif($users['xstatus'] == 6)$xst = '<img src="'.$HOME.'/images/x-status/6.png" alt="*" />';
elseif($users['xstatus'] == 7)$xst = '<img src="'.$HOME.'/images/x-status/7.png" alt="*" />';
elseif($users['xstatus'] == 8)$xst = '<img src="'.$HOME.'/images/x-status/8.png" alt="*" />';
elseif($users['xstatus'] == 9)$xst = '<img src="'.$HOME.'/images/x-status/9.png" alt="*" />';
elseif($users['xstatus'] == 10)$xst = '<img src="'.$HOME.'/images/x-status/10.png" alt="*" />';
elseif($users['xstatus'] == 11)$xst = '<img src="'.$HOME.'/images/x-status/11.png" alt="*" />';
elseif($users['xstatus'] == 12)$xst = '<img src="'.$HOME.'/images/x-status/12.png" alt="*" />';
elseif($users['xstatus'] == 13)$xst = '<img src="'.$HOME.'/images/x-status/13.png" alt="*" />';
elseif($users['xstatus'] == 14)$xst = '<img src="'.$HOME.'/images/x-status/14.png" alt="*" />';
elseif($users['xstatus'] == 15)$xst = '<img src="'.$HOME.'/images/x-status/15.png" alt="*" />';
elseif($users['xstatus'] == 16)$xst = '<img src="'.$HOME.'/images/x-status/16.png" alt="*" />';
elseif($users['xstatus'] == 17)$xst = '<img src="'.$HOME.'/images/x-status/17.png" alt="*" />';
//Админ мужского пола
elseif($users['xstatus'] == 18)$xst = '<img src="'.$HOME.'/images/x-status/18.png" alt="*" />';
elseif($users['xstatus'] == 19)$xst = '<img src="'.$HOME.'/images/x-status/19.png" alt="*" />';
elseif($users['xstatus'] == 20)$xst = '<img src="'.$HOME.'/images/x-status/20.png" alt="*" />';
//Админ женского пола
elseif($users['xstatus'] == 21)$xst = '<img src="'.$HOME.'/images/x-status/21.png" alt="*" />';
elseif($users['xstatus'] == 22)$xst = '<img src="'.$HOME.'/images/x-status/22.png" alt="*" />';
elseif($users['xstatus'] == 23)$xst = '<img src="'.$HOME.'/images/x-status/23.png" alt="*" />';
//-----Если пол == 1(мужской)-----//
if($users['sex'] == 1)
{
//-----Если онлайн-----//
if($users['viz'] > time()-360)
{
$p = $xst;
} else {
$p = '<img src="'.$HOME.'/images/onl/man_of.png" alt="*" title = "Оффлайн"/>';
}
}
//-----Если пол == 2(жен)-----//
elseif($users['sex'] == 2)
{
if($users['viz'] > time()-360)
{
$p = $xst;
} else {
$p = '<img src="'.$HOME.'/images/onl/j_of.png" alt="*" title = "Оффлайн"/>';  
}
}
return (empty($users)?'[Удален]':''.$p.' <a href="/user_'.$users['id'].'"><font color="'.$users['color_nick'].'"><b>'.$users['login'].'</font></a> '.dol($users['id']).'</b>');

    
   
}
###############################
########### Листинг ###########
###############################
function page($k_page=1) {
$page = 1;
$page = strong($page);
$k_page = strong($k_page);
if(isset($_GET['selection'])) {
if ($_GET['selection']=='top')
$page = strong(intval($k_page));
elseif(is_numeric($_GET['selection'])) 
$page = strong(intval($_GET['selection']));
}
if ($page<1)$page=1;
if ($page>$k_page)$page=$k_page;
return $page;
}

// Определяем кол-во страниц
function k_page($k_post = 0,$k_p_str = 10) {
if ($k_post != 0) {
$v_pages = ceil($k_post/$k_p_str);
return $v_pages;
}
else return 1;
}

function str($link='?',$k_page=1,$page=1){
if ($page<1)$page=1;
$page = strong($page);
$k_page = strong($k_page);
echo '<div class="links">';

if ($page>1)echo '<a href="'.$link.'selection='.($page-1).'"><< Назад</a> ';
else echo "<< Назад ";
echo " | ";
if ($page<$k_page)echo ' <a href="'.$link.'selection='.($page+1).'" >Вперед >></a>';
else echo " Вперед >>";
echo '<br />';
if ($page != 1)
echo '<a href="'.$link.'selection=1" >1</a>';
else echo '<b>1</b>';
for ($ot=-3; $ot<=3; $ot++){
if ($page+$ot>1 && $page+$ot<$k_page){
if ($ot==-3 && $page+$ot>2)echo " ..";
if ($ot!=0)echo '|<a href="'.$link.'selection='.($page+$ot).'" >'.($page+$ot).'</a>';
else echo '|<b>'.($page+$ot).'</b>';
if ($ot==3 && $page+$ot<$k_page-1)echo "|..";}}
if ($page!=$k_page)echo '|<a href="'.$link.'selection=top" >'.$k_page.'</a>';
elseif ($k_page>1)echo '|<b>'.$k_page.'</b>';
echo '</div>';
}
###############################
############ Время ############
###############################
function vremja($time = NULL) {
if(!$time) $time = time();
$data = date('j.n.y', $time);
if($data == date('j.n.y')) $res = 'Сегодня в '. date('G:i', $time);
elseif($data == date('j.n.y', time() - 86400)) $res = 'Вчера в '. date('G:i', $time);

else {
$m = array('0',
'Янв', 'Фев', 
'Мар', 'Апр', 'Май', 
'Июн', 'Июл', 'Авг', 
'Сен', 'Окт', 'Ноя', 
'Дек');
$res = date('j '. $m[date('n', $time)] .' Y в G:i', $time);
}
return $res;
}
###############################
############ Смайлы ############
###############################
function smile($msg)
{
$msg = trim($msg);
$s = mysql_query("SELECT * FROM `smile` ORDER BY `id` DESC");
while($smile = mysql_fetch_array($s))
{
$msg = str_replace($smile['name'],' <img src="'.$HOME.'/files/smile/'.$smile['icon'].'" alt="'.$smile['name'].'"/> ',$msg);
}
return $msg;
}
function user($user = NULL) {
    // Определение браузера
    $ank = mysql_fetch_array(mysql_query("SELECT * FROM `users`  WHERE `id` = '$user' LIMIT 1"));
    $ua = strtolower($ank['browser']);
    $path_pc = ' <img src="/design/imgs/PC.png" /> ';
    $path_mob = ' <img src="/design/imgs/phone.png" /> ';


        if (preg_match('#(orca)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/orca.png" alt="" />';
        elseif (preg_match('#(lunascape)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/lunascape.png" alt="" />';
        elseif (preg_match('#(arora)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/arora.png" />';
        elseif (preg_match('#(coolnovo)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/coolnovo.png" alt="" />';
        elseif (preg_match('#(kylo)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/kylo.png" alt="" />';
        elseif (preg_match('#(flock)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/flock.png" alt="" />';
        elseif (preg_match('#(rockmelt)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/rockmelt.png" alt="" />';
        elseif (preg_match('#(cometbird)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/cometbird.png" alt="" />';
        elseif (preg_match('#(seamonkey)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/seamonkey.png" alt="" />';
        elseif (preg_match('#(iron)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/iron.png" alt="" />';
        elseif (preg_match('#(presto)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/opera.png" alt="" />';
        elseif (preg_match('#(yabrowser)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/yabrowser.png" alt="" />';
        elseif (preg_match('#(chrome)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/chrome.png" alt="" />';
        elseif (preg_match('#(msie)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/msie.png" alt="" />';
        elseif (preg_match('#(maxthon)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/maxthon.png" alt="" />';
        elseif (preg_match('#(safari)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/safari.png" alt="" />';
        elseif (preg_match('#(qtweb)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/qtweb.png" alt="" />';
        elseif (preg_match('#(firefox)#ui', $ua)) $browser = $path_pc .'<img src="/design/imgs/browsers/firefox.png" alt="" />';
        // Мобильные браузеры
        if (preg_match('#(sonyericsson)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/sony_ericsson.png" alt="" />';
        elseif (preg_match('#(ipod)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/apple.png" alt="" />';
        elseif (preg_match('#(iphone)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/apple.png" alt="" />';
        elseif (preg_match('#(android)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/android.png" alt="" />';
        elseif (preg_match('#(symbian)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/symbian.png" alt="" />';
        elseif (preg_match('#(windowsphone)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/windows.png" alt="" />';
        elseif (preg_match('#(wp7)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/windows.png" alt="" />';
        elseif (preg_match('#(wp8)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/windows.png" alt="" />';
        elseif (preg_match('#(webos)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/hp.png" alt="" />';
        elseif (preg_match('#(blackberry)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/blackberry.png" alt="" />';
        elseif (preg_match('#(htc)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/htc.png" alt="" />';
        elseif (preg_match('#(opera m)#ui', $ua)) $browser = $path_mob .'<img src="/design/imgs/browsers/opera.png" alt="" />';
        echo $browser;
        
        $arr = array('nokia',
        'samsung',
        'siemens',
        'fly',
        'motorola',
        'sharp',
        'sony',
        'lg',
        'acer',
        'alcatel',
        'asus',
        'gigabyte',
        'highscreen',
        'huawei',
        'philips',
        'mts',
        'midp-2.0',
        'ucweb');
        foreach ($arr as $value) {
            if (strpos($ua, $value) !== false) {
                $browser = $path_mob;
                echo $browser;
            }
        }
   
    
  
}

###############################
############ Бан ############
###############################
require_once ('ban.php');
?>