BxBaseFormAccount.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. protected $_sGhostTemplate = 'account_form_ghost_template.html';
  111. public function __construct($aInfo, $oTemplate)
  112. {
  113. parent::__construct($aInfo, $oTemplate);
  114. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  115. if(($sField = 'picture') && isset($this->aInputs[$sField])) {
  116. /**
  117. * Note. For now Account Picture is available in edit form only.
  118. * To make it available on create form Uploader should correctly support
  119. * uploading by visitor.
  120. */
  121. if($this->aParams['display'] == 'sys_account_settings_info')
  122. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], [
  123. 'storage_object' => 'sys_accounts_pictures',
  124. 'uploaders' => !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : ['sys_crop'],
  125. 'images_transcoder' => 'sys_accounts_thumb',
  126. 'storage_private' => 0,
  127. 'multiple' => false,
  128. 'content_id' => 0,
  129. 'ghost_template' => ''
  130. ]);
  131. else
  132. unset($this->aInputs[$sField]);
  133. }
  134. }
  135. function isValid ()
  136. {
  137. if (!parent::isValid ())
  138. return false;
  139. if (isLogged() || !$this->isSubmitted()) return true; // exit in case it is an account edit or form has not been submitted yet
  140. $sErrorMsg = '';
  141. /**
  142. * @hooks
  143. * @hookdef hook-account-check_join 'account', 'check_join' - hook to check email address for spam
  144. * - $unit_name - equals `account`
  145. * - $action - equals `check_join`
  146. * - $object_id - not used
  147. * - $sender_id - not used
  148. * - $extra_params - array of additional params with the following array keys:
  149. * - `error_msg` - [string] by ref, error message, can be overridden in hook processing
  150. * - `email` - [string] email address to check
  151. * - `approve` - [boolean] by ref, pending approval status for created profile, can be overridden in hook processing
  152. * @hook @ref hook-account-check_join
  153. */
  154. bx_alert('account', 'check_join', 0, false, [
  155. 'error_msg' => &$sErrorMsg,
  156. 'email' => $this->getCleanValue('email'),
  157. 'approve' => &$this->_bSetPendingApproval
  158. ]);
  159. if ($sErrorMsg)
  160. $this->_setCustomError ($sErrorMsg);
  161. return $sErrorMsg ? false : true;
  162. }
  163. public function isSetPendingApproval()
  164. {
  165. return $this->_bSetPendingApproval;
  166. }
  167. public function setPendingApproval($b)
  168. {
  169. return ($this->_bSetPendingApproval = $b);
  170. }
  171. public function initChecker ($aValues = [], $aSpecificValues = [])
  172. {
  173. $bValues = $aValues && !empty($aValues['id']);
  174. if(($sField = 'picture') && isset($this->aInputs[$sField])) {
  175. if($bValues)
  176. $this->aInputs[$sField]['content_id'] = $aValues['id'];
  177. $this->aInputs[$sField]['ghost_template'] = $this->oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getGhostTmplVars($sField));
  178. }
  179. parent::initChecker($aValues, $aSpecificValues);
  180. }
  181. public function insert ($aValsToAdd = array(), $isIgnore = false)
  182. {
  183. $iNow = time();
  184. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  185. $sEmail = trim(strtolower($sEmail));
  186. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  187. $sPhone = trim(strtolower($sPhone));
  188. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  189. $sSalt = genRndSalt();
  190. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  191. return parent::insert(array_merge($aValsToAdd, [
  192. self::$FIELD_EMAIL => $sEmail,
  193. self::$FIELD_PASSWORD => $sPasswordHash,
  194. self::$FIELD_PASSWORD_CHANGED => $iNow,
  195. self::$FIELD_SALT => $sSalt,
  196. self::$FIELD_ADDED => $iNow,
  197. self::$FIELD_CHANGED => $iNow,
  198. self::$FIELD_IP => getVisitorIP(),
  199. self::$FIELD_REFERRED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  200. self::$FIELD_PHONE => $sPhone,
  201. ]), $isIgnore);
  202. }
  203. function update ($val, $aValsToAdd = [], &$aTrackTextFieldsChanges = null)
  204. {
  205. $oDb = BxDolAccountQuery::getInstance();
  206. $iNow = time();
  207. $_aValsToAdd = [
  208. self::$FIELD_CHANGED => $iNow
  209. ];
  210. if(($sPwd = $this->getCleanValue(self::$FIELD_PASSWORD))) {
  211. $oDb->logPassword($val);
  212. $sSalt = genRndSalt();
  213. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  214. $_aValsToAdd = array_merge($_aValsToAdd, [
  215. self::$FIELD_PASSWORD => $sPasswordHash,
  216. self::$FIELD_PASSWORD_CHANGED => $iNow,
  217. self::$FIELD_SALT => $sSalt
  218. ]);
  219. }
  220. $aInfoOld = $oDb->getInfoById($val);
  221. $bResult = parent::update($val, array_merge($aValsToAdd, $_aValsToAdd), $aTrackTextFieldsChanges);
  222. if($bResult) {
  223. $aInfoNew = $oDb->getInfoById($val);
  224. /**
  225. * @hooks
  226. * @hookdef hook-account-change_receive_news 'account', 'change_receive_news' - hook after change receive_news parameter for account
  227. * - $unit_name - equals `system`
  228. * - $action - equals `change_receive_news`
  229. * - $object_id - not used
  230. * - $sender_id - not used
  231. * - $extra_params - array of additional params with the following array keys:
  232. * - `account_id` - [int] account id
  233. * - `old_value` - [bool] old value for receive_news parameter
  234. * - `new_value` - [bool] new value for receive_news parameter
  235. * - `email` - [string] account's email
  236. * @hook @ref hook-account-change_receive_news
  237. */
  238. bx_alert('account', 'change_receive_news', 0, false, [
  239. 'account_id' => $val,
  240. 'old_value' => $aInfoOld['receive_news'],
  241. 'new_value' => $aInfoNew['receive_news'],
  242. 'email' => $aInfoNew['email']]
  243. );
  244. }
  245. return $bResult;
  246. }
  247. public function processFiles($sFieldFile, $iAccountId = 0, $isAssociateWithContent = false)
  248. {
  249. if (!isset($this->aInputs[$sFieldFile]))
  250. return true;
  251. $mixedFileIds = $this->getCleanValue($sFieldFile);
  252. if(!$mixedFileIds)
  253. return true;
  254. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  255. if (!$oStorage)
  256. return false;
  257. $iProfileId = 0;
  258. if($iAccountId && ($aAccountInfo = BxDolAccountQuery::getInstance()->getInfoById($iAccountId)))
  259. $iProfileId = $aAccountInfo['profile_id'];
  260. else
  261. $iProfileId = bx_get_logged_profile_id();
  262. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iAccountId, true, $this->_isAdmin());
  263. if(!empty($aGhostFiles) && is_array($aGhostFiles))
  264. foreach($aGhostFiles as $aFile) {
  265. if(is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  266. continue;
  267. if($aFile['private'])
  268. $oStorage->setFilePrivate ($aFile['id'], 1);
  269. if($iAccountId)
  270. $oStorage->updateGhostsContentId ($aFile['id'], $iProfileId, $iAccountId, $this->_isAdmin());
  271. }
  272. return true;
  273. }
  274. protected function genCustomInputAgreement ($aInput)
  275. {
  276. $oPermalink = BxDolPermalinks::getInstance();
  277. 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>';
  278. }
  279. protected function _setCustomError ($s)
  280. {
  281. $this->aInputs['do_submit']['error'] = $s;
  282. }
  283. protected function _isAdmin()
  284. {
  285. return BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR]);
  286. }
  287. protected function _getGhostTmplVars($sField)
  288. {
  289. return [
  290. 'name' => $this->aInputs[$sField]['name'],
  291. 'content_id' => $this->aInputs[$sField]['content_id'],
  292. 'bx_if:set_thumb' => [
  293. 'condition' => false,
  294. 'content' => [],
  295. ]
  296. ];
  297. }
  298. }
  299. /** @} */