1
0

BxBaseServiceAccount.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  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. if(isLogged()){
  25. header('Location: ' . BX_DOL_URL_ROOT);
  26. exit;
  27. }
  28. if (isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) { // remember referrer
  29. $sJoinReferrer = $_SERVER['HTTP_REFERER'];
  30. $aNoRelocatePages = array('forgot-password', 'login', 'create-account', 'logout');
  31. foreach ($aNoRelocatePages as $s) {
  32. if (false !== mb_stripos($sJoinReferrer, $s)) {
  33. $sJoinReferrer = '';
  34. break;
  35. }
  36. }
  37. if ($sJoinReferrer)
  38. BxDolSession::getInstance()->setValue('join-referrer', $sJoinReferrer);
  39. }
  40. $sLoginText = '';
  41. if (!isset($aParams['no_login_text']) || false === (bool)$aParams['no_login_text'])
  42. $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>';
  43. $sAuth = '';
  44. if (!isset($aParams['no_auth_buttons']) || false === (bool)$aParams['no_auth_buttons'])
  45. $sAuth = BxDolService::call('system', 'member_auth_code', array(), 'TemplServiceLogin');
  46. return $sAuth . $this->_oAccountForms->createAccountForm($aParams) . $sLoginText;
  47. }
  48. public function serviceAccountSettingsEmail ($iAccountId = false)
  49. {
  50. if (false === $iAccountId)
  51. $iAccountId = getLoggedId();
  52. return $this->_oAccountForms->editAccountEmailSettingsForm($iAccountId);
  53. }
  54. public function serviceAccountSettingsPassword ($iAccountId = false)
  55. {
  56. if (false === $iAccountId)
  57. $iAccountId = getLoggedId();
  58. return $this->_oAccountForms->editAccountPasswordSettingsForm($iAccountId);
  59. }
  60. public function serviceAccountSettingsInfo ($iAccountId = false)
  61. {
  62. if (false === $iAccountId)
  63. $iAccountId = getLoggedId();
  64. return $this->_oAccountForms->editAccountInfoForm($iAccountId);
  65. }
  66. public function serviceAccountSettingsDelAccount ($iAccountId = false)
  67. {
  68. if (!$iAccountId)
  69. $iAccountId = bx_process_input(bx_get('id'), BX_DATA_INT);
  70. if (!$iAccountId)
  71. $iAccountId = getLoggedId();
  72. return $this->_oAccountForms->deleteAccountForm($iAccountId);
  73. }
  74. /**
  75. * Display unsubscribe from newsletters form
  76. */
  77. public function serviceUnsubscribeNews()
  78. {
  79. return $this->_unsubscribeForm('sys_unsubscribe_news');
  80. }
  81. /**
  82. * Display unsubscribe from notifications form
  83. */
  84. public function serviceUnsubscribeNotifications()
  85. {
  86. return $this->_unsubscribeForm('sys_unsubscribe_updates');
  87. }
  88. /**
  89. * Display confirmation statuses list
  90. */
  91. public function serviceGetConfirmationTypes()
  92. {
  93. $aResult = array();
  94. $aChoices = array(BX_ACCOUNT_CONFIRMATION_NONE, BX_ACCOUNT_CONFIRMATION_EMAIL, BX_ACCOUNT_CONFIRMATION_PHONE, BX_ACCOUNT_CONFIRMATION_EMAIL_PHONE);
  95. foreach($aChoices as $sChoice)
  96. $aResult[$sChoice] = _t('_sys_acc_confirmation_type_' . $sChoice);
  97. return $aResult;
  98. }
  99. protected function _unsubscribeForm($sDisplay)
  100. {
  101. $iAccountId = bx_process_input(bx_get('id'));
  102. $sCode = bx_process_input(bx_get('code'));
  103. // validate input params
  104. $oAccount = BxDolAccount::getInstance($iAccountId);
  105. if (!$oAccount)
  106. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  107. if ($sCode != $oAccount->getEmailHash())
  108. return MsgBox(_t('_sys_txt_unsubscribe_wrong_link'));
  109. $oForm = BxDolForm::getObjectInstance('sys_unsubscribe', $sDisplay);
  110. if (!$oForm)
  111. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  112. // show form
  113. $aAccountInfo = $oAccount->getInfo();
  114. $aAccountInfo['code'] = $sCode;
  115. $oForm->initChecker($aAccountInfo);
  116. if ($oForm->isSubmittedAndValid()) {
  117. if (!$oForm->update($oAccount->id()))
  118. return MsgBox(_t('_sys_txt_unsubscribe_error_occured'));
  119. return MsgBox(_t('_sys_txt_unsubscribe_email_settings_update_success'));
  120. }
  121. return '<div class="bx-def-padding-bottom">' . _t("_sys_txt_unsubscribe_info", $aAccountInfo['email']) . '</div>' . $oForm->getCode();
  122. }
  123. /**
  124. * Display email confirmation form, if confirmation code is provided then try to confirm profile
  125. */
  126. public function serviceEmailConfirmation($sMsg = false)
  127. {
  128. // if user is logged in and email is confirmed then just display a message
  129. if (isLogged()) {
  130. $oAccount = BxDolAccount::getInstance();
  131. if ($oAccount->isConfirmedEmail())
  132. return MsgBox(_t("_sys_txt_confirm_email_already_confirmed"));
  133. }
  134. // if confirmation code is provided in the URL - perform email confirmation right away
  135. if (bx_get('code') !== false)
  136. return $this->confirmEmail(bx_process_input(bx_get('code')));
  137. // if user requested to resend verification letter then send letter and display message
  138. if (bx_process_input(bx_get('resend')) && isLogged()) {
  139. $oAccount = BxDolAccount::getInstance();
  140. if ($oAccount->sendConfirmationEmail())
  141. $sMsg = _t('_sys_txt_confirm_email_sent');
  142. else
  143. $sMsg = _t('_sys_txt_confirm_email_sent_failed');
  144. return MsgBox($sMsg);
  145. }
  146. // show and process code verification form
  147. $oForm = BxDolForm::getObjectInstance('sys_confirm_email', 'sys_confirm_email');
  148. if (!$oForm)
  149. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  150. $oForm->initChecker();
  151. if ($oForm->isSubmittedAndValid()) {
  152. $oKey = BxDolKey::getInstance();
  153. if (!$oKey)
  154. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  155. else
  156. return $this->confirmEmail(trim($oForm->getCleanValue('code')));
  157. }
  158. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_email_desc") . '</div>' . $oForm->getCode();
  159. }
  160. /**
  161. * Display phone confirmation forms, if confirmation code is provided then try to confirm profile
  162. */
  163. public function servicePhoneConfirmation($sMsg = false)
  164. {
  165. // if user is logged in and phone is confirmed then just display a message
  166. if (isLogged()) {
  167. $oAccount = BxDolAccount::getInstance();
  168. if ($oAccount->isConfirmedPhone())
  169. return MsgBox(_t("_sys_txt_confirm_phone_already_confirmed"));
  170. $iStep = 1;
  171. $a = $oAccount->getInfo();
  172. $sPhoneNumber = $a['phone'];
  173. $oSession = BxDolSession::getInstance();
  174. if ($sPhoneNumber != "" && $oSession->isValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE)){
  175. $iStep = 2;
  176. }
  177. if (bx_get('step'))
  178. $iStep = (int)bx_get('step');
  179. // show and process phone set form
  180. if ($iStep == 1){
  181. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_set_phone');
  182. if (!$oForm)
  183. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_error_occured"));
  184. $oForm->initChecker(array('phone' => $sPhoneNumber));
  185. if ($oForm->isSubmittedAndValid()) {
  186. $oAccount->updatePhone(trim($oForm->getCleanValue('phone')));
  187. $sActivationCode = rand(1000, 9999);
  188. $sActivationText =_t('_sys_txt_confirm_phone_sms_text', $sActivationCode);
  189. $ret = null;
  190. bx_alert('account', 'before_confirm_phone_send_sms', $oAccount->id(), bx_get_logged_profile_id(), array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  191. if ($ret === null)
  192. {
  193. $oTwilio = BxDolTwilio::getInstance();
  194. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  195. return MsgBox(_t("_sys_txt_confirm_phone_send_sms_error_occured"));
  196. }
  197. }
  198. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  199. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=confirm-phone'));
  200. }
  201. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_set_phone") . '</div>' .$oForm->getCode();
  202. }
  203. if ($iStep == 2){
  204. $oForm = BxDolForm::getObjectInstance('sys_confirm_phone', 'sys_confirm_phone_confirmation');
  205. if (!$oForm)
  206. return MsgBox(_t("_sys_txt_confirm_phone_set_phone_confirmation_occured"));
  207. $oForm->initChecker();
  208. if ($oForm->isSubmittedAndValid()) {
  209. $oAccount->updatePhoneConfirmed(true);
  210. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  211. return MsgBox(_t("_sys_txt_confirm_phone_confirm_success"));
  212. }
  213. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_confirm_phone_set_phone_desc_phone_confirmation") . '</div>' .$oForm->getCode();
  214. }
  215. }
  216. }
  217. /**
  218. * Perform email confirmation
  219. */
  220. public function confirmEmail($sKey)
  221. {
  222. // check if key exists
  223. $oKey = BxDolKey::getInstance();
  224. if (!$oKey || !$oKey->isKeyExists($sKey))
  225. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  226. // check if key data exists
  227. $aData = $oKey->getKeyData($sKey);
  228. if (!isset($aData['account_id']))
  229. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  230. // check if account exists
  231. $oAccount = BxDolAccount::getInstance($aData['account_id']);
  232. if (!$oAccount)
  233. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  234. // remove key
  235. $oKey->removeKey($sKey);
  236. // confirm email
  237. if (!$oAccount->updateEmailConfirmed(true))
  238. return MsgBox(_t("_sys_txt_confirm_email_error_occured"));
  239. // login to user's account automatically
  240. bx_login($aData['account_id']);
  241. // redirect with success message
  242. $sUrl = getParam('sys_redirect_after_email_confirmation');
  243. $oTemplate = BxDolTemplate::getInstance();
  244. $oTemplate->setPageNameIndex (BX_PAGE_TRANSITION);
  245. $oTemplate->setPageHeader (_t('_sys_txt_confirm_email_success'));
  246. $oTemplate->setPageContent ('page_main_code', MsgBox(_t('_sys_txt_confirm_email_success')));
  247. $oTemplate->setPageContent ('url_relocate', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($sUrl));
  248. BxDolTemplate::getInstance()->getPageCode();
  249. exit;
  250. }
  251. /**
  252. * Display forgot password form, if reset password key is provided then reset password
  253. */
  254. public function serviceForgotPassword()
  255. {
  256. if(isLogged()){
  257. header('Location: ' . BX_DOL_URL_ROOT);
  258. exit;
  259. }
  260. if (bx_get('key'))
  261. return $this->resetPassword();
  262. $oForm = BxDolForm::getObjectInstance('sys_forgot_password', 'sys_forgot_password');
  263. if (!$oForm)
  264. return '';
  265. $bNeedCheckEmailAndPhone = true;
  266. $sCaptionKey = "_sys_txt_forgot_pasword_by_both_desc";
  267. if (BxDolAccount::isNeedConfirmPhone()){
  268. if (!BxDolAccount::isNeedConfirmEmail()){
  269. unset($oForm->aInputs['email']);
  270. $sCaptionKey = "_sys_txt_forgot_pasword_by_phone_desc";
  271. $oForm->aInputs['phone']['checker']['func'] = 'PhoneExist';
  272. $bNeedCheckEmailAndPhone = false;
  273. }
  274. }
  275. else{
  276. unset($oForm->aInputs['phone']);
  277. $sCaptionKey = "_sys_txt_forgot_pasword_desc";
  278. $oForm->aInputs['email']['checker']['func'] = 'EmailExist';
  279. $bNeedCheckEmailAndPhone = false;
  280. }
  281. if ((BxDolAccount::isNeedConfirmPhone() && BxDolAccount::isNeedConfirmEmail())){
  282. if (isset($oForm->aInputs['phone']))
  283. $oForm->aInputs['phone']['required'] = false;
  284. if (isset($oForm->aInputs['email']))
  285. $oForm->aInputs['email']['required'] = false;
  286. }
  287. $oForm->initChecker();
  288. $bShowErrorEmptyBoth = false;
  289. if ($oForm->isSubmitted() && $bNeedCheckEmailAndPhone && $oForm->getCleanValue('phone') == '' && $oForm->getCleanValue('email') == ''){
  290. $bShowErrorEmptyBoth = true;
  291. $oForm->setValid(false);
  292. }
  293. if ($oForm->isSubmittedAndValid()) {
  294. $oKey = BxDolKey::getInstance();
  295. if (!$oKey) {
  296. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  297. }
  298. else {
  299. $sEmail = $oForm->getCleanValue('email');
  300. $sPhone = $oForm->getCleanValue('phone');
  301. if (isset($oForm->aInputs['email']) && $sEmail != '')
  302. {
  303. $iAccountId = $this->_oAccountQuery->getIdByEmail($sEmail);
  304. $aPlus['key'] = $oKey->getNewKey(array('email' => $sEmail));
  305. $aPlus['forgot_password_url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password', array('key' => $aPlus['key']));
  306. $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_Forgot', $aPlus, $iAccountId);
  307. if ($aTemplate && sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  308. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_email"));
  309. else
  310. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_email_send_failed"));
  311. $sForm = '';
  312. }
  313. if (isset($oForm->aInputs['phone']) && $sPhone != ''){
  314. $iAccountId = $this->_oAccountQuery->getIdByPhone($sPhone);
  315. $aAccountInfo = $this->_oAccountQuery->getInfoById($iAccountId);
  316. $aPlus['key'] = $oKey->getNewKey(array('email' => $aAccountInfo['email']));
  317. $aPlus['forgot_password_url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password', array('key' => $aPlus['key']));
  318. $sSmsText = _t('_sys_txt_forgot_pasword_sms_text', $aPlus['forgot_password_url']);
  319. $ret = null;
  320. bx_alert('account', 'before_forgot_password_send_sms', $aAccountInfo['id'], false, array('phone_number' => $sPhone, 'sms_text' => $sSmsText, 'override_result' => &$ret));
  321. if ($ret === null)
  322. {
  323. $oTwilio = BxDolTwilio::getInstance();
  324. if($oTwilio->sendSms($sPhone, $sSmsText))
  325. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_check_phone"));
  326. else
  327. $sResultMsg = MsgBox(_t("_sys_txt_forgot_pasword_error_occured"));
  328. }
  329. $sForm = '';
  330. }
  331. }
  332. }
  333. else {
  334. $sResultMsg = _t($sCaptionKey);
  335. $sForm = $oForm->getCode();
  336. }
  337. return '<div class="bx-def-padding-sec-bottom">' . $sResultMsg . '</div>' . $sForm . ($bShowErrorEmptyBoth ? '<div class="bx-form-warn">' . _t("_sys_form_forgot_password_phone_and_email_empty_error") . '</div>' : '');
  338. }
  339. /**
  340. * Reset password procedure
  341. */
  342. public function resetPassword()
  343. {
  344. $sKey = bx_process_input(bx_get('key'));
  345. // get link to forgot password page for error message
  346. $sUrlForgotPassword = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password');
  347. // check if key exists
  348. $oKey = BxDolKey::getInstance();
  349. if (!$oKey || !$oKey->isKeyExists($sKey))
  350. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  351. // check if key data exists
  352. $aData = $oKey->getKeyData($sKey);
  353. if (!isset($aData['email']))
  354. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  355. // check if account with such email exists
  356. $iAccountId = $this->_oAccountQuery->getIdByEmail($aData['email']);
  357. if (!$iAccountId)
  358. return _t("_sys_txt_reset_pasword_error_occured", $sUrlForgotPassword);
  359. // generate new password
  360. $sPassword = $this->generateUserNewPwd($iAccountId);
  361. // remove key
  362. $oKey->removeKey($sKey);
  363. // send email with new password and display result message
  364. $aPlus = array ('password' => $sPassword);
  365. $aTemplate = BxDolEmailTemplates::getInstance() -> parseTemplate('t_PasswordReset', $aPlus, $iAccountId);
  366. $oAccountQuery = BxDolAccountQuery::getInstance();
  367. $oAccountQuery->unlockAccount($iAccountId);
  368. if ($aTemplate && sendMail($aData['email'], $aTemplate['Subject'], $aTemplate['Body'], 0, $aPlus, BX_EMAIL_SYSTEM))
  369. return _t("_sys_txt_reset_pasword_email_sent", $sPassword);
  370. else
  371. return _t("_sys_txt_reset_pasword_email_send_failed", $sPassword);
  372. }
  373. /**
  374. * Generate new password.
  375. */
  376. protected function generateUserNewPwd($iAccountId)
  377. {
  378. $sPwd = genRndPwd(8, false);
  379. $sSalt = genRndSalt();
  380. $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
  381. $this->_oAccountQuery->updatePassword($sPasswordHash, $sSalt, $iAccountId);
  382. bx_alert('account', 'edited', $iAccountId, $iAccountId, array('action' => 'forgot_password'));
  383. return $sPwd;
  384. }
  385. }
  386. /** @} */