1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Social helper Google |
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 Google extends \Users\SocialHelper { |
15
|
|
|
|
16
|
|
|
public static function auth() { |
17
|
|
|
|
18
|
|
|
$config = static::getConfig(); |
19
|
|
|
if (empty($_GET['code']) && empty($_GET['error'])) { |
20
|
|
|
$query = [ |
21
|
|
|
'client_id' => $config['client_id'], |
22
|
|
|
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile', |
23
|
|
|
'response_type' => 'code', |
24
|
|
|
'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/google' |
25
|
|
|
]; |
26
|
|
|
\Inji\Tools::redirect("https://accounts.google.com/o/oauth2/auth?" . 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['client_id'], |
33
|
|
|
'client_secret' => $config['secret'], |
34
|
|
|
'code' => $_GET['code'], |
35
|
|
|
'grant_type' => 'authorization_code', |
36
|
|
|
'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/google' |
37
|
|
|
]; |
38
|
|
|
$result = false; |
39
|
|
|
if ($curl = curl_init()) { |
40
|
|
|
curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token'); |
41
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
42
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
43
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($query)); |
44
|
|
|
$result = curl_exec($curl); |
45
|
|
|
curl_close($curl); |
46
|
|
|
} |
47
|
|
|
if ($result === false) { |
48
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
49
|
|
|
} |
50
|
|
|
$result = json_decode($result, true); |
51
|
|
|
if (empty($result['access_token'])) { |
52
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
53
|
|
|
} |
54
|
|
|
$userQuery = [ |
55
|
|
|
'access_token' => $result['access_token'] |
56
|
|
|
]; |
57
|
|
|
$userResult = @file_get_contents("https://www.googleapis.com/oauth2/v1/userinfo?" . http_build_query($userQuery)); |
58
|
|
|
if (!$userResult) { |
59
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
60
|
|
|
} |
61
|
|
|
$userDetail = json_decode($userResult, true); |
62
|
|
|
if (empty($userDetail['id'])) { |
63
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
64
|
|
|
} |
65
|
|
|
$social = static::getObject(); |
66
|
|
|
$userSocial = \Users\User\Social::get([['uid', $userDetail['id']], ['social_id', $social->id]]); |
67
|
|
|
if ($userSocial && $userSocial->user) { |
68
|
|
|
\App::$cur->users->newSession($userSocial->user); |
69
|
|
|
if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) { |
70
|
|
|
\Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]); |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
|
|
if ($userSocial && !$userSocial->user) { |
74
|
|
|
$userSocial->delete(); |
75
|
|
|
} |
76
|
|
|
if (!\Users\User::$cur->id) { |
77
|
|
|
$user = false; |
78
|
|
|
if (!empty($userDetail['email']) && !empty($userDetail['verified_email'])) { |
79
|
|
|
$user = \Users\User::get($userDetail['email'], 'mail'); |
80
|
|
|
} |
81
|
|
|
if (!$user) { |
82
|
|
|
$user = new \Users\User(); |
83
|
|
|
$user->group_id = 2; |
84
|
|
|
$user->role_id = 2; |
85
|
|
|
if (!empty($userDetail['email']) && !empty($userDetail['verified_email'])) { |
86
|
|
|
$user->login = $user->mail = $userDetail['email']; |
87
|
|
|
} |
88
|
|
|
$invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : ''))))); |
89
|
|
|
if (!empty($invite_code)) { |
90
|
|
|
$invite = \Users\User\Invite::get($invite_code, 'code'); |
91
|
|
|
$inveiteError = false; |
92
|
|
|
if (!$invite) { |
93
|
|
|
\Inji\Msg::add('Такой код пришлашения не найден', 'danger'); |
94
|
|
|
$inveiteError = true; |
95
|
|
|
} |
96
|
|
|
if ($invite->limit && !($invite->limit - $invite->count)) { |
|
|
|
|
97
|
|
|
\Inji\Msg::add('Лимит приглашений для данного кода исчерпан', 'danger'); |
98
|
|
|
$inveiteError = true; |
99
|
|
|
} |
100
|
|
|
if (!$inveiteError) { |
101
|
|
|
$user->parent_id = $invite->user_id; |
102
|
|
|
$invite->count++; |
103
|
|
|
$invite->save(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) { |
107
|
|
|
$user->parent_id = \App::$cur->Users->config['defaultPartner']; |
108
|
|
|
} |
109
|
|
|
$user->save(); |
110
|
|
|
$userInfo = new \Users\User\Info(); |
111
|
|
|
$userInfo->user_id = $user->id; |
|
|
|
|
112
|
|
|
$userInfo->save(); |
113
|
|
|
} |
114
|
|
|
} else { |
115
|
|
|
$user = \Users\User::$cur; |
116
|
|
|
} |
117
|
|
|
if (!$user->info->photo_file_id && !empty($userDetail['picture'])) { |
118
|
|
|
$user->info->photo_file_id = \App::$cur->files->uploadFromUrl($userDetail['picture']); |
119
|
|
|
} |
120
|
|
|
if (!$user->info->first_name && !empty($userDetail['given_name'])) { |
121
|
|
|
$user->info->first_name = $userDetail['given_name']; |
122
|
|
|
} |
123
|
|
|
if (!$user->info->last_name && !empty($userDetail['family_name'])) { |
124
|
|
|
$user->info->last_name = $userDetail['family_name']; |
125
|
|
|
} |
126
|
|
|
if (!$user->info->sex && !empty($userDetail['gender'])) { |
127
|
|
|
$user->info->sex = $userDetail['gender'] == 'male' ? 1 : ($userDetail['gender'] == 'female' ? 2 : 0); |
128
|
|
|
} |
129
|
|
|
$user->info->save(); |
130
|
|
|
$userSocial = new \Users\User\Social(); |
131
|
|
|
$userSocial->uid = $userDetail['id']; |
|
|
|
|
132
|
|
|
$userSocial->social_id = $social->id; |
|
|
|
|
133
|
|
|
$userSocial->user_id = $user->id; |
|
|
|
|
134
|
|
|
$userSocial->save(); |
135
|
|
|
\App::$cur->users->newSession($user); |
136
|
|
|
\Inji\Tools::redirect('/users/cabinet/profile', 'Вы успешно зарегистрировались через Google+', 'success'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|