BxBaseServiceAccount.php 26 KB

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