profiles.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreFunctions UNA Core Functions
  7. * @{
  8. */
  9. /**
  10. * @return corrently logged in profile id
  11. */
  12. function bx_get_logged_profile_id ()
  13. {
  14. $o = BxDolProfile::getInstance();
  15. return $o ? $o->id() : false;
  16. }
  17. /**
  18. * @return true if user is logged in
  19. */
  20. function isLogged()
  21. {
  22. return getLoggedId() != 0;
  23. }
  24. /**
  25. * @return logged in account id
  26. */
  27. function getLoggedId()
  28. {
  29. if ($GLOBALS['glForceCurrentProfileId'] ?? false) {
  30. return BxDolProfile::getInstance()->getAccountId();
  31. }
  32. elseif (getParam('sys_session_auth')) {
  33. return (!empty($GLOBALS['logged']['member']) || !empty($GLOBALS['logged']['admin'])) ? BxDolSession::getInstance()->getUserId() : 0;
  34. }
  35. else {
  36. return isset($_COOKIE['memberID']) && (!empty($GLOBALS['logged']['member']) || !empty($GLOBALS['logged']['admin'])) ? (int)$_COOKIE['memberID'] : 0;
  37. }
  38. }
  39. /**
  40. * @return logged in account password
  41. */
  42. function getLoggedPassword()
  43. {
  44. $oAccount = BxDolAccount::getInstance();
  45. return ($GLOBALS['logged']['member'] || $GLOBALS['logged']['admin']) && $oAccount ? BxDolAccountQuery::getInstance()->getPassword($oAccount->id()) : "";
  46. }
  47. /**
  48. * It checks if account role is member.
  49. */
  50. function isMember($iId = 0)
  51. {
  52. return isRole(BX_DOL_ROLE_MEMBER, $iId);
  53. }
  54. if (!function_exists("isAdmin")) {
  55. /**
  56. * @return true if account is admin
  57. */
  58. function isAdmin($iId = 0)
  59. {
  60. if (!$iId && isset($GLOBALS['logged']['admin']) && $GLOBALS['logged']['admin']) // easier check for currently logged in user
  61. return true;
  62. return isRole(BX_DOL_ROLE_ADMIN, $iId);
  63. }
  64. }
  65. /**
  66. * Check user role
  67. * @param $iRole role to check user for
  68. * @param $iId optional account id, if it isn't specified then curently logged in account is used
  69. * @return true if user is in the provided role
  70. */
  71. function isRole($iRole, $iId = 0)
  72. {
  73. if (!(int)$iId)
  74. $iId = getLoggedId();
  75. $oAccount = BxDolAccount::getInstance($iId);
  76. if (!$oAccount)
  77. return false;
  78. $aAccountInfo = $oAccount->getInfo();
  79. if (!$aAccountInfo)
  80. return false;
  81. if (!((int)$aAccountInfo['role'] & $iRole))
  82. return false;
  83. return true;
  84. }
  85. /**
  86. * Login user by setting necessary cookies
  87. * @param $iId account id
  88. * @param $bRememberMe remember session
  89. * @return false if id isn't correct or array of user info if user was logged in
  90. */
  91. function bx_login($iId, $bRememberMe = false)
  92. {
  93. $oAccountQuery = BxDolAccountQuery::getInstance();
  94. $sPassword = $oAccountQuery->getPassword($iId);
  95. if (!$sPassword)
  96. return false;
  97. if (!getParam('sys_session_auth')) {
  98. $iCookieTime = $bRememberMe ? time() + 60 * getParam('sys_session_lifetime_in_min') : 0;
  99. bx_setcookie("memberID", $iId, $iCookieTime, 'auto');
  100. $_COOKIE['memberID'] = $iId;
  101. bx_setcookie("memberPassword", $sPassword, $iCookieTime, 'auto', '', 'auto', true /* http only */);
  102. $_COOKIE['memberPassword'] = $sPassword;
  103. }
  104. BxDolSession::getInstance()->setUserId($iId);
  105. if (getParam('sys_session_auth'))
  106. BxDolSession::getInstance()->setValue('remember_me', $bRememberMe);
  107. $oAccountQuery->updateLoggedIn($iId);
  108. $oProfileQuery = BxDolProfileQuery::getInstance();
  109. if(($iProfileId = $oProfileQuery->getCurrentProfileByAccount($iId, true)) !== false) {
  110. $aProfileInfo = $oProfileQuery->getInfoById($iProfileId);
  111. if(!empty($aProfileInfo) && is_array($aProfileInfo) && $aProfileInfo['type'] != 'system')
  112. BxDolRecommendation::updateData($iProfileId);
  113. }
  114. /**
  115. * @hooks
  116. * @hookdef hook-account-login 'account', 'login' - hook after user performed login
  117. * - $unit_name - equals `account`
  118. * - $action - equals `login`
  119. * - $object_id - account id
  120. * - $sender_id - not used
  121. * - $extra_params - not used
  122. * @hook @ref hook-account-login
  123. */
  124. bx_alert('account', 'login', $iId);
  125. bx_audit($iId, 'bx_accounts', '_sys_audit_action_account_login', array(
  126. 'content_title' => '',
  127. 'data' => ['display_info' => ['User agent' => (isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : '')]]
  128. ));
  129. return $oAccountQuery->getInfoById($iId);
  130. }
  131. /**
  132. * Logout user by removing cookies
  133. */
  134. function bx_logout($bNotify = true)
  135. {
  136. if (!($iMemberId = BxDolSession::getInstance()->getUserId()))
  137. return;
  138. if ($bNotify && isMember()){
  139. /**
  140. * @hooks
  141. * @hookdef hook-account-logout 'account', 'logout' - hook after user performed logout
  142. * - $unit_name - equals `account`
  143. * - $action - equals `logout`
  144. * - $object_id - account id
  145. * - $sender_id - not used
  146. * - $extra_params - not used
  147. * @hook @ref hook-account-logout
  148. */
  149. bx_alert('account', 'logout', $iMemberId);
  150. }
  151. bx_audit(
  152. $iMemberId,
  153. 'bx_accounts',
  154. '_sys_audit_action_account_logout',
  155. array('content_title' => '', 'data' => ['display_info' => ['User agent' => (isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : '')]])
  156. );
  157. if (!getParam('sys_session_auth')) {
  158. bx_setcookie('memberID', '', time() - 96 * 3600);
  159. bx_setcookie('memberPassword', '', time() - 96 * 3600, 'auto', '', 'auto', true);
  160. unset($_COOKIE['memberID']);
  161. unset($_COOKIE['memberPassword']);
  162. }
  163. BxDolSession::getInstance()->destroy();
  164. // BxDolSession::getInstance()->setUserId(0);
  165. }
  166. function bx_is_remember_me()
  167. {
  168. return getParam('sys_account_remember_me') == 'on';
  169. }
  170. /**
  171. * Check if user is logged in (necessary cookies are present) and set some global variables
  172. */
  173. function check_logged()
  174. {
  175. bx_alert('account', 'logged_start', 0);
  176. $aAccTypes = array(
  177. BX_DOL_ROLE_ADMIN => 'admin',
  178. BX_DOL_ROLE_MEMBER => 'member'
  179. );
  180. $bLogged = false;
  181. $bID = false;
  182. $sID = 0;
  183. if (getParam('sys_session_auth')) {
  184. $o = BxDolSession::getInstance();
  185. $sID = $o->getUserId();
  186. if ($sID && $oAccount = BxDolAccount::getInstance((int)$sID)) {
  187. $aAccountInfo = $oAccount->getInfo();
  188. foreach ($aAccTypes as $iRole => $sValue) {
  189. if ($GLOBALS['logged'][$sValue] = ((int)$aAccountInfo['role'] & $iRole)) {
  190. $bLogged = true;
  191. $bID = true;
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. else {
  198. $bID = isset($_COOKIE['memberID']);
  199. $sID = $bID ? bx_process_input($_COOKIE['memberID']) : false;
  200. $bPassword = isset($_COOKIE['memberPassword']);
  201. $sPassword = $bPassword ? bx_process_input($_COOKIE['memberPassword']) : false;
  202. foreach ($aAccTypes as $iRole => $sValue) {
  203. if ($GLOBALS['logged'][$sValue] = ($sID && !bx_check_login($sID, $sPassword, $iRole))) {
  204. BxDolSession::getInstance();
  205. $bLogged = true;
  206. break;
  207. }
  208. }
  209. }
  210. if($bID && $bLogged) {
  211. header("Cache-Control: no-cache, no-store, must-revalidate");
  212. /**
  213. * @hooks
  214. * @hookdef hook-account-logged 'account', 'logged' - hook after user performed logged
  215. * - $unit_name - equals `account`
  216. * - $action - equals `logged`
  217. * - $object_id - account id
  218. * - $sender_id - not used
  219. * - $extra_params - not used
  220. * @hook @ref hook-account-logged
  221. */
  222. bx_alert('account', 'logged', getLoggedId());
  223. }
  224. if($bID && !$bLogged)
  225. bx_logout(false);
  226. }
  227. /**
  228. * Check unencrypted password
  229. * @return empty string on success or error string on error
  230. */
  231. function bx_check_password($sLogin, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  232. {
  233. $oAccount = BxDolAccount::getInstance($sLogin);
  234. if (!$oAccount) {
  235. bx_import('BxDolLanguages');
  236. return _t("_sys_txt_login_error");
  237. }
  238. if ($oAccount->isLocked()){
  239. bx_import('BxDolLanguages');
  240. if (bx_is_api()){
  241. return strip_tags(_t("_sys_txt_login_locked", bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'))),'');
  242. }
  243. return _t("_sys_txt_login_locked", bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password')));
  244. }
  245. $aAccountInfo = $oAccount->getInfo();
  246. $sPassCheck = encryptUserPwd($sPassword, $aAccountInfo['salt']);
  247. // regenerate password using another encrypt function if necessary
  248. /**
  249. * @hooks
  250. * @hookdef hook-account-encrypt_password_after 'account', 'encrypt_password_after' - hook after user performed login
  251. * - $unit_name - equals `system`
  252. * - $action - equals `encrypt_password_after`
  253. * - $object_id - not used
  254. * - $sender_id - not used
  255. * - $extra_params - array of additional params with the following array keys:
  256. * - `info` - [array] contains account info from $oAccount->getInfo()
  257. * - `pwd` - [string] the password entered by the user
  258. * - `password` - [string] the password after encription, to save in db
  259. * @hook @ref hook-account-encrypt_password_after
  260. */
  261. bx_alert('system', 'encrypt_password_after', 0, false, array(
  262. 'info' => $aAccountInfo,
  263. 'pwd' => $sPassword,
  264. 'password' => &$sPassCheck,
  265. 'password_ref' => &$sPassCheck,
  266. ));
  267. if ($sErrorMsg = bx_check_login($aAccountInfo['id'], $sPassCheck, $iRole)){
  268. bx_audit(
  269. $aAccountInfo['id'],
  270. 'bx_accounts',
  271. '_sys_audit_action_account_failed_login_attempt',
  272. array('content_title' => '', 'data' => ['display_info' => ['User agent' => (isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : '')]]),
  273. $aAccountInfo['profile_id']
  274. );
  275. $iMaxLoginAttempts = getParam('sys_account_limit_incorrect_login_attempts');
  276. if ($iMaxLoginAttempts >0){
  277. $oAccountQuery = BxDolAccountQuery::getInstance();
  278. if ($aAccountInfo['login_attempts'] >= $iMaxLoginAttempts){
  279. $oAccountQuery->lockAccount($aAccountInfo['id'], 1);
  280. bx_import('BxDolLanguages');
  281. if (bx_is_api()){
  282. return strip_tags(_t("_sys_txt_login_locked", bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'))),'');
  283. }
  284. }
  285. else{
  286. $oAccountQuery->updateAttemptsCounter($aAccountInfo['id']);
  287. }
  288. }
  289. return $sErrorMsg;
  290. }
  291. // Admin can always login even if he is blocked/banned/suspended/etc
  292. if (isAdmin($aAccountInfo['id']))
  293. return '';
  294. $sErrorMsg = '';
  295. /**
  296. * @hooks
  297. * @hookdef hook-account-check_login 'account', 'check_login' - hook after user's login/password check
  298. * - $unit_name - equals `account`
  299. * - $action - equals `check_login`
  300. * - $object_id - account id
  301. * - $sender_id - not used
  302. * - $extra_params - array of additional params with the following array keys:
  303. * - `error_msg` - [string] by ref, contains string with error description, can be overridden in hook processing
  304. * @hook @ref hook-account-check_login
  305. */
  306. bx_alert('account', 'check_login', $aAccountInfo['id'], false, array('error_msg' => &$sErrorMsg));
  307. return $sErrorMsg;
  308. }
  309. /**
  310. * Check encrypted password (ex., from Cookie)
  311. * @return empty string on success or error string on error
  312. */
  313. function bx_check_login($iID, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  314. {
  315. $oAccount = BxDolAccount::getInstance((int)$iID);
  316. // If no such account available
  317. if (!$oAccount) {
  318. bx_import('BxDolLanguages');
  319. return _t("_sys_txt_login_error");
  320. }
  321. $aAccountInfo = $oAccount->getInfo();
  322. // If password is incorrect
  323. if (strcmp($aAccountInfo['password'], $sPassword) !== 0) {
  324. bx_import('BxDolLanguages');
  325. return _t("_sys_txt_login_error");
  326. }
  327. // If wrong account role
  328. if (!((int)$aAccountInfo['role'] & $iRole)) {
  329. bx_import('BxDolLanguages');
  330. return _t("_sys_txt_login_invalid_role");
  331. }
  332. return '';
  333. }
  334. /**
  335. * Declare that content is require user authoriztion and display login form if user isn't logged in
  336. * @param $bStudio require webmaster authorization
  337. */
  338. function bx_require_authentication ($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  339. {
  340. $iRole = BX_DOL_ROLE_MEMBER;
  341. if ($bStudio)
  342. $iRole = BX_DOL_ROLE_ADMIN;
  343. $sID = 0;
  344. $sPassword = '';
  345. if (getParam('sys_session_auth')) {
  346. $o = BxDolSession::getInstance();
  347. $sID = $o->getUserId();
  348. $sPassword = $sID ? BxDolAccountQuery::getInstance()->getPassword($sID) : '';
  349. }
  350. else {
  351. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  352. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  353. }
  354. if (bx_check_login($sID, $sPassword, $iRole)) {
  355. bx_login_form($bStudio, $bAjaxMode, $sForceRelocate);
  356. }
  357. check_logged();
  358. }
  359. /**
  360. * Display login form and exit
  361. * @param $bStudio display login form for studio
  362. * @param $bAjaxMode login form displayed via AJAX
  363. * @param sForceRelocate forece relocate
  364. */
  365. function bx_login_form($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  366. {
  367. if ($bStudio == 1) {
  368. BxTemplStudioFunctions::getInstance()->getLoginForm();
  369. exit;
  370. }
  371. $sFormCode = BxDolService::call('system', 'login_form', array('', $sForceRelocate), 'TemplServiceLogin');
  372. if ($bAjaxMode) {
  373. echo $GLOBALS['oFunctions']->transBox('', $sFormCode, false, true);
  374. exit;
  375. }
  376. $oPage = BxDolPage::getObjectInstance('sys_login');
  377. if($oPage)
  378. $oPage->displayPage();
  379. else
  380. BxDolTemplate::getInstance()->displayPageNotFound();
  381. exit;
  382. }
  383. /** @} */