BxDrupalModule.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if ($bLoginSuccess) {
  27. $s = 'OK';
  28. $oAccount = BxDolAccount::getInstance(trim($oForm->getCleanValue('ID')));
  29. $aAccount = bx_login($oAccount->id(), ($oForm->getCleanValue('rememberMe') ? true : false));
  30. }
  31. else {
  32. $s = $oForm->getLoginError();
  33. }
  34. if (isset($_SERVER['HTTP_ACCEPT'])) {
  35. if (false !== strpos($_SERVER['HTTP_ACCEPT'], 'application/json') || false !== strpos($_SERVER['HTTP_ACCEPT'], 'text/javascript')) {
  36. header('Content-type: application/json; charset=utf-8');
  37. echo json_encode(['res' => $s, 'form' => $oForm->getCode()]);
  38. exit;
  39. }
  40. }
  41. header('Content-type: text/html; charset=utf-8');
  42. echo $s;
  43. exit;
  44. }
  45. elseif ($bLoginSuccess) {
  46. $sId = trim($oForm->getCleanValue('ID'));
  47. $oAccount = BxDolAccount::getInstance($sId);
  48. $aAccount = bx_login($oAccount->id(), ($oForm->getCleanValue('rememberMe') ? true : false));
  49. $sUrlRelocate = $oForm->getCleanValue('relocate');
  50. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  51. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  52. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  53. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  54. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  55. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  56. BxDolTemplate::getInstance()->getPageCode();
  57. exit;
  58. }
  59. bx_require_authentication();
  60. header('Location: ' . BX_DOL_URL_ROOT);
  61. }
  62. public function serviceHandleUser($aRemoteProfileInfo)
  63. {
  64. if ($aRemoteProfileInfo && isset($aRemoteProfileInfo['user']) && isset($aRemoteProfileInfo['user']['uid'])) {
  65. // check if user logged in before
  66. $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['user']['uid']);
  67. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  68. // user already exists
  69. bx_alert($this->getName(), 'login', 0, $oProfile->id(), array('remote_profile_info' => $aRemoteProfileInfo));
  70. $this->setLogged($oProfile->id());
  71. }
  72. else {
  73. // register new user
  74. if (isset($aRemoteProfileInfo['user']) && isset($aRemoteProfileInfo['user']['uid']))
  75. $aRemoteProfileInfo['id'] = $aRemoteProfileInfo['user']['uid'];
  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. $sFirstName = '';
  91. if (isset($aProfileInfo['user']) && isset($aProfileInfo['user']['field_first_name'])) {
  92. $a = array_pop($aProfileInfo['user']['field_first_name']['und']);
  93. $sFirstName = $a['value'];
  94. }
  95. $sLastName = '';
  96. if (isset($aProfileInfo['user']) && isset($aProfileInfo['user']['field_last_name'])) {
  97. $a = array_pop($aProfileInfo['user']['field_last_name']['und']);
  98. $sLastName = $a['value'];
  99. }
  100. if (empty($sFirstName)) {
  101. if (isset($aProfileInfo['user']['mail']) && false !== ($iPos = mb_strpos($aProfileInfo['user']['mail'], '@')))
  102. $sFirstName = mb_substr($aProfileInfo['user']['mail'], 0, $iPos);
  103. }
  104. $aProfileFields['id'] = $aProfileInfo['user']['uid'];
  105. $aProfileFields['name'] = empty($sFirstName) ? $aProfileInfo['user']['uid'] : $sFirstName;
  106. $aProfileFields['fullname'] = $sFirstName . (empty($sLastName) ? '' : ' ' . $sLastName);
  107. $aProfileFields['email'] = isset($aProfileInfo['user']['mail']) ? $aProfileInfo['user']['mail'] : '';
  108. $aProfileFields['picture'] = isset($aProfileInfo['user']['picture']) ? $aProfileInfo['user']['picture'] : '';
  109. $aProfileFields['allow_view_to'] = getParam('bx_drupal_privacy');
  110. return $aProfileFields;
  111. }
  112. }
  113. /** @} */