BxBaseFormAccount.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxTemplFormView');
  10. class BxFormAccountCheckerHelper extends BxDolFormCheckerHelper
  11. {
  12. /**
  13. * Password confirmation check.
  14. */
  15. function checkPasswordConfirm ($s)
  16. {
  17. return $s == bx_process_input(bx_get(BxTemplFormAccount::$FIELD_PASSWORD));
  18. }
  19. /**
  20. * Password confirmation check.
  21. */
  22. function checkPasswordCurrent ($s)
  23. {
  24. bx_import('BxDolAccount');
  25. $oAccount = BxDolAccount::getInstance();
  26. if (!$oAccount)
  27. return false;
  28. $aInfo = $oAccount->getInfo();
  29. return $aInfo['password'] == encryptUserPwd($s, $aInfo['salt']);
  30. }
  31. /**
  32. * Check if email is uniq.
  33. */
  34. function checkEmailUniq ($s)
  35. {
  36. if (!$this->checkEmail($s))
  37. return false;
  38. bx_import('BxDolAccount');
  39. bx_import('BxDolPermalinks');
  40. $oAccount = BxDolAccount::getInstance();
  41. if ($oAccount) { // user is logged in
  42. $aAccountInfo = $oAccount->getInfo();
  43. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  44. return true;
  45. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  46. }
  47. 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;
  48. }
  49. }
  50. /**
  51. * Create/Edit Account Form.
  52. */
  53. class BxBaseFormAccount extends BxTemplFormView
  54. {
  55. static $FIELD_PASSWORD = 'password';
  56. static $FIELD_SALT = 'salt';
  57. static $FIELD_ADDED = 'added';
  58. static $FIELD_CHANGED = 'changed';
  59. protected $_bSetPendingApproval = false;
  60. public function __construct($aInfo, $oTemplate)
  61. {
  62. parent::__construct($aInfo, $oTemplate);
  63. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  64. }
  65. function isValid ()
  66. {
  67. if (!parent::isValid ())
  68. return false;
  69. $sErrorMsg = '';
  70. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $this->getCleanValue('email'), 'approve' => &$this->_bSetPendingApproval));
  71. if ($sErrorMsg)
  72. $this->_setCustomError ($sErrorMsg);
  73. return $sErrorMsg ? false : true;
  74. }
  75. public function isSetPendingApproval()
  76. {
  77. return $this->_bSetPendingApproval;
  78. }
  79. public function insert ($aValsToAdd = array(), $isIgnore = false)
  80. {
  81. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  82. $sSalt = genRndSalt();
  83. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  84. $aValsToAdd = array_merge($aValsToAdd, array (
  85. self::$FIELD_PASSWORD => $sPasswordHash,
  86. self::$FIELD_SALT => $sSalt,
  87. self::$FIELD_ADDED => time(),
  88. self::$FIELD_CHANGED => time(),
  89. ));
  90. return parent::insert ($aValsToAdd, $isIgnore);
  91. }
  92. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  93. {
  94. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  95. if ($sPwd) {
  96. $sSalt = genRndSalt();
  97. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  98. }
  99. $aValsToAdd = array_merge(
  100. $aValsToAdd,
  101. array (self::$FIELD_CHANGED => time()),
  102. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  103. );
  104. return parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  105. }
  106. protected function _setCustomError ($s)
  107. {
  108. $this->aInputs['do_submit']['error'] = $s;
  109. }
  110. }
  111. /** @} */