BxBaseServiceAccount.php 25 KB

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