| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'gpc_api.php' ); |
| |
| |
| |
| $g_script_login_cookie = null; |
| $g_cache_anonymous_user_cookie_string = null; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_ensure_user_authenticated( $p_return_page = '' ) { |
| |
| if ( auth_is_user_authenticated() ) { |
| |
| |
| if ( OFF == current_user_get_field( 'enabled' ) ) { |
| print_header_redirect( 'logout_page.php' ); |
| } |
| } else { |
| if ( is_blank( $p_return_page ) ) { |
| if (!isset($_SERVER['REQUEST_URI'])) { |
| $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']; |
| } |
| $p_return_page = $_SERVER['REQUEST_URI']; |
| } |
| $p_return_page = string_url( $p_return_page ); |
| print_header_redirect( 'login_page.php?return=' . $p_return_page ); |
| } |
| } |
| |
| |
| |
| |
| function auth_is_user_authenticated() { |
| return ( auth_is_cookie_valid( auth_get_current_user_cookie() ) ); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_attempt_login( $p_username, $p_password, $p_perm_login=false ) { |
| $t_user_id = user_get_id_by_name( $p_username ); |
| |
| $t_login_method = config_get( 'login_method' ); |
| |
| if ( false === $t_user_id ) { |
| if ( BASIC_AUTH == $t_login_method ) { |
| |
| $t_cookie_string = user_create( $p_username, $p_password ); |
| |
| if ( false === $t_cookie_string ) { |
| |
| return false; |
| } |
| |
| |
| $t_user_id = user_get_id_by_name( $p_username ); |
| |
| if ( false === $t_user_id ) { |
| |
| |
| |
| |
| return false; |
| } |
| } else { |
| return false; |
| } |
| } |
| |
| |
| if ( !user_is_enabled( $t_user_id ) ) { |
| return false; |
| } |
| |
| |
| if( !user_is_login_request_allowed( $t_user_id ) ) { |
| return false; |
| } |
| |
| $t_anon_account = config_get( 'anonymous_account' ); |
| $t_anon_allowed = config_get( 'allow_anonymous_login' ); |
| |
| |
| if ( !( ( ON == $t_anon_allowed ) && ( $t_anon_account == $p_username) ) ) { |
| |
| |
| if ( !auth_does_password_match( $t_user_id, $p_password ) ) { |
| user_increment_failed_login_count( $t_user_id ); |
| return false; |
| } |
| } |
| |
| |
| |
| |
| user_increment_login_count( $t_user_id ); |
| |
| user_reset_failed_login_count_to_zero( $t_user_id ); |
| user_reset_lost_password_in_progress_count_to_zero( $t_user_id ); |
| |
| |
| auth_set_cookies( $t_user_id, $p_perm_login ); |
| auth_set_tokens( $t_user_id ); |
| |
| return true; |
| } |
| |
| |
| |
| function auth_attempt_script_login( $p_username, $p_password = null ) { |
| global $g_script_login_cookie, $g_cache_current_user_id; |
| |
| $t_user_id = user_get_id_by_name( $p_username ); |
| |
| $t_user = user_get_row( $t_user_id ); |
| |
| |
| if ( OFF == $t_user['enabled'] ) { |
| return false; |
| } |
| |
| |
| if ( null !== $p_password ) { |
| if ( !auth_does_password_match( $t_user_id, $p_password ) ) { |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $g_script_login_cookie = $t_user['cookie_string']; |
| |
| |
| $g_cache_current_user_id = $t_user_id; |
| |
| return true; |
| } |
| |
| |
| |
| |
| function auth_logout() { |
| global $g_cache_current_user_id; |
| |
| |
| $g_cache_current_user_id = null; |
| |
| |
| if (auth_clear_cookies()) { |
| helper_clear_pref_cookies(); |
| } |
| return true; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_does_password_match( $p_user_id, $p_test_password ) { |
| $t_configured_login_method = config_get( 'login_method' ); |
| |
| if ( LDAP == $t_configured_login_method ) { |
| return ldap_authenticate( $p_user_id, $p_test_password ); |
| } |
| |
| $t_password = user_get_field( $p_user_id, 'password' ); |
| $t_login_methods = Array(MD5, CRYPT, PLAIN); |
| foreach ( $t_login_methods as $t_login_method ) { |
| |
| |
| if ( auth_process_plain_password( $p_test_password, $t_password, $t_login_method ) == $t_password ) { |
| |
| |
| |
| if ( $t_configured_login_method != PLAIN && $t_login_method == PLAIN ) { |
| continue; |
| } |
| |
| |
| |
| if ( ( $t_login_method != $t_configured_login_method ) || |
| ( ( CRYPT == $t_configured_login_method ) && substr( $t_password, 0, 2 ) == substr( $p_test_password, 0, 2 ) ) ) { |
| user_set_password( $p_user_id, $p_test_password, true ); |
| } |
| |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_process_plain_password( $p_password, $p_salt=null, $p_method=null ) { |
| $t_login_method = config_get( 'login_method' ); |
| if ( $p_method !== null ) { |
| $t_login_method = $p_method; |
| } |
| |
| switch ( $t_login_method ) { |
| case CRYPT: |
| |
| |
| $t_processed_password = crypt( $p_password, $p_salt ); |
| break; |
| case MD5: |
| $t_processed_password = md5( $p_password ); |
| break; |
| case BASIC_AUTH: |
| case PLAIN: |
| default: |
| $t_processed_password = $p_password; |
| break; |
| } |
| |
| |
| return substr( $t_processed_password, 0, 32 ); |
| } |
| |
| |
| |
| |
| function auth_generate_random_password( $p_email ) { |
| $t_val = mt_rand( 0, mt_getrandmax() ) + mt_rand( 0, mt_getrandmax() ); |
| $t_val = md5( $t_val ); |
| |
| return substr( $t_val, 0, 12 ); |
| } |
| |
| |
| |
| function auth_generate_confirm_hash( $p_user_id ) { |
| $t_confirm_hash_generator = config_get( 'password_confirm_hash_magic_string' ); |
| $t_password = user_get_field( $p_user_id, 'password' ); |
| $t_last_visit = user_get_field( $p_user_id, 'last_visit' ); |
| |
| $t_confirm_hash = md5( $t_confirm_hash_generator . $t_password . $t_last_visit ); |
| |
| return $t_confirm_hash; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_set_cookies( $p_user_id, $p_perm_login=false ) { |
| $t_cookie_string = user_get_field( $p_user_id, 'cookie_string' ); |
| |
| $t_cookie_name = config_get( 'string_cookie' ); |
| |
| if ( $p_perm_login ) { |
| |
| gpc_set_cookie( $t_cookie_name, $t_cookie_string, true ); |
| } else { |
| |
| gpc_set_cookie( $t_cookie_name, $t_cookie_string, false ); |
| } |
| } |
| |
| |
| |
| function auth_clear_cookies() { |
| global $g_script_login_cookie; |
| |
| $t_cookies_cleared = false; |
| |
| |
| if ($g_script_login_cookie == null) { |
| $t_cookie_name = config_get( 'string_cookie' ); |
| $t_cookie_path = config_get( 'cookie_path' ); |
| |
| gpc_clear_cookie( $t_cookie_name, $t_cookie_path ); |
| $t_cookies_cleared = true; |
| } else { |
| $g_script_login_cookie = null; |
| } |
| return $t_cookies_cleared; |
| } |
| |
| |
| |
| |
| |
| function auth_generate_cookie_string() { |
| $t_val = mt_rand( 0, mt_getrandmax() ) + mt_rand( 0, mt_getrandmax() ); |
| $t_val = md5( $t_val ) . md5( time() ); |
| |
| return substr( $t_val, 0, 64 ); |
| } |
| |
| |
| |
| |
| function auth_generate_unique_cookie_string() { |
| do { |
| $t_cookie_string = auth_generate_cookie_string(); |
| } while ( !auth_is_cookie_string_unique( $t_cookie_string ) ); |
| |
| return $t_cookie_string; |
| } |
| |
| |
| |
| function auth_is_cookie_string_unique( $p_cookie_string ) { |
| $t_user_table = config_get( 'mantis_user_table' ); |
| |
| $c_cookie_string = db_prepare_string( $p_cookie_string ); |
| |
| $query = "SELECT COUNT(*) |
| FROM $t_user_table |
| WHERE cookie_string='$c_cookie_string'"; |
| $result = db_query( $query ); |
| $t_count = db_result( $result ); |
| |
| if ( $t_count > 0 ) { |
| return false; |
| } else { |
| return true; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_get_current_user_cookie() { |
| global $g_script_login_cookie, $g_cache_anonymous_user_cookie_string; |
| |
| |
| if ( $g_script_login_cookie !== null ) { |
| return $g_script_login_cookie; |
| } |
| |
| |
| $t_cookie_name = config_get( 'string_cookie' ); |
| $t_cookie = gpc_get_cookie( $t_cookie_name, '' ); |
| |
| |
| if ( is_blank( $t_cookie ) ) { |
| if ( ON == config_get( 'allow_anonymous_login' ) ) { |
| if ( $g_cache_anonymous_user_cookie_string === null ) { |
| if ( function_exists( 'db_is_connected' ) && db_is_connected() ) { |
| |
| $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; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_set_tokens( $p_user_id ) { |
| $t_auth_token = token_get( TOKEN_AUTHENTICATED, $p_user_id ); |
| if ( null == $t_auth_token ) { |
| token_set( TOKEN_AUTHENTICATED, true, TOKEN_EXPIRY_AUTHENTICATED, $p_user_id ); |
| } else { |
| token_touch( $t_auth_token['id'], TOKEN_EXPIRY_AUTHENTICATED ); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| function auth_reauthenticate() { |
| if ( BASIC_AUTH == config_get( 'login_method' ) || |
| HTTP_AUTH == config_get( 'login_method' ) ) { |
| return true; |
| } |
| |
| $t_auth_token = token_get( TOKEN_AUTHENTICATED ); |
| if ( null != $t_auth_token ) { |
| token_touch( $t_auth_token['id'], TOKEN_EXPIRY_AUTHENTICATED ); |
| return true; |
| } else { |
| $t_anon_account = config_get( 'anonymous_account' ); |
| $t_anon_allowed = config_get( 'allow_anonymous_login' ); |
| |
| $t_user_id = auth_get_current_user_id(); |
| $t_username = user_get_field( $t_user_id, 'username' ); |
| |
| |
| if ( ON == $t_anon_allowed && $t_anon_account == $t_username ) { |
| return true; |
| } |
| |
| return auth_reauthenticate_page( $t_user_id, $t_username ); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| function auth_reauthenticate_page( $p_user_id, $p_username ) { |
| $t_error = false; |
| |
| if ( true == gpc_get_bool( '_authenticate' ) ) { |
| $f_password = gpc_get_string( 'password', '' ); |
| |
| if ( auth_attempt_login( $p_username, $f_password ) ) { |
| auth_set_tokens( $p_user_id ); |
| return true; |
| } else { |
| $t_error = true; |
| } |
| } |
| |
| html_page_top1(); |
| html_page_top2(); |
| |
| ?> |
| <div id="message" class="red"> |
| <?php |
| echo '<p>' . lang_get( 'reauthenticate_message' ) . '</p>'; |
| if ( $t_error != false ) { |
| echo '<p>' . lang_get( 'login_error' ) . '</p>'; |
| } |
| ?> |
| </div> |
| |
| <div align="center"> |
| <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> |
| |
| <?php |
| print_hidden_inputs( gpc_strip_slashes( $_POST ) ); |
| print_hidden_inputs( gpc_strip_slashes( $_GET ) ); |
| ?> |
| |
| <input type="hidden" name="_authenticate" value="1" /> |
| |
| <table class="width50 center"> |
| <tr class="title"> |
| <td class="form-title" colspan="2"><?php echo lang_get( 'reauthenticate_title' ); ?></td> |
| </tr> |
| |
| <tr class="row-1"> |
| <td class="category"><?php echo lang_get( 'username' ); ?></td> |
| <td><input type="text" disabled="disabled" size="32" maxlength="32" value="<?php echo $p_username; ?>" /></td> |
| </tr> |
| |
| <tr class="row-2"> |
| <td class="category"><?php echo lang_get( 'password' ); ?></td> |
| <td><input type="password" name="password" size="16" maxlength="32" /></td> |
| </tr> |
| |
| <tr> |
| <td class="center" colspan="2"><input type="submit" class="button" value="<?php echo lang_get( 'login_button' ); ?>" /></td> |
| </tr> |
| </table> |
| |
| </form> |
| </div> |
| <?php |
| html_page_bottom1(); |
| |
| exit; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function auth_is_cookie_valid( $p_cookie_string ) { |
| global $g_cache_current_user_id; |
| |
| |
| if ( !db_is_connected() ) { |
| return false; |
| } |
| |
| |
| if ( '' === $p_cookie_string ) { |
| return false; |
| } |
| |
| |
| if ( null !== $g_cache_current_user_id ) { |
| return true; |
| } |
| |
| |
| $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 ( 1 == db_num_rows( $result ) ); |
| } |
| |
| |
| |
| |
| |
| $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(); |
| |
| |
| |
| |
| $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 ); |
| |
| |
| |
| if ( db_num_rows( $result ) < 1 ) { |
| auth_clear_cookies(); |
| access_denied(); |
| return false; |
| } |
| |
| $t_user_id = (int)db_result( $result ); |
| $g_cache_current_user_id = $t_user_id; |
| |
| return $t_user_id; |
| } |
| |
| |
| |
| |
| |
| 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 '<center>'; |
| echo '<p>'.error_string(ERROR_ACCESS_DENIED).'</p>'; |
| print_bracket_link( 'main_page.php', lang_get( 'proceed' ) ); |
| echo '</center>'; |
| |
| exit; |
| } |
| |
| function auth_http_set_logout_pending( $p_pending ) { |
| $t_cookie_name = config_get( 'logout_cookie' ); |
| |
| if ( $p_pending ) { |
| gpc_set_cookie( $t_cookie_name, "1", false ); |
| } else { |
| $t_cookie_path = config_get( 'cookie_path' ); |
| gpc_clear_cookie( $t_cookie_name, $t_cookie_path ); |
| } |
| } |
| |
| function auth_http_is_logout_pending() { |
| $t_cookie_name = config_get( 'logout_cookie' ); |
| $t_cookie = gpc_get_cookie( $t_cookie_name, '' ); |
| |
| return( $t_cookie > '' ); |
| } |
| ?> |