1
0

BxBaseFormAccount.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_PASSWORD_CHANGED = 'password_changed';
  103. static $FIELD_SALT = 'salt';
  104. static $FIELD_ADDED = 'added';
  105. static $FIELD_CHANGED = 'changed';
  106. static $FIELD_IP = 'ip';
  107. static $FIELD_REFERRED = 'referred';
  108. static $FIELD_PHONE = 'phone';
  109. protected $_bSetPendingApproval = false;
  110. public function __construct($aInfo, $oTemplate)
  111. {
  112. parent::__construct($aInfo, $oTemplate);
  113. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  114. }
  115. function isValid ()
  116. {
  117. if (!parent::isValid ())
  118. return false;
  119. if (isLogged() || !$this->isSubmitted()) return true; // exit in case it is an account edit or form has not been submitted yet
  120. $sErrorMsg = '';
  121. /**
  122. * @hooks
  123. * @hookdef hook-account-check_join 'account', 'check_join' - hook to check email address for spam
  124. * - $unit_name - equals `account`
  125. * - $action - equals `check_join`
  126. * - $object_id - not used
  127. * - $sender_id - not used
  128. * - $extra_params - array of additional params with the following array keys:
  129. * - `error_msg` - [string] by ref, error message, can be overridden in hook processing
  130. * - `email` - [string] email address to check
  131. * - `approve` - [boolean] by ref, pending approval status for created profile, can be overridden in hook processing
  132. * @hook @ref hook-account-check_join
  133. */
  134. bx_alert('account', 'check_join', 0, false, [
  135. 'error_msg' => &$sErrorMsg,
  136. 'email' => $this->getCleanValue('email'),
  137. 'approve' => &$this->_bSetPendingApproval
  138. ]);
  139. if ($sErrorMsg)
  140. $this->_setCustomError ($sErrorMsg);
  141. return $sErrorMsg ? false : true;
  142. }
  143. public function isSetPendingApproval()
  144. {
  145. return $this->_bSetPendingApproval;
  146. }
  147. public function setPendingApproval($b)
  148. {
  149. return ($this->_bSetPendingApproval = $b);
  150. }
  151. public function insert ($aValsToAdd = array(), $isIgnore = false)
  152. {
  153. $iNow = time();
  154. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  155. $sEmail = trim(strtolower($sEmail));
  156. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  157. $sPhone = trim(strtolower($sPhone));
  158. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  159. $sSalt = genRndSalt();
  160. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  161. return parent::insert(array_merge($aValsToAdd, [
  162. self::$FIELD_EMAIL => $sEmail,
  163. self::$FIELD_PASSWORD => $sPasswordHash,
  164. self::$FIELD_PASSWORD_CHANGED => $iNow,
  165. self::$FIELD_SALT => $sSalt,
  166. self::$FIELD_ADDED => $iNow,
  167. self::$FIELD_CHANGED => $iNow,
  168. self::$FIELD_IP => getVisitorIP(),
  169. self::$FIELD_REFERRED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  170. self::$FIELD_PHONE => $sPhone,
  171. ]), $isIgnore);
  172. }
  173. function update ($val, $aValsToAdd = [], &$aTrackTextFieldsChanges = null)
  174. {
  175. $oDb = BxDolAccountQuery::getInstance();
  176. $iNow = time();
  177. $_aValsToAdd = [
  178. self::$FIELD_CHANGED => $iNow
  179. ];
  180. if(($sPwd = $this->getCleanValue(self::$FIELD_PASSWORD))) {
  181. $oDb->logPassword($val);
  182. $sSalt = genRndSalt();
  183. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  184. $_aValsToAdd = array_merge($_aValsToAdd, [
  185. self::$FIELD_PASSWORD => $sPasswordHash,
  186. self::$FIELD_PASSWORD_CHANGED => $iNow,
  187. self::$FIELD_SALT => $sSalt
  188. ]);
  189. }
  190. $aInfoOld = $oDb->getInfoById($val);
  191. $bResult = parent::update($val, array_merge($aValsToAdd, $_aValsToAdd), $aTrackTextFieldsChanges);
  192. if($bResult) {
  193. $aInfoNew = $oDb->getInfoById($val);
  194. /**
  195. * @hooks
  196. * @hookdef hook-account-change_receive_news 'account', 'change_receive_news' - hook after change receive_news parameter for account
  197. * - $unit_name - equals `system`
  198. * - $action - equals `change_receive_news`
  199. * - $object_id - not used
  200. * - $sender_id - not used
  201. * - $extra_params - array of additional params with the following array keys:
  202. * - `account_id` - [int] account id
  203. * - `old_value` - [bool] old value for receive_news parameter
  204. * - `new_value` - [bool] new value for receive_news parameter
  205. * - `email` - [string] account's email
  206. * @hook @ref hook-account-change_receive_news
  207. */
  208. bx_alert('account', 'change_receive_news', 0, false, [
  209. 'account_id' => $val,
  210. 'old_value' => $aInfoOld['receive_news'],
  211. 'new_value' => $aInfoNew['receive_news'],
  212. 'email' => $aInfoNew['email']]
  213. );
  214. }
  215. return $bResult;
  216. }
  217. protected function genCustomInputAgreement ($aInput)
  218. {
  219. $oPermalink = BxDolPermalinks::getInstance();
  220. 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>';
  221. }
  222. protected function _setCustomError ($s)
  223. {
  224. $this->aInputs['do_submit']['error'] = $s;
  225. }
  226. }
  227. /** @} */