profiles.inc.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. /**
  9. * It is needed to check whether user is logged in or not
  10. */
  11. function isLogged() {
  12. return getLoggedId() != 0;
  13. }
  14. /**
  15. * It returns logged in account id
  16. */
  17. function getLoggedId() {
  18. return isset($_COOKIE['memberID']) && (!empty($GLOBALS['logged']['member']) || !empty($GLOBALS['logged']['admin'])) ? (int)$_COOKIE['memberID'] : 0;
  19. }
  20. /**
  21. * It returns logged in account password
  22. */
  23. function getLoggedPassword() {
  24. return isset($_COOKIE['memberPassword']) && ($GLOBALS['logged']['member'] || $GLOBALS['logged']['admin']) ? $_COOKIE['memberPassword'] : '';
  25. }
  26. /**
  27. * It checks if account is member.
  28. */
  29. function isMember($iId = 0) {
  30. return isRole(BX_DOL_ROLE_MEMBER, $iId);
  31. }
  32. if (!function_exists("isAdmin")) {
  33. /**
  34. * It checks if account is admin.
  35. */
  36. function isAdmin($iId = 0) {
  37. if (!$iId && isset($GLOBALS['logged']['admin']) && $GLOBALS['logged']['admin']) // easier check for currently logged in user
  38. return true;
  39. return isRole(BX_DOL_ROLE_ADMIN, $iId);
  40. }
  41. }
  42. /**
  43. * It checks account's role
  44. */
  45. function isRole($iRole, $iId = 0) {
  46. if (!(int)$iId)
  47. $iId = getLoggedId();
  48. bx_import('BxDolAccount');
  49. $oAccount = BxDolAccount::getInstance($iId);
  50. if (!$oAccount)
  51. return false;
  52. $aAccountInfo = $oAccount->getInfo();
  53. if (!$aAccountInfo)
  54. return false;
  55. if (!((int)$aAccountInfo['role'] & $iRole))
  56. return false;
  57. return true;
  58. }
  59. function isLoggedBanned($iCurUserID = 0) {
  60. $iCCurUserID = ($iCurUserID>0) ? $iCurUserID : (int)$_COOKIE['memberID'];
  61. if ($iCCurUserID) {
  62. $CheckSQL = "
  63. SELECT *
  64. FROM `sys_admin_ban_list`
  65. WHERE `ProfID`='{$iCCurUserID}'
  66. ";
  67. db_res($CheckSQL);
  68. if (db_affected_rows()>0) {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. function bx_login($iId, $bRememberMe = false) {
  75. bx_import('BxDolAccountQuery');
  76. $oAccountQuery = BxDolAccountQuery::getInstance();
  77. $sPassword = $oAccountQuery->getPassword($iId);
  78. if (!$sPassword)
  79. return false;
  80. $aUrl = parse_url(BX_DOL_URL_ROOT);
  81. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  82. $sHost = '';
  83. $iCookieTime = $bRememberMe ? time() + 24*60*60*30 : 0;
  84. setcookie("memberID", $iId, $iCookieTime, $sPath, $sHost);
  85. $_COOKIE['memberID'] = $iId;
  86. setcookie("memberPassword", $sPassword, $iCookieTime, $sPath, $sHost, false, true /* http only */);
  87. $_COOKIE['memberPassword'] = $sPassword;
  88. bx_import('BxDolSession');
  89. BxDolSession::getInstance()->setUserId($iId);
  90. $oAccountQuery->updateLoggedIn($iId);
  91. bx_alert('account', 'login', $iId);
  92. return $oAccountQuery->getInfoById($iId);
  93. }
  94. function bx_logout($bNotify = true) {
  95. if ($bNotify && isMember())
  96. bx_alert('account', 'logout', (int)$_COOKIE['memberID']);
  97. $aUrl = parse_url(BX_DOL_URL_ROOT);
  98. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  99. setcookie('memberID', '', time() - 96 * 3600, $sPath);
  100. setcookie('memberPassword', '', time() - 96 * 3600, $sPath);
  101. unset($_COOKIE['memberID']);
  102. unset($_COOKIE['memberPassword']);
  103. bx_import('BxDolSession');
  104. BxDolSession::getInstance()->destroy();
  105. }
  106. function check_logged() {
  107. $aAccTypes = array(
  108. BX_DOL_ROLE_ADMIN => 'admin',
  109. BX_DOL_ROLE_MEMBER => 'member'
  110. );
  111. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  112. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  113. $bLogged = false;
  114. foreach ($aAccTypes as $iRole => $sValue) {
  115. if ($GLOBALS['logged'][$sValue] = ($sID && !bx_check_login($sID, $sPassword, $iRole))) {
  116. $bLogged = true;
  117. break;
  118. }
  119. }
  120. if ((isset($_COOKIE['memberID']) || isset($_COOKIE['memberPassword'])) && !$bLogged)
  121. bx_logout(false);
  122. }
  123. /**
  124. * check unencrypted password
  125. * @return empty string on success or error string on error
  126. */
  127. function bx_check_password($sLogin, $sPassword, $iRole = BX_DOL_ROLE_MEMBER) {
  128. bx_import('BxDolAccount');
  129. $oAccount = BxDolAccount::getInstance($sLogin);
  130. if (!$oAccount) {
  131. bx_import('BxDolLanguages');
  132. return _t("_sys_txt_login_error");
  133. }
  134. $aAccountInfo = $oAccount->getInfo();
  135. $sPassCheck = encryptUserPwd($sPassword, $aAccountInfo['salt']);
  136. return bx_check_login($aAccountInfo['id'], $sPassCheck, $iRole);
  137. }
  138. /**
  139. * check encrypted password (ex., from Cookie)
  140. * @return empty string on success or error string on error
  141. */
  142. function bx_check_login($iID, $sPassword, $iRole = BX_DOL_ROLE_MEMBER) {
  143. bx_import('BxDolAccount');
  144. $oAccount = BxDolAccount::getInstance((int)$iID);
  145. // If no such account available
  146. if (!$oAccount) {
  147. bx_import('BxDolLanguages');
  148. return _t("_sys_txt_login_error");
  149. }
  150. $aAccountInfo = $oAccount->getInfo();
  151. // If password is incorrect
  152. if (strcmp($aAccountInfo['password'], $sPassword) != 0) {
  153. bx_import('BxDolLanguages');
  154. return _t("_sys_txt_login_error");
  155. }
  156. // If wrong account role
  157. if (!((int)$aAccountInfo['role'] & $iRole)) {
  158. bx_import('BxDolLanguages');
  159. return _t("_sys_txt_login_invalid_role");
  160. }
  161. // Admin can always login even if his ip is blocked
  162. if (isAdmin($aAccountInfo['id']))
  163. return '';
  164. // Check if IP is banned
  165. if (BxDolRequest::serviceExists('bx_antispam', 'check_login') && ($sErrorMsg = BxDolService::call('bx_antispam', 'check_login')))
  166. return $sErrorMsg;
  167. // If account is banned
  168. if (isLoggedBanned($aAccountInfo['id'])) {
  169. bx_import('BxDolLanguages');
  170. return _t('_member_banned');
  171. }
  172. return '';
  173. }
  174. function bx_require_authentication ($bStudio = false) {
  175. $iRole = BX_DOL_ROLE_MEMBER;
  176. if ($bStudio)
  177. $iRole = BX_DOL_ROLE_ADMIN;
  178. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  179. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  180. if ($sLoginError = bx_check_login($sID, $sPassword, $iRole)) {
  181. bx_login_form($bStudio);
  182. }
  183. check_logged();
  184. }
  185. function bx_login_form($bStudio = false, $bAjaxMode = false) {
  186. if ($bStudio == 1) {
  187. bx_import("BxTemplStudioFunctions");
  188. BxTemplStudioFunctions::getInstance()->getLoginForm();
  189. exit;
  190. }
  191. $sFormCode = BxDolService::call('system', 'login_form', array(), 'TemplServiceLogin');
  192. if ($bAjaxMode) {
  193. echo $GLOBALS['oFunctions']->transBox('', $sFormCode, false, true);
  194. exit;
  195. }
  196. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_DEFAULT);
  197. BxDolTemplate::getInstance()->setPageHeader (getParam('site_title') . ' ' . _t("_Member Login"));
  198. BxDolTemplate::getInstance()->setPageContent ('page_main_code', DesignBoxContent(_t("_Member Login"), $sFormCode, BX_DB_PADDING_DEF));
  199. BxDolTemplate::getInstance()->getPageCode();
  200. exit;
  201. }
  202. /**
  203. * get corrently logged in profile id
  204. */
  205. function bx_get_logged_profile_id () {
  206. bx_import('BxDolProfile');
  207. $o = BxDolProfile::getInstance();
  208. return $o ? $o->id() : false;
  209. }
  210. check_logged();