BxDolConModule.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 UnaConnect Dolphin Connect
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDolConModule extends BxBaseModConnectModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. /**
  18. * Redirect to remote site login form
  19. *
  20. * @return n/a/ - redirect or HTML page in case of error
  21. */
  22. function actionStart()
  23. {
  24. if (isLogged())
  25. $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
  26. if (!$this->_oConfig->sApiID || !$this->_oConfig->sApiSecret || !$this->_oConfig->sApiUrl) {
  27. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  28. bx_import('BxDolLanguages');
  29. $sCode = MsgBox( _t('_bx_dolcon_profile_error_api_keys') );
  30. $this->_oTemplate->getPage(_t('_bx_dolcon'), $sCode);
  31. }
  32. else {
  33. // define redirect URL to the remote site
  34. $sUrl = bx_append_url_params($this->_oConfig->sApiUrl . 'auth', array(
  35. 'response_type' => 'code',
  36. 'client_id' => $this->_oConfig->sApiID,
  37. 'redirect_uri' => $this->_oConfig->sPageHandle,
  38. 'scope' => $this->_oConfig->sScope,
  39. 'state' => $this->_genToken(),
  40. ));
  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 ($this->_getToken() != bx_get('state')) {
  49. $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_state_invalid')));
  50. return;
  51. }
  52. // check code
  53. $sCode = bx_get('code');
  54. if (!$sCode) {
  55. $sErrorDescription = bx_get('error_description') ? bx_get('error_description') : _t('_error occured');
  56. $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
  57. return;
  58. }
  59. // make request for token
  60. $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'token', array(
  61. 'client_id' => $this->_oConfig->sApiID,
  62. 'client_secret' => $this->_oConfig->sApiSecret,
  63. 'grant_type' => 'authorization_code',
  64. 'code' => $sCode,
  65. 'redirect_uri' => $this->_oConfig->sPageHandle,
  66. ), 'post');
  67. // handle error
  68. if (!$s || NULL === ($aResponse = json_decode($s, true)) || !isset($aResponse['access_token']) || isset($aResponse['error'])) {
  69. $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  70. $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
  71. return;
  72. }
  73. // get the data, especially access_token
  74. $sAccessToken = $aResponse['access_token'];
  75. $sExpiresIn = $aResponse['expires_in'];
  76. $sExpiresAt = new \DateTime('+' . $sExpiresIn . ' seconds');
  77. $sRefreshToken = $aResponse['refresh_token'];
  78. $oSession = BxDolSession::getInstance();
  79. $oSession->setValue($this->getName() . '_access_token', $sAccessToken);
  80. // request info about profile
  81. $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'api/me', array(), 'get', array(
  82. 'Authorization: Bearer ' . $sAccessToken,
  83. ));
  84. // handle error
  85. if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || isset($aResponse['error'])) {
  86. $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  87. $this->_oTemplate->getPage(_t('_Error'), MsgBox($sErrorDescription));
  88. return;
  89. }
  90. $aRemoteProfileInfo = $aResponse;
  91. if ($aRemoteProfileInfo) {
  92. // check if user logged in before
  93. $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['id']);
  94. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  95. // user already exists
  96. $this->setLogged($oProfile ->id());
  97. }
  98. else {
  99. // register new user
  100. $this->_createProfile($aRemoteProfileInfo);
  101. }
  102. }
  103. else {
  104. $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_profile_error_info')));
  105. }
  106. }
  107. /**
  108. * Make friends
  109. *
  110. * @param $iProfileId integer
  111. * @return void
  112. */
  113. protected function _makeFriends($iProfileId)
  114. {
  115. if (!$this->_oConfig->bAutoFriends)
  116. return;
  117. $oConnFrinds = BxDolConnection::getObjectInstance('sys_profiles_friends');
  118. if (!$oConnFrinds)
  119. return;
  120. // request info about profile
  121. if (!($iRemoteProfileId = $this->_oDb->getRemoteProfileId($iProfileId)))
  122. return;
  123. $oSession = BxDolSession::getInstance();
  124. if (!($sAccessToken = $oSession->getValue($this->getName() . '_access_token')))
  125. return;
  126. $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'api/friends?id=' . $iRemoteProfileId, array(), 'get', array(
  127. 'Authorization: Bearer ' . $sAccessToken,
  128. ));
  129. // handle error
  130. if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || isset($aResponse['error']) || !isset($aResponse['friends'])) {
  131. $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  132. return;
  133. }
  134. // add friends & followers
  135. foreach ($aResponse['friends'] as $iRemoteProfileId => $a) {
  136. if (!($iLocalProfileId = $this->_oDb->getProfileId($iRemoteProfileId)))
  137. continue;
  138. $oConnFrinds->actionAdd($iProfileId, $iLocalProfileId);
  139. $oConnFrinds->actionAdd($iLocalProfileId, $iProfileId);
  140. }
  141. }
  142. /**
  143. * @param $aProfileInfo - remote profile info
  144. * @param $sAlternativeName - suffix to add to NickName to make it unique
  145. * @return profile array info, ready for the local database
  146. */
  147. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  148. {
  149. $aProfileFields = $aProfileInfo;
  150. $sFullname = $aProfileInfo['NickName'];
  151. if (!empty($aProfileInfo['FullName']))
  152. $sFullname = $aProfileInfo['FullName'];
  153. if (!empty($aProfileInfo['FirstName']))
  154. $sFullname = $aProfileInfo['FirstName'] . (!empty($aProfileInfo['LastName']) ? ' ' . $aProfileInfo['LastName'] : '');
  155. $aProfileFields['name'] = $aProfileInfo['NickName'];
  156. $aProfileFields['gender'] = isset($aProfileInfo['Sex']) && ('male' == $aProfileInfo['Sex'] || 'female' == $aProfileInfo['Sex']) ? ('male' == $aProfileInfo['Sex'] ? 1 : 2) : '';
  157. $aProfileFields['birthday'] = isset($aProfileInfo['DateOfBirth']) ? $aProfileInfo['DateOfBirth'] : '';
  158. $aProfileFields['fullname'] = $sFullname;
  159. $aProfileFields['picture'] = $aProfileInfo['picture'];
  160. $aProfileFields['description'] = $aProfileInfo['DescriptionMe'];
  161. $aProfileFields['allow_view_to'] = getParam('bx_dolcon_privacy');
  162. return $aProfileFields;
  163. }
  164. }
  165. /** @} */