1
0

BxBaseServiceAccount.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. if (isLogged() && 'create-account' == bx_get('i')){
  44. header('Location: ' . BX_DOL_URL_ROOT);
  45. exit;
  46. }
  47. if (isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) { // remember referrer
  48. $sJoinReferrer = $_SERVER['HTTP_REFERER'];
  49. $aNoRelocatePages = array('forgot-password', 'login', 'create-account', 'logout');
  50. foreach ($aNoRelocatePages as $s) {
  51. if (false !== mb_stripos($sJoinReferrer, $s)) {
  52. $sJoinReferrer = '';
  53. break;
  54. }
  55. }
  56. if ($sJoinReferrer)
  57. BxDolSession::getInstance()->setValue('join-referrer', $sJoinReferrer);
  58. }
  59. $sLoginText = '';
  60. if (!isset($aParams['no_login_text']) || false === (bool)$aParams['no_login_text'])
  61. $sLoginText = '<hr class="bx-def-hr bx-def-margin-sec-topbottom" /><div>' . _t('_sys_txt_join_description', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login')) . '</div>';
  62. $sAuth = '';
  63. if (!isset($aParams['no_auth_buttons']) || false === (bool)$aParams['no_auth_buttons'])
  64. $sAuth = BxDolService::call('system', 'member_auth_code', array(), 'TemplServiceLogin');
  65. return $sAuth . $this->_oAccountForms->createAccountForm($aParams) . $sLoginText;
  66. }
  67. public function serviceAccountSettingsEmail ($iAccountId = false)
  68. {
  69. if (false === $iAccountId)
  70. $iAccountId = getLoggedId();
  71. return $this->_oAccountForms->editAccountEmailSettingsForm($iAccountId);
  72. }
  73. public function serviceAccountSettingsPassword ($iAccountId = false)
  74. {
  75. if (false === $iAccountId)
  76. $iAccountId = getLoggedId();
  77. return $this->_oAccountForms->editAccountPasswordSettingsForm($iAccountId);
  78. }
  79. public function serviceAccountSettingsInfo ($iAccountId = false)
  80. {
  81. if (false === $iAccountId)
  82. $iAccountId = getLoggedId();
  83. return $this->_oAccountForms->editAccountInfoForm($iAccountId);
  84. }
  85. public function serviceAccountSettingsDelAccount ($iAccountId = false)
  86. {
  87. if (!$iAccountId)
  88. $iAccountId = bx_process_input(bx_get('id'), BX_DATA_INT);
  89. if (!$iAccountId)
  90. $iAccountId = getLoggedId();
  91. return $this->_oAccountForms->deleteAccountForm($iAccountId);
  92. }
  93. /**
  94. * Display unsubscribe from newsletters form
  95. */
  96. public function serviceUnsubscribeNews()
  97. {
  98. return $this->_unsubscribeForm('sys_unsubscribe_news');
  99. }
  100. /**
  101. * Display unsubscribe from notifications form
  102. */
  103. public function serviceUnsubscribeNotifications()
  104. {
  105. return $this->_unsubscribeForm('sys_unsubscribe_updates');
  106. }
  107. /**
  108. * Display confirmation statuses list
  109. */
  110. public function serviceGetConfirmationTypes()
  111. {
  112. $aResult = array();
  113. $aChoices = array(BX_ACCOUNT_CONFIRMATION_NONE, BX_ACCOUNT_CONFIRMATION_EMAIL, BX_ACCOUNT_CONFIRMATION_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE);
  114. foreach($aChoices as $sChoice)
  115. $aResult[$sChoice] = _t('_sys_acc_confirmation_type_' . $sChoice);
  116. return $aResult;
  117. }
  118. protected function _unsubscribeForm($sDisplay)
  119. {
  120. $iAccountId = bx_process_input(bx_get('id'));
  121. $sCode = bx_process_input(bx_get('code'));
  122. // validate input params
  123. $oAccount = BxDolAccount::getInstance($iAccountId);
  124. if (!$oAccount)
  125. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  126. if ($sCode != $oAccount->getEmailHash())
  127. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  128. $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
  129. if (!$oForm)
  130. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  131. // show form
  132. $aAccountInfo = $oAccount->getInfo();
  133. $aAccountInfo['code'] = $sCode;
  134. $oForm->initChecker($aAccountInfo);
  135. if ($oForm->isSubmittedAndValid()) {
  136. if (!$oForm->update($oAccount->id()))
  137. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  138. return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
  139. }
  140. return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
  141. }
  142. /**
  143. * Display email confirmation form, if confirmation code is provided then try to confirm profile
  144. */
  145. public function serviceEmailConfirmation($sMsg = false)
  146. {
  147. // if user is logged in and email is confirmed then just display a message
  148. if (isLogged()) {
  149. $oAccount = BxDolAccount::getInstance();
  150. if ($oAccount->isConfirmedEmail())
  151. return MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
  152. }
  153. // if confirmation code is provided in the URL - perform email confirmation right away
  154. if (bx_get('code') !== false)
  155. return $this->confirmEmail(bx_process_input(bx_get('code')));
  156. // if user requested to resend verification letter then send letter and display message
  157. if (bx_process_input(bx_get('resend')) && isLogged()) {
  158. $oAccount = BxDolAccount::getInstance();
  159. if ($oAccount->sendConfirmationEmail())
  160. $sMsg = _t('_sys_txt_confirm_email_sent');
  161. else
  162. $sMsg = _t('_sys_txt_confirm_email_sent_failed');
  163. return MsgBox($sMsg);
  164. }
  165. // show and process code verification form
  166. $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
  167. if (!$oForm)
  168. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  169. $oForm->initChecker();
  170. if ($oForm->isSubmittedAndValid()) {
  171. $oKey = BxDolKey::getInstance();
  172. if (!$oKey)
  173. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  174. else
  175. return $this->confirmEmail(trim($oForm->getCleanValue('code')));
  176. }
  177. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
  178. }
  179. /**
  180. * Display phone confirmation forms, if confirmation code is provided then try to confirm profile
  181. */
  182. public function servicePhoneConfirmation($sMsg = false)
  183. {
  184. // if user is logged in and phone is confirmed then just display a message
  185. if (isLogged()) {
  186. $oAccount = BxDolAccount::getInstance();
  187. if ($oAccount->isConfirmedPhone())
  188. return MsgBox(_t("_sys_txt_confirm_phone_already_confirmed"));
  189. $iStep = 1;
  190. $a = $oAccount->getInfo();
  191. $sPhoneNumber = $a['phone'];
  192. $oSession = BxDolSession::getInstance();
  193. if ($sPhoneNumber != "" && $oSession->isValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE)){
  194. $iStep = 2;
  195. }
  196. if (bx_get('step'))
  197. $iStep = (int)bx_get('step');
  198. // show and process phone set form
  199. if ($iStep == 1){
  200. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_set_phone');
  201. if (!$oForm)
  202. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_error_occured"));
  203. $oForm->initChecker(array('phone' => $sPhoneNumber));
  204. if ($oForm->isSubmittedAndValid()) {
  205. $oAccount->updatePhone(trim($oForm->getCleanValue('phone')));
  206. $sActivationCode = rand(1000, 9999);
  207. $sActivationText =_t('_sys_txt_confirm_phone_sms_text', $sActivationCode);
  208. $ret = null;
  209. 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));
  210. if ($ret === null)
  211. {
  212. $oTwilio = BxDolTwilio::getInstance();
  213. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  214. return MsgBox(_t("_sys_txt_confirm_phone_send_sms_error_occured"));
  215. }
  216. }
  217. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  218. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone'));
  219. }
  220. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_set_phone") . '</div>' .$oForm->getCode();
  221. }
  222. if ($iStep == 2){
  223. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_confirmation');
  224. if (!$oForm)
  225. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_confirmation_occured"));
  226. $oForm->initChecker();
  227. if ($oForm->isSubmittedAndValid()) {
  228. $oAccount->updatePhoneConfirmed(true);
  229. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  230. return MsgBox(_t("_sys_txt_confirm_phone_confirm_success"));
  231. }
  232. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_phone_confirmation") . '</div>' .$oForm->getCode();
  233. }
  234. }
  235. }
  236. /**
  237. * Perform email confirmation
  238. */
  239. public function confirmEmail($sKey)
  240. {
  241. // check if key exists
  242. $oKey = BxDolKey::getInstance();
  243. if (!$oKey || !$oKey->isKeyExists($sKey))
  244. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  245. // check if key data exists
  246. $aData = $oKey->getKeyData($sKey);
  247. if (!isset($aData['account_id']))
  248. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  249. // check if account exists
  250. $oAccount = BxDolAccount::getInstance($aData['account_id']);
  251. if (!$oAccount)
  252. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  253. // remove key
  254. $oKey->removeKey($sKey);
  255. // confirm email
  256. if (!$oAccount->updateEmailConfirmed(true))
  257. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  258. // login to user's account automatically
  259. bx_login($aData['account_id']);
  260. // redirect with success message
  261. $sUrl = getParam('sys_redirect_after_email_confirmation');
  262. $oTemplate = BxDolTemplate::getInstance();
  263. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  264. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  265. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  266. $oTemplate->setPageContent ('url_relocate', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl));
  267. BxDolTemplate::getInstance()->getPageCode();
  268. exit;
  269. }
  270. /**
  271. * @page service Service Calls
  272. * @section bx_system_general System Services
  273. * @subsection bx_system_general-account Account
  274. * @subsubsection bx_system_general-forgot_password forgot_password
  275. *
  276. * @code bx_srv('system', 'forgot_password', [], 'TemplServiceAccount'); @endcode
  277. * @code {{~system:forgot_password:TemplServiceAccount~}} @endcode
  278. *
  279. * Display forgot password form,
  280. * if reset password key is provided then reset password
  281. *
  282. * @see BxBaseServiceAccount::serviceForgotPassword
  283. */
  284. /**
  285. * @ref bx_system_general-forgot_password "forgot_password"
  286. */
  287. public function serviceForgotPassword()
  288. {
  289. if(isLogged()){
  290. header('Location: ' . BX_DOL_URL_ROOT);
  291. exit;
  292. }
  293. if (bx_get('key'))
  294. return $this->resetPassword();
  295. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  296. if (!$oForm)
  297. return '';
  298. $bNeedCheckEmailAndPhone = true;
  299. $sCaptionKey = "_sys_txt_forgot_pasword_by_both_desc";
  300. if (BxDolAccount::isNeedConfirmPhone()){
  301. if (!BxDolAccount::isNeedConfirmEmail()){
  302. unset($oForm->aInputs['email']);
  303. $sCaptionKey = "_sys_txt_forgot_pasword_by_phone_desc";
  304. $oForm->aInputs['phone']['checker']['func'] = 'PhoneExist';
  305. $bNeedCheckEmailAndPhone = false;
  306. }
  307. }
  308. else{
  309. unset($oForm->aInputs['phone']);
  310. $sCaptionKey = "_sys_txt_forgot_pasword_desc";
  311. $oForm->aInputs['email']['checker']['func'] = 'EmailExist';
  312. $bNeedCheckEmailAndPhone = false;
  313. }
  314. if ((BxDolAccount::isNeedConfirmPhone() && BxDolAccount::isNeedConfirmEmail())){
  315. if (isset($oForm->aInputs['phone']))
  316. $oForm->aInputs['phone']['required'] = false;
  317. if (isset($oForm->aInputs['email']))
  318. $oForm->aInputs['email']['required'] = false;
  319. }
  320. $oForm->initChecker();
  321. $bShowErrorEmptyBoth = false;
  322. if ($oForm->isSubmitted() && $bNeedCheckEmailAndPhone && $oForm->getCleanValue('phone') == '' && $oForm->getCleanValue('email') == ''){
  323. $bShowErrorEmptyBoth = true;
  324. $oForm->setValid(false);
  325. }
  326. if ($oForm->isSubmittedAndValid()) {
  327. $oKey = BxDolKey::getInstance();
  328. if (!$oKey) {
  329. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  330. }
  331. else {
  332. $sEmail = $oForm->getCleanValue('email');
  333. $sPhone = $oForm->getCleanValue('phone');
  334. if (isset($oForm->aInputs['email']) && $sEmail != '')
  335. {
  336. $iAccountId = $this->_oAccountQuery->getIdByEmail($sEmail);
  337. $aPlus['key'] = $oKey->getNewKey(array('email' => $sEmail));
  338. $aPlus['forgot_password_url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password', array('key' => $aPlus['key']));
  339. $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_Forgot', $aPlus, $iAccountId);
  340. if ($aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  341. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  342. else
  343. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  344. $sForm = '';
  345. }
  346. if (isset($oForm->aInputs['phone']) && $sPhone != ''){
  347. $iAccountId = $this->_oAccountQuery->getIdByPhone($sPhone);
  348. $aAccountInfo = $this->_oAccountQuery->getInfoById($iAccountId);
  349. $aPlus['key'] = $oKey->getNewKey(array('email' => $aAccountInfo['email']));
  350. $aPlus['forgot_password_url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password', array('key' => $aPlus['key']));
  351. $sSmsText = _t('_sys_txt_forgot_pasword_sms_text', $aPlus['forgot_password_url']);
  352. $ret = null;
  353. bx_alert('account', 'before_forgot_password_send_sms', $aAccountInfo['id'], false, array('phone_number' => $sPhone, 'sms_text' => $sSmsText, 'override_result' => &$ret));
  354. if ($ret === null)
  355. {
  356. $oTwilio = BxDolTwilio::getInstance();
  357. if($oTwilio->sendSms($sPhone, $sSmsText))
  358. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_phone"));
  359. else
  360. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  361. }
  362. $sForm = '';
  363. }
  364. }
  365. }
  366. else {
  367. $sResultMsg = _t($sCaptionKey);
  368. $sForm = $oForm->getCode();
  369. }
  370. 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>' : '');
  371. }
  372. /**
  373. * @page service Service Calls
  374. * @section bx_system_general System Services
  375. * @subsection bx_system_general-account Account
  376. * @subsubsection bx_system_general-switch_profile switch_profile
  377. *
  378. * @code bx_srv('system', 'switch_profile', [123], 'TemplServiceAccount'); @endcode
  379. * @code {{~system:switch_profile:TemplServiceAccount:[123]~}} @endcode
  380. *
  381. * Switch current user profile.
  382. * User can switch between profiles if multiple profiles are created.
  383. * Also it's possible to switch to Organization profile if user is admin of Organization,
  384. * or other module which can `actAsProfile`.
  385. * Admin can switch to any prifile by default
  386. *
  387. * @param $iSwitchToProfileId profile ID to switch to
  388. * @return true on success or error message otherwise, so make sue to make strict (===) comparision
  389. *
  390. * @see BxBaseServiceAccount::serviceSwitchProfile
  391. */
  392. /**
  393. * @ref bx_system_general-switch_profile "switch_profile"
  394. */
  395. public function serviceSwitchProfile($iSwitchToProfileId)
  396. {
  397. $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
  398. if (!$oProfile) {
  399. return _t('_sys_txt_error_occured');
  400. }
  401. $iViewerAccountId = getLoggedId();
  402. $iSwitchToAccountId = $oProfile->getAccountId();
  403. $iViewerProfileId = bx_get_logged_profile_id();
  404. $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
  405. $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  406. $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
  407. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  408. if (!$bCanSwitch) {
  409. return isset($aCheck[CHECK_ACTION_MESSAGE]) ? $aCheck[CHECK_ACTION_MESSAGE] : _t('_sys_txt_error_occured');
  410. }
  411. $oAccount = BxDolAccount::getInstance();
  412. if (!$oAccount->updateProfileContext($iSwitchToProfileId)) {
  413. return _t('_sys_txt_error_occured');
  414. }
  415. checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
  416. return true;
  417. }
  418. /**
  419. * Reset password procedure
  420. */
  421. public function resetPassword()
  422. {
  423. $sKey = bx_process_input(bx_get('key'));
  424. // get link to forgot password page for error message
  425. $sUrlForgotPassword = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password');
  426. // check if key exists
  427. $oKey = BxDolKey::getInstance();
  428. if (!$oKey || !$oKey->isKeyExists($sKey))
  429. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  430. // check if key data exists
  431. $aData = $oKey->getKeyData($sKey);
  432. if (!isset($aData['email']))
  433. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  434. // check if account with such email exists
  435. $iAccountId = $this->_oAccountQuery->getIdByEmail($aData['email']);
  436. if (!$iAccountId)
  437. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  438. // generate new password
  439. $sPassword = $this->generateUserNewPwd($iAccountId);
  440. // remove key
  441. $oKey->removeKey($sKey);
  442. // send email with new password and display result message
  443. $aPlus = array ('password' => $sPassword);
  444. $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_PasswordReset', $aPlus, $iAccountId);
  445. $oAccountQuery = BxDolAccountQuery::getInstance();
  446. $oAccountQuery->unlockAccount($iAccountId);
  447. if ($aTemplate && sendMail($aData['email'], $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  448. return _t("_sys_txt_reset_pasword_email_sent", $sPassword);
  449. else
  450. return _t("_sys_txt_reset_pasword_email_send_failed", $sPassword);
  451. }
  452. /**
  453. * Generate new password.
  454. */
  455. public function generateUserNewPwd($iAccountId)
  456. {
  457. $sPwd = genRndPwd(8, false);
  458. $sSalt = genRndSalt();
  459. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  460. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
  461. bx_alert('account', 'edited', $iAccountId, $iAccountId, array('action' => 'forgot_password'));
  462. return $sPwd;
  463. }
  464. }
  465. /** @} */