BxBaseServiceAccount.php 11 KB

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