BxBaseServiceAccount.php 29 KB

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