BxBaseServiceAccount.php 28 KB

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