BxBaseFormAccount.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /**
  47. * @hooks
  48. * @hookdef hook-system-encrypt_password_after 'system', 'encrypt_password_after' - hook to override password using another encrypt function
  49. * - $unit_name - equals `system`
  50. * - $action - equals `encrypt_password_after`
  51. * - $object_id - not used
  52. * - $sender_id - not used
  53. * - $extra_params - array of additional params with the following array keys:
  54. * - `info` - [array] account info array as key&value pairs
  55. * - `pwd` - [string] original password
  56. * - `password` - [string] by ref, encrypt password, can be overridden in hook processing
  57. * @hook @ref hook-system-encrypt_password_after
  58. */
  59. bx_alert('system', 'encrypt_password_after', 0, false, [
  60. 'info' => $aInfo,
  61. 'pwd' => $s,
  62. 'password' => &$sPassCheck,
  63. ]);
  64. return $aInfo['password'] == $sPassCheck;
  65. }
  66. /**
  67. * Check if phone is exists.
  68. */
  69. function checkPhoneExist ($s)
  70. {
  71. $s = trim($s);
  72. if(!preg_match("/^\+[0-9\s]*$/", $s)) {
  73. return _t('_sys_form_forgot_password_phone_error_format');
  74. }
  75. return BxDolAccountQuery::getInstance()->getIdByPhone($s) ? _t('_sys_form_account_input_phone_uniq_error_loggedin') : true;
  76. }
  77. /**
  78. * Check if email is uniq.
  79. */
  80. function checkEmailUniq ($s)
  81. {
  82. $s = trim($s);
  83. if (!$this->checkEmail($s))
  84. return false;
  85. $oAccount = BxDolAccount::getInstance();
  86. if ($oAccount) { // user is logged in
  87. $aAccountInfo = $oAccount->getInfo();
  88. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  89. return true;
  90. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  91. }
  92. 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;
  93. }
  94. }
  95. /**
  96. * Create/Edit Account Form.
  97. */
  98. class BxBaseFormAccount extends BxTemplFormView
  99. {
  100. static $FIELD_EMAIL = 'email';
  101. static $FIELD_PASSWORD = 'password';
  102. static $FIELD_SALT = 'salt';
  103. static $FIELD_ADDED = 'added';
  104. static $FIELD_CHANGED = 'changed';
  105. static $FIELD_IP = 'ip';
  106. static $FIELD_REFERRED = 'referred';
  107. static $FIELD_PHONE = 'phone';
  108. protected $_bSetPendingApproval = false;
  109. public function __construct($aInfo, $oTemplate)
  110. {
  111. parent::__construct($aInfo, $oTemplate);
  112. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  113. }
  114. function isValid ()
  115. {
  116. if (!parent::isValid ())
  117. return false;
  118. if (isLogged() || !$this->isSubmitted()) return true; // exit in case it is an account edit or form has not been submitted yet
  119. $sErrorMsg = '';
  120. /**
  121. * @hooks
  122. * @hookdef hook-account-check_join 'account', 'check_join' - hook to check email address for spam
  123. * - $unit_name - equals `account`
  124. * - $action - equals `check_join`
  125. * - $object_id - not used
  126. * - $sender_id - not used
  127. * - $extra_params - array of additional params with the following array keys:
  128. * - `error_msg` - [string] by ref, error message, can be overridden in hook processing
  129. * - `email` - [string] email address to check
  130. * - `approve` - [boolean] by ref, pending approval status for created profile, can be overridden in hook processing
  131. * @hook @ref hook-account-check_join
  132. */
  133. bx_alert('account', 'check_join', 0, false, [
  134. 'error_msg' => &$sErrorMsg,
  135. 'email' => $this->getCleanValue('email'),
  136. 'approve' => &$this->_bSetPendingApproval
  137. ]);
  138. if ($sErrorMsg)
  139. $this->_setCustomError ($sErrorMsg);
  140. return $sErrorMsg ? false : true;
  141. }
  142. public function isSetPendingApproval()
  143. {
  144. return $this->_bSetPendingApproval;
  145. }
  146. public function setPendingApproval($b)
  147. {
  148. return ($this->_bSetPendingApproval = $b);
  149. }
  150. public function insert ($aValsToAdd = array(), $isIgnore = false)
  151. {
  152. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  153. $sEmail = trim(strtolower($sEmail));
  154. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  155. $sPhone = trim(strtolower($sPhone));
  156. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  157. $sSalt = genRndSalt();
  158. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  159. $aValsToAdd = array_merge($aValsToAdd, array (
  160. self::$FIELD_EMAIL => $sEmail,
  161. self::$FIELD_PASSWORD => $sPasswordHash,
  162. self::$FIELD_SALT => $sSalt,
  163. self::$FIELD_ADDED => time(),
  164. self::$FIELD_CHANGED => time(),
  165. self::$FIELD_IP => getVisitorIP(),
  166. self::$FIELD_REFERRED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  167. self::$FIELD_PHONE => $sPhone,
  168. ));
  169. return parent::insert ($aValsToAdd, $isIgnore);
  170. }
  171. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  172. {
  173. $sPwd = $this->getCleanValue(self::$FIELD_PASSWORD);
  174. if ($sPwd) {
  175. $sSalt = genRndSalt();
  176. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  177. $oAccount = BxDolAccount::getInstance($val, true);
  178. $iPasswordExpired = $oAccount->getPasswordExpiredDateByAccount($val);
  179. BxDolAccountQuery::getInstance()->logPassword($val);
  180. BxDolAccountQuery::getInstance()->updatePasswordExpired($val, $iPasswordExpired);
  181. }
  182. $aValsToAdd = array_merge(
  183. $aValsToAdd,
  184. array (self::$FIELD_CHANGED => time()),
  185. $sPwd ? array (self::$FIELD_PASSWORD => $sPasswordHash, self::$FIELD_SALT => $sSalt) : array()
  186. );
  187. $bResult = parent::update ($val, $aValsToAdd, $aTrackTextFieldsChanges);
  188. if ($bResult){
  189. $oAccount = BxDolAccount::getInstance($val, true);
  190. if ($oAccount) {
  191. $aAccountInfo = $oAccount->getInfo();
  192. /**
  193. * @hooks
  194. * @hookdef hook-account-change_receive_news 'account', 'change_receive_news' - hook after change receive_news parameter for account
  195. * - $unit_name - equals `system`
  196. * - $action - equals `change_receive_news`
  197. * - $object_id - not used
  198. * - $sender_id - not used
  199. * - $extra_params - array of additional params with the following array keys:
  200. * - `account_id` - [int] account id
  201. * - `old_value` - [bool] old value for receive_news parameter
  202. * - `new_value` - [bool] new value for receive_news parameter
  203. * - `email` - [string] account's email
  204. * @hook @ref hook-account-change_receive_news
  205. */
  206. 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']));
  207. }
  208. }
  209. return $bResult;
  210. }
  211. protected function genCustomInputAgreement ($aInput)
  212. {
  213. $oPermalink = BxDolPermalinks::getInstance();
  214. 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>';
  215. }
  216. protected function _setCustomError ($s)
  217. {
  218. $this->aInputs['do_submit']['error'] = $s;
  219. }
  220. }
  221. /** @} */