BxBaseServiceLogin.php 8.7 KB

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