0 ) { return false; } else { return true; } } # -------------------- # Return the current user login cookie string, # if no user is logged in and anonymous login is enabled, returns cookie for anonymous user # otherwise returns '' (an empty string) function auth_get_current_user_cookie() { global $g_script_login_cookie, $g_cache_anonymous_user_cookie_string; $t_cookie_name = config_get( 'string_cookie' ); $t_cookie = gpc_get_cookie( $t_cookie_name, '' ); # if cookie not found, and anonymous login enabled, use cookie of anonymous account. if ( is_blank( $t_cookie ) ) { if ( $g_script_login_cookie !== null ) { return $g_script_login_cookie; } else { if ( ON == config_get( 'allow_anonymous_login' ) ) { if ( $g_cache_anonymous_user_cookie_string == null ) { if ( function_exists( 'db_is_connected' ) && db_is_connected() ) { # get anonymous information if database is available $query = sprintf('SELECT id, cookie_string FROM %s WHERE username = "%s"', config_get( 'mantis_user_table' ), config_get( 'anonymous_account' ) ); $result = db_query( $query ); if ( 1 == db_num_rows( $result ) ) { $row = db_fetch_array( $result ); $t_cookie = $row['cookie_string']; $g_cache_anonymous_user_cookie_string = $t_cookie; $g_cache_current_user_id = $row['id']; } } } else { $t_cookie = $g_cache_anonymous_user_cookie_string; } } } } return $t_cookie; } #=================================== # Data Access #=================================== ######################################### # is cookie valid? function auth_is_cookie_valid( $p_cookie_string ) { global $g_cache_current_user_id; # fail if DB isn't accessible if ( !db_is_connected() ) { return false; } # fail if cookie is blank if ( '' === $p_cookie_string ) { return false; } # succeeed if user has already been authenticated if ( null !== $g_cache_current_user_id ) { return true; } # look up cookie in the database to see if it is valid $t_user_table = config_get( 'mantis_user_table' ); $c_cookie_string = db_prepare_string( $p_cookie_string ); $query = "SELECT id FROM $t_user_table WHERE cookie_string='$c_cookie_string'"; $result = db_query( $query ); # return true if a matching cookie was found return ( 1 == db_num_rows( $result ) ); } ######################################### # SECURITY NOTE: cache globals are initialized here to prevent them # being spoofed if register_globals is turned on # $g_cache_current_user_id = null; function auth_get_current_user_id() { global $g_cache_current_user_id; if ( null !== $g_cache_current_user_id ) { return $g_cache_current_user_id; } $t_user_table = config_get( 'mantis_user_table' ); $t_cookie_string = auth_get_current_user_cookie(); # @@@ error with an error saying they aren't logged in? # Or redirect to the login page maybe? $c_cookie_string = db_prepare_string( $t_cookie_string ); $query = "SELECT id FROM $t_user_table WHERE cookie_string='$c_cookie_string'"; $result = db_query( $query ); # The cookie was invalid. Clear the cookie (to allow people to log in again) # and give them an Access Denied message. if ( db_num_rows( $result ) < 1 ) { auth_clear_cookies(); access_denied(); # never returns return false; } $t_user_id = (int)db_result( $result ); $g_cache_current_user_id = $t_user_id; return $t_user_id; } #=================================== # HTTP Auth #=================================== function auth_http_prompt() { header( "HTTP/1.0 401 Authorization Required" ); header( "WWW-Authenticate: Basic realm=\"" . lang_get( 'http_auth_realm' ) . "\"" ); header( 'status: 401 Unauthorized' ); echo '
'.error_string(ERROR_ACCESS_DENIED).'
'; print_bracket_link( 'main_page.php', lang_get( 'proceed' ) ); echo '