BxBaseServiceAccount.php 11 KB

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