BxBaseServiceAccount.php 19 KB

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