BxDrupalModule.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $this->setLogged($oProfile->id());
  55. }
  56. else {
  57. // register new user
  58. if (isset($aRemoteProfileInfo['user']) && isset($aRemoteProfileInfo['user']['uid']))
  59. $aRemoteProfileInfo['id'] = $aRemoteProfileInfo['user']['uid'];
  60. $this->_createProfile($aRemoteProfileInfo);
  61. }
  62. }
  63. else {
  64. $this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_profile_error_info')));
  65. }
  66. }
  67. /**
  68. * @param $aProfileInfo - remote profile info
  69. * @param $sAlternativeName - suffix to add to NickName to make it unique
  70. * @return profile array info, ready for the local database
  71. */
  72. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  73. {
  74. $sFirstName = '';
  75. if (isset($aProfileInfo['user']) && isset($aProfileInfo['user']['field_first_name'])) {
  76. $a = array_pop($aProfileInfo['user']['field_first_name']['und']);
  77. $sFirstName = $a['value'];
  78. }
  79. $sLastName = '';
  80. if (isset($aProfileInfo['user']) && isset($aProfileInfo['user']['field_last_name'])) {
  81. $a = array_pop($aProfileInfo['user']['field_last_name']['und']);
  82. $sLastName = $a['value'];
  83. }
  84. $aProfileFields['id'] = $aProfileInfo['user']['uid'];
  85. $aProfileFields['name'] = $sFirstName;
  86. $aProfileFields['fullname'] = $sFirstName . (empty($sLastName) ? '' : ' ' . $sLastName);
  87. $aProfileFields['email'] = isset($aProfileInfo['user']['mail']) ? $aProfileInfo['user']['mail'] : '';
  88. $aProfileFields['picture'] = isset($aProfileInfo['user']['picture']) ? $aProfileInfo['user']['picture'] : '';
  89. $aProfileFields['allow_view_to'] = getParam('bx_drupal_privacy');
  90. return $aProfileFields;
  91. }
  92. }
  93. /** @} */