1
0

BxBaseFormAccount.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. 'password_ref' => &$sPassCheck,
  64. ]);
  65. return $aInfo['password'] == $sPassCheck;
  66. }
  67. /**
  68. * Check if phone is exists.
  69. */
  70. function checkPhoneExist ($s)
  71. {
  72. $s = trim($s);
  73. if(!preg_match("/^\+[0-9\s]*$/", $s)) {
  74. return _t('_sys_form_forgot_password_phone_error_format');
  75. }
  76. return BxDolAccountQuery::getInstance()->getIdByPhone($s) ? _t('_sys_form_account_input_phone_uniq_error_loggedin') : true;
  77. }
  78. /**
  79. * Check if email is uniq.
  80. */
  81. function checkEmailUniq ($s)
  82. {
  83. $s = trim($s);
  84. if (!$this->checkEmail($s))
  85. return false;
  86. $oAccount = BxDolAccount::getInstance();
  87. if ($oAccount) { // user is logged in
  88. $aAccountInfo = $oAccount->getInfo();
  89. if ($s == $aAccountInfo['email']) // don't check email for uniq, if it wasn't changed
  90. return true;
  91. return BxDolAccountQuery::getInstance()->getIdByEmail($s) ? _t('_sys_form_account_input_email_uniq_error_loggedin') : true;
  92. }
  93. 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;
  94. }
  95. }
  96. /**
  97. * Create/Edit Account Form.
  98. */
  99. class BxBaseFormAccount extends BxTemplFormView
  100. {
  101. static $FIELD_PICTURE = 'picture';
  102. static $FIELD_EMAIL = 'email';
  103. static $FIELD_PASSWORD = 'password';
  104. static $FIELD_PASSWORD_CHANGED = 'password_changed';
  105. static $FIELD_SALT = 'salt';
  106. static $FIELD_ADDED = 'added';
  107. static $FIELD_CHANGED = 'changed';
  108. static $FIELD_IP = 'ip';
  109. static $FIELD_REFERRED = 'referred';
  110. static $FIELD_PHONE = 'phone';
  111. protected $_bSetPendingApproval = false;
  112. protected $_sGhostTemplate = 'account_form_ghost_template.html';
  113. public function __construct($aInfo, $oTemplate)
  114. {
  115. parent::__construct($aInfo, $oTemplate);
  116. $this->_bSetPendingApproval = !(bool)getParam('sys_account_autoapproval');
  117. $this->aParams['checker_errors']['summary']['disabled'] = true;
  118. if(($sField = 'picture') && isset($this->aInputs[$sField])) {
  119. /**
  120. * Note. For now Account Picture is available in edit form only.
  121. * To make it available on create form Uploader should correctly support
  122. * uploading by visitor.
  123. */
  124. if($this->aParams['display'] == 'sys_account_settings_info')
  125. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], [
  126. 'storage_object' => 'sys_accounts_pictures',
  127. 'uploaders' => !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : ['sys_crop'],
  128. 'images_transcoder' => 'sys_accounts_thumb',
  129. 'storage_private' => 0,
  130. 'multiple' => false,
  131. 'content_id' => 0,
  132. 'ghost_template' => ''
  133. ]);
  134. else
  135. unset($this->aInputs[$sField]);
  136. }
  137. }
  138. function isValid ()
  139. {
  140. if (!parent::isValid ())
  141. return false;
  142. if (isLogged() || !$this->isSubmitted()) return true; // exit in case it is an account edit or form has not been submitted yet
  143. $sErrorMsg = '';
  144. /**
  145. * @hooks
  146. * @hookdef hook-account-check_join 'account', 'check_join' - hook to check email address for spam
  147. * - $unit_name - equals `account`
  148. * - $action - equals `check_join`
  149. * - $object_id - not used
  150. * - $sender_id - not used
  151. * - $extra_params - array of additional params with the following array keys:
  152. * - `error_msg` - [string] by ref, error message, can be overridden in hook processing
  153. * - `email` - [string] email address to check
  154. * - `approve` - [boolean] by ref, pending approval status for created profile, can be overridden in hook processing
  155. * @hook @ref hook-account-check_join
  156. */
  157. bx_alert('account', 'check_join', 0, false, [
  158. 'error_msg' => &$sErrorMsg,
  159. 'error_msg_ref' => &$sErrorMsg,
  160. 'email' => $this->getCleanValue('email'),
  161. 'approve' => &$this->_bSetPendingApproval,
  162. 'approve_ref' => &$this->_bSetPendingApproval,
  163. ]);
  164. if ($sErrorMsg)
  165. $this->_setCustomError ($sErrorMsg);
  166. return $sErrorMsg ? false : true;
  167. }
  168. public function isSetPendingApproval()
  169. {
  170. return $this->_bSetPendingApproval;
  171. }
  172. public function setPendingApproval($b)
  173. {
  174. return ($this->_bSetPendingApproval = $b);
  175. }
  176. public function initChecker ($aValues = [], $aSpecificValues = [])
  177. {
  178. $bValues = $aValues && !empty($aValues['id']);
  179. if(($sField = 'picture') && isset($this->aInputs[$sField])) {
  180. if($bValues)
  181. $this->aInputs[$sField]['content_id'] = $aValues['id'];
  182. $this->aInputs[$sField]['ghost_template'] = $this->oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getGhostTmplVars($sField));
  183. }
  184. parent::initChecker($aValues, $aSpecificValues);
  185. }
  186. public function insert ($aValsToAdd = array(), $isIgnore = false)
  187. {
  188. $iNow = time();
  189. $sEmail = isset($aValsToAdd[self::$FIELD_EMAIL]) ? $aValsToAdd[self::$FIELD_EMAIL] : $this->getCleanValue(self::$FIELD_EMAIL);
  190. $sEmail = trim(strtolower($sEmail));
  191. $sPhone = isset($aValsToAdd[self::$FIELD_PHONE]) ? $aValsToAdd[self::$FIELD_PHONE] : $this->getCleanValue(self::$FIELD_PHONE);
  192. $sPhone = trim(strtolower($sPhone));
  193. $sPwd = isset($aValsToAdd[self::$FIELD_PASSWORD]) ? $aValsToAdd[self::$FIELD_PASSWORD] : $this->getCleanValue(self::$FIELD_PASSWORD);
  194. $sSalt = genRndSalt();
  195. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  196. return parent::insert(array_merge($aValsToAdd, [
  197. self::$FIELD_EMAIL => $sEmail,
  198. self::$FIELD_PASSWORD => $sPasswordHash,
  199. self::$FIELD_PASSWORD_CHANGED => $iNow,
  200. self::$FIELD_SALT => $sSalt,
  201. self::$FIELD_ADDED => $iNow,
  202. self::$FIELD_CHANGED => $iNow,
  203. self::$FIELD_IP => getVisitorIP(),
  204. self::$FIELD_REFERRED => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  205. self::$FIELD_PHONE => $sPhone,
  206. ]), $isIgnore);
  207. }
  208. function update ($val, $aValsToAdd = [], &$aTrackTextFieldsChanges = null)
  209. {
  210. $oDb = BxDolAccountQuery::getInstance();
  211. $iNow = time();
  212. $_aValsToAdd = [
  213. self::$FIELD_CHANGED => $iNow
  214. ];
  215. if(($mixedPicture = $this->getCleanValue(self::$FIELD_PICTURE))) {
  216. $iPicture = is_array($mixedPicture) ? array_pop($mixedPicture) : (int)$mixedPicture;
  217. if($iPicture)
  218. $_aValsToAdd[self::$FIELD_PICTURE] = $iPicture;
  219. }
  220. if(($sPwd = $this->getCleanValue(self::$FIELD_PASSWORD))) {
  221. $oDb->logPassword($val);
  222. $sSalt = genRndSalt();
  223. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  224. $_aValsToAdd = array_merge($_aValsToAdd, [
  225. self::$FIELD_PASSWORD => $sPasswordHash,
  226. self::$FIELD_PASSWORD_CHANGED => $iNow,
  227. self::$FIELD_SALT => $sSalt
  228. ]);
  229. }
  230. $aInfoOld = $oDb->getInfoById($val);
  231. $bResult = parent::update($val, array_merge($aValsToAdd, $_aValsToAdd), $aTrackTextFieldsChanges);
  232. if($bResult) {
  233. $aInfoNew = $oDb->getInfoById($val);
  234. /**
  235. * @hooks
  236. * @hookdef hook-account-change_receive_news 'account', 'change_receive_news' - hook after change receive_news parameter for account
  237. * - $unit_name - equals `system`
  238. * - $action - equals `change_receive_news`
  239. * - $object_id - not used
  240. * - $sender_id - not used
  241. * - $extra_params - array of additional params with the following array keys:
  242. * - `account_id` - [int] account id
  243. * - `old_value` - [bool] old value for receive_news parameter
  244. * - `new_value` - [bool] new value for receive_news parameter
  245. * - `email` - [string] account's email
  246. * @hook @ref hook-account-change_receive_news
  247. */
  248. bx_alert('account', 'change_receive_news', 0, false, [
  249. 'account_id' => $val,
  250. 'old_value' => $aInfoOld['receive_news'],
  251. 'new_value' => $aInfoNew['receive_news'],
  252. 'email' => $aInfoNew['email']]
  253. );
  254. }
  255. return $bResult;
  256. }
  257. public function processFiles($sFieldFile, $iAccountId = 0, $isAssociateWithContent = false)
  258. {
  259. if (!isset($this->aInputs[$sFieldFile]))
  260. return true;
  261. $mixedFileIds = $this->getCleanValue($sFieldFile);
  262. if(!$mixedFileIds)
  263. return true;
  264. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  265. if (!$oStorage)
  266. return false;
  267. $iProfileId = 0;
  268. if($iAccountId && ($aAccountInfo = BxDolAccountQuery::getInstance()->getInfoById($iAccountId)))
  269. $iProfileId = $aAccountInfo['profile_id'];
  270. else
  271. $iProfileId = bx_get_logged_profile_id();
  272. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iAccountId, true, $this->_isAdmin());
  273. if(!empty($aGhostFiles) && is_array($aGhostFiles))
  274. foreach($aGhostFiles as $aFile) {
  275. if(is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  276. continue;
  277. if($aFile['private'])
  278. $oStorage->setFilePrivate ($aFile['id'], 1);
  279. if($iAccountId)
  280. $oStorage->updateGhostsContentId ($aFile['id'], $iProfileId, $iAccountId, $this->_isAdmin());
  281. }
  282. return true;
  283. }
  284. protected function genCustomInputAgreement ($aInput)
  285. {
  286. $oPermalink = BxDolPermalinks::getInstance();
  287. 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>';
  288. }
  289. protected function _setCustomError ($s)
  290. {
  291. $this->aInputs['do_submit']['error'] = $s;
  292. }
  293. protected function _isAdmin()
  294. {
  295. return BxDolAcl::getInstance()->isMemberLevelInSet([MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR]);
  296. }
  297. protected function _getGhostTmplVars($sField)
  298. {
  299. return [
  300. 'name' => $this->aInputs[$sField]['name'],
  301. 'content_id' => $this->aInputs[$sField]['content_id'],
  302. 'bx_if:set_thumb' => [
  303. 'condition' => false,
  304. 'content' => [],
  305. ]
  306. ];
  307. }
  308. }
  309. /** @} */