BxBaseServiceLogin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 bx-def-margin-sec-left-auto ' : '') . $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. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login');
  141. $sCustomHtmlBefore = '';
  142. $sCustomHtmlAfter = '';
  143. bx_alert('profile', 'show_login_form', 0, 0, array('oForm' => &$oForm, 'sParams' => &$sParams, 'sCustomHtmlBefore' => &$sCustomHtmlBefore, 'sCustomHtmlAfter' => &$sCustomHtmlAfter, 'aAuthTypes' => &$aAuthTypes));
  144. if (isset($oForm->aInputs['relocate'])) {
  145. if ($sForceRelocate && 0 === mb_stripos($sForceRelocate, BX_DOL_URL_ROOT))
  146. $oForm->aInputs['relocate']['value'] = $sForceRelocate;
  147. elseif ('homepage' == $sForceRelocate)
  148. $oForm->aInputs['relocate']['value'] = BX_DOL_URL_ROOT;
  149. }
  150. $sFormCode = '';
  151. if (!(bool)getParam('sys_account_disable_login_form'))
  152. $sFormCode = $oForm->getCode();
  153. BxDolTemplate::getInstance()->addJs(array('jquery.form.min.js'));
  154. $sJoinText = '';
  155. if (strpos($sParams, 'no_join_text') === false)
  156. $sJoinText = '<hr class="bx-def-hr bx-def-margin-sec-topbottom" /><div>' . _t('_sys_txt_login_description', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account')) . '</div>';
  157. $sAuth = '';
  158. if (strpos($sParams, 'no_auth_buttons') === false)
  159. $sAuth = $this->serviceMemberAuthCode();
  160. $sAjaxForm = '';
  161. if (strpos($sParams, 'ajax_form') !== false)
  162. $sAjaxForm = "<script>
  163. (bx_login_init_ajax_form = function () {
  164. $('#sys-form-login').ajaxForm({
  165. dataType: 'json',
  166. success: function (oData) {
  167. if ('undefined' !== typeof(oData['res']) && 'OK' == oData['res']) {
  168. location.reload();
  169. }
  170. else if ('undefined' !== typeof(oData['form'])) {
  171. $('#sys-form-login').replaceWith(oData['form']);
  172. bx_login_init_ajax_form();
  173. }
  174. }
  175. });
  176. })();
  177. </script>";
  178. return $sCustomHtmlBefore . $sAuth . $sFormCode . $sCustomHtmlAfter . $sJoinText . $sAjaxForm;
  179. }
  180. public function serviceLoginFormStep2 ()
  181. {
  182. if(isLogged()){
  183. header('Location: ' . BX_DOL_URL_ROOT);
  184. exit;
  185. }
  186. $oSession = BxDolSession::getInstance();
  187. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  188. if ($iAccountId == '')
  189. return false;
  190. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step2');
  191. $oForm->aFormAttrs['action'] = '';
  192. $oAccount = BxDolAccount::getInstance($iAccountId);
  193. $a = $oAccount->getInfo($iAccountId);
  194. $sPhoneNumber = trim($a['phone']);
  195. $oForm->initChecker(array('phone' => $sPhoneNumber));
  196. if ($oForm->isSubmittedAndValid()) {
  197. $sNewPhoneNumber = trim($oForm->getCleanValue('phone'));
  198. if ($sPhoneNumber != $sNewPhoneNumber)
  199. $oAccount->updatePhone($sNewPhoneNumber);
  200. $sActivationCode = rand(1000, 9999);
  201. $sActivationText =_t('_sys_txt_login_2fa_sms_text', $sActivationCode);
  202. $ret = null;
  203. bx_alert('account', 'before_2fa_send_sms', $oAccount->id(), false, array('phone_number' => $sPhoneNumber, 'sms_text' => $sActivationText, 'override_result' => &$ret));
  204. if ($ret === null)
  205. {
  206. $oTwilio = BxDolTwilio::getInstance();
  207. if(!$oTwilio->sendSms($sPhoneNumber, $sActivationText)){
  208. return MsgBox(_t('_sys_txt_login_2fa_sms_error_occured'));
  209. }
  210. }
  211. $oSession = BxDolSession::getInstance();
  212. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  213. header('Location: ' . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=login-step3'));
  214. }
  215. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_login_2fa_description") . '</div>' .$oForm->getCode();
  216. }
  217. public function serviceLoginFormStep3 ()
  218. {
  219. if(isLogged()){
  220. header('Location: ' . BX_DOL_URL_ROOT);
  221. exit;
  222. }
  223. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step3');
  224. $oForm->aFormAttrs['action'] = '';
  225. $oForm->initChecker();
  226. if ($oForm->isSubmittedAndValid()) {
  227. $oSession = BxDolSession::getInstance();
  228. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  229. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  230. $oAccount = BxDolAccount::getInstance($iAccountId);
  231. $aAccount = bx_login($oAccount->id(), $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_IS_REMEMBER));
  232. $sUrlRelocate = $oForm->getCleanValue('relocate');
  233. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  234. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  235. bx_alert('account', 'login_after', $oAccount->id(), false, array(
  236. 'account' => $aAccount,
  237. 'url_relocate' => &$sUrlRelocate
  238. ));
  239. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  240. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  241. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  242. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  243. header('Location: ' . $sUrlRelocate);
  244. }
  245. return $oForm->getCode();
  246. }
  247. }
  248. /** @} */