BxDolConModule.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. return $this->_actionHandle();
  47. }
  48. /**
  49. * Make friends
  50. *
  51. * @param $iProfileId integer
  52. * @return void
  53. */
  54. protected function _makeFriends($iProfileId)
  55. {
  56. if (!$this->_oConfig->bAutoFriends)
  57. return;
  58. $oConnFrinds = BxDolConnection::getObjectInstance('sys_profiles_friends');
  59. if (!$oConnFrinds)
  60. return;
  61. // request info about profile
  62. if (!($iRemoteProfileId = $this->_oDb->getRemoteProfileId($iProfileId)))
  63. return;
  64. $oSession = BxDolSession::getInstance();
  65. if (!($sAccessToken = $oSession->getValue($this->getName() . '_access_token')))
  66. return;
  67. $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'api/friends?id=' . $iRemoteProfileId, array(), 'get', array(
  68. 'Authorization: Bearer ' . $sAccessToken,
  69. ));
  70. // handle error
  71. if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || isset($aResponse['error']) || !isset($aResponse['friends'])) {
  72. $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  73. return;
  74. }
  75. // add friends & followers
  76. foreach ($aResponse['friends'] as $iRemoteProfileId => $a) {
  77. if (!($iLocalProfileId = $this->_oDb->getProfileId($iRemoteProfileId)))
  78. continue;
  79. $oConnFrinds->actionAdd($iProfileId, $iLocalProfileId);
  80. $oConnFrinds->actionAdd($iLocalProfileId, $iProfileId);
  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. $sFullname = $aProfileInfo['NickName'];
  92. if (!empty($aProfileInfo['FullName']))
  93. $sFullname = $aProfileInfo['FullName'];
  94. if (!empty($aProfileInfo['FirstName']))
  95. $sFullname = $aProfileInfo['FirstName'] . (!empty($aProfileInfo['LastName']) ? ' ' . $aProfileInfo['LastName'] : '');
  96. $aProfileFields['name'] = $aProfileInfo['NickName'];
  97. $aProfileFields['gender'] = isset($aProfileInfo['Sex']) && ('male' == $aProfileInfo['Sex'] || 'female' == $aProfileInfo['Sex']) ? ('male' == $aProfileInfo['Sex'] ? 1 : 2) : '';
  98. $aProfileFields['birthday'] = isset($aProfileInfo['DateOfBirth']) ? $aProfileInfo['DateOfBirth'] : '';
  99. $aProfileFields['fullname'] = $sFullname;
  100. $aProfileFields['picture'] = $aProfileInfo['picture'];
  101. $aProfileFields['description'] = $aProfileInfo['DescriptionMe'];
  102. $aProfileFields['allow_view_to'] = getParam('bx_dolcon_privacy');
  103. return $aProfileFields;
  104. }
  105. }
  106. /** @} */