Skip to content

Commit

Permalink
Update fb_handlers to use fb api v5.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
teldosas committed Nov 18, 2016
1 parent 0e8707c commit 43cc708
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions handlers/fb_handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
// and Facebook

// TODO: this only works if the script is installed in root
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/../include/facebook-php-sdk-v4/src/Facebook/');
require_once(__DIR__ . '/../include/facebook-php-sdk-v4/autoload.php');
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/../include/php-graph-sdk/src/Facebook');
require_once(__DIR__ . '/../include/php-graph-sdk/src/Facebook/autoload.php');

require_once(__DIR__ . '/../tokens.php');

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

FacebookSession::setDefaultApplication(APP_ID,
APP_SECRET);
Flight::set('fb', new Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.8',
]));


Flight::set('retry_url', MY_URL .'login');
Expand Down Expand Up @@ -67,25 +69,20 @@ function render_boilerplate() {
}


function check_permissions($session) {

$request = new FacebookRequest(
$session,
'GET',
'/me/permissions'
);

function check_permissions($accessToken) {

$fb = Flight::get('fb');

try {
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
// http://stackoverflow.com/q/23527919
foreach ($graphObject as $key => $permissionObject) {
//print_r($permission);
if ($permissionObject->permission == 'publish_actions') {
return $permissionObject->status == 'granted';
$response = $fb->get('/me/permissions', $accessToken);
$graphEdge = $response->getGraphEdge();

foreach($graphEdge as $graphNode) {
if ($graphNode->getField('permission') == 'publish_actions') {
return $graphNode->getField('status') == 'granted';
}
}
} catch (FacebookRequestException $ex) {
} catch (FacebookResponseException $ex) {
Flight::error($ex);
} catch (\Exception $ex) {
Flight::error($ex);
Expand All @@ -107,12 +104,13 @@ function handle_root() {
// if the user does not grant publish_actions,
// we go here and ask again
function handle_rerequest_permission() {
$fb = Flight::get('fb');
render_boilerplate();
// Simplification: always assume we are not logged in!
$helper = new FacebookRedirectLoginHelper(MY_URL . 'fb_callback/');
$helper = $fb->getRedirectLoginHelper();
// We do want to publish to the user's wall!
$scope = array('publish_actions');
$fb_login_url = $helper->getReRequestUrl($scope);
$fb_login_url = $helper->getReRequestUrl(MY_URL . 'fb_callback/', $scope);
Flight::render('rerequest_permission', array(
'fburl' => $fb_login_url,
));
Expand All @@ -121,20 +119,21 @@ function handle_rerequest_permission() {
// In the FB callback, we show a form to the user
// or an error message if something went wrong.
function handle_fb_callback() {
$fb = Flight::get('fb');
render_boilerplate();
$helper = new FacebookRedirectLoginHelper(MY_URL . 'fb_callback/');
$helper = $fb->getRedirectLoginHelper();
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
$accessToken = $helper->getAccessToken();
} catch(FacebookSDKException $ex) {
// When Facebook returns an error
Flight::error($ex);
} catch(\Exception $ex) {
// When validation fails or other local issues
Flight:error($ex);
}
if ($session) {
$_SESSION['FBTOKEN'] = $session->getToken();
if (check_permissions($session)) {
if (isset($accessToken)) {
$_SESSION['FBTOKEN'] = (string) $accessToken;
if (check_permissions($accessToken)) {
$_SESSION['FB_CHECKIN_NONCE'] = make_nonce();
Flight::render('fb_callback', array(
'post_action' => MY_URL .'checkin',
Expand All @@ -160,6 +159,7 @@ function handle_fb_callback() {
}

function handle_checkin() {
$fb = Flight::get('fb');
render_boilerplate();
// This happens if we unset the nonce below.
// Or if the nonce was never set, in which case the user
Expand Down Expand Up @@ -195,29 +195,22 @@ function handle_checkin() {
if (empty($token)) {
Flight::error(new Exception('No FB token in session!'));
}
$session = new FacebookSession($token);
$message = Flight::request()->query->message;

$config = array('place' => PAGE_ID);
if (! empty($message)) {
$config['message'] = $message;
}
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
$config
);
// Some exceptions can be caught and handled sanely,
// e.g. Duplicate status message (506)
try {
$response = $request->execute()->getGraphObject();
} catch (FacebookRequestException $ex) {
$response = $fb->post('/me/feed', $config, $token);
} catch (FacebookResponseException $ex) {
Flight::error($ex);
} catch (\Exception $ex) {
Flight::error($ex);
}
$postid = $response->asArray()['id'];
$postid = $response->getGraphNode()->getField('id');
$posturl = 'https://www.facebook.com/' . $postid;
Flight::render('checkin',
array(
Expand All @@ -227,17 +220,17 @@ function handle_checkin() {
}

function fblogin() {

$fb = Flight::get('fb');
// Simplification: always assume we are not logged in!
$helper = new FacebookRedirectLoginHelper(MY_URL . 'fb_callback/');
$helper = $fb->getRedirectLoginHelper();
// We do want to publish to the user's wall!
// Note: Facebook docs state that login and write permission request
// should be two separate requests.
// The code is already set up to handle this separately, but I believe
// the combined flow provides better UX.
// https://developers.facebook.com/docs/facebook-login/permissions/v2.2
$scope = array('publish_actions');
$fb_login_url = $helper->getLoginUrl($scope);
$fb_login_url = $helper->getLoginUrl(MY_URL . 'fb_callback/', $scope);
$code_login_url = MY_URL . 'access_code/';
Flight::render('login', array(
'fburl' => $fb_login_url,
Expand Down

0 comments on commit 43cc708

Please sign in to comment.