| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
- * CC-BY License - http://creativecommons.org/licenses/by/3.0/
- *
- * @defgroup FacebookConnect Facebook Connect
- * @ingroup TridentModules
- *
- * @{
- */
- class BxFaceBookConnectModule extends BxBaseModConnectModule
- {
- protected $oFacebook;
- protected $sLastError;
- /**
- * Class constructor ;
- *
- * @param : $aModule (array) - contain some information about this module;
- * [ id ] - (integer) module's id ;
- * [ title ] - (string) module's title ;
- * [ vendor ] - (string) module's vendor ;
- * [ path ] - (string) path to this module ;
- * [ uri ] - (string) this module's URI ;
- * [ class_prefix ] - (string) this module's php classes file prefix ;
- * [ db_prefix ] - (string) this module's Db tables prefix ;
- * [ date ] - (string) this module's date installation ;
- */
- function __construct(&$aModule)
- {
- parent::__construct($aModule);
- require_once(BX_DIRECTORY_PATH_PLUGINS . 'facebook-php-sdk/src/Facebook/autoload.php');
- // Create our Application instance.
- $this -> oFacebook = null;
- if ($this -> _oConfig -> mApiID) {
- session_start();
- $this -> oFacebook = new Facebook\Facebook(array(
- 'app_id' => $this -> _oConfig -> mApiID,
- 'app_secret' => $this -> _oConfig -> mApiSecret,
- 'default_graph_version' => 'v2.4',
- ));
- }
- }
- /**
- * Facebook login callback url;
- *
- * @return (text) - html presentation data;
- */
- function actionLoginCallback()
- {
- if (isLogged()) {
- header ('Location:' . $this -> _oConfig -> sDefaultRedirectUrl);
- exit;
- }
- require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
- bx_import('BxDolLanguages');
- if (!$this -> _oConfig -> mApiID || !$this -> _oConfig -> mApiSecret)
- $this->_setLastError(_t('_bx_facebook_profile_error_api_keys'));
- if ($sError = $this->_setAccessToken())
- $this->_setLastError($sError);
- if (!$this->_getLastError()) {
- //we already logged in facebook
- try {
- $oResponse = $this -> oFacebook -> get('/me?fields=' . $this -> _oConfig -> sFaceBookFields);
- $aFacebookProfileInfo = $oResponse -> getDecodedBody();
- $aFacebookProfileInfo['nick_name'] = $aFacebookProfileInfo['name'];
- } catch (Facebook\Exceptions\FacebookResponseException $e) {
- $this->_setLastError($e->getMessage());
- } catch (Facebook\Exceptions\FacebookSDKException $e) {
- $this->_setLastError($e->getMessage());
- }
- //process profile info
- if($aFacebookProfileInfo) {
- // try define user id
- $iProfileId = $this -> _oDb
- -> getProfileId($aFacebookProfileInfo['id']);
- if ($iProfileId) {
- $this -> setLogged($iProfileId);
- }
- else {
- $sAlternativeNickName = '';
- // process profile's nickname
- $aFacebookProfileInfo['nick_name'] = $this
- -> _proccesNickName($aFacebookProfileInfo['first_name']);
- // try to get profile's image
- if ($oFacebookProfileImageResponse = $this -> oFacebook -> get('/me/picture?type=large&redirect=false')) {
- $aFacebookProfileImage = $oFacebookProfileImageResponse -> getDecodedBody();
- $aFacebookProfileInfo['picture'] = isset($aFacebookProfileImage['data']['url']) && !$aFacebookProfileImage['data']['is_silhouette']
- ? $aFacebookProfileImage['data']['url']
- : '';
- }
- // create new profile
- $this -> _createProfile($aFacebookProfileInfo, $sAlternativeNickName);
- }
- } else {
- // FB profile info is not defined;
- $this->_setLastError(_t('_bx_facebook_profile_error_info'));
- }
- }
- $this -> _oTemplate -> dislayPageError();
- }
- /**
- * Generare facebook login form;
- *
- * @return (text) - html presentation data;
- */
- function actionLoginForm()
- {
- if (isLogged()) {
- header ('Location:' . $this -> _oConfig -> sDefaultRedirectUrl);
- exit;
- }
- if (!$this -> _oConfig -> mApiID || !$this -> _oConfig -> mApiSecret) {
- require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
- bx_import('BxDolLanguages');
- $this->_setLastError(_t('_bx_facebook_profile_error_api_keys'));
- }
- else {
- $oFacebookRedirectLoginHelper = $this -> oFacebook -> getRedirectLoginHelper();
- //redirect to facebook login form
- $sLoginUrl = $oFacebookRedirectLoginHelper->getLoginUrl(
- $this -> _oConfig -> aFaceBookReqParams['redirect_uri'],
- explode(',', $this -> _oConfig -> aFaceBookReqParams['scope'])
- );
- header('location: ' . $sLoginUrl);
- exit;
- }
- $this -> _oTemplate -> dislayPageError();
- }
- function serviceLastError()
- {
- return MsgBox($this->sLastError ? $this->sLastError : _t('_Empty'));
- }
- /**
- * Make friends
- *
- * @param $iProfileId integer
- * @return void
- */
- protected function _makeFriends($iProfileId)
- {
- if (!$this->_oConfig->bAutoFriends) {
- return;
- }
- try {
- //get friends from facebook
- $oFriendsResponse = $this -> oFacebook -> get('/me/friends?limit=50');
- } catch(Facebook\Exceptions\FacebookResponseException $e) {
- return;
- } catch(Facebook\Exceptions\FacebookSDKException $e) {
- return;
- }
- // paginate through the result
- $oPagesEdge = $oFriendsResponse->getGraphEdge();
- do {
- foreach ($oPagesEdge as $oPage) {
- $aFriend = $oPage->asArray();
- $iFriendId = $this -> _oDb -> getProfileId($aFriend['id']);
- // TODO:
- }
- } while ($oPagesEdge = $this -> oFacebook -> next($oPagesEdge));
- }
- /**
- * @param $aProfileInfo - remote profile info
- * @param $sAlternativeName - suffix to add to NickName to make it unique
- * @return profile array info, ready for the local database
- */
- protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
- {
- // process the date of birth
- if( isset($aProfileInfo['birthday']) ) {
- $aProfileInfo['birthday'] = isset($aProfileInfo['birthday'])
- ? date('Y-m-d', strtotime($aProfileInfo['birthday']))
- : '';
- }
- // define user's country and city
- $aLocation = array();
- if (isset($aProfileInfo['location']['name']))
- $aLocation = $aProfileInfo['location']['name'];
- elseif (isset($aProfileInfo['hometown']['name']))
- $aLocation = $aProfileInfo['hometown']['name'];
- if($aLocation) {
- $aCountryInfo = explode(',', $aLocation);
- $sCountry = $this -> _oDb -> getCountryCode( trim($aCountryInfo[1]) );
- $sCity = trim($aCountryInfo[0]);
- //set default country name, especially for American brothers
- if($sCity && !$sCountry) {
- $sCountry = $this -> _oConfig -> sDefaultCountryCode;
- }
- }
- // fill array with all needed values
- $aProfileFields = array(
- 'name' => $aProfileInfo['nick_name'] . $sAlternativeName,
- 'email' => isset($aProfileInfo['email']) ? $aProfileInfo['email'] : '',
- 'gender' => isset($aProfileInfo['gender']) ? $aProfileInfo['gender'] : '',
- 'birthday' => isset($aProfileInfo['birthday']) ? $aProfileInfo['birthday'] : '',
- 'fullname' => (isset($aProfileInfo['first_name']) ? $aProfileInfo['first_name'] : '') . (isset($aProfileInfo['last_name']) ? ' ' . $aProfileInfo['last_name'] : ''),
- 'description' => clear_xss(isset($aProfileInfo['bio']) ? $aProfileInfo['bio'] : ''),
- 'interests' => clear_xss(isset($aProfileInfo['interests']) ? $aProfileInfo['interests'] : ''),
- 'religion' => clear_xss(isset($aProfileInfo['religion']) ? $aProfileInfo['religion'] : ''),
- 'country' => isset($sCountry) ? $sCountry : '',
- 'city' => isset($sCity) ? $sCity : '',
- 'picture' => $aProfileInfo['picture'],
- );
- return $aProfileFields;
- }
- /**
- * Function will clear all unnecessary sybmols from profile's nickname;
- *
- * @param : $sProfileName (string) - profile's nickname;
- * @return : (string) - cleared nickname;
- */
- protected function _proccesNickName($sProfileName)
- {
- $sProfileName = preg_replace("/^http:\/\/|^https:\/\/|\/$/", '', $sProfileName);
- $sProfileName = str_replace('/', '_', $sProfileName);
- $sProfileName = str_replace('.', '-', $sProfileName);
- return $sProfileName;
- }
- protected function _setAccessToken()
- {
- $oFacebookRedirectLoginHelper = $this -> oFacebook -> getRedirectLoginHelper();
- try {
- $sAccessToken = $oFacebookRedirectLoginHelper->getAccessToken();
- } catch(Facebook\Exceptions\FacebookResponseException $e) {
- // When Graph returns an error
- return $e->getMessage();
- } catch(Facebook\Exceptions\FacebookSDKException $e) {
- // When validation fails or other local issues
- return $e->getMessage();
- }
- if (!isset($sAccessToken))
- return $oFacebookRedirectLoginHelper->getErrorDescription() ? $oFacebookRedirectLoginHelper->getErrorDescription() : (bx_get('error_message') ? rawurldecode(bx_get('error_message')) : _t('_error occured'));
- $this -> oFacebook -> setDefaultAccessToken($sAccessToken);
- return '';
- }
- protected function _setLastError($s)
- {
- $this->sLastError = $s;
- }
- protected function _getLastError()
- {
- return $this->sLastError;
- }
- }
- /** @} */
|