BxBaseFormAccount.php 8.0 KB

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