-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa-pplogin-login.php
268 lines (211 loc) · 7.23 KB
/
qa-pplogin-login.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
class qa_publicityport_login {
var $directory;
var $urltoroot;
var $provider;
function load_module($directory, $urltoroot, $type, $provider) {
$this->directory = $directory;
$this->urltoroot = $urltoroot;
$this->provider = $provider;
}
function check_login() {
$action = null;
$key = null;
if( !empty($_GET['hauth_start']) ) {
$key = trim(strip_tags($_GET['hauth_start']));
$action = 'process';
} else if( !empty($_GET['hauth_done']) ) {
$key = trim(strip_tags($_GET['hauth_done']));
$action = 'process';
} else if( !empty($_GET['login']) ) {
$key = trim(strip_tags($_GET['login']));
$action = 'login';
} else if(isset($_GET['fb_source']) && $_GET['fb_source'] == 'appcenter' &&
isset($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'], 'www.facebook.com') !== false &&
isset($_GET['fb_appcenter']) && $_GET['fb_appcenter'] == '1' && isset($_GET['code']) ) {
// allow AppCenter users to login directly
$key = 'facebook';
$action = 'login';
}
if($key == null || strcasecmp($key, $this->provider) != 0) {
return false;
}
if($action == 'login') {
// handle the login
// after login come back to the same page
$loginCallback = qa_path('', array(), qa_opt('site_url'));
require_once $this->directory . 'Hybrid/Auth.php';
require_once $this->directory . 'qa-pplogin-utils.php';
// prepare the configuration of HybridAuth
$config = $this->getConfig($loginCallback);
$topath = qa_get('to');
if(!isset($topath)) {
$topath = ''; // redirect to front page
}
try {
// try to login
$hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( $this->provider );
// if ok, create/refresh the user account
$user = $adapter->getUserProfile();
$duplicates = 0;
if (!empty($user))
$duplicates = qa_log_in_external_user($key, $user->identifier, array(
'email' => @$user->email,
'handle' => @$user->displayName,
'confirmed' => !empty($user->emailVerified),
'name' => @$user->displayName,
'location' => @$user->region,
'website' => @$user->webSiteURL,
'about' => @$user->description,
'avatar' => strlen(@$user->photoURL) ? qa_retrieve_url($user->photoURL) : null,
));
if($duplicates > 0) {
qa_redirect('logins', array('confirm' => '1', 'to' => $topath));
} else {
qa_redirect_raw(qa_opt('site_url') . $topath);
}
} catch(Exception $e) {
// not really interested in the error message - for now
// however, in case we have errors 6 or 7, then we have to call logout to clean everything up
if($e->getCode() == 6 || $e->getCode() == 7) {
$adapter->logout();
}
$qry = 'provider=' . $this->provider . '&code=' . $e->getCode();
if(strstr($topath, '?') === false) {
$topath .= '?' . $qry;
} else {
$topath .= '&' . $qry;
}
// redirect
qa_redirect_raw(qa_opt('site_url') . $topath);
}
}
if($action == 'process') {
require_once( "Hybrid/Auth.php" );
require_once( "Hybrid/Endpoint.php" );
Hybrid_Endpoint::process();
}
return false;
}
function do_logout() {
// after login come back to the same page
$loginCallback = qa_path('', array(), qa_opt('site_url'));
require_once( "Hybrid/Auth.php" );
// prepare the configuration of HybridAuth
$config = $this->getConfig($loginCallback);
try {
// try to logout
$hybridauth = new Hybrid_Auth( $config );
if($hybridauth->isConnectedWith( $this->provider )) {
$adapter = $hybridauth->getAdapter( $this->provider );
$adapter->logout();
}
} catch(Exception $e) {
// not really interested in the error message - for now
// however, in case we have errors 6 or 7, then we have to call logout to clean everything up
if($e->getCode() == 6 || $e->getCode() == 7) {
$adapter->logout();
}
}
}
function match_source($source) {
// the session source will be in the format 'provider-xyx'
$pos = strpos($source, '-');
if($pos === false) {
$pos = strlen($source);
}
// identify the provider out of the session source
$provider = substr($source, 0, $pos);
// verify if the identified provider matches the current one
return stripos($this->provider, $provider) !== false;
}
function login_html($tourl, $context) {
self::printCode($this->provider, $tourl, $context, 'login');
}
function logout_html($tourl) {
self::printCode($this->provider, $tourl, 'menu', 'logout');
}
static function printCode($provider, $tourl, $context, $action = 'login', $print = true) {
$css = $key = strtolower($provider);
if ($key == 'live') {
$css = 'windows'; // translate provider name to zocial css class
} else if ($key == 'googleplus') {
$provider = 'Google+';
}
$showInHeader = qa_opt("{$key}_app_shortcut") ? true : false;
if($action == 'login' && !$showInHeader && $context == 'menu') {
// do not show login button in the header for this
return;
}
if($action == 'logout') {
$url = $tourl;
$classes = "$context action-logout $css";
$title = qa_lang_html('main/nav_logout');
$text = qa_lang_html('main/nav_logout');
} else if($action == 'login') {
$topath = qa_get('to'); // lets user switch between login and register without losing destination page
// clean GET parameters (not performed when to parameter is already passed)
$get = $_GET;
unset($get['provider']);
unset($get['code']);
$tourl = isset($topath) ? $topath : qa_path(qa_request(), $get, ''); // build our own tourl
$params = array(
'login' => $key,
);
$url = qa_path('login', $params, qa_path_to_root());
if(strlen($tourl) > 0) {
$url .= '&to=' . htmlspecialchars($tourl); // play nice with validators
}
$classes = "$context action-login $css";
$title = qa_lang_html_sub('plugin_open/login_using', $provider);
$text = $provider . ' ' . qa_lang_html('main/nav_login');
if($context != 'menu') {
$text = $title;
}
} else if($action == 'link') {
$url = qa_path('logins', array('link' => $key), qa_path_to_root()); // build our own url
$classes = "$context action-link $css";
$title = qa_lang_html_sub('plugin_open/login_using', $provider);
$text = qa_lang_html('main/nav_login');
if($context != 'menu') {
$text = $title;
}
} else if($action == 'view') {
$url = 'javascript://';
$classes = "$context action-link $css";
$title = $provider;
$text = $tourl;
}
$html = <<<HTML
<a class="publicityport-login-button context-$classes" title="$title" href="$url" rel="nofollow">$text</a>
HTML;
if($print) {
echo $html;
} else {
return $html;
}
}
function getConfig($url) {
$key = strtolower($this->provider);
return array(
'base_url' => $url,
'providers' => array (
$this->provider => array (
'enabled' => true,
'keys' => array(
'id' => qa_opt("{$key}_app_id"),
'key' => qa_opt("{$key}_app_id"),
'secret' => qa_opt("{$key}_app_secret")
),
'scope' => $this->provider == 'Facebook' ? 'email,user_about_me,user_location,user_website' : null,
)
),
'debug_mode' => false,
'debug_file' => ''
);
}
}
/*
Omit PHP closing tag to help avoid accidental output
*/