<?php


#Мелкие настройки
$HeadTime=microtime(1);
error_reporting(0);

#mysql_connect('mysql.jahost.ru','sulei888_cms','suhomskhmsk') or die('Сервер MySQL не доступен!');
mysql_connect('mysql.jahost.ru','sulei888_cms','suhokum4iks') or die('Сервер MySQL не доступен!');
mysql_select_db('sulei888_cms') or die('База данных не доступна!');

mysql_query('SET NAMES utf8');

$q = mysql_query("SELECT * FROM `settings`");
while($arr=mysql_fetch_assoc($q)) $set[$arr['id']]=$arr['value'];
$act = isset($_GET['act']) ? $_GET['act'] : '';

# Cookies
if (isset($_COOKIE['login']) and isset($_COOKIE['password']))
{
    $user = mysql_query("SELECT * FROM `users` WHERE `login` = '".mysql_real_escape_string($_COOKIE['login'])."' AND `password` = '".mysql_real_escape_string($_COOKIE['password'])."'");
    if(mysql_num_rows($user) == 1)
    {
     $user = mysql_fetch_assoc($user);
    }
    else
    {
     $user = array();
    }
}
else
{
    $user = array();
}

# Определение темы
if ($user)
{
 $style = $user['style'];
}
else
{
 $style = $set['style'];
}

# Определение онлайна
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$_SERVER['HTTP_X_FORWARDED_FOR']))
{
 $ip = trim(htmlspecialchars(mysql_real_escape_string($_SERVER['HTTP_X_FORWARDED_FOR'])));
}
elseif(isset($_SERVER['HTTP_CLIENT_IP']) and preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$_SERVER['HTTP_CLIENT_IP']))
{
 $ip = trim(htmlspecialchars(mysql_real_escape_string($_SERVER['HTTP_CLIENT_IP'])));
}
elseif(isset($_SERVER['REMOTE_ADDR']) and preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$_SERVER['REMOTE_ADDR']))
{
 $ip = trim(htmlspecialchars(mysql_real_escape_string($_SERVER['REMOTE_ADDR'])));
}
else
{
 $ip = 'скрыт';
}


if (isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']))$ua=stripcslashes(htmlspecialchars($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']));
elseif (isset($_SERVER['HTTP_USER_AGENT']))$ua=stripcslashes(htmlspecialchars($_SERVER['HTTP_USER_AGENT']));
else $ua = 'скрыт';

$ua=strtok($ua, '/');
$ua=strtok($ua, ' ');

if (!$user)
{
 if (mysql_result(mysql_query("SELECT COUNT(*) FROM `online` WHERE `ip` = '".$ip."' AND `ua` = '".mysql_real_escape_string($ua)."' AND `time` > '".(time()-300)."'"),0)==1)
 {
  $onlin=mysql_fetch_array(mysql_query("SELECT * FROM `online` WHERE `ip` = '".$ip."' AND `ua` = '".mysql_real_escape_string($ua)."' AND `time` > '".(time()-300)."'"));
  mysql_query("UPDATE `online` SET `pereh` = '".($onlin['pereh']+1)."', `time` = '".time()."' WHERE `ip` = '$ip' AND `ua` = '$ua' LIMIT 1");
 }
 else
 {
  mysql_query("DELETE FROM `online` WHERE `time` < '".(time()-300)."'");
  mysql_query("OPTIMIZE TABLE `online`");
  mysql_query("INSERT INTO `online` (`ip`, `ua`, `time`) values('".$ip."', '".mysql_real_escape_string($ua)."', ".time().")");
 }
}
if (isset($user['login']))
{
    if ($user['ban'] == 1)
    {
        if (basename($_SERVER['PHP_SELF']) != 'ban.php') {
            header("Location: http://vlofe.ru/ban.php");
            exit;
        }
    }
    mysql_query("UPDATE `users` SET `time`='" . time() . "' WHERE `id` = ".$user['id']."");
    mysql_query("UPDATE `users` SET `ip`='".mysql_real_escape_string($ip)."' WHERE `id` = ".$user['id']."");
}
$online = mysql_result(mysql_query("select count(*) from `users` where `time` > '".(time()-300)."'"),0);
$online_g = mysql_result(mysql_query("select count(*) from `online` where `time` > '".(time()-300)."'"),0);
$online = $online+$online_g;
?>
