BxBaseFormAccount.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. function checkPassword ($s, $r)
  19. {
  20. $bValid = parent::checkPreg($s, $r);
  21. if (!$bValid)
  22. return $bValid;
  23. $aPasswords = BxDolAccountQuery::getInstance()->getLastPasswordLog( BxDolAccount::getInstance()->id());
  24. $bUsed = false;
  25. foreach ($aPasswords as $aPassword){
  26. if($aPassword['password'] == encryptUserPwd($s, $aPassword['salt']))
  27. $bUsed = true;
  28. }
  29. if ($bUsed)
  30. return _t('_sys_form_account_input_password_error_old_used');
  31. return true;
  32. }
  33. /**
  34. * Password confirmation check.
  35. */
  36. function checkPasswordCurrent ($s)
  37. {
  38. $oAccount = BxDolAccount::getInstance();
  39. if (!$oAccount)
  40. return false;
  41. $aInfo = $oAccount->getInfo();
  42. $sPassCheck = encryptUserPwd($s, $aInfo['salt']);
  43. // regenerate password using another encrypt function if necessary
  44. bx_alert('system', 'encrypt_password_after', 0, false, array(
  45. 'info' => $aInfo,
  46. 'pwd' => $s,
  47. 'password' => &$sPassCheck,
  48. ));
  49. return $aInfo['password'] == $sPassCheck;
  50. }
  51. /**
  52. * Check if phone is exists.
  53. */
  54. function checkPhoneExist ($s)
  55. {
  56. $s = trim($s);
  57. if(!preg_match("/^\+[0-9\s]*$/", $s)) {
  58. return _t('_sys_form_forgot_password_phone_error_format');
  59. }
  60. return BxDolAccountQuery::getInstance()->getIdByPhone($s) ? _t('_sys_form_account_input_phone_uniq_error_loggedin') : true;
  61. }
  62. /**
  63. * Check if email is uniq.
  64. */
  65. function checkEmailUniq ($s)
  66. {
  67. $s = trim($s);
  68. if (!$this->checkEmail($s))
  69. return false;
  70. $oAccount = BxDolAccount::getInstance();
  71. if ($oAccount) { // user is logged in
  72. $aAccountInfo = $oAccount->getInfo();
  73. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  74. return true;
  75. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  76. }
  77. 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;
  78. }
  79. }
  80. /**
  81. * Create/Edit Account Form.
  82. */
  83. class BxBaseFormAccount extends BxTemplFormView
  84. {
  85. static $FIELD_EMAIL = 'email';
  86. static $FIELD_PASSWORD = 'password';
  87. static $FIELD_SALT = 'salt';
  88. static $FIELD_ADDED = 'added';
  89. static $FIELD_CHANGED = 'changed';
  90. static $FIELD_IP = 'ip';
  91. static $FIELD_REFERRED = 'referred';
  92. static $FIELD_PHONE = 'phone';
  93. protected $_bSetPendingApproval = false;
  94. public function __construct($aInfo, $oTemplate)
  95. {
  96. parent::__construct($aInfo, $oTemplate);
  97. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  98. }
  99. function isValid ()
  100. {
  101. if (!parent::isValid ())
  102. return false;
  103. if (isLogged() || !$this->isSubmitted()) return true; // exit in case it is an account edit or form has not been submitted yet
  104. $sErrorMsg = '';
  105. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $this->getCleanValue('email'), 'approve' => &$this->_bSetPendingApproval));
  106. if ($sErrorMsg)
  107. $this->_setCustomError ($sErrorMsg);
  108. return $sErrorMsg ? false : true;
  109. }
  110. public function isSetPendingApproval()
  111. {
  112. return $this->_bSetPendingApproval;
  113. }
  114. public function setPendingApproval($b)
  115. {
  116. return ($this->_bSetPendingApproval = $b);
  117. }
  118. public function insert ($aValsToAdd = array(), $isIgnore = false)
  119. {
  120. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  121. $sEmail = trim(strtolower($sEmail));
  122. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  123. $sPhone = trim(strtolower($sPhone));
  124. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  125. $sSalt = genRndSalt();
  126. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  127. $aValsToAdd = array_merge($aValsToAdd, array (
  128. self::$FIELD_EMAIL => $sEmail,
  129. self::$FIELD_PASSWORD => $sPasswordHash,
  130. self::$FIELD_SALT => $sSalt,
  131. self::$FIELD_ADDED => time(),
  132. self::$FIELD_CHANGED => time(),
  133. self::$FIELD_IP => getVisitorIP(),
  134. self::$FIELD_REFERRED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  135. self::$FIELD_PHONE => $sPhone,
  136. ));
  137. return parent::insert ($aValsToAdd, $isIgnore);
  138. }
  139. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  140. {
  141. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  142. if ($sPwd) {
  143. $sSalt = genRndSalt();
  144. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  145. $oAccount = BxDolAccount::getInstance($val, true);
  146. $iPasswordExpired = $oAccount->getPasswordExpiredDateByAccount($val);
  147. BxDolAccountQuery::getInstance()->logPassword($val);
  148. BxDolAccountQuery::getInstance()->updatePasswordExpired($val, $iPasswordExpired);
  149. }
  150. $aValsToAdd = array_merge(
  151. $aValsToAdd,
  152. array (self::$FIELD_CHANGED => time()),
  153. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  154. );
  155. $bResult = parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  156. if ($bResult){
  157. $oAccount = BxDolAccount::getInstance($val, true);
  158. if ($oAccount) {
  159. $aAccountInfo = $oAccount->getInfo();
  160. 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']));
  161. }
  162. }
  163. return $bResult;
  164. }
  165. protected function genCustomInputAgreement ($aInput)
  166. {
  167. $oPermalink = BxDolPermalinks::getInstance();
  168. 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>';
  169. }
  170. protected function _setCustomError ($s)
  171. {
  172. $this->aInputs['do_submit']['error'] = $s;
  173. }
  174. }
  175. /** @} */