BxBaseServiceLogin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. }
  144. $oPemalink = BxDolPermalinks::getInstance();
  145. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login');
  146. $sCustomHtmlBefore = '';
  147. $sCustomHtmlAfter = '';
  148. /**
  149. * @hooks
  150. * @hookdef hook-profile-show_login_form 'profile', 'show_login_form' - hook before dhow login form
  151. * - $unit_name - equals `profile`
  152. * - $action - equals `show_login_form`
  153. * - $object_id - not used
  154. * - $sender_id - not used
  155. * - $extra_params - array of additional params with the following array keys:
  156. * - `oForm` - [object] by ref, object of BxDolForm, can be overridden in hook processing
  157. * - `sParams` - [string] by ref, paramenters, can be overridden in hook processing
  158. * - `sCustomHtmlBefore` - [string] by ref, html before form, can be overridden in hook processing
  159. * - `sCustomHtmlAfter` - [string] by ref, html after form, can be overridden in hook processing
  160. * - `aAuthTypes` - [array] by ref, Auth Types, can be overridden in hook processing
  161. * @hook @ref hook-account-show_login_form
  162. */
  163. bx_alert('profile', 'show_login_form', 0, 0, array('oForm' => &$oForm, 'sParams' => &$sParams, 'sCustomHtmlBefore' => &$sCustomHtmlBefore, 'sCustomHtmlAfter' => &$sCustomHtmlAfter, 'aAuthTypes' => &$aAuthTypes));
  164. if (isset($oForm->aInputs['relocate'])) {
  165. if ($sForceRelocate && 0 === mb_stripos($sForceRelocate, BX_DOL_URL_ROOT))
  166. $oForm->aInputs['relocate']['value'] = $sForceRelocate;
  167. elseif ('homepage' == $sForceRelocate)
  168. $oForm->aInputs['relocate']['value'] = BX_DOL_URL_ROOT;
  169. }
  170. if (bx_is_api()) {
  171. if ($oForm->isSubmittedAndValid()) {
  172. $oAccount = BxDolAccount::getInstance(trim($oForm->getCleanValue('ID')));
  173. bx_login($oAccount->id(), $oForm->getRememberMe());
  174. return [
  175. //bx_api_get_block('login', ['session' => BxDolSession::getInstance()->getId()], ['id' => 2]),
  176. bx_api_get_block('redirect', ['uri' => '/']),
  177. bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=system/login_form/TemplServiceLogin', 'immutable' => true]]]),
  178. ];
  179. }
  180. else {
  181. return getParam('sys_account_disable_login_form') ? null : [
  182. bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=system/login_form/TemplServiceLogin', 'immutable' => true]]]),
  183. ];
  184. }
  185. }
  186. $aTmplVarsForm = [];
  187. if(!(bool)getParam('sys_account_disable_login_form'))
  188. $aTmplVarsForm['content'] = $oForm->getCode();
  189. $aTmplVarsAuth = [];
  190. if(strpos($sParams, 'no_auth_buttons') === false)
  191. $aTmplVarsAuth['content'] = $this->serviceMemberAuthCode();
  192. $aTmplVarsJoin = [];
  193. if(strpos($sParams, 'no_join_text') === false)
  194. $aTmplVarsJoin['url'] = bx_absolute_url($oPemalink->permalink('page.php?i=create-account'));
  195. $oTemplate = BxDolTemplate::getInstance();
  196. $oTemplate->addJs(array('jquery.form.min.js'));
  197. return $oTemplate->parseHtmlByName('block_login.html', [
  198. 'custom_html_before' => $sCustomHtmlBefore,
  199. 'bx_if:show_auth' => [
  200. 'condition' => !empty($aTmplVarsAuth),
  201. 'content' => $aTmplVarsAuth
  202. ],
  203. 'bx_if:show_form' => [
  204. 'condition' => !empty($aTmplVarsForm),
  205. 'content' => $aTmplVarsForm,
  206. ],
  207. 'custom_html_after' => $sCustomHtmlAfter,
  208. 'bx_if:show_join' => [
  209. 'condition' => !empty($aTmplVarsJoin),
  210. 'content' => $aTmplVarsJoin
  211. ],
  212. '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'))),
  213. 'bx_if:show_js_code' => [
  214. 'condition' => strpos($sParams, 'ajax_form') !== false,
  215. 'content' => []
  216. ]
  217. ]);
  218. }
  219. public function serviceLoginFormStep2 ()
  220. {
  221. if(isLogged()){
  222. header('Location: ' . BX_DOL_URL_ROOT);
  223. exit;
  224. }
  225. $oSession = BxDolSession::getInstance();
  226. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  227. if ($iAccountId == '')
  228. return false;
  229. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step2');
  230. $oForm->aFormAttrs['action'] = '';
  231. $oAccount = BxDolAccount::getInstance($iAccountId);
  232. $a = $oAccount->getInfo($iAccountId);
  233. $sPhoneNumber = trim($a['phone']);
  234. $oForm->initChecker(array('phone' => $sPhoneNumber));
  235. if ($oForm->isSubmittedAndValid()) {
  236. $sNewPhoneNumber = trim($oForm->getCleanValue('phone'));
  237. if ($sPhoneNumber != $sNewPhoneNumber)
  238. $oAccount->updatePhone($sNewPhoneNumber);
  239. $sActivationCode = rand(1000, 9999);
  240. $sActivationText =_t('_sys_txt_login_2fa_sms_text', $sActivationCode);
  241. $mixedOverrideResult = null;
  242. /**
  243. * @hooks
  244. * @hookdef hook-account-before_2fa_send_sms 'account', 'before_2fa_send_sms' - hook after user performed login
  245. * - $unit_name - equals `system`
  246. * - $action - equals `before_2fa_send_sms`
  247. * - $object_id - account id
  248. * - $sender_id - not used
  249. * - $extra_params - array of additional params with the following array keys:
  250. * - `phone_number` - [array] contains account info from $oAccount->getInfo()
  251. * - `sms_text` - [string] the password entered by the user
  252. * - `override_result` - [string] the password after encription, to save in db
  253. * @hook @ref hook-account-before_2fa_send_sms
  254. */
  255. bx_alert('account', 'before_2fa_send_sms', $oAccount->id(), false, [
  256. 'phone_number' => &$sPhoneNumber,
  257. 'sms_text' => &$sActivationText,
  258. 'override_result' => &$mixedOverrideResult
  259. ]);
  260. if($mixedOverrideResult === null && ($oSms = BxDolSms::getObjectInstance()) !== false && !$oSms->sendSms($sNewPhoneNumber, $sActivationText))
  261. return MsgBox(_t('_sys_txt_login_2fa_sms_error_occured'));
  262. $oSession = BxDolSession::getInstance();
  263. $oSession->setValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE, $sActivationCode);
  264. header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=login-step3')));
  265. }
  266. return '<div class="bx-def-padding-sec-bottom">' . _t("_sys_txt_login_2fa_description") . '</div>' .$oForm->getCode();
  267. }
  268. public function serviceLoginFormStep3 ()
  269. {
  270. if(isLogged()){
  271. header('Location: ' . BX_DOL_URL_ROOT);
  272. exit;
  273. }
  274. $oForm = BxDolForm::getObjectInstance('sys_login', 'sys_login_step3');
  275. $oForm->aInputs['back']['caption'] = _t( $oForm->aInputs['back']['caption_src'], bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=login-step2')));
  276. $oForm->aFormAttrs['action'] = '';
  277. $oForm->initChecker();
  278. if ($oForm->isSubmittedAndValid()) {
  279. $oSession = BxDolSession::getInstance();
  280. $oSession->unsetValue(BX_ACCOUNT_SESSION_KEY_FOR_PHONE_ACTIVATEION_CODE);
  281. $iAccountId = $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_ACCOUNT_ID);
  282. $oAccount = BxDolAccount::getInstance($iAccountId);
  283. $aAccount = bx_login($oAccount->id(), $oSession->getValue(BX_ACCOUNT_SESSION_KEY_FOR_2FA_LOGIN_IS_REMEMBER));
  284. $sUrlRelocate = $oForm->getCleanValue('relocate');
  285. if (!$sUrlRelocate || 0 !== strncmp($sUrlRelocate, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  286. $sUrlRelocate = BX_DOL_ROLE_ADMIN == $oForm->getRole() ? BX_DOL_URL_STUDIO . 'launcher.php' : BX_DOL_URL_ROOT . 'member.php';
  287. /**
  288. * @hooks
  289. * @hookdef hook-account-login_after 'account', 'login_after' - hook on after login to rewrite url for redirect after login
  290. * - $unit_name - equals `account`
  291. * - $action - equals `login_after`
  292. * - $object_id - account id
  293. * - $sender_id - not used
  294. * - $extra_params - array of additional params with the following array keys:
  295. * - `account` - [array] contains account info from $oAccount->getInfo()
  296. * - `url_relocate` - [string] by ref, rl for redirect after login, can be overridden in hook processing
  297. * @hook @ref hook-account-login_after
  298. */
  299. bx_alert('account', 'login_after', $oAccount->id(), false, array(
  300. 'account' => $aAccount,
  301. 'url_relocate' => &$sUrlRelocate
  302. ));
  303. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_TRANSITION);
  304. BxDolTemplate::getInstance()->setPageHeader (_t('_Please Wait'));
  305. BxDolTemplate::getInstance()->setPageContent ('page_main_code', MsgBox(_t('_Please Wait')));
  306. BxDolTemplate::getInstance()->setPageContent ('url_relocate', bx_html_attribute($sUrlRelocate, BX_ESCAPE_STR_QUOTE));
  307. header('Location: ' . $sUrlRelocate);
  308. }
  309. return $oForm->getCode();
  310. }
  311. /**
  312. * @page service Service Calls
  313. * @section bx_system_general System Services
  314. * @subsection bx_system_general-login Login
  315. * @subsubsection bx_system_general-logout logout
  316. *
  317. * @code bx_srv('system', 'logout', [], 'TemplServiceLogin'); @endcode
  318. *
  319. * Logout
  320. *
  321. * @see BxBaseServiceLogin::serviceLogout
  322. */
  323. /**
  324. * @ref bx_system_general-logout "logout"
  325. * @api @ref bx_system_general-logout "logout"
  326. */
  327. public function serviceLogout ()
  328. {
  329. bx_logout();
  330. if (bx_is_api()) {
  331. return [
  332. ['id' => 2, 'type' => 'redirect', 'data' => ['uri' => '/']],
  333. ];
  334. }
  335. else {
  336. header("Location: " . BX_DOL_URL_ROOT);
  337. }
  338. return true;
  339. }
  340. }
  341. /** @} */