BxBaseServiceAccount.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. public function serviceAccountDelete($iId, $bWithContent = false)
  247. {
  248. if(($oAccount = BxDolAccount::getInstance($iId)) !== false)
  249. return $oAccount->delete($bWithContent);
  250. return false;
  251. }
  252. /**
  253. * Display unsubscribe from newsletters form
  254. */
  255. public function serviceUnsubscribeNews()
  256. {
  257. return $this->_unsubscribeForm('sys_unsubscribe_news');
  258. }
  259. /**
  260. * Display unsubscribe from notifications form
  261. */
  262. public function serviceUnsubscribeNotifications()
  263. {
  264. return $this->_unsubscribeForm('sys_unsubscribe_updates');
  265. }
  266. /**
  267. * Display confirmation statuses list
  268. */
  269. public function serviceGetConfirmationTypes()
  270. {
  271. $aResult = array();
  272. $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);
  273. foreach($aChoices as $sChoice)
  274. $aResult[$sChoice] = _t('_sys_acc_confirmation_type_' . $sChoice);
  275. return $aResult;
  276. }
  277. protected function _unsubscribeForm($sDisplay)
  278. {
  279. $iAccountId = bx_process_input(bx_get('id'));
  280. $sCode = bx_process_input(bx_get('code'));
  281. // validate input params
  282. $oAccount = BxDolAccount::getInstance($iAccountId);
  283. if (!$oAccount)
  284. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  285. if ($sCode != $oAccount->getEmailHash())
  286. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  287. $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
  288. if (!$oForm)
  289. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  290. // show form
  291. $aAccountInfo = $oAccount->getInfo();
  292. $aAccountInfo['code'] = $sCode;
  293. $oForm->initChecker($aAccountInfo);
  294. if ($oForm->isSubmittedAndValid()) {
  295. if (!$oForm->update($oAccount->id()))
  296. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  297. return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
  298. }
  299. return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
  300. }
  301. /**
  302. * Display email confirmation form, if confirmation code is provided then try to confirm profile
  303. */
  304. public function serviceEmailConfirmation($sMsg = false)
  305. {
  306. $bApi = bx_is_api();
  307. // if user is logged in and email is confirmed then just display a message
  308. if (isLogged() && !bx_get('code')) {
  309. $oAccount = BxDolAccount::getInstance();
  310. if ($oAccount->isConfirmedEmail())
  311. return $bApi ? [bx_api_get_msg(_t("_sys_txt_confirm_email_already_confirmed"))] : MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
  312. }
  313. // if confirmation code is provided in the URL - perform email confirmation right away
  314. if (bx_get('code') !== false)
  315. return $this->confirmEmail(bx_process_input(bx_get('code')));
  316. // if user requested to resend verification letter then send letter and display message
  317. if (bx_process_input(bx_get('resend')) && isLogged()) {
  318. $oAccount = BxDolAccount::getInstance();
  319. if ($oAccount->sendConfirmationEmail())
  320. $sMsg = _t('_sys_txt_confirm_email_sent');
  321. else
  322. $sMsg = _t('_sys_txt_confirm_email_sent_failed');
  323. return $bApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg);
  324. }
  325. // show and process code verification form
  326. $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
  327. if (!$oForm)
  328. return $bApi ? [bx_api_get_msg(_t("_sys_txt_confirm_email_error_occured"))] : MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  329. $oForm->initChecker();
  330. if ($oForm->isSubmittedAndValid()) {
  331. $oKey = BxDolKey::getInstance();
  332. if (!$oKey){
  333. return $bApi ? [bx_api_get_msg(_t("_sys_txt_confirm_email_error_occured"))] : MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  334. }
  335. else{
  336. $oRv = $this->confirmEmail(trim($oForm->getCleanValue('code')));
  337. if (!$bApi)
  338. return $oRv;
  339. return $oRv;
  340. }
  341. }
  342. if ($bApi){
  343. return [bx_api_get_block('form', $oForm->getCodeAPI(), [
  344. 'ext' => [
  345. 'request' => ['url' => '/api.php?r=system/email_confirmation/TemplServiceAccount', 'immutable' => true]
  346. ]
  347. ])];
  348. }
  349. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
  350. }
  351. /**
  352. * Display phone confirmation forms, if confirmation code is provided then try to confirm profile
  353. */
  354. public function servicePhoneConfirmation($sMsg = false)
  355. {
  356. // if user is logged in and phone is confirmed then just display a message
  357. if (isLogged()) {
  358. $oAccount = BxDolAccount::getInstance();
  359. if ($oAccount->isConfirmedPhone())
  360. return MsgBox(_t("_sys_txt_confirm_phone_already_confirmed"));
  361. $iStep = 1;
  362. $a = $oAccount->getInfo();
  363. $sPhoneNumber = $a['phone'];
  364. $oSession = BxDolSession::getInstance();
  365. if ($sPhoneNumber != "" && $oSession->isValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE))
  366. $iStep = 2;
  367. if (bx_get('step'))
  368. $iStep = (int)bx_get('step');
  369. // show and process phone set form
  370. if ($iStep == 1){
  371. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_set_phone');
  372. if (!$oForm)
  373. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_error_occured"));
  374. $oForm->initChecker(['phone' => $sPhoneNumber]);
  375. if ($oForm->isSubmittedAndValid()) {
  376. $sPhoneNumber = trim($oForm->getCleanValue('phone'));
  377. $oAccount->updatePhone($sPhoneNumber);
  378. $sActivationCode = rand(1000, 9999);
  379. $sActivationText =_t('_sys_txt_confirm_phone_sms_text', $sActivationCode);
  380. $mixedResult = null;
  381. /**
  382. * @hooks
  383. * @hookdef hook-account-before_confirm_phone_send_sms 'account', 'before_confirm_phone_send_sms' - hook in confirm phone
  384. * - $unit_name - equals `account`
  385. * - $action - equals `before_confirm_phone_send_sms`
  386. * - $object_id - account id
  387. * - $sender_id - logged profile_id
  388. * - $extra_params - array of additional params with the following array keys:
  389. * - `phone_number` - [string] by ref, phone number to send sms, can be overridden in hook processing
  390. * - `sms_text` - [string] by ref, text for sms, can be overridden in hook processing
  391. * - `override_result` - [mixed] by ref, can be object, can be overridden in hook processing
  392. * @hook @ref hook-account-before_confirm_phone_send_sms
  393. */
  394. bx_alert('account', 'before_confirm_phone_send_sms', $oAccount->id(), bx_get_logged_profile_id(), [
  395. 'phone_number' => &$sPhoneNumber,
  396. 'sms_text' => &$sActivationText,
  397. 'override_result' => &$mixedResult
  398. ]);
  399. if ($mixedResult === null && ($oSms = BxDolSms::getObjectInstance()) !== false && !$oSms->send($sPhoneNumber, $sActivationText))
  400. return MsgBox(_t("_sys_txt_confirm_phone_send_sms_error_occured"));
  401. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  402. header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone')));
  403. }
  404. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_set_phone") . '</div>' .$oForm->getCode();
  405. }
  406. if ($iStep == 2){
  407. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_confirmation');
  408. if (!$oForm)
  409. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_confirmation_occured"));
  410. $oForm->initChecker();
  411. if ($oForm->isSubmittedAndValid()) {
  412. $oAccount->updatePhoneConfirmed(true);
  413. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  414. return MsgBox(_t("_sys_txt_confirm_phone_confirm_success"));
  415. }
  416. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_phone_confirmation") . '</div>' .$oForm->getCode();
  417. }
  418. }
  419. }
  420. /**
  421. * Perform email confirmation
  422. */
  423. public function confirmEmail($sKey)
  424. {
  425. $bApi = bx_is_api();
  426. $mixedAccountId = $this->_confirmEmail($sKey);
  427. if(is_string($mixedAccountId) && !is_numeric($mixedAccountId))
  428. return $bApi ? [bx_api_get_msg($mixedAccountId)] : MsgBox($mixedAccountId);
  429. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  430. $oSockets->sendEvent('sys_account', $mixedAccountId, 'confirmed', '');
  431. // redirect with success message
  432. $sUrl = getParam('sys_redirect_after_email_confirmation');
  433. /**
  434. * @hooks
  435. * @hookdef hook-account-after_email_confirmation 'account', 'after_email_confirmation' - hook after email confirmed
  436. * - $unit_name - equals `account`
  437. * - $action - equals `after_email_confirmation`
  438. * - $object_id - account id
  439. * - $sender_id - not used
  440. * - $extra_params - array of additional params with the following array keys:
  441. * - `override_result` - [string] by ref, url for redirect after email confirmed, can be overridden in hook processing
  442. * @hook @ref hook-account-after_email_confirmation
  443. */
  444. bx_alert('account', 'after_email_confirmation', $mixedAccountId, false, ['override_result' => &$sUrl]);
  445. if ($bApi){
  446. return [bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000])];
  447. }
  448. $oTemplate = BxDolTemplate::getInstance();
  449. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  450. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  451. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  452. $oTemplate->setPageContent ('url_relocate', bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sUrl)));
  453. BxDolTemplate::getInstance()->getPageCode();
  454. exit;
  455. }
  456. public function serviceConfirmEmail($sKey)
  457. {
  458. $mixedAccountId = $this->_confirmEmail($sKey);
  459. if(is_string($mixedAccountId) && !is_numeric($mixedAccountId))
  460. return $bApi ? [bx_api_get_msg($mixedAccountId)] : MsgBox($mixedAccountId);
  461. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()){
  462. $oSockets->sendEvent('sys_account', $mixedAccountId, 'confirmed', '');
  463. }
  464. bx_alert('account', 'after_email_confirmation', $mixedAccountId, false, ['override_result' => &$sUrl]);
  465. return true;
  466. }
  467. public function serviceGetOptionsResetPasswordRedirect()
  468. {
  469. $aResult = array();
  470. $aChoices = array('home', 'profile', 'profile_edit', 'custom');
  471. foreach($aChoices as $sChoice)
  472. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_reset_password_redirect_' . $sChoice));
  473. return $aResult;
  474. }
  475. public function serviceGetOptionsSwitchToProfileRedirect()
  476. {
  477. $aResult = array();
  478. $aChoices = array('back', 'home', 'profile', 'custom');
  479. foreach($aChoices as $sChoice)
  480. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_switch_to_profile_redirect_' . $sChoice));
  481. return $aResult;
  482. }
  483. public function serviceGetOptionsPruningInterval()
  484. {
  485. $aResult = array();
  486. $aChoices = array('no_login_delete', 'no_login_suspend', 'no_confirm_delete', 'no_profile_delete');
  487. foreach($aChoices as $sChoice)
  488. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_pruning_' . $sChoice));
  489. return $aResult;
  490. }
  491. /**
  492. * @page service Service Calls
  493. * @section bx_system_general System Services
  494. * @subsection bx_system_general-account Account
  495. * @subsubsection bx_system_general-forgot_password forgot_password
  496. *
  497. * @code bx_srv('system', 'forgot_password', [], 'TemplServiceAccount'); @endcode
  498. * @code {{~system:forgot_password:TemplServiceAccount~}} @endcode
  499. *
  500. * Display forgot password form,
  501. * if reset password key is provided then reset password
  502. *
  503. * @see BxBaseServiceAccount::serviceForgotPassword
  504. */
  505. /**
  506. * @ref bx_system_general-forgot_password "forgot_password"
  507. */
  508. public function serviceForgotPassword()
  509. {
  510. if(isLogged()){
  511. $bApi = bx_is_api();
  512. if($bApi) {
  513. return [
  514. bx_api_get_msg(_t("_sys_txt_forgot_pasword_logged_in"), ['ext' => ['msg_type' => 'result']]),
  515. ];
  516. }
  517. header('Location: ' . BX_DOL_URL_ROOT);
  518. exit;
  519. }
  520. $bApi = bx_is_api();
  521. if (bx_get('key') !== false)
  522. return $this->resetPassword();
  523. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  524. if (!$oForm)
  525. return '';
  526. $bNeedCheckEmailAndPhone = true;
  527. $sCaptionKey = "_sys_txt_forgot_pasword_by_both_desc";
  528. if (BxDolAccount::isNeedConfirmPhone()){
  529. if (!BxDolAccount::isNeedConfirmEmail()){
  530. unset($oForm->aInputs['email']);
  531. $sCaptionKey = "_sys_txt_forgot_pasword_by_phone_desc";
  532. $oForm->aInputs['phone']['checker']['func'] = 'PhoneExist';
  533. $bNeedCheckEmailAndPhone = false;
  534. }
  535. }
  536. else{
  537. unset($oForm->aInputs['phone']);
  538. $sCaptionKey = "_sys_txt_forgot_pasword_desc";
  539. $oForm->aInputs['email']['checker']['func'] = 'EmailExist';
  540. $bNeedCheckEmailAndPhone = false;
  541. }
  542. if ((BxDolAccount::isNeedConfirmPhone() && BxDolAccount::isNeedConfirmEmail())){
  543. if (isset($oForm->aInputs['phone']))
  544. $oForm->aInputs['phone']['required'] = false;
  545. if (isset($oForm->aInputs['email']))
  546. $oForm->aInputs['email']['required'] = false;
  547. }
  548. $oForm->initChecker();
  549. $bShowErrorEmptyBoth = false;
  550. if ($oForm->isSubmitted() && $bNeedCheckEmailAndPhone && $oForm->getCleanValue('phone') == '' && $oForm->getCleanValue('email') == ''){
  551. $bShowErrorEmptyBoth = true;
  552. $oForm->setValid(false);
  553. }
  554. $sResultMsg = '';
  555. if ($oForm->isSubmittedAndValid()) {
  556. $sEmail = $oForm->getCleanValue('email');
  557. $sPhone = $oForm->getCleanValue('phone');
  558. if (isset($oForm->aInputs['email']) && $sEmail != '') {
  559. $oAccount = BxDolAccount::getInstance($this->_oAccountQuery->getIdByEmail($sEmail));
  560. if($oAccount && $oAccount->sendResetPasswordEmail())
  561. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  562. else
  563. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  564. $sForm = '';
  565. }
  566. if (isset($oForm->aInputs['phone']) && $sPhone != '') {
  567. $iAccountId = $this->_oAccountQuery->getIdByPhone($sPhone);
  568. $aAccountInfo = $this->_oAccountQuery->getInfoById($iAccountId);
  569. $sKey = bx_get_reset_password_key($aAccountInfo['email']);
  570. $sForgotPasswordUrl = bx_get_reset_password_link_by_key($sKey);
  571. $sSmsText = _t('_sys_txt_forgot_pasword_sms_text', $sForgotPasswordUrl);
  572. $mixedOverrideResult = null;
  573. /**
  574. * @hooks
  575. * @hookdef hook-account-before_forgot_password_send_sms 'account', 'before_forgot_password_send_sms' - hook in $oAccount->isConfirmed check
  576. * - $unit_name - equals `account`
  577. * - $action - equals `before_forgot_password_send_sms`
  578. * - $object_id - account id
  579. * - $sender_id - not used
  580. * - $extra_params - array of additional params with the following array keys:
  581. * - `phone_number` - [string] by ref, phone number to send sms, can be overridden in hook processing
  582. * - `sms_text` - [string] by ref, text for sms, can be overridden in hook processing
  583. * - `override_result` - [mixed] by ref, can be object, can be overridden in hook processing
  584. * @hook @ref hook-account-before_forgot_password_send_sms
  585. */
  586. bx_alert('account', 'before_forgot_password_send_sms', $aAccountInfo['id'], false, [
  587. 'phone_number' => &$sPhone,
  588. 'sms_text' => &$sSmsText,
  589. 'override_result' => &$mixedOverrideResult
  590. ]);
  591. if ($mixedOverrideResult === null) {
  592. if(($oSms = BxDolSms::getObjectInstance()) !== false && $oSms->send($sPhone, $sSmsText))
  593. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_phone"));
  594. else
  595. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  596. }
  597. $sForm = '';
  598. }
  599. if($bApi) {
  600. return [
  601. bx_api_get_msg(strip_tags($sResultMsg)),
  602. ];
  603. }
  604. }
  605. else {
  606. $sResultMsg = _t($sCaptionKey);
  607. $sForm = $oForm->getCode();
  608. if($bApi) {
  609. return [
  610. bx_api_get_msg($sResultMsg, ['ext' => ['msg_type' => 'caption']]),
  611. bx_api_get_block('form', $oForm->getCodeAPI(), [
  612. 'ext' => [
  613. 'request' => ['url' => '/api.php?r=system/forgot_password/TemplServiceAccount', 'immutable' => true]
  614. ]
  615. ])];
  616. }
  617. }
  618. 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>' : '');
  619. }
  620. /**
  621. * @page service Service Calls
  622. * @section bx_system_general System Services
  623. * @subsection bx_system_general-account Account
  624. * @subsubsection bx_system_general-switch_profile switch_profile
  625. *
  626. * @code bx_srv('system', 'switch_profile', [123], 'TemplServiceAccount'); @endcode
  627. * @code {{~system:switch_profile:TemplServiceAccount:[123]~}} @endcode
  628. *
  629. * Switch current user profile.
  630. * User can switch between profiles if multiple profiles are created.
  631. * Also it's possible to switch to Organization profile if user is admin of Organization,
  632. * or other module which can `actAsProfile`.
  633. * Admin can switch to any prifile by default
  634. *
  635. * @param $iSwitchToProfileId profile ID to switch to
  636. * @return true on success or error message otherwise, so make sue to make strict (===) comparision
  637. *
  638. * @see BxBaseServiceAccount::serviceSwitchProfile
  639. */
  640. /**
  641. * @ref bx_system_general-switch_profile "switch_profile"
  642. */
  643. public function serviceSwitchProfile($iSwitchToProfileId)
  644. {
  645. $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
  646. if (!$oProfile) {
  647. return _t('_sys_txt_error_occured');
  648. }
  649. $iViewerAccountId = getLoggedId();
  650. $iSwitchToAccountId = $oProfile->getAccountId();
  651. $iViewerProfileId = bx_get_logged_profile_id();
  652. $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
  653. $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  654. $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
  655. /**
  656. * @hooks
  657. * @hookdef hook-account-check_switch_context 'account', 'check_switch_context' - hook on switch profile_id
  658. * - $unit_name - equals `account`
  659. * - $action - equals `check_switch_context`
  660. * - $object_id - account id
  661. * - $sender_id - viewer profile_id
  662. * - $extra_params - array of additional params with the following array keys:
  663. * - `switch_to_profile` - [int] profile_id to switch
  664. * - `viewer_account` - [int] viewer's profile_id, can be overridden in hook processing
  665. * - `override_result` - [bool] by ref, true if allow switch profile, otherwise false, can be overridden in hook processing
  666. * @hook @ref hook-account-check_switch_context
  667. */
  668. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  669. if (!$bCanSwitch) {
  670. return isset($aCheck[CHECK_ACTION_MESSAGE]) ? $aCheck[CHECK_ACTION_MESSAGE] : _t('_sys_txt_error_occured');
  671. }
  672. $oAccount = BxDolAccount::getInstance();
  673. if (!$oAccount->updateProfileContext($iSwitchToProfileId)) {
  674. return _t('_sys_txt_error_occured');
  675. }
  676. checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
  677. if (bx_is_api()){
  678. return BxDolProfile::getDataForPage($iSwitchToProfileId);
  679. }
  680. return true;
  681. }
  682. /**
  683. * Reset password procedure
  684. */
  685. public function resetPassword()
  686. {
  687. $bApi = bx_is_api();
  688. $sKey = bx_process_input(bx_get('key'));
  689. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password_reset');
  690. if(!empty($sKey) && isset($oForm->aInputs['key']))
  691. $oForm->aInputs['key']['value'] = $sKey;
  692. $oForm->initChecker();
  693. if($oForm->isSubmittedAndValid()) {
  694. $sErrorUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  695. // check if key exists
  696. $oKey = BxDolKey::getInstance();
  697. $sKey = $oForm->getCleanValue('key');
  698. if(!$oKey || !$oKey->isKeyExists($sKey))
  699. 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));
  700. // check if key data exists
  701. $aKeyData = $oKey->getKeyData($sKey);
  702. if(empty($aKeyData['email']))
  703. 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));
  704. // check if account with such email exists
  705. $iAccountId = $this->_oAccountQuery->getIdByEmail($aKeyData['email']);
  706. if(empty($iAccountId))
  707. 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));;
  708. $sPassword = $oForm->getCleanValue('password');
  709. $oAccount = BxDolAccount::getInstance($iAccountId);
  710. if (!$oAccount || !$oAccount->updatePassword($sPassword))
  711. return $bApi ?[ bx_api_get_msg(_t('_sys_txt_reset_pasword_error_occured', $sErrorUrl))] : MsgBox(_t('_sys_txt_reset_pasword_error_occured', $sErrorUrl));
  712. $this->_oAccountQuery->unlockAccount($iAccountId);
  713. $oKey->removeKey($sKey);
  714. 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');
  715. }
  716. $sTitleReset = _t('_sys_page_title_forgot_password_reset');
  717. BxDolTemplate::getInstance()->setPageHeader($sTitleReset);
  718. if(($oPage = BxDolPage::getObjectInstanceByURI()) !== false)
  719. $oPage->setTitle($sTitleReset);
  720. if($bApi) {
  721. return [
  722. bx_api_get_block('form', $oForm->getCodeAPI(), [
  723. 'ext' => [
  724. 'request' => ['url' => '/api.php?r=system/forgot_password/TemplServiceAccount', 'immutable' => true]
  725. ]
  726. ])];
  727. }
  728. return $oForm->getCode();
  729. }
  730. /**
  731. * Generate new password.
  732. */
  733. public function generateUserNewPwd($iAccountId)
  734. {
  735. $sPassword = genRndPwd(8, false);
  736. $sSalt = genRndSalt();
  737. $sPasswordHash = encryptUserPwd($sPassword, $sSalt);
  738. $this->_oAccountQuery->logPassword($iAccountId);
  739. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
  740. /**
  741. * @hooks
  742. * @hookdef hook-account-edited 'account', 'edited' - hook on account edited
  743. * - $unit_name - equals `account`
  744. * - $action - equals `edited`
  745. * - $object_id - account id
  746. * - $sender_id - account id
  747. * - $extra_params - array of additional params with the following array keys:
  748. * - `action` - [string] can be forgot_password/change_password or $sDisplayName (display name for current form)
  749. * @hook @ref hook-account-edited
  750. */
  751. bx_alert('account', 'edited', $iAccountId, $iAccountId, [
  752. 'action' => 'forgot_password'
  753. ]);
  754. return $sPassword;
  755. }
  756. protected function _confirmEmail($sKey)
  757. {
  758. // check if key exists
  759. $oKey = BxDolKey::getInstance();
  760. if (!$oKey || !$oKey->isKeyExists($sKey))
  761. return _t("_sys_txt_confirm_email_error_occured");
  762. // check if key data exists
  763. $aData = $oKey->getKeyData($sKey);
  764. if (!isset($aData['account_id']))
  765. return _t("_sys_txt_confirm_email_error_occured");
  766. // check if account exists
  767. $oAccount = BxDolAccount::getInstance($aData['account_id']);
  768. if (!$oAccount)
  769. return _t("_sys_txt_confirm_email_error_occured");
  770. // remove key
  771. $oKey->removeKey($sKey);
  772. // confirm email
  773. if (!$oAccount->updateEmailConfirmed(true))
  774. return _t("_sys_txt_confirm_email_error_occured");
  775. // login to user's account automatically
  776. bx_login($aData['account_id'], bx_is_remember_me());
  777. return (int)$aData['account_id'];
  778. }
  779. }
  780. /** @} */