BxBaseServiceAccount.php 11 KB

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