profiles.inc.php 14 KB

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