profiles.inc.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. $aUrl = parse_url(BX_DOL_URL_ROOT);
  89. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  90. $sHost = '';
  91. $iCookieTime = $bRememberMe ? time() + 24*60*60*30 : 0;
  92. setcookie("memberID", $iId, $iCookieTime, $sPath, $sHost);
  93. $_COOKIE['memberID'] = $iId;
  94. setcookie("memberPassword", $sPassword, $iCookieTime, $sPath, $sHost, false, true /* http only */);
  95. $_COOKIE['memberPassword'] = $sPassword;
  96. BxDolSession::getInstance()->setUserId($iId);
  97. $oAccountQuery->updateLoggedIn($iId);
  98. bx_alert('account', 'login', $iId);
  99. return $oAccountQuery->getInfoById($iId);
  100. }
  101. /**
  102. * Logout user by removing cookies
  103. */
  104. function bx_logout($bNotify = true)
  105. {
  106. if ($bNotify && isMember())
  107. bx_alert('account', 'logout', (int)$_COOKIE['memberID']);
  108. $aUrl = parse_url(BX_DOL_URL_ROOT);
  109. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  110. setcookie('memberID', '', time() - 96 * 3600, $sPath);
  111. setcookie('memberPassword', '', time() - 96 * 3600, $sPath);
  112. unset($_COOKIE['memberID']);
  113. unset($_COOKIE['memberPassword']);
  114. BxDolSession::getInstance()->destroy();
  115. }
  116. /**
  117. * Check if user is logged in (necessary cookies are present) and set some global variables
  118. */
  119. function check_logged()
  120. {
  121. $aAccTypes = array(
  122. BX_DOL_ROLE_ADMIN => 'admin',
  123. BX_DOL_ROLE_MEMBER => 'member'
  124. );
  125. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  126. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  127. $bLogged = false;
  128. foreach ($aAccTypes as $iRole => $sValue) {
  129. if ($GLOBALS['logged'][$sValue] = ($sID && !bx_check_login($sID, $sPassword, $iRole))) {
  130. $bLogged = true;
  131. break;
  132. }
  133. }
  134. if ((isset($_COOKIE['memberID']) || isset($_COOKIE['memberPassword'])) && !$bLogged)
  135. bx_logout(false);
  136. }
  137. /**
  138. * Check unencrypted password
  139. * @return empty string on success or error string on error
  140. */
  141. function bx_check_password($sLogin, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  142. {
  143. $oAccount = BxDolAccount::getInstance($sLogin);
  144. if (!$oAccount) {
  145. bx_import('BxDolLanguages');
  146. return _t("_sys_txt_login_error");
  147. }
  148. $aAccountInfo = $oAccount->getInfo();
  149. $sPassCheck = encryptUserPwd($sPassword, $aAccountInfo['salt']);
  150. // regenerate password using another encrypt function if necessary
  151. bx_alert('system', 'encrypt_password_after', 0, false, array(
  152. 'info' => $aAccountInfo,
  153. 'pwd' => $sPassword,
  154. 'password' => &$sPassCheck,
  155. ));
  156. if ($sErrorMsg = bx_check_login($aAccountInfo['id'], $sPassCheck, $iRole)){
  157. $iMaxLoginAttempts = getParam('sys_account_limit_incorrect_login_attempts');
  158. if ($iMaxLoginAttempts >0){
  159. $oAccountQuery = BxDolAccountQuery::getInstance();
  160. if ($aAccountInfo['login_attempts'] >= $iMaxLoginAttempts){
  161. $oAccountQuery->lockAccount($aAccountInfo['id'], 1);
  162. bx_import('BxDolLanguages');
  163. return _t("_sys_txt_login_locked", BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  164. }
  165. else{
  166. $oAccountQuery->updateAttemptsCounter($aAccountInfo['id']);
  167. }
  168. }
  169. return $sErrorMsg;
  170. }
  171. // Admin can always login even if he is blocked/banned/suspended/etc
  172. if (isAdmin($aAccountInfo['id']))
  173. return '';
  174. $sErrorMsg = '';
  175. bx_alert('account', 'check_login', $aAccountInfo['id'], false, array('error_msg' => &$sErrorMsg));
  176. return $sErrorMsg;
  177. }
  178. /**
  179. * Check encrypted password (ex., from Cookie)
  180. * @return empty string on success or error string on error
  181. */
  182. function bx_check_login($iID, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  183. {
  184. $oAccount = BxDolAccount::getInstance((int)$iID);
  185. // If no such account available
  186. if (!$oAccount) {
  187. bx_import('BxDolLanguages');
  188. return _t("_sys_txt_login_error");
  189. }
  190. $aAccountInfo = $oAccount->getInfo();
  191. // If password is incorrect
  192. if (strcmp($aAccountInfo['password'], $sPassword) !== 0) {
  193. bx_import('BxDolLanguages');
  194. return _t("_sys_txt_login_error");
  195. }
  196. // If wrong account role
  197. if (!((int)$aAccountInfo['role'] & $iRole)) {
  198. bx_import('BxDolLanguages');
  199. return _t("_sys_txt_login_invalid_role");
  200. }
  201. return '';
  202. }
  203. /**
  204. * Declare that content is require user authoriztion and display login form if user isn't logged in
  205. * @param $bStudio require webmaster authorization
  206. */
  207. function bx_require_authentication ($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  208. {
  209. $iRole = BX_DOL_ROLE_MEMBER;
  210. if ($bStudio)
  211. $iRole = BX_DOL_ROLE_ADMIN;
  212. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  213. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  214. if (bx_check_login($sID, $sPassword, $iRole)) {
  215. bx_login_form($bStudio, $bAjaxMode, $sForceRelocate);
  216. }
  217. check_logged();
  218. }
  219. /**
  220. * Display login form and exit
  221. * @param $bStudio display login form for studio
  222. * @param $bAjaxMode login form displayed via AJAX
  223. * @param sForceRelocate forece relocate
  224. */
  225. function bx_login_form($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  226. {
  227. if ($bStudio == 1) {
  228. BxTemplStudioFunctions::getInstance()->getLoginForm();
  229. exit;
  230. }
  231. $sFormCode = BxDolService::call('system', 'login_form', array('', $sForceRelocate), 'TemplServiceLogin');
  232. if ($bAjaxMode) {
  233. echo $GLOBALS['oFunctions']->transBox('', $sFormCode, false, true);
  234. exit;
  235. }
  236. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_DEFAULT);
  237. BxDolTemplate::getInstance()->setPageHeader (getParam('site_title') . ' ' . _t("_Member Login"));
  238. BxDolTemplate::getInstance()->setPageContent ('page_main_code', DesignBoxContent(_t("_Member Login"), $sFormCode, BX_DB_PADDING_DEF));
  239. BxDolTemplate::getInstance()->getPageCode();
  240. exit;
  241. }
  242. check_logged();
  243. /** @} */