BxBaseServiceAccount.php 25 KB

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