BxBaseServiceAccount.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. bx_alert('account', 'after_email_confirmation', $aData['account_id'], false, ['override_result' => &$sUrl]);
  304. $oTemplate = BxDolTemplate::getInstance();
  305. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  306. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  307. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  308. $oTemplate->setPageContent ('url_relocate', bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sUrl)));
  309. BxDolTemplate::getInstance()->getPageCode();
  310. exit;
  311. }
  312. public function serviceGetOptionsResetPasswordRedirect()
  313. {
  314. $aResult = array();
  315. $aChoices = array('home', 'profile', 'profile_edit', 'custom');
  316. foreach($aChoices as $sChoice)
  317. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_reset_password_redirect_' . $sChoice));
  318. return $aResult;
  319. }
  320. public function serviceGetOptionsSwitchToProfileRedirect()
  321. {
  322. $aResult = array();
  323. $aChoices = array('back', 'home', 'profile', 'custom');
  324. foreach($aChoices as $sChoice)
  325. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_switch_to_profile_redirect_' . $sChoice));
  326. return $aResult;
  327. }
  328. public function serviceGetOptionsPruningInterval()
  329. {
  330. $aResult = array();
  331. $aChoices = array('no', 'no_login_delete', 'no_login_suspend', 'no_confirm_delete', 'no_profile_delete');
  332. foreach($aChoices as $sChoice)
  333. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_pruning_' . $sChoice));
  334. return $aResult;
  335. }
  336. /**
  337. * @page service Service Calls
  338. * @section bx_system_general System Services
  339. * @subsection bx_system_general-account Account
  340. * @subsubsection bx_system_general-forgot_password forgot_password
  341. *
  342. * @code bx_srv('system', 'forgot_password', [], 'TemplServiceAccount'); @endcode
  343. * @code {{~system:forgot_password:TemplServiceAccount~}} @endcode
  344. *
  345. * Display forgot password form,
  346. * if reset password key is provided then reset password
  347. *
  348. * @see BxBaseServiceAccount::serviceForgotPassword
  349. */
  350. /**
  351. * @ref bx_system_general-forgot_password "forgot_password"
  352. */
  353. public function serviceForgotPassword()
  354. {
  355. if(isLogged()){
  356. header('Location: ' . BX_DOL_URL_ROOT);
  357. exit;
  358. }
  359. if (bx_get('key') !== false)
  360. return $this->resetPassword();
  361. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  362. if (!$oForm)
  363. return '';
  364. $bNeedCheckEmailAndPhone = true;
  365. $sCaptionKey = "_sys_txt_forgot_pasword_by_both_desc";
  366. if (BxDolAccount::isNeedConfirmPhone()){
  367. if (!BxDolAccount::isNeedConfirmEmail()){
  368. unset($oForm->aInputs['email']);
  369. $sCaptionKey = "_sys_txt_forgot_pasword_by_phone_desc";
  370. $oForm->aInputs['phone']['checker']['func'] = 'PhoneExist';
  371. $bNeedCheckEmailAndPhone = false;
  372. }
  373. }
  374. else{
  375. unset($oForm->aInputs['phone']);
  376. $sCaptionKey = "_sys_txt_forgot_pasword_desc";
  377. $oForm->aInputs['email']['checker']['func'] = 'EmailExist';
  378. $bNeedCheckEmailAndPhone = false;
  379. }
  380. if ((BxDolAccount::isNeedConfirmPhone() && BxDolAccount::isNeedConfirmEmail())){
  381. if (isset($oForm->aInputs['phone']))
  382. $oForm->aInputs['phone']['required'] = false;
  383. if (isset($oForm->aInputs['email']))
  384. $oForm->aInputs['email']['required'] = false;
  385. }
  386. $oForm->initChecker();
  387. $bShowErrorEmptyBoth = false;
  388. if ($oForm->isSubmitted() && $bNeedCheckEmailAndPhone && $oForm->getCleanValue('phone') == '' && $oForm->getCleanValue('email') == ''){
  389. $bShowErrorEmptyBoth = true;
  390. $oForm->setValid(false);
  391. }
  392. if ($oForm->isSubmittedAndValid()) {
  393. $sEmail = $oForm->getCleanValue('email');
  394. $sPhone = $oForm->getCleanValue('phone');
  395. if (isset($oForm->aInputs['email']) && $sEmail != '') {
  396. $oAccount = BxDolAccount::getInstance($this->_oAccountQuery->getIdByEmail($sEmail));
  397. if($oAccount && $oAccount->sendResetPasswordEmail())
  398. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  399. else
  400. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  401. $sForm = '';
  402. }
  403. if (isset($oForm->aInputs['phone']) && $sPhone != '') {
  404. $iAccountId = $this->_oAccountQuery->getIdByPhone($sPhone);
  405. $aAccountInfo = $this->_oAccountQuery->getInfoById($iAccountId);
  406. $sKey = bx_get_reset_password_key($aAccountInfo['email']);
  407. $sForgotPasswordUrl = bx_get_reset_password_link_by_key($sKey);
  408. $sSmsText = _t('_sys_txt_forgot_pasword_sms_text', $sForgotPasswordUrl);
  409. $mixedOverrideResult = null;
  410. bx_alert('account', 'before_forgot_password_send_sms', $aAccountInfo['id'], false, array('phone_number' => &$sPhone, 'sms_text' => &$sSmsText, 'override_result' => &$mixedOverrideResult));
  411. if ($mixedOverrideResult === null) {
  412. $oTwilio = BxDolTwilio::getInstance();
  413. if($oTwilio->sendSms($sPhone, $sSmsText))
  414. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_phone"));
  415. else
  416. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  417. }
  418. $sForm = '';
  419. }
  420. }
  421. else {
  422. $sResultMsg = _t($sCaptionKey);
  423. $sForm = $oForm->getCode();
  424. }
  425. 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>' : '');
  426. }
  427. /**
  428. * @page service Service Calls
  429. * @section bx_system_general System Services
  430. * @subsection bx_system_general-account Account
  431. * @subsubsection bx_system_general-switch_profile switch_profile
  432. *
  433. * @code bx_srv('system', 'switch_profile', [123], 'TemplServiceAccount'); @endcode
  434. * @code {{~system:switch_profile:TemplServiceAccount:[123]~}} @endcode
  435. *
  436. * Switch current user profile.
  437. * User can switch between profiles if multiple profiles are created.
  438. * Also it's possible to switch to Organization profile if user is admin of Organization,
  439. * or other module which can `actAsProfile`.
  440. * Admin can switch to any prifile by default
  441. *
  442. * @param $iSwitchToProfileId profile ID to switch to
  443. * @return true on success or error message otherwise, so make sue to make strict (===) comparision
  444. *
  445. * @see BxBaseServiceAccount::serviceSwitchProfile
  446. */
  447. /**
  448. * @ref bx_system_general-switch_profile "switch_profile"
  449. */
  450. public function serviceSwitchProfile($iSwitchToProfileId)
  451. {
  452. $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
  453. if (!$oProfile) {
  454. return _t('_sys_txt_error_occured');
  455. }
  456. $iViewerAccountId = getLoggedId();
  457. $iSwitchToAccountId = $oProfile->getAccountId();
  458. $iViewerProfileId = bx_get_logged_profile_id();
  459. $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
  460. $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  461. $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
  462. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  463. if (!$bCanSwitch) {
  464. return isset($aCheck[CHECK_ACTION_MESSAGE]) ? $aCheck[CHECK_ACTION_MESSAGE] : _t('_sys_txt_error_occured');
  465. }
  466. $oAccount = BxDolAccount::getInstance();
  467. if (!$oAccount->updateProfileContext($iSwitchToProfileId)) {
  468. return _t('_sys_txt_error_occured');
  469. }
  470. checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
  471. return true;
  472. }
  473. /**
  474. * Reset password procedure
  475. */
  476. public function resetPassword()
  477. {
  478. $sKey = bx_process_input(bx_get('key'));
  479. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password_reset');
  480. if(!empty($sKey) && isset($oForm->aInputs['key']))
  481. $oForm->aInputs['key']['value'] = $sKey;
  482. $oForm->initChecker();
  483. if($oForm->isSubmittedAndValid()) {
  484. $sErrorUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  485. // check if key exists
  486. $oKey = BxDolKey::getInstance();
  487. $sKey = $oForm->getCleanValue('key');
  488. if(!$oKey || !$oKey->isKeyExists($sKey))
  489. return MsgBox(_t('_sys_txt_reset_pasword_error_invalid_key', $sErrorUrl));
  490. // check if key data exists
  491. $aKeyData = $oKey->getKeyData($sKey);
  492. if(empty($aKeyData['email']))
  493. return MsgBox(_t('_sys_txt_reset_pasword_error_invalid_key', $sErrorUrl));
  494. // check if account with such email exists
  495. $iAccountId = $this->_oAccountQuery->getIdByEmail($aKeyData['email']);
  496. if(empty($iAccountId))
  497. return MsgBox(_t('_sys_txt_reset_pasword_error_not_found', $sErrorUrl));;
  498. $sPassword = $oForm->getCleanValue('password');
  499. $oAccount = BxDolAccount::getInstance($iAccountId);
  500. if (!$oAccount || !$oAccount->updatePassword($sPassword))
  501. return MsgBox(_t('_sys_txt_reset_pasword_error_occured', $sErrorUrl));
  502. $this->_oAccountQuery->unlockAccount($iAccountId);
  503. $oKey->removeKey($sKey);
  504. return MsgBox(_t('_sys_txt_reset_pasword_success'), 3) . bx_srv('system', 'login_form_only', array('', bx_get_reset_password_redirect($iAccountId)), 'TemplServiceLogin');
  505. }
  506. $sTitleReset = _t('_sys_page_title_forgot_password_reset');
  507. BxDolTemplate::getInstance()->setPageHeader($sTitleReset);
  508. if(($oPage = BxDolPage::getObjectInstanceByURI()) !== false)
  509. $oPage->setTitle($sTitleReset);
  510. return $oForm->getCode();
  511. }
  512. /**
  513. * Generate new password.
  514. */
  515. public function generateUserNewPwd($iAccountId)
  516. {
  517. $sPwd = genRndPwd(8, false);
  518. $sSalt = genRndSalt();
  519. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  520. $oAccount = BxDolAccount::getInstance($iAccountId);
  521. $iPasswordExpired = $oAccount->getPasswordExpiredDateByAccount($iAccountId);
  522. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId, $iPasswordExpired);
  523. bx_alert('account', 'edited', $iAccountId, $iAccountId, array('action' => 'forgot_password'));
  524. return $sPwd;
  525. }
  526. }
  527. /** @} */