-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfacebook.php
More file actions
146 lines (121 loc) · 4.84 KB
/
Copy pathfacebook.php
File metadata and controls
146 lines (121 loc) · 4.84 KB
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
<?php
/**
* @package RedSHOP
* @subpackage Plugin
*
* @copyright Copyright (C) 2008 - 2017 redCOMPONENT.com. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('_JEXEC') or die;
// Load redSHOP Library
JLoader::import('redshop.library');
require_once JPATH_PLUGINS . '/redshop_login/facebook/library/vendor/autoload.php';
/**
* Generate login facebook
*
* @since __DEPLOY_VERSION__
*/
class PlgRedshop_LoginFacebook extends JPlugin
{
/**
* @return array|false
*/
public function onThirdPartyLogin()
{
try {
$fb = $this->getFbObject();
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$linkLogin = $helper->getLoginUrl(
JRoute::_(
\JUri::root() . 'index.php?option=com_ajax&group=redshop_login&plugin=fbLoginCallBack&format=raw'
),
$permissions
);
return [
'linkLogin' => $linkLogin,
'plugin' => $this->_name
];
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
return false;
}
}
/**
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public function onAjaxFbLoginCallBack()
{
$app = \JFactory::getApplication();
$fb = $this->getFbObject();
$helper = $fb->getRedirectLoginHelper();
$urlCallback = JURI::root() . 'index.php?option=com_ajax&group=redshop_login&plugin=fbLoginCallBack&format=raw';
try {
$accessToken = $helper->getAccessToken($urlCallback);
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$app->enqueueMessage('Graph returned an error: ' . $e->getMessage());
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$app->enqueueMessage('Facebook SDK returned an error: ' . $e->getMessage());
}
if ( ! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
$msg = "Error: " . $helper->getError() . "\n";
$msg .= "Error Code: " . $helper->getErrorCode() . "\n";
$msg .= "Error Reason: " . $helper->getErrorReason() . "\n";
$msg .= "Error Description: " . $helper->getErrorDescription() . "\n";
$app->enqueueMessage($msg);
} else {
header('HTTP/1.0 400 Bad Request');
$app->enqueueMessage('Bad request');
}
}
// Logged in
$token = $accessToken->getValue();
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$userFbId = $tokenMetadata->getUserId();
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId($this->params->get('app_id', ''));
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if ( ! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$app->enqueueMessage("Error getting long-lived access token: " . $e->getMessage());
}
$token = $accessToken->getValue();
}
$response = $fb->get('/me?fields=id,name,email', $token);
$userFb = $response->getGraphUser();
$_SESSION['fb_access_token'] = (string)$accessToken;
$password = \JUserHelper::genRandomPassword(32);
$data = [];
$data['password'] = $password;
$data['password2'] = $password;
$data['email'] = $data['email1'] = $data['username'] = $userFb->getEmail();
$data['name'] = $data['firstname'] = $userFb->getName();
Redshop\Helper\Login::loginJoomlaRedShop($data);
}
/**
* @return \Facebook\Facebook
* @throws \Facebook\Exceptions\FacebookSDKException
* @since 1.0
*/
public function getFbObject()
{
return new \Facebook\Facebook(
[
'app_id' => $this->params->get('app_id', ''),
'app_secret' => $this->params->get('app_secret', ''),
'default_graph_version' => 'v2.10',
]
);
}
}