<?php
/**
* @author: Egor Smolyakov, 2012.
* @e-mail: mr.stelhammer@gmail.com
*/

// устанавливаем серверное время
if (@function_exists('ini_set')) {
    ini_set('date.timezone', 'Europe/Moscow');
}

$act = isset($_GET['act']) ? $_GET['act'] : ''; // работа со switch

$is_user = 0;
if (isset($_COOKIE['login'], $_COOKIE['password'])) {
      $querycheck = DB :: run() -> query("SELECT * FROM `users` WHERE `login`=? LIMIT 1;", array($_COOKIE['login']));
      $user = $querycheck -> fetch();
      
      if (!empty($user)) {
        if ($_COOKIE['login'] == $user['login'] && md5_hash($_COOKIE['password']) == $user['password']) {
          $is_user = 1;
        }
      }
}

// настройки сайта
$q = DB :: run() -> query("SELECT * FROM `settings`");
while($arr = $q -> fetch()) $set[$arr['id']]=$arr['value'];


// Определение IP-адресса посетителя
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 = protect($_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 = protect($_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 = protect($_SERVER['REMOTE_ADDR']); }
else
{ $ip = 'скрыт'; }
// Определение UA посетителя
if (isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']))
{ $ua = protect($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']); }
elseif (isset($_SERVER['HTTP_USER_AGENT']))
{ $ua = protect($_SERVER['HTTP_USER_AGENT']); } else
{ $ua = 'скрыт'; }
$ua=strtok($ua, '/');
$ua=strtok($ua, ' ');
if (isset($_SERVER['HTTP_USER_AGENT'])) $useragent = protect($_SERVER['HTTP_USER_AGENT']); else $useragent = 'скрыт';

if ($is_user)   DB :: run() -> query("UPDATE `users` SET `last_time` = ?, `ip` = ?,`ua` = ? WHERE `id` = ? ;", array(TIME, $ip, $useragent, $user['id']));