<?php
include_once($_SERVER["DOCUMENT_ROOT"].'/php/system/includes_system.php');
$sess = md5(time());
$s = file_get_contents('http://ulogin.ru/token.php?token=' . $_POST['token'] . '&host=' . $_SERVER['HTTP_HOST']);
$user = json_decode($s, true);
$query = $db->query("SELECT * FROM `users` WHERE `uid`='".$user['uid']."' AND `network`='".$user['network']."'");
if($query->num_rows==0){
	// Регистрация
	$db->query("INSERT INTO `users` SET `email`='".$user['email']."', `name`='".$user['first_name']."', `surname`='".$user['last_name']."', `uid`='".$user['uid']."', `network`='".$user['network']."', `sess`='".$sess."'");
	setcookie("id", $db->insert_id, time() + 60 * 60 * 24 * 30 * 12, "/");
	setcookie("sess", $sess, time() + 60 * 60 * 24 * 30 * 12, "/");
	header('location:/');
}else{
	// Апдейт
	$us = $query->fetch_assoc();
	$db->query("UPDATE `users` SET `email`='".$user['email']."', `name`='".$user['first_name']."', `surname`='".$user['last_name']."', `sess`='".$sess."' WHERE `id`='".$us['id']."'");
	setcookie("id", $us['id'], time() + 60 * 60 * 24 * 30 * 12, "/");
	setcookie("sess", $sess, time() + 60 * 60 * 24 * 30 * 12, "/");
	header('location:/');
}
?>