BxBaseServiceAccount.php 27 KB

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