BxBaseFormAccount.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  64. function isValid ()
  65. {
  66. if (!parent::isValid ())
  67. return false;
  68. $sErrorMsg = '';
  69. bx_alert('account', 'check_join', 0, false, array('error_msg' => &$sErrorMsg, 'email' => $this->getCleanValue('email'), 'approve' => &$this->_bSetPendingApproval));
  70. if ($sErrorMsg)
  71. $this->_setCustomError ($sErrorMsg);
  72. return $sErrorMsg ? false : true;
  73. }
  74. public function isSetPendingApproval()
  75. {
  76. return $this->_bSetPendingApproval;
  77. }
  78. public function insert ($aValsToAdd = array(), $isIgnore = false)
  79. {
  80. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  81. $sSalt = genRndSalt();
  82. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  83. $aValsToAdd = array_merge($aValsToAdd, array (
  84. self::$FIELD_PASSWORD => $sPasswordHash,
  85. self::$FIELD_SALT => $sSalt,
  86. self::$FIELD_ADDED => time(),
  87. self::$FIELD_CHANGED => time(),
  88. ));
  89. return parent::insert ($aValsToAdd, $isIgnore);
  90. }
  91. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  92. {
  93. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  94. if ($sPwd) {
  95. $sSalt = genRndSalt();
  96. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  97. }
  98. $aValsToAdd = array_merge(
  99. $aValsToAdd,
  100. array (self::$FIELD_CHANGED => time()),
  101. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  102. );
  103. return parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  104. }
  105. protected function _setCustomError ($s)
  106. {
  107. $this->aInputs['do_submit']['error'] = $s;
  108. }
  109. }
  110. /** @} */