BxBaseServiceAccount.php 11 KB

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