| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- /**
- * System service for creating system profile functionality.
- */
- class BxBaseServiceAccount extends BxDol
- {
- protected $_oAccountForms;
- protected $_oAccountQuery;
- public function __construct()
- {
- parent::__construct();
- $this->_oAccountForms = new BxTemplAccountForms();
- $this->_oAccountQuery = BxDolAccountQuery::getInstance();
- }
- public function serviceCreateAccountForm ($aParams = array())
- {
- if (isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) { // remember referrer
-
- $sJoinReferrer = $_SERVER['HTTP_REFERER'];
- $aNoRelocatePages = array('forgot-password', 'login', 'create-account', 'logout');
- foreach ($aNoRelocatePages as $s) {
- if (false !== mb_stripos($sJoinReferrer, $s)) {
- $sJoinReferrer = '';
- break;
- }
- }
- if ($sJoinReferrer)
- BxDolSession::getInstance()->setValue('join-referrer', $sJoinReferrer);
- }
- $sLoginText = '';
- if (!isset($aParams['no_login_text']) || false === (bool)$aParams['no_login_text'])
- $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>';
-
- $sAuth = '';
- if (!isset($aParams['no_auth_buttons']) || false === (bool)$aParams['no_auth_buttons'])
- $sAuth = BxDolService::call('system', 'member_auth_code', array(), 'TemplServiceLogin');
- return $sAuth . $this->_oAccountForms->createAccountForm($aParams) . $sLoginText;
- }
- public function serviceAccountSettingsEmail ($iAccountId = false)
- {
- if (false === $iAccountId)
- $iAccountId = getLoggedId();
- return $this->_oAccountForms->editAccountEmailSettingsForm($iAccountId);
- }
- public function serviceAccountSettingsPassword ($iAccountId = false)
- {
- if (false === $iAccountId)
- $iAccountId = getLoggedId();
- return $this->_oAccountForms->editAccountPasswordSettingsForm($iAccountId);
- }
- public function serviceAccountSettingsInfo ($iAccountId = false)
- {
- if (false === $iAccountId)
- $iAccountId = getLoggedId();
- return $this->_oAccountForms->editAccountInfoForm($iAccountId);
- }
- public function serviceAccountSettingsDelAccount ($iAccountId = false)
- {
- if (!$iAccountId)
- $iAccountId = bx_process_input(bx_get('id'), BX_DATA_INT);
- if (!$iAccountId)
- $iAccountId = getLoggedId();
- return $this->_oAccountForms->deleteAccountForm($iAccountId);
- }
- /**
- * Display unsubscribe from newsletters form
- */
- public function serviceUnsubscribeNews()
- {
- return $this->_unsubscribeForm('sys_unsubscribe_news');
- }
- /**
- * Display unsubscribe from notifications form
- */
- public function serviceUnsubscribeNotifications()
- {
- return $this->_unsubscribeForm('sys_unsubscribe_updates');
- }
- protected function _unsubscribeForm($sDisplay)
- {
- $iAccountId = bx_process_input(bx_get('id'));
- $sCode = bx_process_input(bx_get('code'));
- // validate input params
- $oAccount = BxDolAccount::getInstance($iAccountId);
- if (!$oAccount)
- return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
- if ($sCode != $oAccount->getEmailHash())
- return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
- $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
- if (!$oForm)
- return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
- // show form
- $aAccountInfo = $oAccount->getInfo();
- $aAccountInfo['code'] = $sCode;
- $oForm->initChecker($aAccountInfo);
- if ($oForm->isSubmittedAndValid()) {
- if (!$oForm->update($oAccount->id()))
- return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
- return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
- }
- return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
- }
- /**
- * Display email confirmation form, if confirmation code is provided then try to confirm profile
- */
- public function serviceEmailConfirmation($sMsg = false)
- {
- // if user is logged in and email is confirmed then just display a message
- if (isLogged()) {
- $oAccount = BxDolAccount::getInstance();
- if ($oAccount->isConfirmed())
- return MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
- }
- // if confirmation code is provided in the URL - perform email confirmation right away
- if (!empty($_GET['code']))
- return $this->confirmEmail(bx_process_input($_GET['code']));
- // if user requested to resend verification letter then send letter and display message
- if (bx_process_input(bx_get('resend')) && isLogged()) {
- $oAccount = BxDolAccount::getInstance();
- if ($oAccount->sendConfirmationEmail())
- $sMsg = _t('_sys_txt_confirm_email_sent');
- else
- $sMsg = _t('_sys_txt_confirm_email_sent_failed');
- return MsgBox($sMsg);
- }
- // show and process code verification form
- $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
- if (!$oForm)
- return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
- $oForm->initChecker();
- if ($oForm->isSubmittedAndValid()) {
- $oKey = BxDolKey::getInstance();
- if (!$oKey)
- return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
- else
- return $this->confirmEmail(trim($oForm->getCleanValue('code')));
- }
- return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
- }
- /**
- * Perform email confirmation
- */
- public function confirmEmail($sKey)
- {
- // check if key exists
- $oKey = BxDolKey::getInstance();
- if (!$oKey || !$oKey->isKeyExists($sKey))
- return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
- // check if key data exists
- $aData = $oKey->getKeyData($sKey);
- if (!isset($aData['account_id']))
- return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
- // check if account exists
- $oAccount = BxDolAccount::getInstance($aData['account_id']);
- if (!$oAccount)
- return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
- // remove key
- $oKey->removeKey($sKey);
- // confirm email
- if (!$oAccount->updateEmailConfirmed(true))
- return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
- // login to user's account automatically
- bx_login($aData['account_id']);
- // redirect with success message
- $oTemplate = BxDolTemplate::getInstance();
- $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
- $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
- $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
- $oTemplate->setPageContent ('url_relocate', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=account-settings-info'));
- BxDolTemplate::getInstance()->getPageCode();
- exit;
- }
- /**
- * Display forgot password form, if reset password key is provided then reset password
- */
- public function serviceForgotPassword()
- {
- if (bx_get('key'))
- return $this->resetPassword();
- $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
- if (!$oForm)
- return '';
- $oForm->initChecker();
- if ( $oForm->isSubmittedAndValid() ) {
- $oKey = BxDolKey::getInstance();
- if (!$oKey) {
- $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
- } else {
- $sEmail = $oForm->getCleanValue('email');
- $iAccountId = $this->_oAccountQuery->getIdByEmail($sEmail);
- $aPlus['key'] = $oKey->getNewKey(array('email' => $sEmail));
- $aPlus['forgot_password_url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password') . '&key=' . $aPlus['key'];
- $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_Forgot', $aPlus, $iAccountId);
- if ($aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
- $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
- else
- $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
- $sForm = '';
- }
- } else {
- $sResultMsg = _t("_sys_txt_forgot_pasword_desc");
- $sForm = $oForm->getCode();
- }
- return '<div class="bx-def-padding-sec-bottom">' . $sResultMsg . '</div>' . $sForm;
- }
- /**
- * Reset password procedure
- */
- public function resetPassword()
- {
- $sKey = bx_process_input(bx_get('key'));
- // get link to forgot password page for error message
- $sUrlForgotPassword = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password');
- // check if key exists
- $oKey = BxDolKey::getInstance();
- if (!$oKey || !$oKey->isKeyExists($sKey))
- return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
- // check if key data exists
- $aData = $oKey->getKeyData($sKey);
- if (!isset($aData['email']))
- return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
- // check if account with such email exists
- $iAccountId = $this->_oAccountQuery->getIdByEmail($aData['email']);
- if (!$iAccountId)
- return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
- // generate new password
- $sPassword = $this->generateUserNewPwd($iAccountId);
- // remove key
- $oKey->removeKey($sKey);
- // send email with new password and display result message
- $aPlus = array ('password' => $sPassword);
- $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_PasswordReset', $aPlus, $iAccountId);
- if ($aTemplate && sendMail($aData['email'], $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
- return _t("_sys_txt_reset_pasword_email_sent", $sPassword);
- else
- return _t("_sys_txt_reset_pasword_email_send_failed", $sPassword);
- }
- /**
- * Generate new password.
- */
- protected function generateUserNewPwd($iAccountId)
- {
- $sPwd = genRndPwd();
- $sSalt = genRndSalt();
- $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
- $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
- bx_alert('account', 'edited', $iAccountId, $iAccountId, array('action' => 'forgot_password'));
- return $sPwd;
- }
- }
- /** @} */
|