Exemple #1
0
 public function actionTestFacebook()
 {
     if (!XenForo_Application::get('options')->facebookAppId) {
         $group = $this->getModelFromCache('XenForo_Model_Option')->getOptionGroupById('facebook');
         $url = XenForo_Link::buildAdminLink('options/list', $group);
         return $this->responseError(new XenForo_Phrase('to_test_facebook_integration_must_enter_application_info', array('url' => $url)));
     }
     $fbRedirectUri = XenForo_Link::buildAdminLink('canonical:tools/test-facebook', false, array('x' => '?/&=', 'y' => 2));
     if ($this->_input->filterSingle('test', XenForo_Input::UINT)) {
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL, XenForo_Helper_Facebook::getFacebookRequestUrl($fbRedirectUri));
     }
     $info = false;
     $userToken = false;
     $code = $this->_input->filterSingle('code', XenForo_Input::STRING);
     if ($code) {
         $token = XenForo_Helper_Facebook::getAccessTokenFromCode($code, $fbRedirectUri);
         $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($token, 'access_token');
         if ($fbError) {
             return $this->responseError($fbError);
         }
         $userToken = $token['access_token'];
         $info = XenForo_Helper_Facebook::getUserInfo($userToken);
         $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($info, 'id');
         if ($fbError) {
             return $this->responseError($fbError);
         }
     }
     $viewParams = array('fbInfo' => $info, 'userToken' => $userToken);
     return $this->responseView('XenForo_ViewAdmin_Tools_TestFacebook', 'tools_test_facebook', $viewParams);
 }
