410 ) ); } $current = set_url_scheme( ( is_ssl() ? 'https://' : 'http://' ) . ( isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '' ) . $uri ); if ( '' === $target || untrailingslashit( $target ) === untrailingslashit( $current ) ) { return; // Never loop. } // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- redirects to other sites are the point of this feature; targets are set by administrators. wp_redirect( $target, (int) $rule['code'], 'AI GuardWall' ); exit; } private static function count_hit( $id ) { $hits = self::hits(); $hits[ $id ] = array( 'count' => ( isset( $hits[ $id ]['count'] ) ? (int) $hits[ $id ]['count'] : 0 ) + 1, 'last' => time(), ); update_option( self::HITS, $hits, false ); } /** * Add or change a rule. * * @param array $data id (optional), source, target, code, regex, enabled, note. * @return array|WP_Error The saved rule. */ public static function save( array $data ) { $rules = self::rules(); $id = isset( $data['id'] ) ? sanitize_key( $data['id'] ) : ''; $regex = ! empty( $data['regex'] ) && 'false' !== $data['regex']; $code = isset( $data['code'] ) ? (int) $data['code'] : 301; $source = trim( (string) ( isset( $data['source'] ) ? $data['source'] : '' ) ); $target = trim( (string) ( isset( $data['target'] ) ? $data['target'] : '' ) ); if ( ! in_array( $code, self::CODES, true ) ) { $code = 301; } if ( '' === $source ) { return new WP_Error( 'svabg_redirect', __( 'Enter the old address (source).', 'ai-guardwall' ) ); } if ( $regex ) { if ( false === @preg_match( '~' . str_replace( '~', '\~', $source ) . '~i', '' ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return new WP_Error( 'svabg_redirect', __( 'The regular expression is not valid.', 'ai-guardwall' ) ); } } else { // A full address of this site becomes a path. if ( preg_match( '#^https?://#i', $source ) ) { $home = wp_parse_url( home_url(), PHP_URL_HOST ); if ( strtolower( (string) wp_parse_url( $source, PHP_URL_HOST ) ) !== strtolower( (string) $home ) ) { return new WP_Error( 'svabg_redirect', __( 'The source must be an address on this site.', 'ai-guardwall' ) ); } list( $p, $q ) = self::relative( $source ); $source = $p . ( '' !== $q ? '?' . $q : '' ); } $source = '/' . ltrim( $source, '/' ); } if ( 410 !== $code && '' === $target ) { return new WP_Error( 'svabg_redirect', __( 'Enter where the old address should go (target).', 'ai-guardwall' ) ); } if ( 410 !== $code && ! $regex && self::norm( wp_parse_url( $source, PHP_URL_PATH ) ) === self::norm( (string) wp_parse_url( self::absolute( $target ), PHP_URL_PATH ) ) && false === strpos( $target, '://' ) ) { return new WP_Error( 'svabg_redirect', __( 'The source and the target are the same address.', 'ai-guardwall' ) ); } $key = self::norm( wp_parse_url( $source, PHP_URL_PATH ) ) . '?' . (string) wp_parse_url( $source, PHP_URL_QUERY ); foreach ( $rules as $r ) { if ( $r['id'] !== $id && ! $regex && empty( $r['regex'] ) && self::norm( wp_parse_url( $r['source'], PHP_URL_PATH ) ) . '?' . (string) wp_parse_url( $r['source'], PHP_URL_QUERY ) === $key ) { return new WP_Error( 'svabg_redirect', __( 'There is already a redirect for this address. Edit that one instead.', 'ai-guardwall' ) ); } } if ( 410 === $code ) { $clean = ''; } elseif ( $regex ) { $clean = $target; // May hold $1 references; used as typed. } elseif ( preg_match( '#^[a-z][a-z0-9+.-]*://#i', $target ) ) { $clean = esc_url_raw( $target ); } else { $clean = '/' . ltrim( $target, '/' ); } $rule = array( 'id' => '' !== $id && isset( $rules[ $id ] ) ? $id : 'r' . substr( md5( uniqid( '', true ) ), 0, 10 ), 'source' => $source, 'target' => $clean, 'code' => $code, 'regex' => $regex ? 1 : 0, 'enabled' => isset( $data['enabled'] ) ? ( empty( $data['enabled'] ) || 'false' === $data['enabled'] ? 0 : 1 ) : 1, 'note' => isset( $data['note'] ) ? sanitize_text_field( (string) $data['note'] ) : '', 'created' => '' !== $id && isset( $rules[ $id ] ) ? (int) $rules[ $id ]['created'] : time(), ); $rules[ $rule['id'] ] = $rule; update_option( self::RULES, $rules, true ); self::forget_404( $source ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'redirect_saved', sprintf( /* translators: 1: source, 2: code, 3: target. */ __( 'Redirect saved: %1$s → %2$s %3$s', 'ai-guardwall' ), $source, $code, $rule['target'] ) ); return $rule; } public static function delete( $id ) { $rules = self::rules(); $id = sanitize_key( $id ); if ( ! isset( $rules[ $id ] ) ) { return new WP_Error( 'svabg_redirect', __( 'That redirect no longer exists.', 'ai-guardwall' ) ); } $source = $rules[ $id ]['source']; unset( $rules[ $id ] ); update_option( self::RULES, $rules, true ); $hits = self::hits(); unset( $hits[ $id ] ); update_option( self::HITS, $hits, false ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'redirect_deleted', sprintf( /* translators: %s: source. */ __( 'Redirect deleted: %s', 'ai-guardwall' ), $source ) ); return array( 'message' => __( 'Redirect deleted.', 'ai-guardwall' ) ); } public static function toggle( $id, $on ) { $rules = self::rules(); $id = sanitize_key( $id ); if ( ! isset( $rules[ $id ] ) ) { return new WP_Error( 'svabg_redirect', __( 'That redirect no longer exists.', 'ai-guardwall' ) ); } $rules[ $id ]['enabled'] = $on ? 1 : 0; update_option( self::RULES, $rules, true ); return $rules[ $id ]; } /* ------------------------------ 404 log ------------------------------ */ public static function log_404() { if ( ! is_404() || is_admin() ) { return; } $uri = Softvolt_Ai_Bodyguard_Helpers::request_uri(); list( $path ) = self::relative( $uri ); if ( strlen( $path ) > 300 || preg_match( '#\.(php|asp|aspx|cgi|env|git|sql|bak|ini|log)$|/wp-admin|/wp-login|/xmlrpc#i', $path ) ) { return; // Attack probes are the firewall's business, not a redirect candidate. } $log = get_option( self::LOG_404, array() ); $log = is_array( $log ) ? $log : array(); $key = md5( strtolower( $path ) ); $ref = Softvolt_Ai_Bodyguard_Helpers::loggable_url( Softvolt_Ai_Bodyguard_Helpers::referer() ); $log[ $key ] = array( 'path' => $path, 'count' => ( isset( $log[ $key ]['count'] ) ? (int) $log[ $key ]['count'] : 0 ) + 1, 'last' => time(), 'ref' => '' !== $ref ? substr( $ref, 0, 250 ) : ( isset( $log[ $key ]['ref'] ) ? $log[ $key ]['ref'] : '' ), ); if ( count( $log ) > self::LOG_MAX ) { uasort( $log, function ( $a, $b ) { return $b['last'] - $a['last']; } ); $log = array_slice( $log, 0, self::LOG_MAX, true ); } update_option( self::LOG_404, $log, false ); } public static function log() { $log = get_option( self::LOG_404, array() ); $log = is_array( $log ) ? $log : array(); uasort( $log, function ( $a, $b ) { return $b['count'] === $a['count'] ? $b['last'] - $a['last'] : $b['count'] - $a['count']; } ); $out = array(); foreach ( $log as $key => $row ) { $row['key'] = $key; $row['last_h'] = human_time_diff( (int) $row['last'] ); $out[] = $row; } return $out; } private static function forget_404( $source ) { $log = get_option( self::LOG_404, array() ); $key = md5( strtolower( '/' . ltrim( (string) wp_parse_url( $source, PHP_URL_PATH ), '/' ) ) ); if ( is_array( $log ) && isset( $log[ $key ] ) ) { unset( $log[ $key ] ); update_option( self::LOG_404, $log, false ); } } public static function clear_404( $key = '' ) { if ( '' === $key ) { delete_option( self::LOG_404 ); return array( 'message' => __( '404 log cleared.', 'ai-guardwall' ) ); } $log = get_option( self::LOG_404, array() ); unset( $log[ sanitize_key( $key ) ] ); update_option( self::LOG_404, $log, false ); return array( 'message' => __( 'Removed from the log.', 'ai-guardwall' ) ); } /** * Everything the Redirects tab shows. */ public static function overview() { $hits = self::hits(); $rules = array(); foreach ( self::rules() as $id => $r ) { $r['hits'] = isset( $hits[ $id ] ) ? (int) $hits[ $id ]['count'] : 0; $r['last_h'] = isset( $hits[ $id ] ) ? human_time_diff( (int) $hits[ $id ]['last'] ) : ''; $r['from'] = $r['regex'] ? $r['source'] : home_url( $r['source'] ); $rules[] = $r; } usort( $rules, function ( $a, $b ) { return $b['created'] - $a['created']; } ); return array( 'rules' => $rules, 'log' => self::log(), 'home' => untrailingslashit( home_url() ), 'log404' => (bool) Softvolt_Ai_Bodyguard_Modules::get( 'redirect_log_404' ), ); } /** * Which rule a test address would hit. */ public static function test( $url ) { $hit = self::match( (string) $url ); if ( ! $hit ) { return array( 'match' => false, 'message' => __( 'No redirect matches this address.', 'ai-guardwall' ) ); } return array( 'match' => true, 'rule' => $hit[0], 'target' => $hit[1], 'message' => 410 === (int) $hit[0]['code'] ? __( 'Matches: the visitor gets "410 Gone".', 'ai-guardwall' ) : sprintf( /* translators: 1: code, 2: target. */ __( 'Matches: %1$d to %2$s', 'ai-guardwall' ), (int) $hit[0]['code'], $hit[1] ), ); } }