BxBaseServiceAccount.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. /**
  10. * System service for creating system profile functionality.
  11. */
  12. class BxBaseServiceAccount extends BxDol
  13. {
  14. protected $_oAccountForms;
  15. protected $_oAccountQuery;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->_oAccountForms = new BxTemplAccountForms();
  20. $this->_oAccountQuery = BxDolAccountQuery::getInstance();
  21. }
  22. /**
  23. * @page service Service Calls
  24. * @section bx_system_general System Services
  25. * @subsection bx_system_general-account Account
  26. * @subsubsection bx_system_general-create_account_form create_account_form
  27. *
  28. * @code bx_srv('system', 'create_account_form', [], 'TemplServiceAccount'); @endcode
  29. * @code {{~system:create_account_form:TemplServiceAccount[{"no_login_text":true, "no_auth_buttons":true}]~}} @endcode
  30. *
  31. * Join form.
  32. * @param $aParams array of additional params, such as:
  33. * - no_login_text
  34. * - no_auth_buttons
  35. *
  36. * @see BxBaseServiceAccount::serviceGetCreatePostForm
  37. */
  38. /**
  39. * @ref bx_system_general-create_account_form "create_account_form"
  40. */
  41. public function serviceCreateAccountForm ($aParams = array())
  42. {
  43. if (isLogged() && 'create-account' == bx_get('i')){
  44. header('Location: ' . BX_DOL_URL_ROOT);
  45. exit;
  46. }
  47. if (isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) { // remember referrer
  48. $sJoinReferrer = $_SERVER['HTTP_REFERER'];
  49. $aNoRelocatePages = array('forgot-password', 'login', 'create-account', 'logout');
  50. foreach ($aNoRelocatePages as $s) {
  51. if (false !== mb_stripos($sJoinReferrer, $s)) {
  52. $sJoinReferrer = '';
  53. break;
  54. }
  55. }
  56. if ($sJoinReferrer)
  57. BxDolSession::getInstance()->setValue('join-referrer', $sJoinReferrer);
  58. }
  59. $sLoginText = '';
  60. if (!isset($aParams['no_login_text']) || false === (bool)$aParams['no_login_text'])
  61. $sLoginText = '<hr class="bx-def-hr bx-def-margin-sec-topbottom" /><div>' . _t('_sys_txt_join_description', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login')) . '</div>';
  62. $sAuth = '';
  63. if (!isset($aParams['no_auth_buttons']) || false === (bool)$aParams['no_auth_buttons'])
  64. $sAuth = BxDolService::call('system', 'member_auth_code', array(), 'TemplServiceLogin');
  65. $sFormCode = '';
  66. if (!(bool)getParam('sys_account_disable_join_form'))
  67. $sFormCode = $this->_oAccountForms->createAccountForm($aParams);
  68. return $sAuth . $sFormCode . $sLoginText;
  69. }
  70. public function serviceAccountSettingsEmail ($iAccountId = false)
  71. {
  72. if (false === $iAccountId)
  73. $iAccountId = getLoggedId();
  74. return $this->_oAccountForms->editAccountEmailSettingsForm($iAccountId);
  75. }
  76. public function serviceAccountSettingsPassword ($iAccountId = false)
  77. {
  78. if (false === $iAccountId)
  79. $iAccountId = getLoggedId();
  80. return $this->_oAccountForms->editAccountPasswordSettingsForm($iAccountId);
  81. }
  82. public function serviceAccountSettingsInfo ($iAccountId = false)
  83. {
  84. if (false === $iAccountId)
  85. $iAccountId = getLoggedId();
  86. return $this->_oAccountForms->editAccountInfoForm($iAccountId);
  87. }
  88. public function serviceAccountSettingsDelAccount ($iAccountId = false)
  89. {
  90. if (!$iAccountId)
  91. $iAccountId = bx_process_input(bx_get('id'), BX_DATA_INT);
  92. if (!$iAccountId)
  93. $iAccountId = getLoggedId();
  94. return $this->_oAccountForms->deleteAccountForm($iAccountId);
  95. }
  96. /**
  97. * Display unsubscribe from newsletters form
  98. */
  99. public function serviceUnsubscribeNews()
  100. {
  101. return $this->_unsubscribeForm('sys_unsubscribe_news');
  102. }
  103. /**
  104. * Display unsubscribe from notifications form
  105. */
  106. public function serviceUnsubscribeNotifications()
  107. {
  108. return $this->_unsubscribeForm('sys_unsubscribe_updates');
  109. }
  110. /**
  111. * Display confirmation statuses list
  112. */
  113. public function serviceGetConfirmationTypes()
  114. {
  115. $aResult = array();
  116. $aChoices = array(BX_ACCOUNT_CONFIRMATION_NONE, BX_ACCOUNT_CONFIRMATION_EMAIL, BX_ACCOUNT_CONFIRMATION_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE);
  117. foreach($aChoices as $sChoice)
  118. $aResult[$sChoice] = _t('_sys_acc_confirmation_type_' . $sChoice);
  119. return $aResult;
  120. }
  121. protected function _unsubscribeForm($sDisplay)
  122. {
  123. $iAccountId = bx_process_input(bx_get('id'));
  124. $sCode = bx_process_input(bx_get('code'));
  125. // validate input params
  126. $oAccount = BxDolAccount::getInstance($iAccountId);
  127. if (!$oAccount)
  128. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  129. if ($sCode != $oAccount->getEmailHash())
  130. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  131. $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
  132. if (!$oForm)
  133. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  134. // show form
  135. $aAccountInfo = $oAccount->getInfo();
  136. $aAccountInfo['code'] = $sCode;
  137. $oForm->initChecker($aAccountInfo);
  138. if ($oForm->isSubmittedAndValid()) {
  139. if (!$oForm->update($oAccount->id()))
  140. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  141. return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
  142. }
  143. return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
  144. }
  145. /**
  146. * Display email confirmation form, if confirmation code is provided then try to confirm profile
  147. */
  148. public function serviceEmailConfirmation($sMsg = false)
  149. {
  150. // if user is logged in and email is confirmed then just display a message
  151. if (isLogged()) {
  152. $oAccount = BxDolAccount::getInstance();
  153. if ($oAccount->isConfirmedEmail())
  154. return MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
  155. }
  156. // if confirmation code is provided in the URL - perform email confirmation right away
  157. if (bx_get('code') !== false)
  158. return $this->confirmEmail(bx_process_input(bx_get('code')));
  159. // if user requested to resend verification letter then send letter and display message
  160. if (bx_process_input(bx_get('resend')) && isLogged()) {
  161. $oAccount = BxDolAccount::getInstance();
  162. if ($oAccount->sendConfirmationEmail())
  163. $sMsg = _t('_sys_txt_confirm_email_sent');
  164. else
  165. $sMsg = _t('_sys_txt_confirm_email_sent_failed');
  166. return MsgBox($sMsg);
  167. }
  168. // show and process code verification form
  169. $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
  170. if (!$oForm)
  171. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  172. $oForm->initChecker();
  173. if ($oForm->isSubmittedAndValid()) {
  174. $oKey = BxDolKey::getInstance();
  175. if (!$oKey)
  176. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  177. else
  178. return $this->confirmEmail(trim($oForm->getCleanValue('code')));
  179. }
  180. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
  181. }
  182. /**
  183. * Display phone confirmation forms, if confirmation code is provided then try to confirm profile
  184. */
  185. public function servicePhoneConfirmation($sMsg = false)
  186. {
  187. // if user is logged in and phone is confirmed then just display a message
  188. if (isLogged()) {
  189. $oAccount = BxDolAccount::getInstance();
  190. if ($oAccount->isConfirmedPhone())
  191. return MsgBox(_t("_sys_txt_confirm_phone_already_confirmed"));
  192. $iStep = 1;
  193. $a = $oAccount->getInfo();
  194. $sPhoneNumber = $a['phone'];
  195. $oSession = BxDolSession::getInstance();
  196. if ($sPhoneNumber != "" && $oSession->isValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE)){
  197. $iStep = 2;
  198. }
  199. if (bx_get('step'))
  200. $iStep = (int)bx_get('step');
  201. // show and process phone set form
  202. if ($iStep == 1){
  203. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_set_phone');
  204. if (!$oForm)
  205. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_error_occured"));
  206. $oForm->initChecker(array('phone' => $sPhoneNumber));
  207. if ($oForm->isSubmittedAndValid()) {
  208. $oAccount->updatePhone(trim($oForm->getCleanValue('phone')));
  209. $sActivationCode = rand(1000, 9999);
  210. $sActivationText =_t('_sys_txt_confirm_phone_sms_text', $sActivationCode);
  211. $ret = null;
  212. bx_alert('account', 'before_confirm_phone_send_sms', $oAccount->id(), bx_get_logged_profile_id(), array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  213. if ($ret === null)
  214. {
  215. $oTwilio = BxDolTwilio::getInstance();
  216. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  217. return MsgBox(_t("_sys_txt_confirm_phone_send_sms_error_occured"));
  218. }
  219. }
  220. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  221. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone'));
  222. }
  223. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_set_phone") . '</div>' .$oForm->getCode();
  224. }
  225. if ($iStep == 2){
  226. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_confirmation');
  227. if (!$oForm)
  228. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_confirmation_occured"));
  229. $oForm->initChecker();
  230. if ($oForm->isSubmittedAndValid()) {
  231. $oAccount->updatePhoneConfirmed(true);
  232. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  233. return MsgBox(_t("_sys_txt_confirm_phone_confirm_success"));
  234. }
  235. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_phone_confirmation") . '</div>' .$oForm->getCode();
  236. }
  237. }
  238. }
  239. /**
  240. * Perform email confirmation
  241. */
  242. public function confirmEmail($sKey)
  243. {
  244. // check if key exists
  245. $oKey = BxDolKey::getInstance();
  246. if (!$oKey || !$oKey->isKeyExists($sKey))
  247. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  248. // check if key data exists
  249. $aData = $oKey->getKeyData($sKey);
  250. if (!isset($aData['account_id']))
  251. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  252. // check if account exists
  253. $oAccount = BxDolAccount::getInstance($aData['account_id']);
  254. if (!$oAccount)
  255. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  256. // remove key
  257. $oKey->removeKey($sKey);
  258. // confirm email
  259. if (!$oAccount->updateEmailConfirmed(true))
  260. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  261. // login to user's account automatically
  262. bx_login($aData['account_id']);
  263. // redirect with success message
  264. $sUrl = getParam('sys_redirect_after_email_confirmation');
  265. $oTemplate = BxDolTemplate::getInstance();
  266. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  267. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  268. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  269. $oTemplate->setPageContent ('url_relocate', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl));
  270. BxDolTemplate::getInstance()->getPageCode();
  271. exit;
  272. }
  273. public function serviceGetOptionsResetPasswordRedirect()
  274. {
  275. $aResult = array();
  276. $aChoices = array('home', 'profile', 'profile_edit', 'custom');
  277. foreach($aChoices as $sChoice)
  278. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_reset_password_redirect_' . $sChoice));
  279. return $aResult;
  280. }
  281. /**
  282. * @page service Service Calls
  283. * @section bx_system_general System Services
  284. * @subsection bx_system_general-account Account
  285. * @subsubsection bx_system_general-forgot_password forgot_password
  286. *
  287. * @code bx_srv('system', 'forgot_password', [], 'TemplServiceAccount'); @endcode
  288. * @code {{~system:forgot_password:TemplServiceAccount~}} @endcode
  289. *
  290. * Display forgot password form,
  291. * if reset password key is provided then reset password
  292. *
  293. * @see BxBaseServiceAccount::serviceForgotPassword
  294. */
  295. /**
  296. * @ref bx_system_general-forgot_password "forgot_password"
  297. */
  298. public function serviceForgotPassword()
  299. {
  300. if(isLogged()){
  301. header('Location: ' . BX_DOL_URL_ROOT);
  302. exit;
  303. }
  304. if (bx_get('key') !== false)
  305. return $this->resetPassword();
  306. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  307. if (!$oForm)
  308. return '';
  309. $bNeedCheckEmailAndPhone = true;
  310. $sCaptionKey = "_sys_txt_forgot_pasword_by_both_desc";
  311. if (BxDolAccount::isNeedConfirmPhone()){
  312. if (!BxDolAccount::isNeedConfirmEmail()){
  313. unset($oForm->aInputs['email']);
  314. $sCaptionKey = "_sys_txt_forgot_pasword_by_phone_desc";
  315. $oForm->aInputs['phone']['checker']['func'] = 'PhoneExist';
  316. $bNeedCheckEmailAndPhone = false;
  317. }
  318. }
  319. else{
  320. unset($oForm->aInputs['phone']);
  321. $sCaptionKey = "_sys_txt_forgot_pasword_desc";
  322. $oForm->aInputs['email']['checker']['func'] = 'EmailExist';
  323. $bNeedCheckEmailAndPhone = false;
  324. }
  325. if ((BxDolAccount::isNeedConfirmPhone() && BxDolAccount::isNeedConfirmEmail())){
  326. if (isset($oForm->aInputs['phone']))
  327. $oForm->aInputs['phone']['required'] = false;
  328. if (isset($oForm->aInputs['email']))
  329. $oForm->aInputs['email']['required'] = false;
  330. }
  331. $oForm->initChecker();
  332. $bShowErrorEmptyBoth = false;
  333. if ($oForm->isSubmitted() && $bNeedCheckEmailAndPhone && $oForm->getCleanValue('phone') == '' && $oForm->getCleanValue('email') == ''){
  334. $bShowErrorEmptyBoth = true;
  335. $oForm->setValid(false);
  336. }
  337. if ($oForm->isSubmittedAndValid()) {
  338. $sEmail = $oForm->getCleanValue('email');
  339. $sPhone = $oForm->getCleanValue('phone');
  340. if (isset($oForm->aInputs['email']) && $sEmail != '') {
  341. $iAccountId = $this->_oAccountQuery->getIdByEmail($sEmail);
  342. $aPlus['key'] = bx_get_reset_password_key($sEmail);
  343. $aPlus['forgot_password_url'] = bx_get_reset_password_link_by_key($aPlus['key']);
  344. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Forgot', $aPlus, $iAccountId);
  345. if(!empty($aTemplate) && is_array($aTemplate) && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  346. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  347. else
  348. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  349. $sForm = '';
  350. }
  351. if (isset($oForm->aInputs['phone']) && $sPhone != '') {
  352. $iAccountId = $this->_oAccountQuery->getIdByPhone($sPhone);
  353. $aAccountInfo = $this->_oAccountQuery->getInfoById($iAccountId);
  354. $aPlus['key'] = bx_get_reset_password_key($aAccountInfo['email']);
  355. $aPlus['forgot_password_url'] = bx_get_reset_password_link_by_key($aPlus['key']);
  356. $sSmsText = _t('_sys_txt_forgot_pasword_sms_text', $aPlus['forgot_password_url']);
  357. $mixedOverrideResult = null;
  358. bx_alert('account', 'before_forgot_password_send_sms', $aAccountInfo['id'], false, array('phone_number' => $sPhone, 'sms_text' => $sSmsText, 'override_result' => &$mixedOverrideResult));
  359. if ($mixedOverrideResult === null) {
  360. $oTwilio = BxDolTwilio::getInstance();
  361. if($oTwilio->sendSms($sPhone, $sSmsText))
  362. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_phone"));
  363. else
  364. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  365. }
  366. $sForm = '';
  367. }
  368. }
  369. else {
  370. $sResultMsg = _t($sCaptionKey);
  371. $sForm = $oForm->getCode();
  372. }
  373. return '<div class="bx-def-padding-sec-bottom">' . $sResultMsg . '</div>' . $sForm . ($bShowErrorEmptyBoth ? '<div class="bx-form-warn">' . _t("_sys_form_forgot_password_phone_and_email_empty_error") . '</div>' : '');
  374. }
  375. /**
  376. * @page service Service Calls
  377. * @section bx_system_general System Services
  378. * @subsection bx_system_general-account Account
  379. * @subsubsection bx_system_general-switch_profile switch_profile
  380. *
  381. * @code bx_srv('system', 'switch_profile', [123], 'TemplServiceAccount'); @endcode
  382. * @code {{~system:switch_profile:TemplServiceAccount:[123]~}} @endcode
  383. *
  384. * Switch current user profile.
  385. * User can switch between profiles if multiple profiles are created.
  386. * Also it's possible to switch to Organization profile if user is admin of Organization,
  387. * or other module which can `actAsProfile`.
  388. * Admin can switch to any prifile by default
  389. *
  390. * @param $iSwitchToProfileId profile ID to switch to
  391. * @return true on success or error message otherwise, so make sue to make strict (===) comparision
  392. *
  393. * @see BxBaseServiceAccount::serviceSwitchProfile
  394. */
  395. /**
  396. * @ref bx_system_general-switch_profile "switch_profile"
  397. */
  398. public function serviceSwitchProfile($iSwitchToProfileId)
  399. {
  400. $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
  401. if (!$oProfile) {
  402. return _t('_sys_txt_error_occured');
  403. }
  404. $iViewerAccountId = getLoggedId();
  405. $iSwitchToAccountId = $oProfile->getAccountId();
  406. $iViewerProfileId = bx_get_logged_profile_id();
  407. $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
  408. $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  409. $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
  410. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  411. if (!$bCanSwitch) {
  412. return isset($aCheck[CHECK_ACTION_MESSAGE]) ? $aCheck[CHECK_ACTION_MESSAGE] : _t('_sys_txt_error_occured');
  413. }
  414. $oAccount = BxDolAccount::getInstance();
  415. if (!$oAccount->updateProfileContext($iSwitchToProfileId)) {
  416. return _t('_sys_txt_error_occured');
  417. }
  418. checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
  419. return true;
  420. }
  421. /**
  422. * Reset password procedure
  423. */
  424. public function resetPassword()
  425. {
  426. $sKey = bx_process_input(bx_get('key'));
  427. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password_reset');
  428. if(!empty($sKey) && isset($oForm->aInputs['key']))
  429. $oForm->aInputs['key']['value'] = $sKey;
  430. $oForm->initChecker();
  431. if($oForm->isSubmittedAndValid()) {
  432. $sErrorUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password');
  433. // check if key exists
  434. $oKey = BxDolKey::getInstance();
  435. $sKey = $oForm->getCleanValue('key');
  436. if(!$oKey || !$oKey->isKeyExists($sKey))
  437. return MsgBox(_t('_sys_txt_reset_pasword_error_invalid_key', $sErrorUrl));
  438. // check if key data exists
  439. $aKeyData = $oKey->getKeyData($sKey);
  440. if(empty($aKeyData['email']))
  441. return MsgBox(_t('_sys_txt_reset_pasword_error_invalid_key', $sErrorUrl));
  442. // check if account with such email exists
  443. $iAccountId = $this->_oAccountQuery->getIdByEmail($aKeyData['email']);
  444. if(empty($iAccountId))
  445. return MsgBox(_t('_sys_txt_reset_pasword_error_not_found', $sErrorUrl));;
  446. $sPassword = $oForm->getCleanValue('password');
  447. $sSalt = genRndSalt();
  448. if(!$this->_oAccountQuery->updatePassword(encryptUserPwd($sPassword, $sSalt), $sSalt, $iAccountId))
  449. return MsgBox(_t('_sys_txt_reset_pasword_error_occured', $sErrorUrl));;
  450. $this->_oAccountQuery->unlockAccount($iAccountId);
  451. $oKey->removeKey($sKey);
  452. return MsgBox(_t('_sys_txt_reset_pasword_success'), 3) . bx_srv('system', 'login_form_only', array('', bx_get_reset_password_redirect($iAccountId)), 'TemplServiceLogin');
  453. }
  454. return $oForm->getCode();
  455. }
  456. /**
  457. * Generate new password.
  458. */
  459. public function generateUserNewPwd($iAccountId)
  460. {
  461. $sPwd = genRndPwd(8, false);
  462. $sSalt = genRndSalt();
  463. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  464. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
  465. bx_alert('account', 'edited', $iAccountId, $iAccountId, array('action' => 'forgot_password'));
  466. return $sPwd;
  467. }
  468. }
  469. /** @} */