profiles.inc.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 UnaCore UNA Core
  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. return isset($_COOKIE['memberID']) && (!empty($GLOBALS['logged']['member']) || !empty($GLOBALS['logged']['admin'])) ? (int)$_COOKIE['memberID'] : 0;
  30. }
  31. /**
  32. * @return logged in account password
  33. */
  34. function getLoggedPassword()
  35. {
  36. return isset($_COOKIE['memberPassword']) && ($GLOBALS['logged']['member'] || $GLOBALS['logged']['admin']) ? $_COOKIE['memberPassword'] : '';
  37. }
  38. /**
  39. * It checks if account role is member.
  40. */
  41. function isMember($iId = 0)
  42. {
  43. return isRole(BX_DOL_ROLE_MEMBER, $iId);
  44. }
  45. if (!function_exists("isAdmin")) {
  46. /**
  47. * @return true if account is admin
  48. */
  49. function isAdmin($iId = 0)
  50. {
  51. if (!$iId && isset($GLOBALS['logged']['admin']) && $GLOBALS['logged']['admin']) // easier check for currently logged in user
  52. return true;
  53. return isRole(BX_DOL_ROLE_ADMIN, $iId);
  54. }
  55. }
  56. /**
  57. * Check user role
  58. * @param $iRole role to check user for
  59. * @param $iId optional account id, if it isn't specified then curently logged in account is used
  60. * @return true if user is in the provided role
  61. */
  62. function isRole($iRole, $iId = 0)
  63. {
  64. if (!(int)$iId)
  65. $iId = getLoggedId();
  66. $oAccount = BxDolAccount::getInstance($iId);
  67. if (!$oAccount)
  68. return false;
  69. $aAccountInfo = $oAccount->getInfo();
  70. if (!$aAccountInfo)
  71. return false;
  72. if (!((int)$aAccountInfo['role'] & $iRole))
  73. return false;
  74. return true;
  75. }
  76. /**
  77. * Login user by setting necessary cookies
  78. * @param $iId account id
  79. * @param $bRememberMe remember session
  80. * @return false if id isn't correct or array of user info if user was logged in
  81. */
  82. function bx_login($iId, $bRememberMe = false)
  83. {
  84. $oAccountQuery = BxDolAccountQuery::getInstance();
  85. $sPassword = $oAccountQuery->getPassword($iId);
  86. if (!$sPassword)
  87. return false;
  88. $iCookieTime = $bRememberMe ? time() + 60 * getParam('sys_session_lifetime_in_min') : 0;
  89. bx_setcookie("memberID", $iId, $iCookieTime, 'auto');
  90. $_COOKIE['memberID'] = $iId;
  91. bx_setcookie("memberPassword", $sPassword, $iCookieTime, 'auto', '', 'auto', true /* http only */);
  92. $_COOKIE['memberPassword'] = $sPassword;
  93. BxDolSession::getInstance()->setUserId($iId);
  94. $oAccountQuery->updateLoggedIn($iId);
  95. bx_alert('account', 'login', $iId);
  96. bx_audit(
  97. $iId,
  98. 'bx_accounts',
  99. '_sys_audit_action_account_login',
  100. array('content_title' => '', 'data' => ['display_info' => ['User agent' => $_SERVER["HTTP_USER_AGENT"]]])
  101. );
  102. return $oAccountQuery->getInfoById($iId);
  103. }
  104. /**
  105. * Logout user by removing cookies
  106. */
  107. function bx_logout($bNotify = true)
  108. {
  109. if ($bNotify && isMember())
  110. bx_alert('account', 'logout', (int)$_COOKIE['memberID']);
  111. bx_audit(
  112. $_COOKIE['memberID'],
  113. 'bx_accounts',
  114. '_sys_audit_action_account_logout',
  115. array('content_title' => '', 'data' => ['display_info' => ['User agent' => $_SERVER["HTTP_USER_AGENT"]]])
  116. );
  117. bx_setcookie('memberID', '', time() - 96 * 3600);
  118. bx_setcookie('memberPassword', '', time() - 96 * 3600, 'auto', '', 'auto', true /* http only */);
  119. unset($_COOKIE['memberID']);
  120. unset($_COOKIE['memberPassword']);
  121. BxDolSession::getInstance()->destroy();
  122. }
  123. /**
  124. * Check if user is logged in (necessary cookies are present) and set some global variables
  125. */
  126. function check_logged()
  127. {
  128. $aAccTypes = array(
  129. BX_DOL_ROLE_ADMIN => 'admin',
  130. BX_DOL_ROLE_MEMBER => 'member'
  131. );
  132. $bID = isset($_COOKIE['memberID']);
  133. $sID = $bID ? bx_process_input($_COOKIE['memberID']) : false;
  134. $bPassword = isset($_COOKIE['memberPassword']);
  135. $sPassword = $bPassword ? bx_process_input($_COOKIE['memberPassword']) : false;
  136. $bLogged = false;
  137. foreach ($aAccTypes as $iRole => $sValue) {
  138. if ($GLOBALS['logged'][$sValue] = ($sID && !bx_check_login($sID, $sPassword, $iRole))) {
  139. BxDolSession::getInstance();
  140. $bLogged = true;
  141. break;
  142. }
  143. }
  144. if($bID && $bPassword && $bLogged) {
  145. header("Cache-Control: no-cache, no-store, must-revalidate");
  146. bx_alert('account', 'logged', getLoggedId());
  147. }
  148. if(($bID || $bPassword) && !$bLogged)
  149. bx_logout(false);
  150. }
  151. /**
  152. * Check unencrypted password
  153. * @return empty string on success or error string on error
  154. */
  155. function bx_check_password($sLogin, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  156. {
  157. $oAccount = BxDolAccount::getInstance($sLogin);
  158. if (!$oAccount) {
  159. bx_import('BxDolLanguages');
  160. return _t("_sys_txt_login_error");
  161. }
  162. if ($oAccount->isLocked()){
  163. bx_import('BxDolLanguages');
  164. return _t("_sys_txt_login_locked", BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  165. }
  166. $aAccountInfo = $oAccount->getInfo();
  167. $sPassCheck = encryptUserPwd($sPassword, $aAccountInfo['salt']);
  168. // regenerate password using another encrypt function if necessary
  169. bx_alert('system', 'encrypt_password_after', 0, false, array(
  170. 'info' => $aAccountInfo,
  171. 'pwd' => $sPassword,
  172. 'password' => &$sPassCheck,
  173. ));
  174. if ($sErrorMsg = bx_check_login($aAccountInfo['id'], $sPassCheck, $iRole)){
  175. bx_audit(
  176. $aAccountInfo['id'],
  177. 'bx_accounts',
  178. '_sys_audit_action_account_failed_login_attempt',
  179. array('content_title' => '', 'data' => ['display_info' => ['User agent' => $_SERVER["HTTP_USER_AGENT"]]]),
  180. $aAccountInfo['profile_id']
  181. );
  182. $iMaxLoginAttempts = getParam('sys_account_limit_incorrect_login_attempts');
  183. if ($iMaxLoginAttempts >0){
  184. $oAccountQuery = BxDolAccountQuery::getInstance();
  185. if ($aAccountInfo['login_attempts'] >= $iMaxLoginAttempts){
  186. $oAccountQuery->lockAccount($aAccountInfo['id'], 1);
  187. bx_import('BxDolLanguages');
  188. return _t("_sys_txt_login_locked", BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  189. }
  190. else{
  191. $oAccountQuery->updateAttemptsCounter($aAccountInfo['id']);
  192. }
  193. }
  194. return $sErrorMsg;
  195. }
  196. // Admin can always login even if he is blocked/banned/suspended/etc
  197. if (isAdmin($aAccountInfo['id']))
  198. return '';
  199. $sErrorMsg = '';
  200. bx_alert('account', 'check_login', $aAccountInfo['id'], false, array('error_msg' => &$sErrorMsg));
  201. return $sErrorMsg;
  202. }
  203. /**
  204. * Check encrypted password (ex., from Cookie)
  205. * @return empty string on success or error string on error
  206. */
  207. function bx_check_login($iID, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  208. {
  209. $oAccount = BxDolAccount::getInstance((int)$iID);
  210. // If no such account available
  211. if (!$oAccount) {
  212. bx_import('BxDolLanguages');
  213. return _t("_sys_txt_login_error");
  214. }
  215. $aAccountInfo = $oAccount->getInfo();
  216. // If password is incorrect
  217. if (strcmp($aAccountInfo['password'], $sPassword) !== 0) {
  218. bx_import('BxDolLanguages');
  219. return _t("_sys_txt_login_error");
  220. }
  221. // If wrong account role
  222. if (!((int)$aAccountInfo['role'] & $iRole)) {
  223. bx_import('BxDolLanguages');
  224. return _t("_sys_txt_login_invalid_role");
  225. }
  226. return '';
  227. }
  228. /**
  229. * Declare that content is require user authoriztion and display login form if user isn't logged in
  230. * @param $bStudio require webmaster authorization
  231. */
  232. function bx_require_authentication ($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  233. {
  234. $iRole = BX_DOL_ROLE_MEMBER;
  235. if ($bStudio)
  236. $iRole = BX_DOL_ROLE_ADMIN;
  237. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  238. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  239. if (bx_check_login($sID, $sPassword, $iRole)) {
  240. bx_login_form($bStudio, $bAjaxMode, $sForceRelocate);
  241. }
  242. check_logged();
  243. }
  244. /**
  245. * Display login form and exit
  246. * @param $bStudio display login form for studio
  247. * @param $bAjaxMode login form displayed via AJAX
  248. * @param sForceRelocate forece relocate
  249. */
  250. function bx_login_form($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  251. {
  252. if ($bStudio == 1) {
  253. BxTemplStudioFunctions::getInstance()->getLoginForm();
  254. exit;
  255. }
  256. $sFormCode = BxDolService::call('system', 'login_form', array('', $sForceRelocate), 'TemplServiceLogin');
  257. if ($bAjaxMode) {
  258. echo $GLOBALS['oFunctions']->transBox('', $sFormCode, false, true);
  259. exit;
  260. }
  261. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_DEFAULT);
  262. BxDolTemplate::getInstance()->setPageHeader (getParam('site_title') . ' ' . _t("_Member Login"));
  263. BxDolTemplate::getInstance()->setPageContent ('page_main_code', DesignBoxContent(_t("_Member Login"), $sFormCode, BX_DB_PADDING_DEF));
  264. BxDolTemplate::getInstance()->getPageCode();
  265. exit;
  266. }
  267. check_logged();
  268. /** @} */