BxBaseServiceLogin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-create_account_form "create_account_form"
  34. */
  35. public function serviceTest ($n = 1)
  36. {
  37. return $n*2;
  38. }
  39. /**
  40. * @page service Service Calls
  41. * @section bx_system_general System Services
  42. * @subsection bx_system_general-login Login
  43. * @subsubsection bx_system_general-member_auth_code member_auth_code
  44. *
  45. * @code bx_srv('system', 'member_auth_code', [], 'TemplServiceLogin'); @endcode
  46. * @code {{~system:member_auth_code:TemplServiceLogin~}} @endcode
  47. *
  48. * Display external login buttons such as Facebook, Twitter, etc
  49. * @param $aAuthTypes optional list of external login buttons
  50. *
  51. * @see BxBaseServiceLogin::serviceMemberAuthCode
  52. */
  53. /**
  54. * @ref bx_system_general-member_auth_code "member_auth_code"
  55. */
  56. public function serviceMemberAuthCode($aAuthTypes = array())
  57. {
  58. if(empty($aAuthTypes) || !is_array($aAuthTypes))
  59. $aAuthTypes = BxDolDb::getInstance()->fromCache('sys_objects_auths', 'getAll', 'SELECT * FROM `sys_objects_auths`');
  60. $bCompact = getParam('site_login_social_compact') == 'on';
  61. $aTmplButtons = array();
  62. foreach($aAuthTypes as $iKey => $aItems) {
  63. $sTitle = _t($aItems['Title']);
  64. $aTmplButtons[] = array(
  65. 'class' => ($bCompact ? 'sys-auth-compact ' : '') . $aItems['Name'],
  66. 'href' => !empty($aItems['Link']) ? BX_DOL_URL_ROOT . $aItems['Link'] : 'javascript:void(0)',
  67. 'title_alt' => bx_html_attribute($sTitle),
  68. 'bx_if:show_onclick' => array(
  69. 'condition' => !empty($aItems['OnClick']),
  70. 'content' => array(
  71. 'onclick' => 'javascript:' . $aItems['OnClick']
  72. )
  73. ),
  74. 'bx_if:show_icon' => array(
  75. 'condition' => !empty($aItems['Icon']),
  76. 'content' => array(
  77. 'icon' => $aItems['Icon']
  78. )
  79. ),
  80. 'bx_if:show_title' => array(
  81. 'condition' => !$bCompact || empty($aItems['Icon']),
  82. 'content' => array(
  83. 'title' => $sTitle
  84. )
  85. )
  86. );
  87. if ($aItems['Style'] != ""){
  88. $aStyles = unserialize($aItems['Style']);
  89. foreach($aStyles as $sKey => $aValues) {
  90. BxDolTemplate::getInstance()->addCssStyle('.'. $aItems['Name'] .' ' . $sKey, $aValues);
  91. }
  92. }
  93. }
  94. if(empty($aTmplButtons) || !is_array($aTmplButtons))
  95. return '';
  96. $oTemplate = BxDolTemplate::getInstance();
  97. $oTemplate->addCss(array('auth.css'));
  98. return $oTemplate->parseHtmlByName('auth.html', array(
  99. 'bx_repeat:buttons' => $aTmplButtons,
  100. 'class_container' => ($bCompact ? 'sys-auth-compact-container' : '')
  101. ));
  102. }
  103. /**
  104. * Display login form without join text
  105. * @see ::serviceLoginForm
  106. */
  107. public function serviceLoginFormOnly ($sParams = '', $sForceRelocate = '')
  108. {
  109. if(strpos($sParams, 'no_join_text') === false)
  110. $sParams = ($sParams != '' ? ' ' : '') . 'no_join_text';
  111. return $this->serviceLoginForm($sParams, $sForceRelocate);
  112. }
  113. /**
  114. * @page service Service Calls
  115. * @section bx_system_general System Services
  116. * @subsection bx_system_general-login Login
  117. * @subsubsection bx_system_general-login_form login_form
  118. *
  119. * @code bx_srv('system', 'login_form', ["no_auth_buttons"], 'TemplServiceLogin'); @endcode
  120. * @code {{~system:login_form:TemplServiceLogin["no_auth_buttons"]~}} @endcode
  121. *
  122. * Display login form
  123. * @param $sParams optional string with additional params:
  124. * - no_join_text
  125. * - no_auth_buttons
  126. * - ajax_form
  127. * @param $sForceRelocate optional URL to redirect to after login
  128. *
  129. * @see BxBaseServiceLogin::serviceLoginForm
  130. */
  131. /**
  132. * @ref bx_system_general-login_form "login_form"
  133. */
  134. public function serviceLoginForm ($sParams = '', $sForceRelocate = '')
  135. {
  136. if (isLogged() && 'login' == bx_get('i')) {
  137. header('Location: ' . BX_DOL_URL_ROOT);
  138. exit;
  139. }
  140. $oPemalink = BxDolPermalinks::getInstance();
  141. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login');
  142. $sCustomHtmlBefore = '';
  143. $sCustomHtmlAfter = '';
  144. bx_alert('profile', 'show_login_form', 0, 0, array('oForm' => &$oForm, 'sParams' => &$sParams, 'sCustomHtmlBefore' => &$sCustomHtmlBefore, 'sCustomHtmlAfter' => &$sCustomHtmlAfter, 'aAuthTypes' => &$aAuthTypes));
  145. if (isset($oForm->aInputs['relocate'])) {
  146. if ($sForceRelocate && 0 === mb_stripos($sForceRelocate, BX_DOL_URL_ROOT))
  147. $oForm->aInputs['relocate']['value'] = $sForceRelocate;
  148. elseif ('homepage' == $sForceRelocate)
  149. $oForm->aInputs['relocate']['value'] = BX_DOL_URL_ROOT;
  150. }
  151. $aTmplVarsForm = [];
  152. if(!(bool)getParam('sys_account_disable_login_form'))
  153. $aTmplVarsForm['content'] = $oForm->getCode();
  154. $aTmplVarsAuth = [];
  155. if(strpos($sParams, 'no_auth_buttons') === false)
  156. $aTmplVarsAuth['content'] = $this->serviceMemberAuthCode();
  157. $aTmplVarsJoin = [];
  158. if(strpos($sParams, 'no_join_text') === false)
  159. $aTmplVarsJoin['url'] = BX_DOL_URL_ROOT . $oPemalink->permalink('page.php?i=create-account');
  160. $oTemplate = BxDolTemplate::getInstance();
  161. $oTemplate->addJs(array('jquery.form.min.js'));
  162. return $oTemplate->parseHtmlByName('block_login.html', [
  163. 'custom_html_before' => $sCustomHtmlBefore,
  164. 'bx_if:show_auth' => [
  165. 'condition' => !empty($aTmplVarsAuth),
  166. 'content' => $aTmplVarsAuth
  167. ],
  168. 'bx_if:show_form' => [
  169. 'condition' => !empty($aTmplVarsForm),
  170. 'content' => $aTmplVarsForm,
  171. ],
  172. 'custom_html_after' => $sCustomHtmlAfter,
  173. 'bx_if:show_join' => [
  174. 'condition' => !empty($aTmplVarsJoin),
  175. 'content' => $aTmplVarsJoin
  176. ],
  177. 'agreement' => _t('_sys_form_login_input_agreement_value', BX_DOL_URL_ROOT . $oPemalink->permalink('page.php?i=terms'), BX_DOL_URL_ROOT . $oPemalink->permalink('page.php?i=privacy')),
  178. 'bx_if:show_js_code' => [
  179. 'condition' => strpos($sParams, 'ajax_form') !== false,
  180. 'content' => []
  181. ]
  182. ]);
  183. }
  184. public function serviceLoginFormStep2 ()
  185. {
  186. if(isLogged()){
  187. header('Location: ' . BX_DOL_URL_ROOT);
  188. exit;
  189. }
  190. $oSession = BxDolSession::getInstance();
  191. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  192. if ($iAccountId == '')
  193. return false;
  194. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step2');
  195. $oForm->aFormAttrs['action'] = '';
  196. $oAccount = BxDolAccount::getInstance($iAccountId);
  197. $a = $oAccount->getInfo($iAccountId);
  198. $sPhoneNumber = trim($a['phone']);
  199. $oForm->initChecker(array('phone' => $sPhoneNumber));
  200. if ($oForm->isSubmittedAndValid()) {
  201. $sNewPhoneNumber = trim($oForm->getCleanValue('phone'));
  202. if ($sPhoneNumber != $sNewPhoneNumber)
  203. $oAccount->updatePhone($sNewPhoneNumber);
  204. $sActivationCode = rand(1000, 9999);
  205. $sActivationText =_t('_sys_txt_login_2fa_sms_text', $sActivationCode);
  206. $ret = null;
  207. bx_alert('account', 'before_2fa_send_sms', $oAccount->id(), false, array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  208. if ($ret === null)
  209. {
  210. $oTwilio = BxDolTwilio::getInstance();
  211. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  212. return MsgBox(_t('_sys_txt_login_2fa_sms_error_occured'));
  213. }
  214. }
  215. $oSession = BxDolSession::getInstance();
  216. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  217. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login-step3'));
  218. }
  219. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_login_2fa_description") . '</div>' .$oForm->getCode();
  220. }
  221. public function serviceLoginFormStep3 ()
  222. {
  223. if(isLogged()){
  224. header('Location: ' . BX_DOL_URL_ROOT);
  225. exit;
  226. }
  227. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step3');
  228. $oForm->aFormAttrs['action'] = '';
  229. $oForm->initChecker();
  230. if ($oForm->isSubmittedAndValid()) {
  231. $oSession = BxDolSession::getInstance();
  232. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  233. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  234. $oAccount = BxDolAccount::getInstance($iAccountId);
  235. $aAccount = bx_login($oAccount->id(), $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_IS_REMEMBER));
  236. $sUrlRelocate = $oForm->getCleanValue('relocate');
  237. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  238. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  239. bx_alert('account', 'login_after', $oAccount->id(), false, array(
  240. 'account' => $aAccount,
  241. 'url_relocate' => &$sUrlRelocate
  242. ));
  243. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  244. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  245. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  246. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  247. header('Location: ' . $sUrlRelocate);
  248. }
  249. return $oForm->getCode();
  250. }
  251. }
  252. /** @} */