BxBaseFormAccount.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  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. return $aInfo['password'] == encryptUserPwd($s, $aInfo['salt']);
  28. }
  29. /**
  30. * Check if email is uniq.
  31. */
  32. function checkEmailUniq ($s)
  33. {
  34. $s = trim($s);
  35. if (!$this->checkEmail($s))
  36. return false;
  37. $oAccount = BxDolAccount::getInstance();
  38. if ($oAccount) { // user is logged in
  39. $aAccountInfo = $oAccount->getInfo();
  40. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  41. return true;
  42. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  43. }
  44. 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;
  45. }
  46. }
  47. /**
  48. * Create/Edit Account Form.
  49. */
  50. class BxBaseFormAccount extends BxTemplFormView
  51. {
  52. static $FIELD_EMAIL = 'email';
  53. static $FIELD_PASSWORD = 'password';
  54. static $FIELD_SALT = 'salt';
  55. static $FIELD_ADDED = 'added';
  56. static $FIELD_CHANGED = 'changed';
  57. protected $_bSetPendingApproval = false;
  58. public function __construct($aInfo, $oTemplate)
  59. {
  60. if(isset($aInfo['inputs']['agreement'])) {
  61. $oPermalink = BxDolPermalinks::getInstance();
  62. $aInfo['inputs']['agreement']['content'] = _t('_sys_form_account_input_agreement_value', $oPermalink->permalink('page.php?i=terms'), $oPermalink->permalink('page.php?i=privacy'));
  63. }
  64. parent::__construct($aInfo, $oTemplate);
  65. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  66. }
  67. function isValid ()
  68. {
  69. if (!parent::isValid ())
  70. return false;
  71. $sErrorMsg = '';
  72. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $this->getCleanValue('email'), 'approve' => &$this->_bSetPendingApproval));
  73. if ($sErrorMsg)
  74. $this->_setCustomError ($sErrorMsg);
  75. return $sErrorMsg ? false : true;
  76. }
  77. public function isSetPendingApproval()
  78. {
  79. return $this->_bSetPendingApproval;
  80. }
  81. public function setPendingApproval($b)
  82. {
  83. return ($this->_bSetPendingApproval = $b);
  84. }
  85. public function insert ($aValsToAdd = array(), $isIgnore = false)
  86. {
  87. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  88. $sEmail = trim(strtolower($sEmail));
  89. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  90. $sSalt = genRndSalt();
  91. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  92. $aValsToAdd = array_merge($aValsToAdd, array (
  93. self::$FIELD_EMAIL => $sEmail,
  94. self::$FIELD_PASSWORD => $sPasswordHash,
  95. self::$FIELD_SALT => $sSalt,
  96. self::$FIELD_ADDED => time(),
  97. self::$FIELD_CHANGED => time(),
  98. ));
  99. return parent::insert ($aValsToAdd, $isIgnore);
  100. }
  101. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  102. {
  103. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  104. if ($sPwd) {
  105. $sSalt = genRndSalt();
  106. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  107. }
  108. $aValsToAdd = array_merge(
  109. $aValsToAdd,
  110. array (self::$FIELD_CHANGED => time()),
  111. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  112. );
  113. return parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  114. }
  115. protected function _setCustomError ($s)
  116. {
  117. $this->aInputs['do_submit']['error'] = $s;
  118. }
  119. }
  120. /** @} */