profiles.inc.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $bID = isset($_COOKIE['memberID']);
  126. $sID = $bID ? bx_process_input($_COOKIE['memberID']) : false;
  127. $bPassword = isset($_COOKIE['memberPassword']);
  128. $sPassword = $bPassword ? bx_process_input($_COOKIE['memberPassword']) : false;
  129. $bLogged = false;
  130. foreach ($aAccTypes as $iRole => $sValue) {
  131. if ($GLOBALS['logged'][$sValue] = ($sID && !bx_check_login($sID, $sPassword, $iRole))) {
  132. BxDolSession::getInstance()->start();
  133. $bLogged = true;
  134. break;
  135. }
  136. }
  137. if($bID && $bPassword && $bLogged)
  138. bx_alert('account', 'logged', getLoggedId());
  139. if(($bID || $bPassword) && !$bLogged)
  140. bx_logout(false);
  141. }
  142. /**
  143. * Check unencrypted password
  144. * @return empty string on success or error string on error
  145. */
  146. function bx_check_password($sLogin, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  147. {
  148. $oAccount = BxDolAccount::getInstance($sLogin);
  149. if (!$oAccount) {
  150. bx_import('BxDolLanguages');
  151. return _t("_sys_txt_login_error");
  152. }
  153. $aAccountInfo = $oAccount->getInfo();
  154. $sPassCheck = encryptUserPwd($sPassword, $aAccountInfo['salt']);
  155. // regenerate password using another encrypt function if necessary
  156. bx_alert('system', 'encrypt_password_after', 0, false, array(
  157. 'info' => $aAccountInfo,
  158. 'pwd' => $sPassword,
  159. 'password' => &$sPassCheck,
  160. ));
  161. if ($sErrorMsg = bx_check_login($aAccountInfo['id'], $sPassCheck, $iRole)){
  162. $iMaxLoginAttempts = getParam('sys_account_limit_incorrect_login_attempts');
  163. if ($iMaxLoginAttempts >0){
  164. $oAccountQuery = BxDolAccountQuery::getInstance();
  165. if ($aAccountInfo['login_attempts'] >= $iMaxLoginAttempts){
  166. $oAccountQuery->lockAccount($aAccountInfo['id'], 1);
  167. bx_import('BxDolLanguages');
  168. return _t("_sys_txt_login_locked", BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'));
  169. }
  170. else{
  171. $oAccountQuery->updateAttemptsCounter($aAccountInfo['id']);
  172. }
  173. }
  174. return $sErrorMsg;
  175. }
  176. // Admin can always login even if he is blocked/banned/suspended/etc
  177. if (isAdmin($aAccountInfo['id']))
  178. return '';
  179. $sErrorMsg = '';
  180. bx_alert('account', 'check_login', $aAccountInfo['id'], false, array('error_msg' => &$sErrorMsg));
  181. return $sErrorMsg;
  182. }
  183. /**
  184. * Check encrypted password (ex., from Cookie)
  185. * @return empty string on success or error string on error
  186. */
  187. function bx_check_login($iID, $sPassword, $iRole = BX_DOL_ROLE_MEMBER)
  188. {
  189. $oAccount = BxDolAccount::getInstance((int)$iID);
  190. // If no such account available
  191. if (!$oAccount) {
  192. bx_import('BxDolLanguages');
  193. return _t("_sys_txt_login_error");
  194. }
  195. $aAccountInfo = $oAccount->getInfo();
  196. // If password is incorrect
  197. if (strcmp($aAccountInfo['password'], $sPassword) !== 0) {
  198. bx_import('BxDolLanguages');
  199. return _t("_sys_txt_login_error");
  200. }
  201. // If wrong account role
  202. if (!((int)$aAccountInfo['role'] & $iRole)) {
  203. bx_import('BxDolLanguages');
  204. return _t("_sys_txt_login_invalid_role");
  205. }
  206. return '';
  207. }
  208. /**
  209. * Declare that content is require user authoriztion and display login form if user isn't logged in
  210. * @param $bStudio require webmaster authorization
  211. */
  212. function bx_require_authentication ($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  213. {
  214. $iRole = BX_DOL_ROLE_MEMBER;
  215. if ($bStudio)
  216. $iRole = BX_DOL_ROLE_ADMIN;
  217. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  218. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  219. if (bx_check_login($sID, $sPassword, $iRole)) {
  220. bx_login_form($bStudio, $bAjaxMode, $sForceRelocate);
  221. }
  222. check_logged();
  223. }
  224. /**
  225. * Display login form and exit
  226. * @param $bStudio display login form for studio
  227. * @param $bAjaxMode login form displayed via AJAX
  228. * @param sForceRelocate forece relocate
  229. */
  230. function bx_login_form($bStudio = false, $bAjaxMode = false, $sForceRelocate = '')
  231. {
  232. if ($bStudio == 1) {
  233. BxTemplStudioFunctions::getInstance()->getLoginForm();
  234. exit;
  235. }
  236. $sFormCode = BxDolService::call('system', 'login_form', array('', $sForceRelocate), 'TemplServiceLogin');
  237. if ($bAjaxMode) {
  238. echo $GLOBALS['oFunctions']->transBox('', $sFormCode, false, true);
  239. exit;
  240. }
  241. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_DEFAULT);
  242. BxDolTemplate::getInstance()->setPageHeader (getParam('site_title') . ' ' . _t("_Member Login"));
  243. BxDolTemplate::getInstance()->setPageContent ('page_main_code', DesignBoxContent(_t("_Member Login"), $sFormCode, BX_DB_PADDING_DEF));
  244. BxDolTemplate::getInstance()->getPageCode();
  245. exit;
  246. }
  247. check_logged();
  248. /** @} */