1
0

BxBaseServiceAccount.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. if (!getParam('sys_account_auto_profile_creation')){
  66. $aModulesProfile = bx_srv('system', 'get_modules_by_type', ['profile']);
  67. $sDefaultProfileType = getParam('sys_account_default_profile_type');
  68. if(count($aModulesProfile) == 1)
  69. $sProfileModule = $aModulesProfile[0]['name'];
  70. else if(!empty($sDefaultProfileType))
  71. $sProfileModule = $sDefaultProfileType;
  72. $sRedirectUrl = !empty($sProfileModule) ? bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink(BxDolService::call($sProfileModule, 'profile_create_url', array(false)))) : '/';
  73. }
  74. else{
  75. $sRedirectUrl = '/';
  76. }
  77. }
  78. else{
  79. $parts = parse_url($sRedirectUrl);
  80. parse_str($parts['query'], $query);
  81. unset($query['_q']);
  82. $parts['query'] = http_build_query($query);
  83. $sRedirectUrl = $parts['path'] . (!empty($parts['query']) ? '?' . $parts['query'] : '');
  84. }
  85. return [
  86. bx_api_get_block('form', $this->_oAccountForms->getObjectFormAdd()->getCodeAPI(), [
  87. 'ext' => [
  88. 'request' => ['url' => '/api.php?r=system/create_account_form/TemplServiceAccount', 'immutable' => true]
  89. ]
  90. ]),
  91. bx_api_get_block('redirect', ['uri' => $sRedirectUrl]),
  92. ];
  93. }
  94. else
  95. return [bx_api_get_msg($mixedResult)];
  96. }
  97. $oPemalink = BxDolPermalinks::getInstance();
  98. if (isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) { // remember referrer
  99. $sJoinReferrer = $_SERVER['HTTP_REFERER'];
  100. $aNoRelocatePages = array('forgot-password', 'login', 'create-account', 'logout');
  101. foreach ($aNoRelocatePages as $s) {
  102. if (false !== mb_stripos($sJoinReferrer, $s)) {
  103. $sJoinReferrer = '';
  104. break;
  105. }
  106. }
  107. if ($sJoinReferrer)
  108. BxDolSession::getInstance()->setValue('join-referrer', $sJoinReferrer);
  109. }
  110. $aTmplVarsAuth = [];
  111. if(!isset($aParams['no_auth_buttons']) || false === (bool)$aParams['no_auth_buttons'])
  112. $aTmplVarsAuth['content'] = BxDolService::call('system', 'member_auth_code', array(), 'TemplServiceLogin');
  113. $aTmplVarsForm = [];
  114. if(!(bool)getParam('sys_account_disable_join_form'))
  115. $aTmplVarsForm['content'] = $this->_oAccountForms->createAccountForm($aParams);
  116. $aTmplVarsLogin = [];
  117. if(!isset($aParams['no_login_text']) || false === (bool)$aParams['no_login_text'])
  118. $aTmplVarsLogin['url'] = bx_absolute_url($oPemalink->permalink('page.php?i=login'));
  119. return BxDolTemplate::getInstance()->parseHtmlByName('block_join.html', [
  120. 'bx_if:show_auth' => [
  121. 'condition' => !empty($aTmplVarsAuth),
  122. 'content' => $aTmplVarsAuth
  123. ],
  124. 'bx_if:show_form' => [
  125. 'condition' => !empty($aTmplVarsForm),
  126. 'content' => $aTmplVarsForm,
  127. ],
  128. 'bx_if:show_login' => [
  129. 'condition' => !empty($aTmplVarsLogin),
  130. 'content' => $aTmplVarsLogin
  131. ],
  132. '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'))),
  133. ]);
  134. }
  135. public function serviceAccountSettingsEmail ($iAccountId = false)
  136. {
  137. if (false === $iAccountId)
  138. $iAccountId = getLoggedId();
  139. $bApi = bx_is_api();
  140. $mixedResult =$this->_oAccountForms->editAccountEmailSettingsForm($iAccountId);
  141. if($bApi) {
  142. if(is_array($mixedResult)){
  143. if (isset($mixedResult['form'] )){
  144. return [
  145. bx_api_get_msg($mixedResult['msg']),
  146. bx_api_get_block('form', $mixedResult['form'], [
  147. 'ext' => [
  148. 'request' => ['url' => '/api.php?r=system/account_settings_email/TemplServiceAccount', 'immutable' => true]
  149. ]
  150. ])];
  151. }
  152. else{
  153. return [bx_api_get_block('form', $mixedResult, [
  154. 'ext' => [
  155. 'request' => ['url' => '/api.php?r=system/account_settings_email/TemplServiceAccount', 'immutable' => true]
  156. ]
  157. ])];
  158. }
  159. }
  160. else
  161. return [bx_api_get_msg($mixedResult)];
  162. }
  163. return $mixedResult;
  164. }
  165. public function serviceAccountSettingsPassword ($iAccountId = false)
  166. {
  167. if (false === $iAccountId)
  168. $iAccountId = getLoggedId();
  169. $bApi = bx_is_api();
  170. $mixedResult =$this->_oAccountForms->editAccountPasswordSettingsForm($iAccountId);
  171. if($bApi) {
  172. if(is_array($mixedResult)){
  173. if (isset($mixedResult['form'] )){
  174. return [
  175. bx_api_get_msg($mixedResult['msg']),
  176. bx_api_get_block('form', $mixedResult['form'], [
  177. 'ext' => [
  178. 'request' => ['url' => '/api.php?r=system/account_settings_password/TemplServiceAccount', 'immutable' => true]
  179. ]
  180. ])];
  181. }
  182. else{
  183. return [bx_api_get_block('form', $mixedResult, [
  184. 'ext' => [
  185. 'request' => ['url' => '/api.php?r=system/account_settings_password/TemplServiceAccount', 'immutable' => true]
  186. ]
  187. ])];
  188. }
  189. }
  190. else
  191. return [bx_api_get_msg($mixedResult)];
  192. }
  193. return $mixedResult;
  194. }
  195. public function serviceAccountSettingsInfo ($iAccountId = false)
  196. {
  197. if (false === $iAccountId)
  198. $iAccountId = getLoggedId();
  199. $bApi = bx_is_api();
  200. $mixedResult =$this->_oAccountForms->editAccountInfoForm($iAccountId);
  201. if($bApi) {
  202. if(is_array($mixedResult)){
  203. if (isset($mixedResult['form'] )){
  204. return [
  205. bx_api_get_msg($mixedResult['msg']),
  206. bx_api_get_block('form', $mixedResult['form'], [
  207. 'ext' => [
  208. 'request' => ['url' => '/api.php?r=system/account_settings_info/TemplServiceAccount', 'immutable' => true]
  209. ]
  210. ])];
  211. }
  212. else{
  213. return [bx_api_get_block('form', $mixedResult, [
  214. 'ext' => [
  215. 'request' => ['url' => '/api.php?r=system/account_settings_info/TemplServiceAccount', 'immutable' => true]
  216. ]
  217. ])];
  218. }
  219. }
  220. else
  221. return [bx_api_get_msg($mixedResult)];
  222. }
  223. return $mixedResult;
  224. }
  225. public function serviceAccountSettingsDelAccount ($iAccountId = false)
  226. {
  227. if (!$iAccountId)
  228. $iAccountId = bx_process_input(bx_get('id'), BX_DATA_INT);
  229. if (!$iAccountId)
  230. $iAccountId = getLoggedId();
  231. $bApi = bx_is_api();
  232. $mixedResult = $this->_oAccountForms->deleteAccountForm($iAccountId);
  233. if($bApi) {
  234. if(is_array($mixedResult)){
  235. if (isset($mixedResult['form'] )){
  236. return [
  237. bx_api_get_msg($mixedResult['msg']),
  238. bx_api_get_block('form', $mixedResult['form'], [
  239. 'ext' => [
  240. 'request' => ['url' => '/api.php?r=system/account_settings_del_account/TemplServiceAccount', 'immutable' => true]
  241. ]
  242. ])];
  243. }
  244. else{
  245. return [bx_api_get_block('form', $mixedResult, [
  246. 'ext' => [
  247. 'request' => ['url' => '/api.php?r=system/account_settings_del_account/TemplServiceAccount', 'immutable' => true]
  248. ]
  249. ])];
  250. }
  251. }
  252. else if(is_string($mixedResult))
  253. return [
  254. bx_api_get_msg($mixedResult)
  255. ];
  256. else
  257. return [
  258. ['id' => 2, 'type' => 'redirect', 'data' => ['uri' => '/']],
  259. ];
  260. }
  261. return $mixedResult;
  262. }
  263. public function serviceAccountDelete($iId, $bWithContent = false)
  264. {
  265. if(($oAccount = BxDolAccount::getInstance($iId)) !== false)
  266. return $oAccount->delete($bWithContent);
  267. return false;
  268. }
  269. /**
  270. * Display unsubscribe from newsletters form
  271. */
  272. public function serviceUnsubscribeNews()
  273. {
  274. return $this->_unsubscribeForm('sys_unsubscribe_news');
  275. }
  276. /**
  277. * Display unsubscribe from notifications form
  278. */
  279. public function serviceUnsubscribeNotifications()
  280. {
  281. return $this->_unsubscribeForm('sys_unsubscribe_updates');
  282. }
  283. /**
  284. * Display confirmation statuses list
  285. */
  286. public function serviceGetConfirmationTypes()
  287. {
  288. $aResult = array();
  289. $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);
  290. foreach($aChoices as $sChoice)
  291. $aResult[$sChoice] = _t('_sys_acc_confirmation_type_' . $sChoice);
  292. return $aResult;
  293. }
  294. protected function _unsubscribeForm($sDisplay)
  295. {
  296. $iAccountId = bx_process_input(bx_get('id'));
  297. $sCode = bx_process_input(bx_get('code'));
  298. // validate input params
  299. $oAccount = BxDolAccount::getInstance($iAccountId);
  300. if (!$oAccount)
  301. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  302. if ($sCode != $oAccount->getEmailHash())
  303. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  304. $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
  305. if (!$oForm)
  306. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  307. // show form
  308. $aAccountInfo = $oAccount->getInfo();
  309. $aAccountInfo['code'] = $sCode;
  310. $oForm->initChecker($aAccountInfo);
  311. if ($oForm->isSubmittedAndValid()) {
  312. if (!$oForm->update($oAccount->id()))
  313. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  314. return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
  315. }
  316. return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
  317. }
  318. /**
  319. * Display email confirmation form, if confirmation code is provided then try to confirm profile
  320. */
  321. public function serviceEmailConfirmation($sMsg = false)
  322. {
  323. $bApi = bx_is_api();
  324. // if user is logged in and email is confirmed then just display a message
  325. if (isLogged() && !bx_get('code')) {
  326. $oAccount = BxDolAccount::getInstance();
  327. if ($oAccount->isConfirmedEmail())
  328. return $bApi ? [bx_api_get_msg(_t("_sys_txt_confirm_email_already_confirmed"))] : MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
  329. }
  330. // if confirmation code is provided in the URL - perform email confirmation right away
  331. if (bx_get('code') !== false)
  332. return $this->confirmEmail(bx_process_input(bx_get('code')));
  333. // if user requested to resend verification letter then send letter and display message
  334. if (bx_process_input(bx_get('resend')) && isLogged()) {
  335. $oAccount = BxDolAccount::getInstance();
  336. if ($oAccount->sendConfirmationEmail())
  337. $sMsg = _t('_sys_txt_confirm_email_sent');
  338. else
  339. $sMsg = _t('_sys_txt_confirm_email_sent_failed');
  340. return $bApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg);
  341. }
  342. // show and process code verification form
  343. $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
  344. if (!$oForm)
  345. return $bApi ? [bx_api_get_msg(_t("_sys_txt_confirm_email_error_occured"))] : MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  346. $oForm->initChecker();
  347. if ($oForm->isSubmittedAndValid()) {
  348. $oKey = BxDolKey::getInstance();
  349. if (!$oKey){
  350. return $bApi ? [bx_api_get_msg(_t("_sys_txt_confirm_email_error_occured"))] : MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  351. }
  352. else{
  353. $oRv = $this->confirmEmail(trim($oForm->getCleanValue('code')));
  354. if (!$bApi)
  355. return $oRv;
  356. return $oRv;
  357. }
  358. }
  359. if ($bApi){
  360. return [bx_api_get_block('form', $oForm->getCodeAPI(), [
  361. 'ext' => [
  362. 'request' => ['url' => '/api.php?r=system/email_confirmation/TemplServiceAccount', 'immutable' => true]
  363. ]
  364. ])];
  365. }
  366. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
  367. }
  368. /**
  369. * Display phone confirmation forms, if confirmation code is provided then try to confirm profile
  370. */
  371. public function servicePhoneConfirmation($sMsg = false)
  372. {
  373. // if user is logged in and phone is confirmed then just display a message
  374. if (isLogged()) {
  375. $oAccount = BxDolAccount::getInstance();
  376. if ($oAccount->isConfirmedPhone())
  377. return MsgBox(_t("_sys_txt_confirm_phone_already_confirmed"));
  378. $iStep = 1;
  379. $a = $oAccount->getInfo();
  380. $sPhoneNumber = $a['phone'];
  381. $oSession = BxDolSession::getInstance();
  382. if ($sPhoneNumber != "" && $oSession->isValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE))
  383. $iStep = 2;
  384. if (bx_get('step'))
  385. $iStep = (int)bx_get('step');
  386. // show and process phone set form
  387. if ($iStep == 1){
  388. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_set_phone');
  389. if (!$oForm)
  390. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_error_occured"));
  391. $oForm->initChecker(['phone' => $sPhoneNumber]);
  392. if ($oForm->isSubmittedAndValid()) {
  393. $sPhoneNumber = trim($oForm->getCleanValue('phone'));
  394. $oAccount->updatePhone($sPhoneNumber);
  395. $sActivationCode = rand(1000, 9999);
  396. $sActivationText =_t('_sys_txt_confirm_phone_sms_text', $sActivationCode);
  397. $mixedResult = null;
  398. /**
  399. * @hooks
  400. * @hookdef hook-account-before_confirm_phone_send_sms 'account', 'before_confirm_phone_send_sms' - hook in confirm phone
  401. * - $unit_name - equals `account`
  402. * - $action - equals `before_confirm_phone_send_sms`
  403. * - $object_id - account id
  404. * - $sender_id - logged profile_id
  405. * - $extra_params - array of additional params with the following array keys:
  406. * - `phone_number` - [string] by ref, phone number to send sms, can be overridden in hook processing
  407. * - `sms_text` - [string] by ref, text for sms, can be overridden in hook processing
  408. * - `override_result` - [mixed] by ref, can be object, can be overridden in hook processing
  409. * @hook @ref hook-account-before_confirm_phone_send_sms
  410. */
  411. bx_alert('account', 'before_confirm_phone_send_sms', $oAccount->id(), bx_get_logged_profile_id(), [
  412. 'phone_number' => &$sPhoneNumber,
  413. 'sms_text' => &$sActivationText,
  414. 'override_result' => &$mixedResult
  415. ]);
  416. if ($mixedResult === null && ($oSms = BxDolSms::getObjectInstance()) !== false && !$oSms->send($sPhoneNumber, $sActivationText))
  417. return MsgBox(_t("_sys_txt_confirm_phone_send_sms_error_occured"));
  418. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  419. header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone')));
  420. }
  421. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_set_phone") . '</div>' .$oForm->getCode();
  422. }
  423. if ($iStep == 2){
  424. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_confirmation');
  425. if (!$oForm)
  426. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_confirmation_occured"));
  427. $oForm->initChecker();
  428. if ($oForm->isSubmittedAndValid()) {
  429. $oAccount->updatePhoneConfirmed(true);
  430. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  431. return MsgBox(_t("_sys_txt_confirm_phone_confirm_success"));
  432. }
  433. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_phone_confirmation") . '</div>' .$oForm->getCode();
  434. }
  435. }
  436. }
  437. /**
  438. * Perform email confirmation
  439. */
  440. public function confirmEmail($sKey)
  441. {
  442. $bApi = bx_is_api();
  443. $mixedAccountId = $this->_confirmEmail($sKey);
  444. if(is_string($mixedAccountId) && !is_numeric($mixedAccountId))
  445. return $bApi ? [bx_api_get_msg($mixedAccountId)] : MsgBox($mixedAccountId);
  446. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled())
  447. $oSockets->sendEvent('sys_account', $mixedAccountId, 'confirmed', '');
  448. // redirect with success message
  449. $sUrl = getParam('sys_redirect_after_email_confirmation');
  450. /**
  451. * @hooks
  452. * @hookdef hook-account-after_email_confirmation 'account', 'after_email_confirmation' - hook after email confirmed
  453. * - $unit_name - equals `account`
  454. * - $action - equals `after_email_confirmation`
  455. * - $object_id - account id
  456. * - $sender_id - not used
  457. * - $extra_params - array of additional params with the following array keys:
  458. * - `override_result` - [string] by ref, url for redirect after email confirmed, can be overridden in hook processing
  459. * @hook @ref hook-account-after_email_confirmation
  460. */
  461. bx_alert('account', 'after_email_confirmation', $mixedAccountId, false, ['override_result' => &$sUrl]);
  462. if ($bApi){
  463. return [bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl))])];
  464. }
  465. $oTemplate = BxDolTemplate::getInstance();
  466. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  467. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  468. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  469. $oTemplate->setPageContent ('url_relocate', bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sUrl)));
  470. BxDolTemplate::getInstance()->getPageCode();
  471. exit;
  472. }
  473. public function serviceConfirmEmail($sKey)
  474. {
  475. $mixedAccountId = $this->_confirmEmail($sKey);
  476. if(is_string($mixedAccountId) && !is_numeric($mixedAccountId))
  477. return $bApi ? [bx_api_get_msg($mixedAccountId)] : MsgBox($mixedAccountId);
  478. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()){
  479. $oSockets->sendEvent('sys_account', $mixedAccountId, 'confirmed', '');
  480. }
  481. bx_alert('account', 'after_email_confirmation', $mixedAccountId, false, ['override_result' => &$sUrl]);
  482. return true;
  483. }
  484. public function serviceGetOptionsResetPasswordRedirect()
  485. {
  486. $aResult = array();
  487. $aChoices = array('home', 'profile', 'profile_edit', 'custom');
  488. foreach($aChoices as $sChoice)
  489. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_reset_password_redirect_' . $sChoice));
  490. return $aResult;
  491. }
  492. public function serviceGetOptionsSwitchToProfileRedirect()
  493. {
  494. $aResult = array();
  495. $aChoices = array('back', 'home', 'profile', 'custom');
  496. foreach($aChoices as $sChoice)
  497. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_switch_to_profile_redirect_' . $sChoice));
  498. return $aResult;
  499. }
  500. public function serviceGetOptionsPruningInterval()
  501. {
  502. $aResult = array();
  503. $aChoices = array('no_login_delete', 'no_login_suspend', 'no_confirm_delete', 'no_profile_delete');
  504. foreach($aChoices as $sChoice)
  505. $aResult[] = array('key' => $sChoice, 'value' => _t('_adm_stg_cpt_option_value_sys_account_pruning_' . $sChoice));
  506. return $aResult;
  507. }
  508. /**
  509. * @page service Service Calls
  510. * @section bx_system_general System Services
  511. * @subsection bx_system_general-account Account
  512. * @subsubsection bx_system_general-forgot_password forgot_password
  513. *
  514. * @code bx_srv('system', 'forgot_password', [], 'TemplServiceAccount'); @endcode
  515. * @code {{~system:forgot_password:TemplServiceAccount~}} @endcode
  516. *
  517. * Display forgot password form,
  518. * if reset password key is provided then reset password
  519. *
  520. * @see BxBaseServiceAccount::serviceForgotPassword
  521. */
  522. /**
  523. * @ref bx_system_general-forgot_password "forgot_password"
  524. */
  525. public function serviceForgotPassword()
  526. {
  527. $bApi = bx_is_api();
  528. if (isLogged() && 'forgot-password' == bx_get('i')) {
  529. if (!$bApi) {
  530. header('Location: ' . BX_DOL_URL_ROOT);
  531. exit;
  532. }
  533. else
  534. return [
  535. ['id' => 2, 'type' => 'redirect', 'data' => ['uri' => '/']],
  536. ];
  537. }
  538. if (bx_get('key') !== false)
  539. return $this->resetPassword();
  540. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  541. if (!$oForm)
  542. return '';
  543. $bNeedCheckEmailAndPhone = true;
  544. $sCaptionKey = "_sys_txt_forgot_pasword_by_both_desc";
  545. if (BxDolAccount::isNeedConfirmPhone()){
  546. if (!BxDolAccount::isNeedConfirmEmail()){
  547. unset($oForm->aInputs['email']);
  548. $sCaptionKey = "_sys_txt_forgot_pasword_by_phone_desc";
  549. $oForm->aInputs['phone']['checker']['func'] = 'PhoneExist';
  550. $bNeedCheckEmailAndPhone = false;
  551. }
  552. }
  553. else{
  554. unset($oForm->aInputs['phone']);
  555. $sCaptionKey = "_sys_txt_forgot_pasword_desc";
  556. $oForm->aInputs['email']['checker']['func'] = 'EmailExist';
  557. $bNeedCheckEmailAndPhone = false;
  558. }
  559. if ((BxDolAccount::isNeedConfirmPhone() && BxDolAccount::isNeedConfirmEmail())){
  560. if (isset($oForm->aInputs['phone']))
  561. $oForm->aInputs['phone']['required'] = false;
  562. if (isset($oForm->aInputs['email']))
  563. $oForm->aInputs['email']['required'] = false;
  564. }
  565. $oForm->initChecker();
  566. $bShowErrorEmptyBoth = false;
  567. if ($oForm->isSubmitted() && $bNeedCheckEmailAndPhone && $oForm->getCleanValue('phone') == '' && $oForm->getCleanValue('email') == ''){
  568. $bShowErrorEmptyBoth = true;
  569. $oForm->setValid(false);
  570. }
  571. $sResultMsg = '';
  572. if ($oForm->isSubmittedAndValid()) {
  573. $sEmail = $oForm->getCleanValue('email');
  574. $sPhone = $oForm->getCleanValue('phone');
  575. if (isset($oForm->aInputs['email']) && $sEmail != '') {
  576. $oAccount = BxDolAccount::getInstance($this->_oAccountQuery->getIdByEmail($sEmail));
  577. if($oAccount && $oAccount->sendResetPasswordEmail())
  578. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  579. else
  580. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  581. $sForm = '';
  582. }
  583. if (isset($oForm->aInputs['phone']) && $sPhone != '') {
  584. $iAccountId = $this->_oAccountQuery->getIdByPhone($sPhone);
  585. $aAccountInfo = $this->_oAccountQuery->getInfoById($iAccountId);
  586. $sKey = bx_get_reset_password_key($aAccountInfo['email']);
  587. $sForgotPasswordUrl = bx_get_reset_password_link_by_key($sKey);
  588. $sSmsText = _t('_sys_txt_forgot_pasword_sms_text', $sForgotPasswordUrl);
  589. $mixedOverrideResult = null;
  590. /**
  591. * @hooks
  592. * @hookdef hook-account-before_forgot_password_send_sms 'account', 'before_forgot_password_send_sms' - hook in $oAccount->isConfirmed check
  593. * - $unit_name - equals `account`
  594. * - $action - equals `before_forgot_password_send_sms`
  595. * - $object_id - account id
  596. * - $sender_id - not used
  597. * - $extra_params - array of additional params with the following array keys:
  598. * - `phone_number` - [string] by ref, phone number to send sms, can be overridden in hook processing
  599. * - `sms_text` - [string] by ref, text for sms, can be overridden in hook processing
  600. * - `override_result` - [mixed] by ref, can be object, can be overridden in hook processing
  601. * @hook @ref hook-account-before_forgot_password_send_sms
  602. */
  603. bx_alert('account', 'before_forgot_password_send_sms', $aAccountInfo['id'], false, [
  604. 'phone_number' => &$sPhone,
  605. 'sms_text' => &$sSmsText,
  606. 'override_result' => &$mixedOverrideResult
  607. ]);
  608. if ($mixedOverrideResult === null) {
  609. if(($oSms = BxDolSms::getObjectInstance()) !== false && $oSms->send($sPhone, $sSmsText))
  610. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_phone"));
  611. else
  612. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  613. }
  614. $sForm = '';
  615. }
  616. if($bApi) {
  617. return [
  618. bx_api_get_msg(strip_tags($sResultMsg)),
  619. bx_api_get_block('form', $oForm->getCodeAPI(), [
  620. 'ext' => [
  621. 'request' => ['url' => '/api.php?r=system/forgot_password/TemplServiceAccount', 'immutable' => true]
  622. ]
  623. ])
  624. ];
  625. }
  626. }
  627. else {
  628. $sResultMsg = _t($sCaptionKey);
  629. $sForm = $oForm->getCode();
  630. if($bApi) {
  631. return [
  632. bx_api_get_msg($sResultMsg, ['ext' => ['msg_type' => 'caption']]),
  633. bx_api_get_block('form', $oForm->getCodeAPI(), [
  634. 'ext' => [
  635. 'request' => ['url' => '/api.php?r=system/forgot_password/TemplServiceAccount', 'immutable' => true]
  636. ]
  637. ])];
  638. }
  639. }
  640. 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>' : '');
  641. }
  642. /**
  643. * @page service Service Calls
  644. * @section bx_system_general System Services
  645. * @subsection bx_system_general-account Account
  646. * @subsubsection bx_system_general-switch_profile switch_profile
  647. *
  648. * @code bx_srv('system', 'switch_profile', [123], 'TemplServiceAccount'); @endcode
  649. * @code {{~system:switch_profile:TemplServiceAccount:[123]~}} @endcode
  650. *
  651. * Switch current user profile.
  652. * User can switch between profiles if multiple profiles are created.
  653. * Also it's possible to switch to Organization profile if user is admin of Organization,
  654. * or other module which can `actAsProfile`.
  655. * Admin can switch to any prifile by default
  656. *
  657. * @param $iSwitchToProfileId profile ID to switch to
  658. * @return true on success or error message otherwise, so make sue to make strict (===) comparision
  659. *
  660. * @see BxBaseServiceAccount::serviceSwitchProfile
  661. */
  662. /**
  663. * @ref bx_system_general-switch_profile "switch_profile"
  664. */
  665. public function serviceSwitchProfile($iSwitchToProfileId)
  666. {
  667. $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
  668. if (!$oProfile) {
  669. return _t('_sys_txt_error_occured');
  670. }
  671. $iViewerAccountId = getLoggedId();
  672. $iSwitchToAccountId = $oProfile->getAccountId();
  673. $iViewerProfileId = bx_get_logged_profile_id();
  674. $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
  675. $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  676. $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
  677. /**
  678. * @hooks
  679. * @hookdef hook-account-check_switch_context 'account', 'check_switch_context' - hook on switch profile_id
  680. * - $unit_name - equals `account`
  681. * - $action - equals `check_switch_context`
  682. * - $object_id - account id
  683. * - $sender_id - viewer profile_id
  684. * - $extra_params - array of additional params with the following array keys:
  685. * - `switch_to_profile` - [int] profile_id to switch
  686. * - `viewer_account` - [int] viewer's profile_id, can be overridden in hook processing
  687. * - `override_result` - [bool] by ref, true if allow switch profile, otherwise false, can be overridden in hook processing
  688. * @hook @ref hook-account-check_switch_context
  689. */
  690. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  691. if (!$bCanSwitch) {
  692. return isset($aCheck[CHECK_ACTION_MESSAGE]) ? $aCheck[CHECK_ACTION_MESSAGE] : _t('_sys_txt_error_occured');
  693. }
  694. $oAccount = BxDolAccount::getInstance();
  695. if (!$oAccount->updateProfileContext($iSwitchToProfileId)) {
  696. return _t('_sys_txt_error_occured');
  697. }
  698. checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
  699. if (bx_is_api()){
  700. return BxDolProfile::getDataForPage($iSwitchToProfileId);
  701. }
  702. return true;
  703. }
  704. /**
  705. * Reset password procedure
  706. */
  707. public function resetPassword()
  708. {
  709. $bApi = bx_is_api();
  710. $sKey = bx_process_input(bx_get('key'));
  711. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password_reset');
  712. if(!empty($sKey) && isset($oForm->aInputs['key']))
  713. $oForm->aInputs['key']['value'] = $sKey;
  714. $oForm->initChecker();
  715. if($oForm->isSubmittedAndValid()) {
  716. $sErrorUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  717. // check if key exists
  718. $oKey = BxDolKey::getInstance();
  719. $sKey = $oForm->getCleanValue('key');
  720. if(!$oKey || !$oKey->isKeyExists($sKey))
  721. 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));
  722. // check if key data exists
  723. $aKeyData = $oKey->getKeyData($sKey);
  724. if(empty($aKeyData['email']))
  725. 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));
  726. // check if account with such email exists
  727. $iAccountId = $this->_oAccountQuery->getIdByEmail($aKeyData['email']);
  728. if(empty($iAccountId))
  729. 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));;
  730. $sPassword = $oForm->getCleanValue('password');
  731. $oAccount = BxDolAccount::getInstance($iAccountId);
  732. if (!$oAccount || !$oAccount->updatePassword($sPassword))
  733. return $bApi ?[ bx_api_get_msg(_t('_sys_txt_reset_pasword_error_occured', $sErrorUrl))] : MsgBox(_t('_sys_txt_reset_pasword_error_occured', $sErrorUrl));
  734. $this->_oAccountQuery->unlockAccount($iAccountId);
  735. $oKey->removeKey($sKey);
  736. 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');
  737. }
  738. $sTitleReset = _t('_sys_page_title_forgot_password_reset');
  739. BxDolTemplate::getInstance()->setPageHeader($sTitleReset);
  740. if(($oPage = BxDolPage::getObjectInstanceByURI()) !== false)
  741. $oPage->setTitle($sTitleReset);
  742. if($bApi) {
  743. return [
  744. bx_api_get_block('form', $oForm->getCodeAPI(), [
  745. 'ext' => [
  746. 'request' => ['url' => '/api.php?r=system/forgot_password/TemplServiceAccount', 'immutable' => true]
  747. ]
  748. ])];
  749. }
  750. return $oForm->getCode();
  751. }
  752. /**
  753. * Generate new password.
  754. */
  755. public function generateUserNewPwd($iAccountId)
  756. {
  757. $sPassword = genRndPwd(8, false);
  758. $sSalt = genRndSalt();
  759. $sPasswordHash = encryptUserPwd($sPassword, $sSalt);
  760. $this->_oAccountQuery->logPassword($iAccountId);
  761. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
  762. /**
  763. * @hooks
  764. * @hookdef hook-account-edited 'account', 'edited' - hook on account edited
  765. * - $unit_name - equals `account`
  766. * - $action - equals `edited`
  767. * - $object_id - account id
  768. * - $sender_id - account id
  769. * - $extra_params - array of additional params with the following array keys:
  770. * - `action` - [string] can be forgot_password/change_password or $sDisplayName (display name for current form)
  771. * @hook @ref hook-account-edited
  772. */
  773. bx_alert('account', 'edited', $iAccountId, $iAccountId, [
  774. 'action' => 'forgot_password'
  775. ]);
  776. return $sPassword;
  777. }
  778. protected function _confirmEmail($sKey)
  779. {
  780. // check if key exists
  781. $oKey = BxDolKey::getInstance();
  782. if (!$oKey || !$oKey->isKeyExists($sKey))
  783. return _t("_sys_txt_confirm_email_error_occured");
  784. // check if key data exists
  785. $aData = $oKey->getKeyData($sKey);
  786. if (!isset($aData['account_id']))
  787. return _t("_sys_txt_confirm_email_error_occured");
  788. // check if account exists
  789. $oAccount = BxDolAccount::getInstance($aData['account_id']);
  790. if (!$oAccount)
  791. return _t("_sys_txt_confirm_email_error_occured");
  792. // remove key
  793. $oKey->removeKey($sKey);
  794. // confirm email
  795. if (!$oAccount->updateEmailConfirmed(true))
  796. return _t("_sys_txt_confirm_email_error_occured");
  797. // login to user's account automatically
  798. bx_login($aData['account_id'], bx_is_remember_me());
  799. $oAccount->sendWelcomeEmail();
  800. return (int)$aData['account_id'];
  801. }
  802. }
  803. /** @} */