BxBaseFormLogin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class BxFormLoginCheckerHelper extends BxDolFormCheckerHelper
  10. {
  11. /**
  12. * Check if key exists or wrong format
  13. */
  14. function checkPhoneExist($s)
  15. {
  16. $s = trim($s);
  17. if(!preg_match("/^\+[0-9\s]*$/", $s)) {
  18. return _t('_sys_form_login_input_phone_error_format');
  19. }
  20. $oSession = BxDolSession::getInstance();
  21. $oAccount = BxDolAccount::getInstance($oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID));
  22. if ($oAccount) { // user is logged in
  23. $aAccountInfo = $oAccount->getInfo();
  24. if ($s == $aAccountInfo['phone']) // don't check phone for uniq, if it wasn't changed
  25. return true;
  26. }
  27. return BxDolAccountQuery::getInstance()->getIdByPhone($s) ? _t('_sys_form_login_input_phone_error_not_unique') : true;
  28. }
  29. function checkCodeExist($s)
  30. {
  31. $s = trim($s);
  32. if($s == '') {
  33. return _t('_sys_form_login_input_code_error_empty');
  34. }
  35. $oSession = BxDolSession::getInstance();
  36. return $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE) != $s ? _t('_sys_form_login_input_code_error_invalid', bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=login-step2'))) : true;
  37. }
  38. }
  39. /**
  40. * Login Form.
  41. */
  42. class BxBaseFormLogin extends BxTemplFormView
  43. {
  44. protected $_iRole = BX_DOL_ROLE_MEMBER;
  45. public function __construct($aInfo, $oTemplate)
  46. {
  47. parent::__construct($aInfo, $oTemplate);
  48. $this->aParams['checker_errors']['summary']['disabled'] = true;
  49. $this->_aFieldsExcludeFromCheckForSpam = ['ID'];
  50. if (isset($this->aInputs['ID']))
  51. $this->aInputs['ID']['skip_domain_check'] = true;
  52. if (isset($this->aInputs['relocate'])) {
  53. $sRelocate = bx_process_input(bx_get('relocate'));
  54. if (!$sRelocate && isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) {
  55. $sRelocate = $_SERVER['HTTP_REFERER'];
  56. $aNoRelocatePages = ['forgot-password', 'login', 'create-account', 'logout'];
  57. foreach ($aNoRelocatePages as $s) {
  58. if (false !== mb_stripos($_SERVER['HTTP_REFERER'], $s)) {
  59. $sRelocate = BX_DOL_URL_ROOT . 'member.php';
  60. break;
  61. }
  62. }
  63. }
  64. $this->aInputs['relocate']['value'] = $sRelocate ? $sRelocate : BX_DOL_URL_ROOT . 'member.php';
  65. }
  66. }
  67. function isValid ()
  68. {
  69. if (!parent::isValid())
  70. return false;
  71. if (!isset($this->aInputs['ID']))
  72. return true;
  73. $sId = trim($this->getCleanValue('ID'));
  74. $sPassword = trim($this->getCleanValue('Password'));
  75. if ($sId != ''){
  76. $sErrorString = bx_check_password($sId, $sPassword, $this->getRole());
  77. $this->_setCustomError ($sErrorString);
  78. return $sErrorString ? false : true;
  79. }
  80. else {
  81. $this->_setCustomError (_t('_sys_txt_error_occured'));
  82. return false;
  83. }
  84. }
  85. protected function genCustomInputSubmitText ($aInput)
  86. {
  87. return '<div><a href="' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password')) . '">' . _t("_sys_txt_forgot_pasword") . '</a></div>';
  88. }
  89. public function getRole()
  90. {
  91. return $this->_iRole;
  92. }
  93. public function setRole ($iRole)
  94. {
  95. $this->_iRole = $iRole == BX_DOL_ROLE_ADMIN ? BX_DOL_ROLE_ADMIN : BX_DOL_ROLE_MEMBER;
  96. }
  97. public function getRememberMe()
  98. {
  99. return isset($this->aInputs['rememberMe']) ? (bool)$this->getCleanValue('rememberMe') : bx_is_remember_me();
  100. }
  101. public function getLoginError ()
  102. {
  103. return isset($this->aInputs['ID']['error']) ? $this->aInputs['ID']['error'] : '';
  104. }
  105. protected function _setCustomError ($s)
  106. {
  107. $this->aInputs['ID']['error'] = $s;
  108. }
  109. }
  110. /** @} */