1
0

BxBaseServiceAccount.php 11 KB

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