|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Social helper vk |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Users\SocialHelper; |
|
13
|
|
|
|
|
14
|
|
|
class Vk extends \Users\SocialHelper { |
|
15
|
|
|
|
|
16
|
|
|
public static function auth() { |
|
17
|
|
|
$config = static::getConfig(); |
|
18
|
|
|
if (empty($_GET['code']) && empty($_GET['error'])) { |
|
19
|
|
|
$query = [ |
|
20
|
|
|
'client_id' => $config['appId'], |
|
21
|
|
|
'scope' => 'email', |
|
22
|
|
|
'response_type' => 'code', |
|
23
|
|
|
'display' => 'page', |
|
24
|
|
|
'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/vk' |
|
25
|
|
|
]; |
|
26
|
|
|
\Inji\Tools::redirect("https://oauth.vk.com/authorize?" . http_build_query($query)); |
|
27
|
|
|
} |
|
28
|
|
|
if (empty($_GET['code']) && !empty($_GET['error'])) { |
|
29
|
|
|
\Inji\Tools::redirect('/', 'Произошла ошибка во время авторизации через соц. сеть: ' . $_GET['error_description']); |
|
30
|
|
|
} |
|
31
|
|
|
$query = [ |
|
32
|
|
|
'client_id' => $config['appId'], |
|
33
|
|
|
'client_secret' => $config['secret'], |
|
34
|
|
|
'code' => $_GET['code'], |
|
35
|
|
|
'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/vk' |
|
36
|
|
|
]; |
|
37
|
|
|
$result = @file_get_contents("https://oauth.vk.com/access_token?" . http_build_query($query)); |
|
38
|
|
|
if ($result === false) { |
|
39
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
|
40
|
|
|
} |
|
41
|
|
|
$result = json_decode($result, true); |
|
42
|
|
|
if (empty($result['user_id'])) { |
|
43
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
|
44
|
|
|
} |
|
45
|
|
|
$userQuery = [ |
|
46
|
|
|
'user_id' => $result['user_id'], |
|
47
|
|
|
'fields' => 'sex, bdate, photo_max_orig, home_town', |
|
48
|
|
|
'access_token' => $result['access_token'] |
|
49
|
|
|
]; |
|
50
|
|
|
$userResult = @file_get_contents("https://api.vk.com/method/users.get?" . http_build_query($userQuery)); |
|
51
|
|
|
if (!$userResult) { |
|
52
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
|
53
|
|
|
} |
|
54
|
|
|
$userDetail = json_decode($userResult, true); |
|
55
|
|
|
if (empty($userDetail['response'][0])) { |
|
56
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
|
57
|
|
|
} |
|
58
|
|
|
$social = static::getObject(); |
|
59
|
|
|
$userSocial = \Users\User\Social::get([['uid', $result['user_id']], ['social_id', $social->id]]); |
|
60
|
|
|
if ($userSocial && $userSocial->user) { |
|
61
|
|
|
\App::$cur->users->newSession($userSocial->user); |
|
62
|
|
|
if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) { |
|
63
|
|
|
\Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]); |
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
if ($userSocial && !$userSocial->user) { |
|
67
|
|
|
$userSocial->delete(); |
|
68
|
|
|
} |
|
69
|
|
|
if (!\Users\User::$cur->id) { |
|
70
|
|
|
$user = false; |
|
71
|
|
|
if (!empty($result['email'])) { |
|
72
|
|
|
$user = \Users\User::get($result['email'], 'mail'); |
|
73
|
|
|
} |
|
74
|
|
|
if (!$user) { |
|
75
|
|
|
$user = new \Users\User(); |
|
76
|
|
|
$user->group_id = 2; |
|
77
|
|
|
$user->role_id = 2; |
|
78
|
|
|
if (!empty($result['email'])) { |
|
79
|
|
|
$user->login = $user->mail = $result['email']; |
|
80
|
|
|
} |
|
81
|
|
|
$invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : ''))))); |
|
82
|
|
|
if (!empty($invite_code)) { |
|
83
|
|
|
$invite = \Users\User\Invite::get($invite_code, 'code'); |
|
84
|
|
|
$inveiteError = false; |
|
85
|
|
|
if (!$invite) { |
|
86
|
|
|
\Inji\Msg::add('Такой код пришлашения не найден', 'danger'); |
|
87
|
|
|
$inveiteError = true; |
|
88
|
|
|
} |
|
89
|
|
|
if ($invite->limit && !($invite->limit - $invite->count)) { |
|
|
|
|
|
|
90
|
|
|
\Inji\Msg::add('Лимит приглашений для данного кода исчерпан', 'danger'); |
|
91
|
|
|
$inveiteError = true; |
|
92
|
|
|
} |
|
93
|
|
|
if (!$inveiteError) { |
|
94
|
|
|
$user->parent_id = $invite->user_id; |
|
95
|
|
|
$invite->count++; |
|
96
|
|
|
$invite->save(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) { |
|
100
|
|
|
$user->parent_id = \App::$cur->Users->config['defaultPartner']; |
|
101
|
|
|
} |
|
102
|
|
|
$user->save(); |
|
103
|
|
|
$userInfo = new \Users\User\Info(); |
|
104
|
|
|
$userInfo->user_id = $user->id; |
|
|
|
|
|
|
105
|
|
|
$userInfo->save(); |
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
$user = \Users\User::$cur; |
|
109
|
|
|
} |
|
110
|
|
|
if (!$user->info->photo_file_id && !empty($userDetail['response'][0]['photo_max_orig'])) { |
|
111
|
|
|
$user->info->photo_file_id = \App::$cur->files->uploadFromUrl($userDetail['response'][0]['photo_max_orig']); |
|
112
|
|
|
} |
|
113
|
|
|
if (!$user->info->first_name && !empty($userDetail['response'][0]['first_name'])) { |
|
114
|
|
|
$user->info->first_name = $userDetail['response'][0]['first_name']; |
|
115
|
|
|
} |
|
116
|
|
|
if (!$user->info->last_name && !empty($userDetail['response'][0]['last_name'])) { |
|
117
|
|
|
$user->info->last_name = $userDetail['response'][0]['last_name']; |
|
118
|
|
|
} |
|
119
|
|
|
if (!$user->info->city && !empty($userDetail['response'][0]['home_town'])) { |
|
120
|
|
|
$user->info->city = $userDetail['response'][0]['home_town']; |
|
121
|
|
|
} |
|
122
|
|
|
if (!$user->info->sex && !empty($userDetail['response'][0]['sex'])) { |
|
123
|
|
|
$user->info->sex = $userDetail['response'][0]['sex'] == 2 ? 1 : ($userDetail['response'][0]['sex'] == 1 ? 2 : 0); |
|
124
|
|
|
} |
|
125
|
|
|
if ($user->info->bday == '0000-00-00' && !empty($userDetail['response'][0]['bdate'])) { |
|
126
|
|
|
$user->info->bday = substr_count($userDetail['response'][0]['bdate'], '.') == 2 ? \DateTime::createFromFormat('d.m.Y', $userDetail['response'][0]['bdate'])->format('Y-m-d') : (substr_count($userDetail['response'][0]['bdate'], '.') == 1 ? \DateTime::createFromFormat('d.m', $userDetail['response'][0]['bdate'])->format('Y-m-1') : '0000-00-00'); |
|
127
|
|
|
} |
|
128
|
|
|
$user->info->save(); |
|
129
|
|
|
$userSocial = new \Users\User\Social(); |
|
130
|
|
|
$userSocial->uid = $result['user_id']; |
|
|
|
|
|
|
131
|
|
|
$userSocial->social_id = $social->id; |
|
|
|
|
|
|
132
|
|
|
$userSocial->user_id = $user->id; |
|
|
|
|
|
|
133
|
|
|
$userSocial->save(); |
|
134
|
|
|
\App::$cur->users->newSession($user); |
|
135
|
|
|
\Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type], 'Вы успешно зарегистрировались через ВКонтакте', 'success'); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function checkAppAccess() { |
|
140
|
|
|
$viewer_id = filter_input(INPUT_GET, 'viewer_id', FILTER_SANITIZE_NUMBER_INT); |
|
141
|
|
|
$get_auth_key = filter_input(INPUT_GET, 'auth_key', FILTER_SANITIZE_STRING); |
|
142
|
|
|
|
|
143
|
|
|
$config = static::getConfig(); |
|
144
|
|
|
|
|
145
|
|
|
$auth_key = md5($config['appId'] . '_' . $viewer_id . '_' . $config['secret']); |
|
146
|
|
|
|
|
147
|
|
|
if ($auth_key !== $get_auth_key) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
$userQuery = [ |
|
151
|
|
|
'user_id' => $viewer_id, |
|
152
|
|
|
'fields' => 'photo_medium,nickname, domain, sex, bdate, city, country, timezone, photo_50, photo_100, photo_200_orig, has_mobile, contacts, education, online, relation, last_seen, status, can_write_private_message, can_see_all_posts, can_post, universities', |
|
153
|
|
|
'access_token' => filter_input(INPUT_GET, 'access_token', FILTER_SANITIZE_STRING) |
|
154
|
|
|
]; |
|
155
|
|
|
$userResult = json_decode(@file_get_contents("https://api.vk.com/method/users.get?" . http_build_query($userQuery)), true); |
|
156
|
|
|
$object = static::getObject(); |
|
157
|
|
|
|
|
158
|
|
|
$socUser = \Users\User\Social::get([['social_id', $object->pk()], ['uid', $viewer_id]]); |
|
159
|
|
|
if (!$socUser) { |
|
160
|
|
|
$user = new \Users\User(); |
|
161
|
|
|
$user->login = $userResult['response'][0]['domain']; |
|
162
|
|
|
$user->save(); |
|
163
|
|
|
$info = new \Users\User\Info(); |
|
164
|
|
|
$info->user_id = $user->id; |
|
|
|
|
|
|
165
|
|
|
$info->sex = $userResult['response'][0]['sex'] == 2 ? 1 : ($userResult['response'][0]['sex'] == 1 ? 2 : 0); |
|
|
|
|
|
|
166
|
|
|
$info->first_name = $userResult['response'][0]['first_name']; |
|
|
|
|
|
|
167
|
|
|
$info->last_name = $userResult['response'][0]['last_name']; |
|
|
|
|
|
|
168
|
|
|
$info->photo_file_id = \App::$cur->files->uploadFromUrl($userResult['response'][0]['photo_200_orig'], ['accept_group' => 'image']); |
|
|
|
|
|
|
169
|
|
|
$info->save(); |
|
170
|
|
|
$social = new \Users\User\Social(); |
|
171
|
|
|
$social->user_id = $user->id; |
|
|
|
|
|
|
172
|
|
|
$social->social_id = 1; |
|
|
|
|
|
|
173
|
|
|
$social->uid = $userResult['response'][0]['uid']; |
|
|
|
|
|
|
174
|
|
|
$social->save(); |
|
175
|
|
|
} else { |
|
176
|
|
|
$user = $socUser->user; |
|
177
|
|
|
} |
|
178
|
|
|
return $user; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|