Exemple #2
0
 /**
  * Displays a form to join using Facebook or logs in an existing account.
  *
  * @return XenForo_ControllerResponse_Abstract
  */
 public function actionFacebook()
 {
     $assocUserId = $this->_input->filterSingle('assoc', XenForo_Input::UINT);
     $redirect = $this->_getExternalAuthRedirect();
     $fbRedirectUri = XenForo_Link::buildPublicLink('canonical:register/facebook', false, array('assoc' => $assocUserId ? $assocUserId : false));
     if ($this->_input->filterSingle('reg', XenForo_Input::UINT)) {
         XenForo_Application::getSession()->set('loginRedirect', $redirect);
         XenForo_Application::getSession()->remove('fbToken');
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL, XenForo_Helper_Facebook::getFacebookRequestUrl($fbRedirectUri));
     }
     $fbToken = $this->_input->filterSingle('t', XenForo_Input::STRING);
     if (!$fbToken) {
         $fbToken = XenForo_Application::getSession()->get('fbToken');
     }
     if (!$fbToken) {
         $error = $this->_input->filterSingle('error', XenForo_Input::STRING);
         if ($error == 'access_denied') {
             return $this->responseError(new XenForo_Phrase('you_did_not_grant_permission_to_access_external_account'));
         }
         $code = $this->_input->filterSingle('code', XenForo_Input::STRING);
         if (!$code) {
             return $this->responseError(new XenForo_Phrase('error_occurred_while_connecting_with_facebook'));
         }
         $state = $this->_input->filterSingle('state', XenForo_Input::STRING);
         $session = XenForo_Application::getSession();
         if (!$state || !$session->get('fbCsrfState') || $state !== $session->get('fbCsrfState')) {
             return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, XenForo_Link::buildPublicLink('canonical:index'));
         }
         $token = XenForo_Helper_Facebook::getAccessTokenFromCode($code, $fbRedirectUri);
         $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($token, 'access_token');
         if ($fbError) {
             return $this->responseError(new XenForo_Phrase('error_occurred_while_connecting_with_facebook'));
         }
         $fbToken = $token['access_token'];
     }
     $fbUser = XenForo_Helper_Facebook::getUserInfo($fbToken);
     $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($fbUser, 'id');
     if ($fbError) {
         return $this->responseError(new XenForo_Phrase('error_occurred_while_connecting_with_facebook'));
     }
     $userModel = $this->_getUserModel();
     $userExternalModel = $this->_getUserExternalModel();
     $fbAssoc = $userExternalModel->getExternalAuthAssociation('facebook', $fbUser['id']);
     if ($fbAssoc && $userModel->getUserById($fbAssoc['user_id'])) {
         $userExternalModel->updateExternalAuthAssociationExtra($fbAssoc['user_id'], 'facebook', array('token' => $fbToken));
         $redirect = XenForo_Application::getSession()->get('loginRedirect');
         if (!$redirect) {
             $redirect = $this->getDynamicRedirect(false, false);
         }
         XenForo_Helper_Facebook::setUidCookie($fbUser['id']);
         /** @var XenForo_ControllerHelper_Login $loginHelper */
         $loginHelper = $this->getHelper('Login');
         $loginHelper->tfaRedirectIfRequiredPublic($fbAssoc['user_id'], $redirect, true);
         $visitor = XenForo_Visitor::setup($fbAssoc['user_id']);
         XenForo_Application::getSession()->userLogin($fbAssoc['user_id'], $visitor['password_date']);
         $this->_getUserModel()->setUserRememberCookie($fbAssoc['user_id']);
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, $redirect);
     }
     XenForo_Helper_Facebook::setUidCookie(0);
     parent::_assertBoardActive('facebook');
     if (empty($fbUser['email'])) {
         return $this->responseError(new XenForo_Phrase('facebook_did_not_provide_email'));
     }
     $existingUser = false;
     $emailMatch = false;
     if (XenForo_Visitor::getUserId()) {
         $existingUser = XenForo_Visitor::getInstance();
     } else {
         if ($assocUserId) {
             $existingUser = $userModel->getUserById($assocUserId);
         }
     }
     if (!$existingUser) {
         $existingUser = $userModel->getUserByEmail($fbUser['email']);
         $emailMatch = true;
     }
     $viewName = 'XenForo_ViewPublic_Register_Facebook';
     $templateName = 'register_facebook';
     XenForo_Application::getSession()->set('fbToken', $fbToken);
     if ($existingUser) {
         // must associate: matching user
         return $this->_getExternalRegisterFormResponse($viewName, $templateName, array('associateOnly' => true, 'fbUser' => $fbUser, 'existingUser' => $existingUser, 'emailMatch' => $emailMatch, 'redirect' => $redirect));
     }
     $this->_assertRegistrationActive();
     if (!empty($fbUser['birthday'])) {
         $this->_validateBirthdayString($fbUser['birthday'], 'm/d/y');
     }
     return $this->_getExternalRegisterFormResponse($viewName, $templateName, array('fbUser' => $fbUser, 'redirect' => $redirect, 'showDob' => empty($fbUser['birthday'])));
 }
