profiles.inc.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if ($sErrorMsg = bx_check_login($aAccountInfo['id'], $sPassCheck, $iRole))
  137. return $sErrorMsg;
  138. // Admin can always login even if his ip is blocked
  139. if (isAdmin($aAccountInfo['id']))
  140. return '';
  141. // If account is banned
  142. if (isLoggedBanned($aAccountInfo['id'])) {
  143. bx_import('BxDolLanguages');
  144. return _t('_member_banned');
  145. }
  146. $sErrorMsg = '';
  147. bx_alert('account', 'check_login', $aAccountInfo['id'], false, array('error_msg' => &$sErrorMsg));
  148. return $sErrorMsg;
  149. }
  150. /**
  151. * check encrypted password (ex., from Cookie)
  152. * @return empty string on success or error string on error
  153. */
  154. function bx_check_login($iID, $sPassword, $iRole = BX_DOL_ROLE_MEMBER) {
  155. bx_import('BxDolAccount');
  156. $oAccount = BxDolAccount::getInstance((int)$iID);
  157. // If no such account available
  158. if (!$oAccount) {
  159. bx_import('BxDolLanguages');
  160. return _t("_sys_txt_login_error");
  161. }
  162. $aAccountInfo = $oAccount->getInfo();
  163. // If password is incorrect
  164. if (strcmp($aAccountInfo['password'], $sPassword) != 0) {
  165. bx_import('BxDolLanguages');
  166. return _t("_sys_txt_login_error");
  167. }
  168. // If wrong account role
  169. if (!((int)$aAccountInfo['role'] & $iRole)) {
  170. bx_import('BxDolLanguages');
  171. return _t("_sys_txt_login_invalid_role");
  172. }
  173. return '';
  174. }
  175. function bx_require_authentication ($bStudio = false) {
  176. $iRole = BX_DOL_ROLE_MEMBER;
  177. if ($bStudio)
  178. $iRole = BX_DOL_ROLE_ADMIN;
  179. $sID = isset($_COOKIE['memberID']) ? bx_process_input($_COOKIE['memberID']) : false;
  180. $sPassword = isset($_COOKIE['memberPassword']) ? bx_process_input($_COOKIE['memberPassword']) : false;
  181. if ($sLoginError = bx_check_login($sID, $sPassword, $iRole)) {
  182. bx_login_form($bStudio);
  183. }
  184. check_logged();
  185. }
  186. function bx_login_form($bStudio = false, $bAjaxMode = false) {
  187. if ($bStudio == 1) {
  188. bx_import("BxTemplStudioFunctions");
  189. BxTemplStudioFunctions::getInstance()->getLoginForm();
  190. exit;
  191. }
  192. $sFormCode = BxDolService::call('system', 'login_form', array(), 'TemplServiceLogin');
  193. if ($bAjaxMode) {
  194. echo $GLOBALS['oFunctions']->transBox('', $sFormCode, false, true);
  195. exit;
  196. }
  197. BxDolTemplate::getInstance()->setPageNameIndex (BX_PAGE_DEFAULT);
  198. BxDolTemplate::getInstance()->setPageHeader (getParam('site_title') . ' ' . _t("_Member Login"));
  199. BxDolTemplate::getInstance()->setPageContent ('page_main_code', DesignBoxContent(_t("_Member Login"), $sFormCode, BX_DB_PADDING_DEF));
  200. BxDolTemplate::getInstance()->getPageCode();
  201. exit;
  202. }
  203. /**
  204. * get corrently logged in profile id
  205. */
  206. function bx_get_logged_profile_id () {
  207. bx_import('BxDolProfile');
  208. $o = BxDolProfile::getInstance();
  209. return $o ? $o->id() : false;
  210. }
  211. check_logged();