BxBaseServiceLogin.php 16 KB

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