|
<?php
$title = 'Аутентификация через ВКонтакте';
require_once ('system/function.php');
require_once ('system/header.php');
$client_id = '4975621';
$client_secret = 'Somon61tj';
$redirect_uri = 'http://wapwm.ru/vk.php';
$url = 'http://oauth.vk.com/authorize';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
);
echo $link = '<div class="podmenu" ><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Вход через VK</a></div>';
require_once ('system/footer.php');
if (isset($_GET['code'])) {
$result = false;
$params = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $_GET['code'],
'redirect_uri' => $redirect_uri
);
$token = json_decode(file_get_contents('https://oauth.vk.com/access_token' . '?' . urldecode(http_build_query($params))), true);
if (isset($token['access_token'])) {
$params = array(
'uids' => $token['user_id'],
'fields' => 'uid,first_name,last_name,screen_name,sex,bdate,photo_big',
'access_token' => $token['access_token']
);
$userInfo = json_decode(file_get_contents('https://api.vk.com/method/users.get' . '?' . urldecode(http_build_query($params))), true);
if (isset($userInfo['response'][0]['uid'])) {
$userInfo = $userInfo['response'][0];
$result = true;
}
}
if ($result) {
$lo = mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `login` = 'vk-".$userInfo['uid']."'");
if (mysql_result($lo, 0) > 0) {
$info=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `login` = 'vk-".$userInfo['uid']."' limit 1"));
setcookie('uslog', $info['login'], time()+86400*365, '/');
setcookie('uspass', $info['pass'], time()+86400*365, '/');
header('Location: /index.php'); exit;
}else{
$pass=mt_rand (1000, 9999);
$pass='vk-'.$pass.'';
if (isset($userInfo['sex'])){if ($userInfo['sex']=='1'){$sex='2';}else{$sex='1';}}else{$sex='1';}
$log='vk-'.$userInfo['uid'].'';
$pass=md5(md5(md5($pass)));
$name=''.$userInfo['first_name'].'';
$avatar='vk'.$userInfo['uid'].'.jpg';
mysql_query("INSERT INTO `users` SET `login` = '$log', `pass` = '$pass', `name` = '$name', `sex` = '$sex', `avatar` = '$avatar', `datareg` = '".time()."', `level` = '0', `max` = '10'");
copy(''.$userInfo['photo_big'].'', 'files/ava/vk'.$userInfo['uid'].'.jpg');
setcookie('uslog', $log, time()+86400*365, '/');
setcookie('uspass', $pass, time()+86400*365, '/');
header('Location: /index.php'); exit;
}
}
}
?>