1
0

BxBaseServiceLogin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 login form functionality.
  11. */
  12. class BxBaseServiceLogin extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. /**
  19. * @page service Service Calls
  20. * @section bx_system_general System Services
  21. * @subsection bx_system_general-login Login
  22. * @subsubsection bx_system_general-test test
  23. *
  24. * @code bx_srv('system', 'test', [3], 'TemplServiceLogin'); @endcode
  25. * @code {{~system:test:TemplServiceLogin[3]~}} @endcode
  26. *
  27. * Test method which returns provided number multiplied by 2
  28. * @param $n number
  29. *
  30. * @see BxBaseServiceLogin::serviceTest
  31. */
  32. /**
  33. * @ref bx_system_general-test "test"
  34. * @api @ref bx_system_general-test "test"
  35. */
  36. public function serviceTest ($n = 1)
  37. {
  38. return $n*2;
  39. }
  40. /**
  41. * @page service Service Calls
  42. * @section bx_system_general System Services
  43. * @subsection bx_system_general-login Login
  44. * @subsubsection bx_system_general-member_auth_code member_auth_code
  45. *
  46. * @code bx_srv('system', 'member_auth_code', [], 'TemplServiceLogin'); @endcode
  47. * @code {{~system:member_auth_code:TemplServiceLogin~}} @endcode
  48. *
  49. * Display external login buttons such as Facebook, Twitter, etc
  50. * @param $aAuthTypes optional list of external login buttons
  51. *
  52. * @see BxBaseServiceLogin::serviceMemberAuthCode
  53. */
  54. /**
  55. * @ref bx_system_general-member_auth_code "member_auth_code"
  56. */
  57. public function serviceMemberAuthCode($aAuthTypes = array())
  58. {
  59. if(empty($aAuthTypes) || !is_array($aAuthTypes))
  60. $aAuthTypes = BxDolDb::getInstance()->fromCache('sys_objects_auths', 'getAll', 'SELECT * FROM `sys_objects_auths`');
  61. $bCompact = getParam('site_login_social_compact') == 'on';
  62. $aTmplButtons = array();
  63. foreach($aAuthTypes as $iKey => $aItems) {
  64. $sTitle = _t($aItems['Title']);
  65. $aTmplButtons[] = array(
  66. 'class' => ($bCompact ? 'sys-auth-compact ' : '') . $aItems['Name'],
  67. 'href' => !empty($aItems['Link']) ? BX_DOL_URL_ROOT . $aItems['Link'] : 'javascript:void(0)',
  68. 'title_alt' => bx_html_attribute($sTitle),
  69. 'bx_if:show_onclick' => array(
  70. 'condition' => !empty($aItems['OnClick']),
  71. 'content' => array(
  72. 'onclick' => 'javascript:' . $aItems['OnClick']
  73. )
  74. ),
  75. 'bx_if:show_icon' => array(
  76. 'condition' => !empty($aItems['Icon']),
  77. 'content' => array(
  78. 'icon' => $aItems['Icon']
  79. )
  80. ),
  81. 'bx_if:show_title' => array(
  82. 'condition' => !$bCompact || empty($aItems['Icon']),
  83. 'content' => array(
  84. 'title' => $sTitle
  85. )
  86. )
  87. );
  88. if ($aItems['Style'] != ""){
  89. $aStyles = unserialize($aItems['Style']);
  90. foreach($aStyles as $sKey => $aValues) {
  91. BxDolTemplate::getInstance()->addCssStyle('.'. $aItems['Name'] .' ' . $sKey, $aValues);
  92. }
  93. }
  94. }
  95. if(empty($aTmplButtons) || !is_array($aTmplButtons))
  96. return '';
  97. $oTemplate = BxDolTemplate::getInstance();
  98. $oTemplate->addCss(array('auth.css'));
  99. return $oTemplate->parseHtmlByName('auth.html', array(
  100. 'bx_repeat:buttons' => $aTmplButtons,
  101. 'class_container' => ($bCompact ? 'sys-auth-compact-container' : '')
  102. ));
  103. }
  104. /**
  105. * Display login form without join text
  106. * @see ::serviceLoginForm
  107. */
  108. public function serviceLoginFormOnly ($sParams = '', $sForceRelocate = '')
  109. {
  110. if(strpos($sParams, 'no_join_text') === false)
  111. $sParams = ($sParams != '' ? ' ' : '') . 'no_join_text';
  112. return $this->serviceLoginForm($sParams, $sForceRelocate);
  113. }
  114. /**
  115. * @page service Service Calls
  116. * @section bx_system_general System Services
  117. * @subsection bx_system_general-login Login
  118. * @subsubsection bx_system_general-login_form login_form
  119. *
  120. * @code bx_srv('system', 'login_form', ["no_auth_buttons"], 'TemplServiceLogin'); @endcode
  121. * @code {{~system:login_form:TemplServiceLogin["no_auth_buttons"]~}} @endcode
  122. *
  123. * Display login form
  124. * @param $sParams optional string with additional params:
  125. * - no_join_text
  126. * - no_auth_buttons
  127. * - ajax_form
  128. * @param $sForceRelocate optional URL to redirect to after login
  129. *
  130. * @see BxBaseServiceLogin::serviceLoginForm
  131. */
  132. /**
  133. * @ref bx_system_general-login_form "login_form"
  134. * @api @ref bx_system_general-login_form "login_form"
  135. */
  136. public function serviceLoginForm ($sParams = '', $sForceRelocate = '')
  137. {
  138. if (isLogged() && 'login' == bx_get('i')) {
  139. if (!bx_is_api()) {
  140. header('Location: ' . BX_DOL_URL_ROOT);
  141. exit;
  142. }
  143. else {
  144. return [
  145. bx_api_get_msg('You are already logged in 🐵! Redirecting...'),
  146. ['id' => 2, 'type' => 'redirect', 'data' => ['uri' => '/posts-home', 'timeout' => 3000]],
  147. ];
  148. }
  149. }
  150. $oPemalink = BxDolPermalinks::getInstance();
  151. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login');
  152. $sCustomHtmlBefore = '';
  153. $sCustomHtmlAfter = '';
  154. bx_alert('profile', 'show_login_form', 0, 0, array('oForm' => &$oForm, 'sParams' => &$sParams, 'sCustomHtmlBefore' => &$sCustomHtmlBefore, 'sCustomHtmlAfter' => &$sCustomHtmlAfter, 'aAuthTypes' => &$aAuthTypes));
  155. if (isset($oForm->aInputs['relocate'])) {
  156. if ($sForceRelocate && 0 === mb_stripos($sForceRelocate, BX_DOL_URL_ROOT))
  157. $oForm->aInputs['relocate']['value'] = $sForceRelocate;
  158. elseif ('homepage' == $sForceRelocate)
  159. $oForm->aInputs['relocate']['value'] = BX_DOL_URL_ROOT;
  160. }
  161. if (bx_is_api()) {
  162. if ($oForm->isSubmittedAndValid()) {
  163. $oAccount = BxDolAccount::getInstance(trim($oForm->getCleanValue('ID')));
  164. bx_login($oAccount->id(), ($oForm->getCleanValue('rememberMe') ? true : false));
  165. return [
  166. bx_api_get_block('redirect', ['uri' => '/posts-home', 'timeout' => 1000]),
  167. bx_api_get_block('login', ['session' => BxDolSession::getInstance()->getId()], ['id' => 2]),
  168. bx_api_get_msg('Login Success 👍! Redirecting...', ['id' => 3]),
  169. ];
  170. }
  171. else {
  172. return getParam('sys_account_disable_login_form') ? null : [
  173. bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=system/login_form/TemplServiceLogin', 'immutable' => true]]]),
  174. ];
  175. }
  176. }
  177. $aTmplVarsForm = [];
  178. if(!(bool)getParam('sys_account_disable_login_form'))
  179. $aTmplVarsForm['content'] = $oForm->getCode();
  180. $aTmplVarsAuth = [];
  181. if(strpos($sParams, 'no_auth_buttons') === false)
  182. $aTmplVarsAuth['content'] = $this->serviceMemberAuthCode();
  183. $aTmplVarsJoin = [];
  184. if(strpos($sParams, 'no_join_text') === false)
  185. $aTmplVarsJoin['url'] = bx_absolute_url($oPemalink->permalink('page.php?i=create-account'));
  186. $oTemplate = BxDolTemplate::getInstance();
  187. $oTemplate->addJs(array('jquery.form.min.js'));
  188. return $oTemplate->parseHtmlByName('block_login.html', [
  189. 'custom_html_before' => $sCustomHtmlBefore,
  190. 'bx_if:show_auth' => [
  191. 'condition' => !empty($aTmplVarsAuth),
  192. 'content' => $aTmplVarsAuth
  193. ],
  194. 'bx_if:show_form' => [
  195. 'condition' => !empty($aTmplVarsForm),
  196. 'content' => $aTmplVarsForm,
  197. ],
  198. 'custom_html_after' => $sCustomHtmlAfter,
  199. 'bx_if:show_join' => [
  200. 'condition' => !empty($aTmplVarsJoin),
  201. 'content' => $aTmplVarsJoin
  202. ],
  203. 'agreement' => _t('_sys_form_login_input_agreement_value', bx_absolute_url($oPemalink->permalink('page.php?i=terms')), bx_absolute_url($oPemalink->permalink('page.php?i=privacy'))),
  204. 'bx_if:show_js_code' => [
  205. 'condition' => strpos($sParams, 'ajax_form') !== false,
  206. 'content' => []
  207. ]
  208. ]);
  209. }
  210. public function serviceLoginFormStep2 ()
  211. {
  212. if(isLogged()){
  213. header('Location: ' . BX_DOL_URL_ROOT);
  214. exit;
  215. }
  216. $oSession = BxDolSession::getInstance();
  217. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  218. if ($iAccountId == '')
  219. return false;
  220. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step2');
  221. $oForm->aFormAttrs['action'] = '';
  222. $oAccount = BxDolAccount::getInstance($iAccountId);
  223. $a = $oAccount->getInfo($iAccountId);
  224. $sPhoneNumber = trim($a['phone']);
  225. $oForm->initChecker(array('phone' => $sPhoneNumber));
  226. if ($oForm->isSubmittedAndValid()) {
  227. $sNewPhoneNumber = trim($oForm->getCleanValue('phone'));
  228. if ($sPhoneNumber != $sNewPhoneNumber)
  229. $oAccount->updatePhone($sNewPhoneNumber);
  230. $sActivationCode = rand(1000, 9999);
  231. $sActivationText =_t('_sys_txt_login_2fa_sms_text', $sActivationCode);
  232. $ret = null;
  233. bx_alert('account', 'before_2fa_send_sms', $oAccount->id(), false, array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  234. if ($ret === null)
  235. {
  236. $oTwilio = BxDolTwilio::getInstance();
  237. if(!$oTwilio->sendSms($sNewPhoneNumber, $sActivationText)){
  238. return MsgBox(_t('_sys_txt_login_2fa_sms_error_occured'));
  239. }
  240. }
  241. $oSession = BxDolSession::getInstance();
  242. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  243. header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=login-step3')));
  244. }
  245. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_login_2fa_description") . '</div>' .$oForm->getCode();
  246. }
  247. public function serviceLoginFormStep3 ()
  248. {
  249. if(isLogged()){
  250. header('Location: ' . BX_DOL_URL_ROOT);
  251. exit;
  252. }
  253. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step3');
  254. $oForm->aInputs['back']['caption'] = _t( $oForm->aInputs['back']['caption_src'], bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=login-step2')));
  255. $oForm->aFormAttrs['action'] = '';
  256. $oForm->initChecker();
  257. if ($oForm->isSubmittedAndValid()) {
  258. $oSession = BxDolSession::getInstance();
  259. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  260. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  261. $oAccount = BxDolAccount::getInstance($iAccountId);
  262. $aAccount = bx_login($oAccount->id(), $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_IS_REMEMBER));
  263. $sUrlRelocate = $oForm->getCleanValue('relocate');
  264. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  265. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  266. bx_alert('account', 'login_after', $oAccount->id(), false, array(
  267. 'account' => $aAccount,
  268. 'url_relocate' => &$sUrlRelocate
  269. ));
  270. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  271. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  272. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  273. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  274. header('Location: ' . $sUrlRelocate);
  275. }
  276. return $oForm->getCode();
  277. }
  278. /**
  279. * @page service Service Calls
  280. * @section bx_system_general System Services
  281. * @subsection bx_system_general-login Login
  282. * @subsubsection bx_system_general-logout logout
  283. *
  284. * @code bx_srv('system', 'logout', [], 'TemplServiceLogin'); @endcode
  285. *
  286. * Logout
  287. *
  288. * @see BxBaseServiceLogin::serviceLogout
  289. */
  290. /**
  291. * @ref bx_system_general-logout "logout"
  292. * @api @ref bx_system_general-logout "logout"
  293. */
  294. public function serviceLogout ()
  295. {
  296. bx_logout();
  297. if (bx_is_api()) {
  298. return [
  299. ['id' => 2, 'type' => 'redirect', 'data' => ['uri' => '/posts-home']],
  300. ];
  301. }
  302. else {
  303. header("Location: " . BX_DOL_URL_ROOT);
  304. }
  305. return true;
  306. }
  307. }
  308. /** @} */