Exemple #3
0
 /**
  * Displays a form to join using Facebook or logs in an existing account.
  *
  * @return XenForo_ControllerResponse_Abstract
  */
 public function actionFacebook()
 {
     $assocUserId = $this->_input->filterSingle('assoc', XenForo_Input::UINT);
     $redirect = $this->_input->filterSingle('redirect', XenForo_Input::STRING);
     $options = XenForo_Application::get('options');
     $fbRedirectUri = XenForo_Link::buildPublicLink('canonical:register/facebook', false, array('assoc' => $assocUserId ? $assocUserId : false));
     if ($this->_input->filterSingle('reg', XenForo_Input::UINT)) {
         $redirect = XenForo_Link::convertUriToAbsoluteUri($this->getDynamicRedirect());
         $baseDomain = preg_replace('#^([a-z]+://[^/]+).*$#i', '$1', $options->boardUrl);
         if (strpos($redirect, $baseDomain) !== 0) {
             $redirect = XenForo_Link::buildPublicLink('canonical:index');
         }
         XenForo_Application::get('session')->set('fbRedirect', $redirect);
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL, XenForo_Helper_Facebook::getFacebookRequestUrl($fbRedirectUri));
     }
     $fbToken = $this->_input->filterSingle('t', XenForo_Input::STRING);
     if (!$fbToken) {
         $error = $this->_input->filterSingle('error', XenForo_Input::STRING);
         if ($error == 'access_denied') {
             return $this->responseError(new XenForo_Phrase('access_to_facebook_account_denied'));
         }
         $code = $this->_input->filterSingle('code', XenForo_Input::STRING);
         if (!$code) {
             return $this->responseError(new XenForo_Phrase('error_occurred_while_connecting_with_facebook'));
         }
         $token = XenForo_Helper_Facebook::getAccessTokenFromCode($code, $fbRedirectUri);
         $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($token, 'access_token');
         if ($fbError) {
             XenForo_Error::logException(new XenForo_Exception(strval($fbError)));
             return $this->responseError(new XenForo_Phrase('error_occurred_while_connecting_with_facebook'));
         }
         $fbToken = $token['access_token'];
     }
     $fbUser = XenForo_Helper_Facebook::getUserInfo($fbToken);
     $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($fbUser, 'id');
     if ($fbError) {
         XenForo_Error::logException(new XenForo_Exception(strval($fbError)));
         return $this->responseError(new XenForo_Phrase('error_occurred_while_connecting_with_facebook'));
     }
     $userModel = $this->_getUserModel();
     $userExternalModel = $this->_getUserExternalModel();
     $fbAssoc = $userExternalModel->getExternalAuthAssociation('facebook', $fbUser['id']);
     if ($fbAssoc && $userModel->getUserById($fbAssoc['user_id'])) {
         XenForo_Helper_Facebook::setUidCookie($fbUser['id']);
         XenForo_Application::get('session')->changeUserId($fbAssoc['user_id']);
         XenForo_Visitor::setup($fbAssoc['user_id']);
         $redirect = XenForo_Application::get('session')->get('fbRedirect');
         XenForo_Application::get('session')->remove('fbRedirect');
         if (!$redirect) {
             $redirect = $this->getDynamicRedirect(false, false);
         }
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, $redirect);
     }
     XenForo_Helper_Facebook::setUidCookie(0);
     parent::_assertBoardActive('facebook');
     $existingUser = false;
     $emailMatch = false;
     if (XenForo_Visitor::getUserId()) {
         $existingUser = XenForo_Visitor::getInstance();
     } else {
         if ($assocUserId) {
             $existingUser = $userModel->getUserById($assocUserId);
         }
     }
     if (!$existingUser) {
         $existingUser = $userModel->getUserByEmail($fbUser['email']);
         $emailMatch = true;
     }
     if ($existingUser) {
         // must associate: matching user
         return $this->responseView('XenForo_ViewPublic_Register_Facebook', 'register_facebook', array('associateOnly' => true, 'fbToken' => $fbToken, 'fbUser' => $fbUser, 'existingUser' => $existingUser, 'emailMatch' => $emailMatch, 'redirect' => $redirect));
     }
     if (!XenForo_Application::get('options')->get('registrationSetup', 'enabled')) {
         $this->_assertRegistrationActive();
     }
     if (!empty($fbUser['birthday'])) {
         $birthdayParts = explode('/', $fbUser['birthday']);
         if (count($birthdayParts) == 3) {
             list($month, $day, $year) = $birthdayParts;
             $userAge = $this->_getUserProfileModel()->calculateAge($year, $month, $day);
             if ($userAge < intval($options->get('registrationSetup', 'minimumAge'))) {
                 // TODO: set a cookie to prevent re-registration attempts
                 return $this->responseError(new XenForo_Phrase('sorry_you_too_young_to_create_an_account'));
             }
         }
     }
     // give a unique username suggestion
     $i = 2;
     $origName = $fbUser['name'];
     while ($userModel->getUserByName($fbUser['name'])) {
         $fbUser['name'] = $origName . ' ' . $i++;
     }
     return $this->responseView('XenForo_ViewPublic_Register_Facebook', 'register_facebook', array('fbToken' => $fbToken, 'fbUser' => $fbUser, 'redirect' => $redirect, 'timeZones' => XenForo_Helper_TimeZone::getTimeZones(), 'tosUrl' => XenForo_Dependencies_Public::getTosUrl()), $this->_getRegistrationContainerParams());
 }
