BxBaseServiceLogin.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 ' : '') . $aItems['Name'],
  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. if ($aItems['Style'] != ""){
  54. $aStyles = unserialize($aItems['Style']);
  55. foreach($aStyles as $sKey => $aValues) {
  56. BxDolTemplate::getInstance()->addCssStyle('.'. $aItems['Name'] .' ' . $sKey, $aValues);
  57. }
  58. }
  59. }
  60. if(empty($aTmplButtons) || !is_array($aTmplButtons))
  61. return '';
  62. $oTemplate = BxDolTemplate::getInstance();
  63. $oTemplate->addCss(array('auth.css'));
  64. return $oTemplate->parseHtmlByName('auth.html', array(
  65. 'bx_repeat:buttons' => $aTmplButtons,
  66. 'class_container' => ($bCompact ? 'sys-auth-compact-container' : '')
  67. ));
  68. }
  69. public function serviceLoginFormOnly ($sParams = '', $sForceRelocate = '')
  70. {
  71. if(strpos($sParams, 'no_join_text') === false)
  72. $sParams = ($sParams != '' ? ' ' : '') . 'no_join_text';
  73. return $this->serviceLoginForm($sParams, $sForceRelocate);
  74. }
  75. public function serviceLoginForm ($sParams = '', $sForceRelocate = '')
  76. {
  77. if(isLogged()){
  78. header('Location: ' . BX_DOL_URL_ROOT);
  79. exit;
  80. }
  81. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login');
  82. $sCustomHtmlBefore = '';
  83. $sCustomHtmlAfter = '';
  84. bx_alert('profile', 'show_login_form', 0, 0, array('oForm' => &$oForm, 'sParams' => &$sParams, 'sCustomHtmlBefore' => &$sCustomHtmlBefore, 'sCustomHtmlAfter' => &$sCustomHtmlAfter, 'aAuthTypes' => &$aAuthTypes));
  85. if (isset($oForm->aInputs['relocate'])) {
  86. if ($sForceRelocate && 0 === mb_stripos($sForceRelocate, BX_DOL_URL_ROOT))
  87. $oForm->aInputs['relocate']['value'] = $sForceRelocate;
  88. elseif ('homepage' == $sForceRelocate)
  89. $oForm->aInputs['relocate']['value'] = BX_DOL_URL_ROOT;
  90. }
  91. $sFormCode = $oForm->getCode();
  92. BxDolTemplate::getInstance()->addJs(array('jquery.form.min.js'));
  93. $sJoinText = '';
  94. if (strpos($sParams, 'no_join_text') === false)
  95. $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>';
  96. $sAuth = '';
  97. if (strpos($sParams, 'no_auth_buttons') === false)
  98. $sAuth = $this->serviceMemberAuthCode();
  99. $sAjaxForm = '';
  100. if (strpos($sParams, 'ajax_form') !== false)
  101. $sAjaxForm = "<script>
  102. (bx_login_init_ajax_form = function () {
  103. $('#sys-form-login').ajaxForm({
  104. dataType: 'json',
  105. success: function (oData) {
  106. if ('undefined' !== typeof(oData['res']) && 'OK' == oData['res']) {
  107. location.reload();
  108. }
  109. else if ('undefined' !== typeof(oData['form'])) {
  110. $('#sys-form-login').replaceWith(oData['form']);
  111. bx_login_init_ajax_form();
  112. }
  113. }
  114. });
  115. })();
  116. </script>";
  117. return $sCustomHtmlBefore . $sAuth . $sFormCode . $sCustomHtmlAfter . $sJoinText . $sAjaxForm;
  118. }
  119. public function serviceLoginFormStep2 ()
  120. {
  121. if(isLogged()){
  122. header('Location: ' . BX_DOL_URL_ROOT);
  123. exit;
  124. }
  125. $oSession = BxDolSession::getInstance();
  126. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  127. if ($iAccountId == '')
  128. return false;
  129. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step2');
  130. $oForm->aFormAttrs['action'] = '';
  131. $oAccount = BxDolAccount::getInstance($iAccountId);
  132. $a = $oAccount->getInfo($iAccountId);
  133. $sPhoneNumber = trim($a['phone']);
  134. $oForm->initChecker(array('phone' => $sPhoneNumber));
  135. if ($oForm->isSubmittedAndValid()) {
  136. $sNewPhoneNumber = trim($oForm->getCleanValue('phone'));
  137. if ($sPhoneNumber != $sNewPhoneNumber)
  138. $oAccount->updatePhone($sNewPhoneNumber);
  139. $sActivationCode = rand(1000, 9999);
  140. $sActivationText =_t('_sys_txt_login_2fa_sms_text', $sActivationCode);
  141. $ret = null;
  142. bx_alert('account', 'before_2fa_send_sms', $oAccount->id(), false, array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  143. if ($ret === null)
  144. {
  145. $oTwilio = BxDolTwilio::getInstance();
  146. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  147. return MsgBox(_t('_sys_txt_login_2fa_sms_error_occured'));
  148. }
  149. }
  150. $oSession = BxDolSession::getInstance();
  151. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  152. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login-step3'));
  153. }
  154. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_login_2fa_description") . '</div>' .$oForm->getCode();
  155. }
  156. public function serviceLoginFormStep3 ()
  157. {
  158. if(isLogged()){
  159. header('Location: ' . BX_DOL_URL_ROOT);
  160. exit;
  161. }
  162. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step3');
  163. $oForm->aFormAttrs['action'] = '';
  164. $oForm->initChecker();
  165. if ($oForm->isSubmittedAndValid()) {
  166. $oSession = BxDolSession::getInstance();
  167. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  168. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  169. $oAccount = BxDolAccount::getInstance($iAccountId);
  170. $aAccount = bx_login($oAccount->id(), $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_IS_REMEMBER));
  171. $sUrlRelocate = $oForm->getCleanValue('relocate');
  172. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  173. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  174. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  175. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  176. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  177. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  178. header('Location: ' . $sUrlRelocate);
  179. }
  180. return $oForm->getCode();
  181. }
  182. }
  183. /** @} */