BxBaseServiceLogin.php 11 KB

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