BxBaseFormAccount.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 phone is exists.
  38. */
  39. function checkPhoneExist ($s)
  40. {
  41. $s = trim($s);
  42. if(!preg_match("/^\+[0-9\s]*$/", $s)) {
  43. return _t('_sys_form_forgot_password_phone_error_format');
  44. }
  45. return BxDolAccountQuery::getInstance()->getIdByPhone($s) ? _t('_sys_form_account_input_phone_uniq_error_loggedin') : true;
  46. }
  47. /**
  48. * Check if email is uniq.
  49. */
  50. function checkEmailUniq ($s)
  51. {
  52. $s = trim($s);
  53. if (!$this->checkEmail($s))
  54. return false;
  55. $oAccount = BxDolAccount::getInstance();
  56. if ($oAccount) { // user is logged in
  57. $aAccountInfo = $oAccount->getInfo();
  58. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  59. return true;
  60. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  61. }
  62. 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;
  63. }
  64. }
  65. /**
  66. * Create/Edit Account Form.
  67. */
  68. class BxBaseFormAccount extends BxTemplFormView
  69. {
  70. static $FIELD_EMAIL = 'email';
  71. static $FIELD_PASSWORD = 'password';
  72. static $FIELD_SALT = 'salt';
  73. static $FIELD_ADDED = 'added';
  74. static $FIELD_CHANGED = 'changed';
  75. static $FIELD_IP = 'ip';
  76. static $FIELD_REFERRED = 'referred';
  77. static $FIELD_PHONE = 'phone';
  78. protected $_bSetPendingApproval = false;
  79. public function __construct($aInfo, $oTemplate)
  80. {
  81. parent::__construct($aInfo, $oTemplate);
  82. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  83. }
  84. function isValid ()
  85. {
  86. if (!parent::isValid ())
  87. return false;
  88. $sErrorMsg = '';
  89. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $this->getCleanValue('email'), 'approve' => &$this->_bSetPendingApproval));
  90. if ($sErrorMsg)
  91. $this->_setCustomError ($sErrorMsg);
  92. return $sErrorMsg ? false : true;
  93. }
  94. public function isSetPendingApproval()
  95. {
  96. return $this->_bSetPendingApproval;
  97. }
  98. public function setPendingApproval($b)
  99. {
  100. return ($this->_bSetPendingApproval = $b);
  101. }
  102. public function insert ($aValsToAdd = array(), $isIgnore = false)
  103. {
  104. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  105. $sEmail = trim(strtolower($sEmail));
  106. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  107. $sPhone = trim(strtolower($sPhone));
  108. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  109. $sSalt = genRndSalt();
  110. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  111. $aValsToAdd = array_merge($aValsToAdd, array (
  112. self::$FIELD_EMAIL => $sEmail,
  113. self::$FIELD_PASSWORD => $sPasswordHash,
  114. self::$FIELD_SALT => $sSalt,
  115. self::$FIELD_ADDED => time(),
  116. self::$FIELD_CHANGED => time(),
  117. self::$FIELD_IP => getVisitorIP(),
  118. self::$FIELD_REFERRED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  119. self::$FIELD_PHONE => $sPhone,
  120. ));
  121. return parent::insert ($aValsToAdd, $isIgnore);
  122. }
  123. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  124. {
  125. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  126. if ($sPwd) {
  127. $sSalt = genRndSalt();
  128. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  129. }
  130. $aValsToAdd = array_merge(
  131. $aValsToAdd,
  132. array (self::$FIELD_CHANGED => time()),
  133. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  134. );
  135. $bResult = parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  136. if ($bResult){
  137. $oAccount = BxDolAccount::getInstance($val);
  138. if ($oAccount) {
  139. $aAccountInfo = $oAccount->getInfo();
  140. bx_alert('account', 'change_receive_news', 0, false, array('account_id' => $val, 'old_value' => $aAccountInfo['receive_news'], 'new_value' => $this->getCleanValue('receive_news'), 'email' => $aAccountInfo['email']));
  141. }
  142. }
  143. return $bResult;
  144. }
  145. protected function genCustomInputAgreement ($aInput)
  146. {
  147. $oPermalink = BxDolPermalinks::getInstance();
  148. 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>';
  149. }
  150. protected function _setCustomError ($s)
  151. {
  152. $this->aInputs['do_submit']['error'] = $s;
  153. }
  154. }
  155. /** @} */