Exemple #4
0
 public function actionSocialShareAddMoreFacebook()
 {
     $this->assertAdminPermission('option');
     if (!bdSocialShare_Option::hasPermissionFacebook()) {
         return $this->responseError(new XenForo_Phrase('bdsocialshare_facebook_must_be_configured'));
     }
     $inputToken = $this->_input->filterSingle('token', XenForo_Input::STRING);
     /* @var $facebookModel bdSocialShare_Model_Facebook */
     $facebookModel = $this->getModelFromCache('bdSocialShare_Model_Facebook');
     $existingAccounts = $facebookModel->getAccounts();
     if (empty($inputToken)) {
         $inputCode = $this->_input->filterSingle('code', XenForo_Input::STRING);
         $redirectUri = XenForo_Link::buildAdminLink('full:tools/social-share/add-more/facebook');
         if (empty($inputCode)) {
             $requestUrl = XenForo_Helper_Facebook::getFacebookRequestUrl($redirectUri);
             $requestUrl = preg_replace('#&scope=#', '$0publish_actions,manage_pages,user_groups,', $requestUrl);
             return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_UPDATED, $requestUrl);
         }
         $token = XenForo_Helper_Facebook::getAccessTokenFromCode($inputCode, $redirectUri);
         $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($token, 'access_token');
         if (!empty($fbError)) {
             return $this->responseError($fbError);
         }
         $fbToken = $token['access_token'];
     } else {
         $fbToken = $inputToken;
     }
     if ($this->isConfirmedPost()) {
         $accounts = $this->_input->filterSingle('accounts', XenForo_Input::ARRAY_SIMPLE);
         $newAccounts = $existingAccounts;
         foreach ($accounts as $accountId => $account) {
             if (!empty($account['add'])) {
                 $newAccounts[$accountId] = $account;
             } elseif (isset($newAccounts[$accountId])) {
                 unset($newAccounts[$accountId]);
             }
         }
         $facebookModel->setAccounts($newAccounts);
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_UPDATED, XenForo_Link::buildAdminLink('tools/social-share/facebook-targets'));
     }
     $fbUser = XenForo_Helper_Facebook::getUserInfo($fbToken);
     $fbError = XenForo_Helper_Facebook::getFacebookRequestErrorInfo($fbUser, 'id');
     if (!empty($fbError)) {
         return $this->responseError($fbError);
     }
     $accounts = array();
     $accounts[$fbUser['id']] = array('target_id' => bdSocialShare_Helper_Common::encryptTargetId($fbUser['name'], $fbToken), 'name' => $fbUser['name'], 'label' => new XenForo_Phrase('bdsocialshare_your_facebook_timeline'));
     $pages = bdSocialShare_Helper_Facebook::getPages($fbToken);
     if (!empty($pages)) {
         foreach ($pages as $pageId => $page) {
             $accounts[$pageId] = $page;
             $accounts[$pageId]['label'] = new XenForo_Phrase('bdsocialshare_facebook_page');
         }
     }
     $groups = bdSocialShare_Helper_Facebook::getGroups($fbToken);
     if (!empty($groups)) {
         foreach ($groups as $groupId => $group) {
             $accounts[$groupId] = $group;
             $accounts[$groupId]['label'] = new XenForo_Phrase('bdsocialshare_facebook_group');
         }
     }
     foreach ($accounts as $accountId => &$accountRef) {
         if (isset($existingAccounts[$accountId])) {
             $accountRef['selected'] = 1;
         }
     }
     $viewParams = array('type' => 'facebook', 'token' => $fbToken, 'accounts' => $accounts);
     return $this->responseView('bdSocialShare_ViewAdmin_Tools_AddMore', 'bdsocialshare_tools_add_more', $viewParams);
 }