From 43cc7083bc22eefb0c9f850de14b154f933c1d8f Mon Sep 17 00:00:00 2001 From: Aristotelis Dossas Date: Fri, 18 Nov 2016 21:56:53 +0200 Subject: [PATCH] Update fb_handlers to use fb api v5.4.2 --- handlers/fb_handlers.php | 83 ++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/handlers/fb_handlers.php b/handlers/fb_handlers.php index e94455d..41d2760 100644 --- a/handlers/fb_handlers.php +++ b/handlers/fb_handlers.php @@ -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'); @@ -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); @@ -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, )); @@ -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', @@ -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 @@ -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( @@ -227,9 +220,9 @@ 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. @@ -237,7 +230,7 @@ function fblogin() { // 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,