/** * Gets a FacebookSession from the cookies/params set by the Facebook * JavaScript SDK. * * @return FacebookSession|null */ public function getSession() { if ($signedRequest = $this->getSignedRequest()) { return FacebookSession::newSessionFromSignedRequest($signedRequest); } return null; }
/** * Gets a FacebookSession from the cookies/params set by the Facebook * JavaScript SDK. * * @return FacebookSession|null */ public function getSession() { $cookieName = 'fbsr_' . $this->appId; if (isset($_COOKIE[$cookieName])) { return FacebookSession::newSessionFromSignedRequest($_COOKIE[$cookieName]); } return null; }
/** * Gets a FacebookSession from the parameters passed by Facebook to a * Canvas POST request. * * @return FacebookSession|null */ public function getSession() { if ($signedRequest = $this->getSignedRequest()) { try { return FacebookSession::newSessionFromSignedRequest($signedRequest); } catch (FacebookSDKException $ex) { // Signed request is valid but user is not logged in. if ($ex->getCode() == 603) { return null; } throw $ex; } } return null; }
/** * Gets a FacebookSession from the parameters passed by Facebook to a * Canvas POST request. * * @return FacebookSession */ public function getSession() { /** * v2.0 apps use GET for Canvas signed requests. */ if (isset($_GET['signed_request'])) { return FacebookSession::newSessionFromSignedRequest($_GET['signed_request']); } /** * v1.0 apps use POST for Canvas signed requests, will eventually be * deprecated. */ if (isset($_POST['signed_request'])) { return FacebookSession::newSessionFromSignedRequest($_POST['signed_request']); } return null; }
/** * Handles a response from Facebook, including a CSRF check, and returns a * FacebookSession. * * @return FacebookSession|null */ public function getSessionFromRedirect() { $this->loadState(); if ($this->isValidRedirect()) { $params = array('client_id' => FacebookSession::_getTargetAppId($this->appId), 'redirect_uri' => $this->redirectUrl, 'client_secret' => FacebookSession::_getTargetAppSecret($this->appSecret), 'code' => $this->getCode()); $response = (new FacebookRequest(FacebookSession::newAppSession($this->appId, $this->appSecret), 'GET', '/oauth/access_token', $params))->execute()->getResponse(); if (isset($response['access_token'])) { return new FacebookSession($response['access_token']); } } return null; }
/** * FacebookRequest - Returns a new request using the given session. optional * parameters hash will be sent with the request. This object is * immutable. * * @param FacebookSession $session * @param string $method * @param string $path * @param array|null $parameters * * @return FacebookRequest */ public function __construct($session, $method, $path, $parameters = null, $version = null) { $this->session = $session; $this->method = $method; $this->path = $path; if ($version) { $this->version = $version; } else { $this->version = static::GRAPH_API_VERSION; } $params = $parameters ?: array(); if ($session) { $params["access_token"] = $session->getToken(); } $this->params = $params; return $this; }
/** * Handles a response from Facebook, including a CSRF check, and returns a * FacebookSession. * * @return FacebookSession|null */ public function getSessionFromRedirect() { if ($this->isValidRedirect()) { $params = array('client_id' => FacebookSession::_getTargetAppId($this->appId), 'redirect_uri' => $this->redirectUrl, 'client_secret' => FacebookSession::_getTargetAppSecret($this->appSecret), 'code' => $this->getCode()); $response = (new FacebookRequest(FacebookSession::newAppSession($this->appId, $this->appSecret), 'GET', '/oauth/access_token', $params))->execute()->getResponse(); // Graph v2.3 and greater return objects on the /oauth/access_token endpoint $accessToken = null; if (is_object($response) && isset($response->access_token)) { $accessToken = $response->access_token; } elseif (is_array($response) && isset($response['access_token'])) { $accessToken = $response['access_token']; } if (isset($accessToken)) { return new FacebookSession($accessToken); } } return null; }
public function fb_login() { echo "sdxcxcfsdf"; echo "i am here"; $this->layout = 'ajax'; FacebookSession::setDefaultApplication('195891030745689', '130d8f7bcae67a9a71d2c1c372896814'); $helper = new FacebookRedirectLoginHelper(FACEBOOK_REDIRECT_URI); echo "======="; $session = $helper->getSessionFromRedirect(); if (isset($_SESSION['token'])) { pr($_SESSION); echo "<<<<<<>>>>>>"; $session = new FacebookSession($_SESSION['token']); try { echo "XXXXXXXXXX"; $session->validate(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET); } catch (FacebookAuthorizationException $e) { echo $e->getMessage(); } } $data = array(); $fb_data = array(); if (isset($session)) { $_SESSION['token'] = $session->getToken(); $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::className()); $fb_data = $graph->asArray(); $id = $graph->getId(); $image = "https://graph.facebook.com/" . $id . "/picture?width=100"; if (!empty($fb_data)) { $result = $this->User->findByEmail($fb_data['email']); if (!empty($result)) { if ($this->Auth->login($result['User'])) { $this->Session->setFlash(FACEBOOK_LOGIN_SUCCESS, 'default', array('class' => 'message success'), 'success'); //$this->redirect(BASE_PATH); } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'login'); } } else { $data['email'] = $fb_data['email']; $data['first_name'] = $fb_data['first_name']; $data['social_id'] = $fb_data['id']; $data['picture'] = $image; $data['uuid'] = String::uuid(); $this->User->save($data); if ($this->User->save($data)) { $data['id'] = $this->User->getLastInsertID(); if ($this->Auth->login($data)) { $this->Session->setFlash(FACEBOOK_LOGIN_SUCCESS, 'default', array('class' => 'message success'), 'success'); //$this->redirect(BASE_PATH); } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'index'); } } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'index'); } } } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'index'); } } }
/** * Generate and return the appsecret_proof value for an access_token * * @param string $token * * @return string */ public function getAppSecretProof($token) { return hash_hmac('sha256', $token, FacebookSession::_getTargetAppSecret()); }
<?php //Namespace for facebook namespace Facebook; include DIR_LIB . "Facebook/autoload.php"; FacebookSession::setDefaultApplication(APP_ID, APP_SECRET);
/** * Parses a signed request. * * @param string $signedRequest * * @return array * * @throws FacebookSDKException */ private function parseSignedRequest($signedRequest) { if (strpos($signedRequest, '.') !== false) { list($encodedSig, $encodedData) = explode('.', $signedRequest, 2); $sig = FacebookSession::_base64UrlDecode($encodedSig); $data = json_decode(FacebookSession::_base64UrlDecode($encodedData), true); if (isset($data['algorithm']) && $data['algorithm'] === 'HMAC-SHA256') { $expectedSig = hash_hmac('sha256', $encodedData, FacebookSession::_getTargetAppSecret(), true); if (strlen($sig) !== strlen($expectedSig)) { throw new FacebookSDKException('Invalid signature on signed request.', 602); } $validate = 0; for ($i = 0; $i < strlen($sig); $i++) { $validate |= ord($expectedSig[$i]) ^ ord($sig[$i]); } if ($validate !== 0) { throw new FacebookSDKException('Invalid signature on signed request.', 602); } return $data; } else { throw new FacebookSDKException('Invalid signed request, using wrong algorithm.', 605); } } else { throw new FacebookSDKException('Malformed signed request.', 606); } }
/** * setDefaultApplication - Will set the static default appId and appSecret * to be used for API requests. * * @param string $appId Application ID to use by default * @param string $appSecret App secret value to use by default */ public static function setDefaultApplication($appId, $appSecret) { self::$defaultAppId = $appId; self::$defaultAppSecret = $appSecret; }
/** * Handles a response from Facebook, including a CSRF check, and returns a * FacebookSession. * * @return FacebookSession|null */ public function getSessionFromRedirect() { $this->loadState(); if ($this->isValidRedirect()) { $params = array('client_id' => FacebookSession::_getTargetAppId($this->appId), 'redirect_uri' => $this->redirectUrl, 'client_secret' => FacebookSession::_getTargetAppSecret($this->appSecret), 'code' => $this->getCode()); $fbRequest = new FacebookRequest(FacebookSession::newAppSession($this->appId, $this->appSecret), 'GET', '/oauth/access_token', $params); $response = $fbRequest->execute()->getResponse(); // echo __METHOD__ . __LINE__ . " Facebook Authentication response <br><pre>";var_dump($response);echo "</pre><br>"; if (isset($response['access_token'])) { return new FacebookSession($response['access_token']); } } return null; }
</script> <body> <?php //define('FACEBOOK_SDK_V4_SRC_DIR', 'src/Facebook/'); require 'autoload.php'; require_once 'src/Facebook/FacebookSession.php'; FacebookSession::setDefaultApplication('1570515483215478', '3b396bc3f2f9839c280fedabca2cfe25'); ?> <!-- BEGIN wrapper --> <div id="wrapper"> <!-- BEGIN header --> <div id="header"> <ul> <li></li> </ul> <p> </p> <table width="930" border="0" align="center" cellspacing="1"> <tr>
protected function action() { //Init app with app id and secret FacebookSession::setDefaultApplication('416201525206070', 'db946f783550299dcf77faf9f09a8f41'); //Login helper with redirect_uri $helper = new FacebookRedirectLoginHelper('http://www.ituk.it/cinderella/fblogin'); try { $session = $helper->getSessionFromRedirect(); } catch (FacebookRequestException $ex) { //Facebook returns an error Session::setObj(Session::SYSMSG, 'Errore durante l\'autenticazione con Facebook, per favore riprovare più tardi.'); $this->redirect = 'message'; exit; } catch (Exception $ex) { //Validation fails or other local issues Session::setObj(Session::SYSMSG, 'Errore durante l\'autenticazione con Facebook, per favore riprovare più tardi.'); $this->redirect = 'message'; exit; } //See if we have a session if (isset($session)) { //Graph api request for user data $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); //Get response $graphObject = $response->getGraphObject(); $fbid = $graphObject->getProperty('id'); // To Get Facebook ID $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name $fbfirstname = $graphObject->getProperty('first_name'); // To Get Facebook first name $fblastname = $graphObject->getProperty('last_name'); // To Get Facebook last name $fbemail = $graphObject->getProperty('email'); // To Get Facebook email ID //Check if already logged in with Facebook //Create new Facebook user $fb_user = new UtenteFb($fbid); if (!$fb_user->hereIam()) { //Save credentials as regular user $objDateTime = new DateTime('NOW'); $fakepassw = md5($objDateTime->format('c')); $fakeemail = $fakepassw . '@facebook.com'; $fb_user->setNome($fbfirstname); $fb_user->setCognome($fblastname); $fb_user->setEmail(isset($fbemail) && strlen($fbemail) > 0 ? $fbemail : $fakeemail); $fb_user->setUsername('FacebookUser'); $fb_user->set_password($fakepassw); $fb_user->save(); //Save as Facebook user $fb_user->setFbId($fbid); $fb_user->setAvatarUrl($fb_user->getAvatarUrl()); //Save Facebook credentials $fb_user->saveAsFbUser(); } //Login user Session::setObj(Session::UTENTE, $fb_user); Session::setObj(Session::SYSMSG, 'Benvenuto ' . $fb_user->getNome() . ' ' . $fb_user->getCognome()); $this->redirect = 'message'; } else { //Try Facebook Authentication $loginUrl = $helper->getLoginUrl(); $this->redirect = $loginUrl; } }