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 Facebook 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
|
|
|
'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/facebook' |
24
|
|
|
]; |
25
|
|
|
\Inji\Tools::redirect("https://www.facebook.com/dialog/oauth?" . http_build_query($query)); |
26
|
|
|
} |
27
|
|
|
if (empty($_GET['code']) && !empty($_GET['error'])) { |
28
|
|
|
\Inji\Tools::redirect('/', 'Произошла ошибка во время авторизации через соц. сеть: ' . $_GET['error_description']); |
29
|
|
|
} |
30
|
|
|
$query = [ |
31
|
|
|
'client_id' => $config['appId'], |
32
|
|
|
'redirect_uri' => 'http://' . INJI_DOMAIN_NAME . '/users/social/auth/facebook', |
33
|
|
|
'client_secret' => $config['secret'], |
34
|
|
|
'code' => urldecode($_GET['code']), |
35
|
|
|
]; |
36
|
|
|
$result = @file_get_contents("https://graph.facebook.com/oauth/access_token?" . http_build_query($query)); |
37
|
|
|
if ($result === false) { |
38
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
39
|
|
|
} |
40
|
|
|
parse_str($result, $output); |
41
|
|
|
if (empty($output['access_token'])) { |
42
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
43
|
|
|
} |
44
|
|
|
$userQuery = [ |
45
|
|
|
'access_token' => $output['access_token'], |
46
|
|
|
'fields' => 'first_name,middle_name,last_name,email,gender,location,picture' |
47
|
|
|
]; |
48
|
|
|
$userResult = @file_get_contents("https://graph.facebook.com/me?" . http_build_query($userQuery)); |
49
|
|
|
if (!$userResult) { |
50
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
51
|
|
|
} |
52
|
|
|
$userDetail = json_decode($userResult, true); |
53
|
|
|
if (empty($userDetail['id'])) { |
54
|
|
|
\Inji\Tools::redirect('/', 'Во время авторизации произошли ошибки', 'danger'); |
55
|
|
|
} |
56
|
|
|
$social = static::getObject(); |
57
|
|
|
$userSocial = \Users\User\Social::get([['uid', $userDetail['id']], ['social_id', $social->id]]); |
58
|
|
|
if ($userSocial && $userSocial->user) { |
59
|
|
|
\App::$cur->users->newSession($userSocial->user); |
60
|
|
|
if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) { |
61
|
|
|
\Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
if ($userSocial && !$userSocial->user) { |
65
|
|
|
$userSocial->delete(); |
66
|
|
|
} |
67
|
|
|
if (!\Users\User::$cur->id) { |
68
|
|
|
$user = false; |
69
|
|
|
if (!empty($userDetail['email'])) { |
70
|
|
|
$user = \Users\User::get($userDetail['email'], 'mail'); |
71
|
|
|
} |
72
|
|
|
if (!$user) { |
73
|
|
|
$user = new \Users\User(); |
74
|
|
|
$user->group_id = 2; |
75
|
|
|
$user->role_id = 2; |
76
|
|
|
if (!empty($userDetail['email'])) { |
77
|
|
|
$user->login = $user->mail = $userDetail['email']; |
78
|
|
|
} |
79
|
|
|
$invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : ''))))); |
80
|
|
|
if (!empty($invite_code)) { |
81
|
|
|
$invite = \Users\User\Invite::get($invite_code, 'code'); |
82
|
|
|
$inveiteError = false; |
83
|
|
|
if (!$invite) { |
84
|
|
|
\Inji\Msg::add('Такой код пришлашения не найден', 'danger'); |
85
|
|
|
$inveiteError = true; |
86
|
|
|
} |
87
|
|
|
if ($invite->limit && !($invite->limit - $invite->count)) { |
|
|
|
|
88
|
|
|
\Inji\Msg::add('Лимит приглашений для данного кода исчерпан', 'danger'); |
89
|
|
|
$inveiteError = true; |
90
|
|
|
} |
91
|
|
|
if (!$inveiteError) { |
92
|
|
|
$user->parent_id = $invite->user_id; |
93
|
|
|
$invite->count++; |
94
|
|
|
$invite->save(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) { |
98
|
|
|
$user->parent_id = \App::$cur->Users->config['defaultPartner']; |
99
|
|
|
} |
100
|
|
|
$user->save(); |
101
|
|
|
$userInfo = new \Users\User\Info(); |
102
|
|
|
$userInfo->user_id = $user->id; |
|
|
|
|
103
|
|
|
$userInfo->save(); |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
|
|
$user = \Users\User::$cur; |
107
|
|
|
} |
108
|
|
|
if (!$user->info->photo_file_id && !empty($userDetail['picture']['data']['url'])) { |
109
|
|
|
$user->info->photo_file_id = \App::$cur->files->uploadFromUrl($userDetail['picture']['data']['url']); |
110
|
|
|
} |
111
|
|
|
if (!$user->info->first_name && !empty($userDetail['first_name'])) { |
112
|
|
|
$user->info->first_name = $userDetail['first_name']; |
113
|
|
|
} |
114
|
|
|
if (!$user->info->last_name && !empty($userDetail['last_name'])) { |
115
|
|
|
$user->info->last_name = $userDetail['last_name']; |
116
|
|
|
} |
117
|
|
|
if (!$user->info->middle_name && !empty($userDetail['middle_name'])) { |
118
|
|
|
$user->info->middle_name = $userDetail['middle_name']; |
119
|
|
|
} |
120
|
|
|
if (!$user->info->city && !empty($userDetail['location'])) { |
121
|
|
|
$user->info->city = $userDetail['location']; |
122
|
|
|
} |
123
|
|
|
if (!$user->info->sex && !empty($userDetail['gender'])) { |
124
|
|
|
$user->info->sex = $userDetail['gender'] == 'male' ? 1 : ($userDetail['gender'] == 'female' ? 2 : 0); |
125
|
|
|
} |
126
|
|
|
$user->info->save(); |
127
|
|
|
$userSocial = new \Users\User\Social(); |
128
|
|
|
$userSocial->uid = $userDetail['id']; |
|
|
|
|
129
|
|
|
$userSocial->social_id = $social->id; |
|
|
|
|
130
|
|
|
$userSocial->user_id = $user->id; |
|
|
|
|
131
|
|
|
$userSocial->save(); |
132
|
|
|
\App::$cur->users->newSession($user); |
133
|
|
|
if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) { |
134
|
|
|
\Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type], 'Вы успешно зарегистрировались через Facebook', 'success'); |
135
|
|
|
} else { |
136
|
|
|
\Inji\Tools::redirect('/users/cabinet/profile', 'Вы успешно зарегистрировались через Facebook', 'success'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|