BxBaseServiceAccount.php 11 KB

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