Newer
Older
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
<?php
/* ----------------------------------------------------------------------------
* INAF - National Institute for Astrophysics
* IRA - Radioastronomical Institute - Bologna
* OATS - Astronomical Observatory - Trieste
* ----------------------------------------------------------------------------
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
include '../include/init.php';
session_start();
$callback = $_SESSION['rap_callback'];
if (!isset($callback)) {
http_response_code(422);
die("Callback URL not set!");
}
$fb = new Facebook\Facebook([
'app_id' => $Facebook['id'],
'app_secret' => $Facebook['secret'],
'default_graph_version' => $Facebook['version'],
]);
$helper = $fb->getRedirectLoginHelper();
if (isset($_GET['state'])) {
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
}
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
http_response_code(500);
die('Graph returned an error: ' . $e->getMessage());
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
http_response_code(500);
die('Facebook SDK returned an error: ' . $e->getMessage());
}
if (!isset($accessToken)) {
if ($helper->getError()) {
$errorMessage = "Error: " . $helper->getError() . "<br>";
$errorMessage = $errorMessage . "Error Code: " . $helper->getErrorCode() . "<br>";
$errorMessage = $errorMessage . "Error Reason: " . $helper->getErrorReason() . "<br>";
$errorMessage = $errorMessage . "Error Description: " . $helper->getErrorDescription();
} else {
$errorMessage = "Bad request";
}
http_response_code(500);
die($errorMessage);
}
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,first_name,last_name,email', $accessToken);
} catch (Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
$_SESSION['fb_access_token'] = (string) $accessToken;
// Creating user object
$userData = array(
"type" => "Facebook",
"name" => $user["first_name"],
"surname" => $user["last_name"],
"emailAddresses" => [$user["email"]],
"typed_id" => $user["id"]
);
$token = RAP\DAO::insertLogin($userData);
header('Location: ' . $callback . '?token=' . $token);
die();
?>