BxBaseServiceLogin.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System service for login form functionality.
  11. */
  12. class BxBaseServiceLogin extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceTest ($n = 1)
  19. {
  20. return $n*2;
  21. }
  22. public function serviceMemberAuthCode($aAuthTypes = array())
  23. {
  24. if(empty($aAuthTypes) || !is_array($aAuthTypes))
  25. $aAuthTypes = BxDolDb::getInstance()->fromCache('sys_objects_auths', 'getAll', 'SELECT * FROM `sys_objects_auths`');
  26. $bCompact = getParam('site_login_social_compact') == 'on';
  27. $aTmplButtons = array();
  28. foreach($aAuthTypes as $iKey => $aItems) {
  29. $sTitle = _t($aItems['Title']);
  30. $aTmplButtons[] = array(
  31. 'class' => $bCompact ? 'sys-auth-compact bx-def-margin-sec-left-auto' : '',
  32. 'href' => !empty($aItems['Link']) ? BX_DOL_URL_ROOT . $aItems['Link'] : 'javascript:void(0)',
  33. 'title_alt' => bx_html_attribute($sTitle),
  34. 'bx_if:show_onclick' => array(
  35. 'condition' => !empty($aItems['OnClick']),
  36. 'content' => array(
  37. 'onclick' => 'javascript:' . $aItems['OnClick']
  38. )
  39. ),
  40. 'bx_if:show_icon' => array(
  41. 'condition' => !empty($aItems['Icon']),
  42. 'content' => array(
  43. 'icon' => $aItems['Icon']
  44. )
  45. ),
  46. 'bx_if:show_title' => array(
  47. 'condition' => !$bCompact || empty($aItems['Icon']),
  48. 'content' => array(
  49. 'title' => $sTitle
  50. )
  51. )
  52. );
  53. }
  54. BxDolTemplate::getInstance()->addCss(array('auth.css'));
  55. return BxDolTemplate::getInstance()->parseHtmlByName('auth.html', array(
  56. 'bx_repeat:buttons' => $aTmplButtons
  57. ));
  58. }
  59. public function serviceLoginFormOnly ($sParams = '', $sForceRelocate = '')
  60. {
  61. if(strpos($sParams, 'no_join_text') === false)
  62. $sParams = ($sParams != '' ? ' ' : '') . 'no_join_text';
  63. return $this->serviceLoginForm($sParams, $sForceRelocate);
  64. }
  65. public function serviceLoginForm ($sParams = '', $sForceRelocate = '')
  66. {
  67. if(isLogged())
  68. return false;
  69. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login');
  70. $sCustomHtmlBefore = '';
  71. $sCustomHtmlAfter = '';
  72. bx_alert('profile', 'show_login_form', 0, 0, array('oForm' => &$oForm, 'sParams' => &$sParams, 'sCustomHtmlBefore' => &$sCustomHtmlBefore, 'sCustomHtmlAfter' => &$sCustomHtmlAfter, 'aAuthTypes' => &$aAuthTypes));
  73. if ($sForceRelocate && 0 === mb_stripos($sForceRelocate, BX_DOL_URL_ROOT))
  74. $oForm->aInputs['relocate']['value'] = $sForceRelocate;
  75. elseif ('homepage' == $sForceRelocate)
  76. $oForm->aInputs['relocate']['value'] = BX_DOL_URL_ROOT;
  77. $sFormCode = $oForm->getCode();
  78. BxDolTemplate::getInstance()->addJs(array('jquery.form.min.js'));
  79. $sJoinText = '';
  80. if (strpos($sParams, 'no_join_text') === false)
  81. $sJoinText = '<hr class="bx-def-hr bx-def-margin-sec-topbottom" /><div>' . _t('_sys_txt_login_description', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account')) . '</div>';
  82. $sAuth = '';
  83. if (strpos($sParams, 'no_auth_buttons') === false)
  84. $sAuth = $this->serviceMemberAuthCode();
  85. $sAjaxForm = '';
  86. if (strpos($sParams, 'ajax_form') !== false)
  87. $sAjaxForm = "<script>
  88. (bx_login_init_ajax_form = function () {
  89. $('#sys-form-login').ajaxForm({
  90. dataType: 'json',
  91. success: function (oData) {
  92. if ('undefined' !== typeof(oData['res']) && 'OK' == oData['res']) {
  93. location.reload();
  94. }
  95. else if ('undefined' !== typeof(oData['form'])) {
  96. $('#sys-form-login').replaceWith(oData['form']);
  97. bx_login_init_ajax_form();
  98. }
  99. }
  100. });
  101. })();
  102. </script>";
  103. return $sCustomHtmlBefore . $sAuth . $sFormCode . $sCustomHtmlAfter . $sJoinText . $sAjaxForm;
  104. }
  105. public function serviceLoginFormStep2 ()
  106. {
  107. if(isLogged())
  108. return false;
  109. $oSession = BxDolSession::getInstance();
  110. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  111. if ($iAccountId == '')
  112. return false;
  113. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step2');
  114. $oForm->aFormAttrs['action'] = '';
  115. $oAccount = BxDolAccount::getInstance($iAccountId);
  116. $a = $oAccount->getInfo($iAccountId);
  117. $sPhoneNumber = trim($a['phone']);
  118. $oForm->initChecker(array('phone' => $sPhoneNumber));
  119. if ($oForm->isSubmittedAndValid()) {
  120. $sNewPhoneNumber = trim($oForm->getCleanValue('phone'));
  121. if ($sPhoneNumber != $sNewPhoneNumber)
  122. $oAccount->updatePhone($sNewPhoneNumber);
  123. $sActivationCode = rand(1000, 9999);
  124. $sActivationText =_t('_sys_txt_login_2fa_sms_text', $sActivationCode);
  125. $ret = null;
  126. bx_alert('account', 'before_2fa_send_sms', $oAccount->id(), false, array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  127. if ($ret === null)
  128. {
  129. $oTwilio = BxDolTwilio::getInstance();
  130. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  131. return MsgBox(_t('_sys_txt_login_2fa_sms_error_occured'));
  132. }
  133. }
  134. $oSession = BxDolSession::getInstance();
  135. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  136. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login-step3'));
  137. }
  138. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_login_2fa_description") . '</div>' .$oForm->getCode();
  139. }
  140. public function serviceLoginFormStep3 ()
  141. {
  142. if(isLogged())
  143. return false;
  144. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step3');
  145. $oForm->aFormAttrs['action'] = '';
  146. $oForm->initChecker();
  147. if ($oForm->isSubmittedAndValid()) {
  148. $oSession = BxDolSession::getInstance();
  149. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  150. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  151. $oAccount = BxDolAccount::getInstance($iAccountId);
  152. $aAccount = bx_login($oAccount->id(), $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_IS_REMEMBER));
  153. $sUrlRelocate = $oForm->getCleanValue('relocate');
  154. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  155. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  156. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  157. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  158. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  159. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  160. header('Location: ' . $sUrlRelocate);
  161. }
  162. return $oForm->getCode();
  163. }
  164. }
  165. /** @} */