BxBaseServiceAccount.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. public function serviceCreateAccountForm ($aParams = array())
  23. {
  24. if (isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) { // remember referrer
  25. $sJoinReferrer = $_SERVER['HTTP_REFERER'];
  26. $aNoRelocatePages = array('forgot-password', 'login', 'create-account', 'logout');
  27. foreach ($aNoRelocatePages as $s) {
  28. if (false !== mb_stripos($sJoinReferrer, $s)) {
  29. $sJoinReferrer = '';
  30. break;
  31. }
  32. }
  33. if ($sJoinReferrer)
  34. BxDolSession::getInstance()->setValue('join-referrer', $sJoinReferrer);
  35. }
  36. $sLoginText = '';
  37. if (!isset($aParams['no_login_text']) || false === (bool)$aParams['no_login_text'])
  38. $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>';
  39. $sAuth = '';
  40. if (!isset($aParams['no_auth_buttons']) || false === (bool)$aParams['no_auth_buttons'])
  41. $sAuth = BxDolService::call('system', 'member_auth_code', array(), 'TemplServiceLogin');
  42. return $sAuth . $this->_oAccountForms->createAccountForm($aParams) . $sLoginText;
  43. }
  44. public function serviceAccountSettingsEmail ($iAccountId = false)
  45. {
  46. if (false === $iAccountId)
  47. $iAccountId = getLoggedId();
  48. return $this->_oAccountForms->editAccountEmailSettingsForm($iAccountId);
  49. }
  50. public function serviceAccountSettingsPassword ($iAccountId = false)
  51. {
  52. if (false === $iAccountId)
  53. $iAccountId = getLoggedId();
  54. return $this->_oAccountForms->editAccountPasswordSettingsForm($iAccountId);
  55. }
  56. public function serviceAccountSettingsInfo ($iAccountId = false)
  57. {
  58. if (false === $iAccountId)
  59. $iAccountId = getLoggedId();
  60. return $this->_oAccountForms->editAccountInfoForm($iAccountId);
  61. }
  62. public function serviceAccountSettingsDelAccount ($iAccountId = false)
  63. {
  64. if (!$iAccountId)
  65. $iAccountId = bx_process_input(bx_get('id'), BX_DATA_INT);
  66. if (!$iAccountId)
  67. $iAccountId = getLoggedId();
  68. return $this->_oAccountForms->deleteAccountForm($iAccountId);
  69. }
  70. /**
  71. * Display unsubscribe from newsletters form
  72. */
  73. public function serviceUnsubscribeNews()
  74. {
  75. return $this->_unsubscribeForm('sys_unsubscribe_news');
  76. }
  77. /**
  78. * Display unsubscribe from notifications form
  79. */
  80. public function serviceUnsubscribeNotifications()
  81. {
  82. return $this->_unsubscribeForm('sys_unsubscribe_updates');
  83. }
  84. protected function _unsubscribeForm($sDisplay)
  85. {
  86. $iAccountId = bx_process_input(bx_get('id'));
  87. $sCode = bx_process_input(bx_get('code'));
  88. // validate input params
  89. $oAccount = BxDolAccount::getInstance($iAccountId);
  90. if (!$oAccount)
  91. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  92. if ($sCode != $oAccount->getEmailHash())
  93. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  94. $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
  95. if (!$oForm)
  96. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  97. // show form
  98. $aAccountInfo = $oAccount->getInfo();
  99. $aAccountInfo['code'] = $sCode;
  100. $oForm->initChecker($aAccountInfo);
  101. if ($oForm->isSubmittedAndValid()) {
  102. if (!$oForm->update($oAccount->id()))
  103. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  104. return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
  105. }
  106. return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
  107. }
  108. /**
  109. * Display email confirmation form, if confirmation code is provided then try to confirm profile
  110. */
  111. public function serviceEmailConfirmation($sMsg = false)
  112. {
  113. // if user is logged in and email is confirmed then just display a message
  114. if (isLogged()) {
  115. $oAccount = BxDolAccount::getInstance();
  116. if ($oAccount->isConfirmed())
  117. return MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
  118. }
  119. // if confirmation code is provided in the URL - perform email confirmation right away
  120. if (!empty($_GET['code']))
  121. return $this->confirmEmail(bx_process_input($_GET['code']));
  122. // if user requested to resend verification letter then send letter and display message
  123. if (bx_process_input(bx_get('resend')) && isLogged()) {
  124. $oAccount = BxDolAccount::getInstance();
  125. if ($oAccount->sendConfirmationEmail())
  126. $sMsg = _t('_sys_txt_confirm_email_sent');
  127. else
  128. $sMsg = _t('_sys_txt_confirm_email_sent_failed');
  129. return MsgBox($sMsg);
  130. }
  131. // show and process code verification form
  132. $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
  133. if (!$oForm)
  134. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  135. $oForm->initChecker();
  136. if ($oForm->isSubmittedAndValid()) {
  137. $oKey = BxDolKey::getInstance();
  138. if (!$oKey)
  139. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  140. else
  141. return $this->confirmEmail(trim($oForm->getCleanValue('code')));
  142. }
  143. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
  144. }
  145. /**
  146. * Perform email confirmation
  147. */
  148. public function confirmEmail($sKey)
  149. {
  150. // check if key exists
  151. $oKey = BxDolKey::getInstance();
  152. if (!$oKey || !$oKey->isKeyExists($sKey))
  153. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  154. // check if key data exists
  155. $aData = $oKey->getKeyData($sKey);
  156. if (!isset($aData['account_id']))
  157. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  158. // check if account exists
  159. $oAccount = BxDolAccount::getInstance($aData['account_id']);
  160. if (!$oAccount)
  161. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  162. // remove key
  163. $oKey->removeKey($sKey);
  164. // confirm email
  165. if (!$oAccount->updateEmailConfirmed(true))
  166. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  167. // login to user's account automatically
  168. bx_login($aData['account_id']);
  169. // redirect with success message
  170. $sUrl = getParam('sys_redirect_after_email_confirmation');
  171. if(empty($sUrl))
  172. $sUrl = 'page.php?i=account-settings-info';
  173. $oTemplate = BxDolTemplate::getInstance();
  174. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  175. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  176. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  177. $oTemplate->setPageContent ('url_relocate', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl));
  178. BxDolTemplate::getInstance()->getPageCode();
  179. exit;
  180. }
  181. /**
  182. * Display forgot password form, if reset password key is provided then reset password
  183. */
  184. public function serviceForgotPassword()
  185. {
  186. if (bx_get('key'))
  187. return $this->resetPassword();
  188. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  189. if (!$oForm)
  190. return '';
  191. $oForm->initChecker();
  192. if ( $oForm->isSubmittedAndValid() ) {
  193. $oKey = BxDolKey::getInstance();
  194. if (!$oKey) {
  195. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  196. } else {
  197. $sEmail = $oForm->getCleanValue('email');
  198. $iAccountId = $this->_oAccountQuery->getIdByEmail($sEmail);
  199. $aPlus['key'] = $oKey->getNewKey(array('email' => $sEmail));
  200. $aPlus['forgot_password_url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password') . '&key=' . $aPlus['key'];
  201. $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_Forgot', $aPlus, $iAccountId);
  202. if ($aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  203. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  204. else
  205. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  206. $sForm = '';
  207. }
  208. } else {
  209. $sResultMsg = _t("_sys_txt_forgot_pasword_desc");
  210. $sForm = $oForm->getCode();
  211. }
  212. return '<div class="bx-def-padding-sec-bottom">' . $sResultMsg . '</div>' . $sForm;
  213. }
  214. /**
  215. * Reset password procedure
  216. */
  217. public function resetPassword()
  218. {
  219. $sKey = bx_process_input(bx_get('key'));
  220. // get link to forgot password page for error message
  221. $sUrlForgotPassword = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password');
  222. // check if key exists
  223. $oKey = BxDolKey::getInstance();
  224. if (!$oKey || !$oKey->isKeyExists($sKey))
  225. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  226. // check if key data exists
  227. $aData = $oKey->getKeyData($sKey);
  228. if (!isset($aData['email']))
  229. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  230. // check if account with such email exists
  231. $iAccountId = $this->_oAccountQuery->getIdByEmail($aData['email']);
  232. if (!$iAccountId)
  233. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  234. // generate new password
  235. $sPassword = $this->generateUserNewPwd($iAccountId);
  236. // remove key
  237. $oKey->removeKey($sKey);
  238. // send email with new password and display result message
  239. $aPlus = array ('password' => $sPassword);
  240. $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_PasswordReset', $aPlus, $iAccountId);
  241. if ($aTemplate && sendMail($aData['email'], $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  242. return _t("_sys_txt_reset_pasword_email_sent", $sPassword);
  243. else
  244. return _t("_sys_txt_reset_pasword_email_send_failed", $sPassword);
  245. }
  246. /**
  247. * Generate new password.
  248. */
  249. protected function generateUserNewPwd($iAccountId)
  250. {
  251. $sPwd = genRndPwd();
  252. $sSalt = genRndSalt();
  253. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  254. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
  255. bx_alert('account', 'edited', $iAccountId, $iAccountId, array('action' => 'forgot_password'));
  256. return $sPwd;
  257. }
  258. }
  259. /** @} */