BxDrupalModule.php 4.9 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 DrupalConnect Drupal Connect
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDrupalModule extends BxBaseModConnectModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. function actionLoginSubmit()
  18. {
  19. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  20. $oForm = BxDolForm::getObjectInstance('bx_drupal_login', 'bx_drupal_login');
  21. $oForm->initChecker();
  22. $oForm->setRole(bx_get('role'));
  23. $bLoginSuccess = $oForm->isSubmittedAndValid();
  24. $bAjxMode = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') ? true : false;
  25. if ($bAjxMode) {
  26. header( 'Content-type: text/html; charset=utf-8' );
  27. echo $bLoginSuccess ? 'OK' : $oForm->getLoginError();
  28. exit;
  29. }
  30. elseif ($bLoginSuccess) {
  31. $sId = trim($oForm->getCleanValue('ID'));
  32. $oAccount = BxDolAccount::getInstance($sId);
  33. $aAccount = bx_login($oAccount->id(), ($oForm->getCleanValue('rememberMe') ? true : false));
  34. $sUrlRelocate = $oForm->getCleanValue('relocate');
  35. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  36. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  37. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  38. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  39. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  40. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  41. BxDolTemplate::getInstance()->getPageCode();
  42. exit;
  43. }
  44. bx_require_authentication();
  45. header('Location: ' . BX_DOL_URL_ROOT);
  46. }
  47. public function serviceHandleUser($aRemoteProfileInfo)
  48. {
  49. if ($aRemoteProfileInfo && isset($aRemoteProfileInfo['user']) && isset($aRemoteProfileInfo['user']['uid'])) {
  50. // check if user logged in before
  51. $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['user']['uid']);
  52. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  53. // user already exists
  54. bx_alert($this->getName(), 'login', 0, $oProfile->id(), array('remote_profile_info' => $aRemoteProfileInfo));
  55. $this->setLogged($oProfile->id());
  56. }
  57. else {
  58. // register new user
  59. if (isset($aRemoteProfileInfo['user']) && isset($aRemoteProfileInfo['user']['uid']))
  60. $aRemoteProfileInfo['id'] = $aRemoteProfileInfo['user']['uid'];
  61. $this->_createProfile($aRemoteProfileInfo);
  62. }
  63. }
  64. else {
  65. $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_profile_error_info')));
  66. }
  67. }
  68. /**
  69. * @param $aProfileInfo - remote profile info
  70. * @param $sAlternativeName - suffix to add to NickName to make it unique
  71. * @return profile array info, ready for the local database
  72. */
  73. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  74. {
  75. $sFirstName = '';
  76. if (isset($aProfileInfo['user']) && isset($aProfileInfo['user']['field_first_name'])) {
  77. $a = array_pop($aProfileInfo['user']['field_first_name']['und']);
  78. $sFirstName = $a['value'];
  79. }
  80. $sLastName = '';
  81. if (isset($aProfileInfo['user']) && isset($aProfileInfo['user']['field_last_name'])) {
  82. $a = array_pop($aProfileInfo['user']['field_last_name']['und']);
  83. $sLastName = $a['value'];
  84. }
  85. if (empty($sFirstName)) {
  86. if (isset($aProfileInfo['user']['mail']) && false !== ($iPos = mb_strpos($aProfileInfo['user']['mail'], '@')))
  87. $sFirstName = mb_substr($aProfileInfo['user']['mail'], 0, $iPos);
  88. }
  89. $aProfileFields['id'] = $aProfileInfo['user']['uid'];
  90. $aProfileFields['name'] = empty($sFirstName) ? $aProfileInfo['user']['uid'] : $sFirstName;
  91. $aProfileFields['fullname'] = $sFirstName . (empty($sLastName) ? '' : ' ' . $sLastName);
  92. $aProfileFields['email'] = isset($aProfileInfo['user']['mail']) ? $aProfileInfo['user']['mail'] : '';
  93. $aProfileFields['picture'] = isset($aProfileInfo['user']['picture']) ? $aProfileInfo['user']['picture'] : '';
  94. $aProfileFields['allow_view_to'] = getParam('bx_drupal_privacy');
  95. return $aProfileFields;
  96. }
  97. }
  98. /** @} */