1
0

BxBaseServiceAccount.php 24 KB

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