-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk.php
50 lines (44 loc) · 1.51 KB
/
vk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
require_once('app/config.php');
require_once('app/user.php');
if( count($_GET) == 0){
header('Location: index.php');
}
if (isset($_GET['code'])) {
$result = false;
$params = array(
'client_id' => $vk_app_id,
'client_secret' => $vk_api_secure_key,
'code' => $_GET['code'],
'redirect_uri' => $vk_api_redirect_uri
);
$url_with_params = $vk_api_token_url . '?' . urldecode(http_build_query($params));
$content = file_get_contents($url_with_params);
$token = json_decode($content, true);
if (isset($token['access_token'])) {
$params = array(
'uids' => $token['user_id'],
'fields' => 'uid,first_name,last_name',
'access_token' => $token['access_token']
);
$user_info_url = $vk_api_user_info_url . '?' . urldecode(http_build_query($params));
$content = file_get_contents($user_info_url);
$user_info = json_decode($content, true);
if (isset($user_info['response'][0]['uid'])) {
$user_info = $user_info['response'][0];
$result = true;
}
}
if ($result) {
try {
$user = new User(User::ACCOUNT_TYPE_VK, $user_info['uid'], $user_info['first_name'], $user_info['last_name']);
} catch (Exception $e) {
header('Location: index.php');
}
$user->authenticate();
// Redirect auth user to messages page
header('Location: messages.php');
} else {
header('Location: index.php');
}
}