BxBaseServiceAccount.php 19 KB

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