BxBaseFormAccount.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 BxFormAccountCheckerHelper extends BxDolFormCheckerHelper
  10. {
  11. /**
  12. * Password confirmation check.
  13. */
  14. function checkPasswordConfirm ($s)
  15. {
  16. return $s == bx_process_input(bx_get(BxTemplFormAccount::$FIELD_PASSWORD));
  17. }
  18. /**
  19. * Password confirmation check.
  20. */
  21. function checkPasswordCurrent ($s)
  22. {
  23. $oAccount = BxDolAccount::getInstance();
  24. if (!$oAccount)
  25. return false;
  26. $aInfo = $oAccount->getInfo();
  27. $sPassCheck = encryptUserPwd($s, $aInfo['salt']);
  28. // regenerate password using another encrypt function if necessary
  29. bx_alert('system', 'encrypt_password_after', 0, false, array(
  30. 'info' => $aInfo,
  31. 'pwd' => $s,
  32. 'password' => &$sPassCheck,
  33. ));
  34. return $aInfo['password'] == $sPassCheck;
  35. }
  36. /**
  37. * Check if email is uniq.
  38. */
  39. function checkEmailUniq ($s)
  40. {
  41. $s = trim($s);
  42. if (!$this->checkEmail($s))
  43. return false;
  44. $oAccount = BxDolAccount::getInstance();
  45. if ($oAccount) { // user is logged in
  46. $aAccountInfo = $oAccount->getInfo();
  47. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  48. return true;
  49. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  50. }
  51. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password')) : true;
  52. }
  53. }
  54. /**
  55. * Create/Edit Account Form.
  56. */
  57. class BxBaseFormAccount extends BxTemplFormView
  58. {
  59. static $FIELD_EMAIL = 'email';
  60. static $FIELD_PASSWORD = 'password';
  61. static $FIELD_SALT = 'salt';
  62. static $FIELD_ADDED = 'added';
  63. static $FIELD_CHANGED = 'changed';
  64. static $FIELD_IP = 'ip';
  65. static $FIELD_REFFERED = 'reffered';
  66. static $FIELD_PHONE = 'phone';
  67. protected $_bSetPendingApproval = false;
  68. public function __construct($aInfo, $oTemplate)
  69. {
  70. parent::__construct($aInfo, $oTemplate);
  71. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  72. }
  73. function isValid ()
  74. {
  75. if (!parent::isValid ())
  76. return false;
  77. $sErrorMsg = '';
  78. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $this->getCleanValue('email'), 'approve' => &$this->_bSetPendingApproval));
  79. if ($sErrorMsg)
  80. $this->_setCustomError ($sErrorMsg);
  81. return $sErrorMsg ? false : true;
  82. }
  83. public function isSetPendingApproval()
  84. {
  85. return $this->_bSetPendingApproval;
  86. }
  87. public function setPendingApproval($b)
  88. {
  89. return ($this->_bSetPendingApproval = $b);
  90. }
  91. public function insert ($aValsToAdd = array(), $isIgnore = false)
  92. {
  93. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  94. $sEmail = trim(strtolower($sEmail));
  95. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  96. $sPhone = trim(strtolower($sPhone));
  97. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  98. $sSalt = genRndSalt();
  99. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  100. $aValsToAdd = array_merge($aValsToAdd, array (
  101. self::$FIELD_EMAIL => $sEmail,
  102. self::$FIELD_PASSWORD => $sPasswordHash,
  103. self::$FIELD_SALT => $sSalt,
  104. self::$FIELD_ADDED => time(),
  105. self::$FIELD_CHANGED => time(),
  106. self::$FIELD_IP => getVisitorIP(),
  107. self::$FIELD_REFFERED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  108. self::$FIELD_PHONE => $sPhone,
  109. ));
  110. return parent::insert ($aValsToAdd, $isIgnore);
  111. }
  112. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  113. {
  114. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  115. if ($sPwd) {
  116. $sSalt = genRndSalt();
  117. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  118. }
  119. $aValsToAdd = array_merge(
  120. $aValsToAdd,
  121. array (self::$FIELD_CHANGED => time()),
  122. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  123. );
  124. return parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  125. }
  126. protected function genCustomInputAgreement ($aInput)
  127. {
  128. $oPermalink = BxDolPermalinks::getInstance();
  129. return '<div>' . _t('_sys_form_account_input_agreement_value', BX_DOL_URL_ROOT . $oPermalink->permalink('page.php?i=terms'), BX_DOL_URL_ROOT . $oPermalink->permalink('page.php?i=privacy')) . '</div>';
  130. }
  131. protected function _setCustomError ($s)
  132. {
  133. $this->aInputs['do_submit']['error'] = $s;
  134. }
  135. }
  136. /** @} */