/** * Grab the user's ungrabbed posts from Facebook * * @param \yii\authclient\BaseOAuth $client */ public static function grabSocial(\yii\authclient\BaseOAuth $client) { if ($client->getId() === 'facebook') { $token = $client->getAccessToken(); \Facebook\FacebookSession::enableAppSecretProof(false); $session = new \Facebook\FacebookSession($token->getToken()); $request = new \Facebook\FacebookRequest($session, 'GET', "/me/posts"); $response = $request->execute()->getResponse(); $posts = $response->data; foreach ($posts as $post) { if (!Fbposts::find()->where('post_id = :pid', ['pid' => $post->id])->exists()) { $fbpost = new Fbposts(['user_id' => Yii::$app->user->id, 'post_id' => $post->id, 'message' => isset($post->message) ? $post->message : $post->story, 'created_time' => $post->created_time]); $fbpost->save(); } } } }
/** * @return false|Model\User */ public function getUserData() { try { $session = new \Facebook\FacebookSession($this->getAccessToken()); $request = new \Facebook\FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $userData = $response->getResponse(); $userModel = new Model\User(); $userModel->setId($userData->id); $userModel->setEmail($userData->email); $userModel->setName($userData->name); $userModel->setGender($userData->gender); return $userModel; } catch (\Exception $e) { return false; } }
<?php // WPCOM: Don't load the facebook lib within the context of WP_CLI to prevent fatals // from incompatibilities with the autoloader WP_CLI uses. if (defined('WP_CLI') && WP_CLI) { return; } require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/src/FacebookWPHttpClient.php'; Facebook\FacebookRequest::setHttpClientHandler(new FacebookWPHttpClient());
/** * @param $path * @param $options * @return mixed|Facebook\GraphObject * @throws Exception * @throws Facebook\FacebookApiException * @throws Facebook\FacebookRequestException */ public function postPhoto($path, $options) { //$source = '@'.realpath($source); try { $session = $this->getSession(); /*$requestParameters = array( 'access_token' => $session->getAccessToken(), //'message' => $message, //'image' => $source, 'source' => $source, //basename($source) => $source ); if(count($options) > 0){ foreach($options as $k => $v){ $requestParameters[$k] = $v; } }*/ //$this->facebook->setFileUploadSupport(true); //$response = $this->facebook->api($path, 'POST', $requestParameters); $param = array('source' => 'url', 'message' => 'message'); $data = array(); foreach ($param as $key => $val) { if (isset($options[$key])) { $data[$val] = $options[$key]; } } $session = new Facebook\FacebookSession($session->getAccessToken()); $req = new Facebook\FacebookRequest($session, 'POST', $path, $data); $response = $req->execute()->getGraphObject(); } catch (Facebook\FacebookApiException $e) { throw $e; } return $response; }
/** * @return string */ public function FacebookPopupService() { $sResult = ''; $sLoginUrl = ''; $sSocialName = ''; $mData = false; $sUserData = ''; $aUserData = false; $bLogin = false; $iErrorCode = \RainLoop\Notifications::UnknownError; if (0 === \strlen($this->oActions->GetSpecAuthToken()) && $this->oHttp->HasQuery('rlah')) { $this->oActions->SetSpecAuthToken($this->oHttp->GetQuery('rlah', '')); } $oAccount = $this->oActions->GetAccount(); $oFacebook = $this->FacebookConnector($oAccount); if ($oFacebook) { try { $oSession = $oFacebook->getSessionFromRedirect(); if (!$oSession && !$this->oHttp->HasQuery('state')) { $sLoginUrl = $oFacebook->getLoginUrl() . '&display=popup'; } else { if ($oSession) { $oRequest = new \Facebook\FacebookRequest($oSession, 'GET', '/me'); $oResponse = $oRequest->execute(); $oGraphObject = $oResponse->getGraphObject(); $mData = $oGraphObject->getProperty('id'); $sSocialName = $oGraphObject->getProperty('name'); if ($oAccount) { if ($mData && 0 < \strlen($mData)) { $aUserData = array('Email' => $oAccount->Email(), 'Password' => $oAccount->Password()); $oSettings = $this->oActions->SettingsProvider()->Load($oAccount); $oSettings->SetConf('FacebookSocialName', $sSocialName); $oSettings->SetConf('FacebookAccessToken', \RainLoop\Utils::EncodeKeyValues(array('id' => $mData))); $this->oActions->SettingsProvider()->Save($oAccount, $oSettings); $this->oActions->StorageProvider()->Put(null, \RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY, $this->FacebookUserLoginStorageKey($oFacebook, $mData), \RainLoop\Utils::EncodeKeyValues($aUserData)); $iErrorCode = 0; } } else { $bLogin = true; if ($mData && 0 < \strlen($mData)) { $sUserData = $this->oActions->StorageProvider()->Get(null, \RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY, $this->FacebookUserLoginStorageKey($oFacebook, $mData)); if ($sUserData) { $aUserData = \RainLoop\Utils::DecodeKeyValues($sUserData); } } if ($aUserData && \is_array($aUserData) && !empty($aUserData['Email']) && isset($aUserData['Password'])) { $oAccount = $this->oActions->LoginProcess($aUserData['Email'], $aUserData['Password']); if ($oAccount instanceof \RainLoop\Account) { $this->oActions->AuthProcess($oAccount); $iErrorCode = 0; } } else { $iErrorCode = \RainLoop\Notifications::SocialFacebookLoginAccessDisable; } } } } } catch (\Exception $oException) { $this->oActions->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); } } if ($sLoginUrl) { $this->oActions->Location($sLoginUrl); } else { $this->oHttp->ServerNoCache(); @\header('Content-Type: text/html; charset=utf-8'); $sCallBackType = $bLogin ? '_login' : ''; $sConnectionFunc = 'rl_' . \md5(\RainLoop\Utils::GetConnectionToken()) . '_facebook' . $sCallBackType . '_service'; $sResult = '<script type="text/javascript" data-cfasync="false">opener && opener.' . $sConnectionFunc . ' && opener.' . $sConnectionFunc . '(' . $iErrorCode . '); self && self.close && self.close();</script>'; } return $sResult; }
public function getFriends($link) { if (!$link) { return []; } $request = new \Facebook\FacebookRequest($this->session, 'GET', $link); $response = $request->execute(); $f = $response->getGraphObject()->asArray(); return $f; }
/** * Similar to onAuthenticate, except we already have a logged in user, we're just linking accounts * * @param array $options * @return void */ public function link($options = array()) { // Set up the config for the sdk instance $config = array('appId' => $this->params->get('app_id'), 'secret' => $this->params->get('app_secret')); // Set defaults \Facebook\FacebookSession::setDefaultApplication($config['appId'], $config['secret']); $helper = new \Facebook\FacebookRedirectLoginHelper(self::getReturnUrl($options['return'])); try { $session = $helper->getSessionFromRedirect(); } catch (\Facebook\FacebookRequestException $ex) { // When Facebook returns an error } catch (\Exception $ex) { // When validation fails or other local issues } // Make sure we have a user_id (facebook returns 0 for a non-logged in user) if (isset($user_id) && $user_id > 0 || isset($session) && $session) { try { $request = new \Facebook\FacebookRequest($session, 'GET', '/me'); $user_profile = $request->execute()->getGraphObject(\Facebook\GraphUser::className()); $id = $user_profile->getId(); $email = $user_profile->getProperty('email'); } catch (\Facebook\FacebookRequestException $e) { // Error message? $response->status = \Hubzero\Auth\Status::FAILURE; $response->error_message = Lang::txt('PLG_AUTHENTICATION_FACEBOOK_ERROR_RETRIEVING_PROFILE', $e->getMessage()); return; } $hzad = \Hubzero\Auth\Domain::getInstance('authentication', 'facebook', ''); // Create the link if (\Hubzero\Auth\Link::getInstance($hzad->id, $id)) { // This facebook account is already linked to another hub account App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_FACEBOOK_ACCOUNT_ALREADY_LINKED'), 'error'); } else { $hzal = \Hubzero\Auth\Link::find_or_create('authentication', 'facebook', null, $id); $hzal->user_id = User::get('id'); $hzal->email = $email; $hzal->update(); } } else { // User didn't authorize our app, or, clicked cancel App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_FACEBOOK_MUST_AUTHORIZE_TO_LINK', Config::get('sitename')), 'error'); } }
if (Phpfox::isUser() && $Url->uri() == '/user/setting/' && substr(Phpfox::getUserBy('email'), -3) == '@fb') { (new Core\Route('/user/setting'))->run(function (\Core\Controller $Controller) { return $Controller->render('setting.html'); }); } /** * Controller for the FB login routine */ (new Core\Route('/fb/login'))->run(function (\Core\Controller $Controller) { $helper = new Facebook\FacebookRedirectLoginHelper($Controller->url->make('/fb/auth')); $loginUrl = $helper->getLoginUrl(); header('Location: ' . $loginUrl); exit; }); /** * Auth routine for FB Connect. This is where we either create the new user or log them in if they are already a user. */ (new Core\Route('/fb/auth'))->run(function (\Core\Controller $Controller) { $helper = new Facebook\FacebookRedirectLoginHelper($Controller->url->make('/fb/auth')); $session = $helper->getSessionFromRedirect(); if ($session) { $request = new Facebook\FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $user = $response->getGraphObject(Facebook\GraphUser::className()); if ($user instanceof Facebook\GraphUser) { $Service = new \Apps\PHPfox_Facebook\Model\Service(); $Service->create($user); $Controller->url->send('/'); } } });
protected function apiPost($call, $data, $type = 'Facebook\\GraphObject') { $request = new Facebook\FacebookRequest($this->fb_session, 'POST', $call, $data, self::FBVERSION); $response = $request->execute(); $object = $response->getGraphObject($type); return $object; }
// 'client_id' => $app_id, // 'redirect_uri' => $my_url, // 'client_secret' => $app_secret, // 'code' => $code, // ); $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&redirect_uri=" . $my_url . "&client_secret=" . $app_secret . "&code=" . $code; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); $acces_token = $params['access_token']; // If you already have a valid access token: $session = new \Facebook\FacebookSession($acces_token); //var_dump($session); // To validate the session: try { $user_profile = new \Facebook\FacebookRequest($session, 'GET', '/me'); $res = $user_profile->execute(); $graphUser = \Facebook\GraphUser::className(); $user_profile = $res->getGraphObject($graphUser); $email = $user_profile->getEmail(); if ($email) { $UserFacebookQuery = new UserFacebookQuery($crud); $UserFacebookQuery->filterByEmail($email); $UserFacebookQuery->find(); $UserFacebookEntity = $UserFacebookQuery->_getEntity(); // L'utilisateur existe if ($UserFacebookEntity) { $UserFacebookEntity->setAccessToken($acces_token); $UserFacebookEntity->setDateModification(time()); $UserFacebookEntity->save(); $_SESSION['oauth2']['facebook'] = $acces_token;
/** * Get page information by page id * * $data['pageID']= page id * $data['token'] = user token * @param $data * @return array */ public function getPageInformation($data) { $session = new \Facebook\FacebookSession($data['token']); if ($session) { try { $request = new \Facebook\FacebookRequest($session, 'GET', '/' . $data['pageID']); $response = $request->execute()->getGraphObject()->asArray(); $response['status'] = true; $result = array('link' => $response['link'], 'location' => $response['location'], 'name' => $response['name']); return $result; } catch (\Facebook\FacebookRequestException $e) { $response['exceptionCode'] = $e->getCode(); $response['exceptionMessage'] = $e->getMessage(); $response['status'] = false; return $response; } } }
public function getMe($session) { if (!$session) { return false; } // Request for user data try { $request = new Facebook\FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); // Responce $data = $response->getGraphObject(); if ($data) { return array('data' => $data, 'uid' => $data->getProperty("id")); } } catch (Exception $e) { $this->core->message($e->getMessage(), "Facebook Exception getMe()", 'red'); } return false; }
/** * @param array $params * @param array $queryParams * * @return string * @throws \Facebook\FacebookRequestException */ protected function retrieveNumberOfSubscribers(array $params = array(), array $queryParams = array()) { $request = new \Facebook\FacebookRequest($this->session, 'GET', sprintf('/%s?fields=likes', $params['user_id'])); $client = $request->getHttpClientHandler(); $client->addRequestHeader('Accept-Language', 'fr-FR,fr;q=0.8'); $request->setHttpClientHandler($client); $response = $request->execute(); return (string) $response->getResponse()->likes; }
public function getGraph() { $request = new \Facebook\FacebookRequest($this->session, 'GET', '/me'); $response = $request->execute(); return $response->getGraphObject(); }