1
0

BxTwitterModule.php 4.4 KB

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