/**
  * Send a request to the OAuth endpoint.
  *
  * @param array $params
  *
  * @return AccessToken
  *
  * @throws ModomuSDKException
  */
 protected function requestAnAccessToken(array $params)
 {
     $this->lastRequest = new ModomuRequest($this->app, $this->app->getAccessToken(), 'POST', '/oauth2/token', $params);
     $response = $this->client->sendRequest($this->lastRequest);
     $data = $response->getDecodedBody();
     if (!isset($data['access_token'])) {
         throw new ModomuSDKException('Access token was not returned from Graph.', 401);
     }
     // Graph returns two different key names for expiration time
     // on the same endpoint. Doh! :/
     $expiresAt = 0;
     if (isset($data['expires'])) {
         // For exchanging a short lived token with a long lived token.
         // The expiration time in seconds will be returned as "expires".
         $expiresAt = time() + $data['expires'];
     } elseif (isset($data['expires_in'])) {
         // For exchanging a code for a short lived access token.
         // The expiration time in seconds will be returned as "expires_in".
         $expiresAt = time() + $data['expires_in'];
     }
     //Si existe signature validar
     /*if (isset($data['signature'])) {
           $msj = $this->app->getId() . '|' . $data['issued_at'];
           if(!self::verify($msj,$data['signature'],$this->app->getSecret())){
               throw new ModomuSDKException("La firma enviada por el servidor no coincide con la esperada", 1);
           }
       }*/
     //Gets access_token
     return new AccessToken($data['access_token'], $expiresAt);
 }
 /**
  * Return an instance of the FacebookClient
  *
  * @param FacebookClientConfig $config
  *
  * @return FacebookClient
  */
 public static function getInstance(FacebookClientConfig $config = null)
 {
     global $fbAppId, $fbAppSecret;
     // If an instance hasn't been created yet, create one
     if (empty(self::$instance)) {
         // See if an alternate config has been passed in
         if (empty($config)) {
             $config = (new FacebookClientConfig())->setAppID($fbAppId)->setAppSecret($fbAppSecret);
         }
         self::$instance = new FacebookClient($config);
     }
     return self::$instance;
 }
Beispiel #3
0
 /**
  * Update User email to become Facebook-reported email
  *
  * @param int $userId
  * @return bool
  */
 public function updateEmailFromFacebook($userId)
 {
     $userMap = \FacebookMapModel::lookupFromWikiaID($userId);
     if (!$userMap) {
         $this->error('Facebook user email update fail. Missing user mapping', ['title' => __METHOD__, 'userid' => $userId]);
         return false;
     }
     $fbUserId = $userMap->getFacebookUserId();
     $email = \FacebookClient::getInstance()->getEmail($fbUserId);
     if (!$email) {
         $this->info('Facebook user email update: No Facebook email', ['title' => __METHOD__, 'userid' => $userId, 'facebookid' => $fbUserId]);
         return false;
     }
     $this->updateUserEmail($userMap->getWikiaUserId(), $email);
     $this->info('Facebook user email update complete', ['title' => __METHOD__, 'userid' => $userId, 'facebookid' => $fbUserId, 'email' => $email]);
     return true;
 }
 /**
  * Map an existing Wikia user to a Facebook id
  * If an exact or partial match of the map already exists, OR
  * if creation of mapping does not succeed, returns not-OK Status
  *
  * @param int $wikiaUserId
  * @param int $fbUserId
  * @return \Status (status value will be null or \FacebookMapModel if successfully created)
  */
 public function connectToFacebook($wikiaUserId, $fbUserId)
 {
     $status = new Status();
     try {
         $map = \FacebookMapModel::getUserMapping($wikiaUserId, $fbUserId);
         if ($map) {
             // Error! There is already a mapping
             $status->setResult(false);
             $status->error('fbconnect-error-already-connected');
         } else {
             $bizToken = \FacebookClient::getInstance()->getBizToken();
             $map = \FacebookMapModel::createUserMapping($wikiaUserId, $fbUserId, $bizToken);
             if ($map instanceof \FacebookMapModel) {
                 $status->setResult(true, $map);
             } else {
                 $status->setResult(false);
                 $status->error('fbconnect-error');
             }
         }
     } catch (\Exception $e) {
         $messageParams = [];
         switch ($e->getCode()) {
             case \FacebookMapModel::ERROR_WIKIA_USER_ID_MISMATCH:
                 $messageParams[] = 'fbconnect-error-fb-account-in-use';
                 $messageParams[] = \User::whoIs($wikiaUserId);
                 break;
             case \FacebookMapModel::ERROR_FACEBOOK_USER_ID_MISMATCH:
                 $messageParams[] = 'fbconnect-error-already-connected';
                 break;
             default:
                 $messageParams[] = 'fbconnect-error';
         }
         $status->setResult(false);
         call_user_func_array([$status, 'error'], $messageParams);
     }
     return $status;
 }
 /**
  * Ajax endpoint for connecting a logged in Wikia user to a Facebook account.
  * By the time they get here they should already have logged into Facebook and have a Facebook user ID.
  */
 public function connectLoggedInUser()
 {
     $wg = F::app()->wg;
     $fb = FacebookClient::getInstance();
     $fbUserId = $fb->getUserId();
     // The user must be logged into Facebook and Wikia
     if (!$fbUserId || !$wg->User->isLoggedIn()) {
         $this->status = 'error';
         return;
     }
     // Create user mapping
     $status = $this->fbClientFactory->connectToFacebook($wg->User->getId(), $fbUserId);
     if (!$status->isGood()) {
         list($message, $params) = $this->fbClientFactory->getStatusError($status);
         $this->setErrorResponse($message, $params);
         return;
     }
     $this->status = 'ok';
     \FacebookClientHelper::track('facebook-link-existing');
 }
 /**
  * Get Facebook account ID of currently logged-in user.
  *
  * If no user is logged in, then an ID of 0 is returned.
  */
 private function getFacebookUserId()
 {
     return FacebookClient::getInstance()->getUserId();
 }
 public static function onUserLogout(&$user)
 {
     // Clean up any facebook cookies/data
     FacebookClient::getInstance()->logout();
     return true;
 }
 /**
  * Save a user's gender from facebook if one is available. Facebook will return the
  * following values for a user's gender: 'male', 'female', null.
  * @param User $user
  */
 private function saveUserGender(User $user)
 {
     $fbUserInfo = FacebookClient::getInstance()->getUserInfo();
     $gender = $fbUserInfo->getProperty('gender');
     if (!is_null($gender)) {
         $user->setGlobalAttribute('gender', $gender);
         $user->saveSettings();
     }
 }