Skip to content
facebook_token.php 3.26 KiB
Newer Older
<?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();
?>