BxBaseServiceAccount.php 32 KB

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