BxBaseServiceLogin.php 16 KB

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