| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?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 TwitterConnect Twitter Connect
- * @ingroup TridentModules
- *
- * @{
- */
- use OAuth\OAuth1\Service\Twitter;
- use OAuth\Common\Storage\Session;
- use OAuth\Common\Consumer\Credentials;
- require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/bootstrap.php');
- require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/ServiceFactory.php');
- class BxTwitterModule extends BxBaseModConnectModule
- {
- protected $_oStorage;
- protected $_oCredentials;
- function __construct(&$aModule)
- {
- parent::__construct($aModule);
- }
- /**
- * Redirect to remote site login form
- *
- * @return n/a - redirect or HTML page in case of error
- */
- function actionStart()
- {
- if (isLogged())
- $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
-
- if (!$this->_oConfig->sApiID || !$this->_oConfig->sApiSecret) {
- require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
- bx_import('BxDolLanguages');
- $sCode = MsgBox( _t('_bx_twitter_profile_error_api_keys') );
- $this->_oTemplate->getPage(_t('_bx_twitter'), $sCode);
- }
- else {
- $this->_init();
- $oToken = $this->_oTwitterService->requestRequestToken();
- $sUrl = $this->_oTwitterService->getAuthorizationUri(array('oauth_token' => $oToken->getRequestToken()));
- $this->_redirect($sUrl);
- }
- }
- function actionHandle()
- {
- require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
- // check CSRF token
- if (empty($_GET['oauth_token'])) {
- $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_bx_twitter_error_no_oauth_token')));
- return;
- }
- $this->_init();
- $oToken = $this->_oStorage->retrieveAccessToken('Twitter');
-
- $this->_oTwitterService->requestAccessToken(
- $_GET['oauth_token'],
- $_GET['oauth_verifier'],
- $oToken->getRequestTokenSecret()
- );
- $s = $this->_oTwitterService->request('account/verify_credentials.json?include_email=true');
- // handle error
- if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || !isset($aResponse['id'])) {
- $sErrorDescription = _t('_error occured');
- $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
- return;
- }
- $aRemoteProfileInfo = $aResponse;
- if ($aRemoteProfileInfo) {
- // check if user logged in before
- $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['id']);
-
- if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
- // user already exists
- $this->setLogged($oProfile ->id());
- }
- else {
- // register new user
- $this->_createProfile($aRemoteProfileInfo);
- }
- }
- else {
- $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_profile_error_info')));
- }
- }
- /**
- * @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 = '')
- {
- $aProfileFields = $aProfileInfo;
- $aProfileFields['name'] = $aProfileInfo['screen_name'];
- $aProfileFields['fullname'] = $aProfileInfo['name'];
- $aProfileFields['email'] = isset($aProfileInfo['email']) ? $aProfileInfo['email'] : '';
- $aProfileFields['picture'] = isset($aProfileInfo['profile_image_url']) ? str_replace('_normal', '', $aProfileInfo['profile_image_url']) : '';
-
- return $aProfileFields;
- }
- protected function _init()
- {
- if ($this->_oStorage)
- return;
- $this->_oStorage = new Session();
- $this->_oCredentials = new Credentials(
- $this->_oConfig->sApiID,
- $this->_oConfig->sApiSecret,
- $this->_oConfig->sPageHandle
- );
- $oServiceFactory = new OAuth\ServiceFactory();
- $this->_oTwitterService = $oServiceFactory->createService('twitter', $this->_oCredentials, $this->_oStorage);
- }
- }
- /** @} */
|