BxTwitterModule.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TwitterConnect Twitter Connect
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. use OAuth\OAuth1\Service\Twitter;
  12. use OAuth\Common\Storage\Session;
  13. use OAuth\Common\Consumer\Credentials;
  14. require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/bootstrap.php');
  15. require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/ServiceFactory.php');
  16. class BxTwitterModule extends BxBaseModConnectModule
  17. {
  18. protected $_oStorage;
  19. protected $_oCredentials;
  20. function __construct(&$aModule)
  21. {
  22. parent::__construct($aModule);
  23. }
  24. /**
  25. * Redirect to remote site login form
  26. *
  27. * @return n/a - redirect or HTML page in case of error
  28. */
  29. function actionStart()
  30. {
  31. if (isLogged())
  32. $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
  33. if (!$this->_oConfig->sApiID || !$this->_oConfig->sApiSecret) {
  34. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  35. bx_import('BxDolLanguages');
  36. $sCode = MsgBox( _t('_bx_twitter_profile_error_api_keys') );
  37. $this->_oTemplate->getPage(_t('_bx_twitter'), $sCode);
  38. }
  39. else {
  40. $this->_init();
  41. $oToken = $this->_oTwitterService->requestRequestToken();
  42. $sUrl = $this->_oTwitterService->getAuthorizationUri(array('oauth_token' => $oToken->getRequestToken()));
  43. $this->_redirect($sUrl);
  44. }
  45. }
  46. function actionHandle()
  47. {
  48. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  49. // check CSRF token
  50. if (empty($_GET['oauth_token'])) {
  51. $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_bx_twitter_error_no_oauth_token')));
  52. return;
  53. }
  54. $this->_init();
  55. $oToken = $this->_oStorage->retrieveAccessToken('Twitter');
  56. $this->_oTwitterService->requestAccessToken(
  57. $_GET['oauth_token'],
  58. $_GET['oauth_verifier'],
  59. $oToken->getRequestTokenSecret()
  60. );
  61. $s = $this->_oTwitterService->request('account/verify_credentials.json?include_email=true');
  62. // handle error
  63. if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || !isset($aResponse['id'])) {
  64. $sErrorDescription = _t('_error occured');
  65. $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
  66. return;
  67. }
  68. $aRemoteProfileInfo = $aResponse;
  69. if ($aRemoteProfileInfo) {
  70. // check if user logged in before
  71. $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['id']);
  72. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  73. // user already exists
  74. $this->setLogged($oProfile ->id());
  75. }
  76. else {
  77. // register new user
  78. $this->_createProfile($aRemoteProfileInfo);
  79. }
  80. }
  81. else {
  82. $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_profile_error_info')));
  83. }
  84. }
  85. /**
  86. * @param $aProfileInfo - remote profile info
  87. * @param $sAlternativeName - suffix to add to NickName to make it unique
  88. * @return profile array info, ready for the local database
  89. */
  90. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  91. {
  92. $aProfileFields = $aProfileInfo;
  93. $aProfileFields['name'] = $aProfileInfo['screen_name'];
  94. $aProfileFields['fullname'] = $aProfileInfo['name'];
  95. $aProfileFields['email'] = isset($aProfileInfo['email']) ? $aProfileInfo['email'] : '';
  96. $aProfileFields['picture'] = isset($aProfileInfo['profile_image_url']) ? str_replace('_normal', '', $aProfileInfo['profile_image_url']) : '';
  97. return $aProfileFields;
  98. }
  99. protected function _init()
  100. {
  101. if ($this->_oStorage)
  102. return;
  103. $this->_oStorage = new Session();
  104. $this->_oCredentials = new Credentials(
  105. $this->_oConfig->sApiID,
  106. $this->_oConfig->sApiSecret,
  107. $this->_oConfig->sPageHandle
  108. );
  109. $oServiceFactory = new OAuth\ServiceFactory();
  110. $this->_oTwitterService = $oServiceFactory->createService('twitter', $this->_oCredentials, $this->_oStorage);
  111. }
  112. }
  113. /** @} */