1
0

BxBaseServiceAccount.php 10 KB

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