Skip to content
wiki.fftac.org

Site Health Guard - Source Excerpt 01

Back to Site Health Guard

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/includes/site-health-guard.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/includes/site-health-guard.php

<?php
/**
 * Embedded site health guard for UAIX Locale Router.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! defined( 'JAI_SITE_HEALTH_GUARD_BOOTSTRAPPED' ) && ! function_exists( 'jai_site_health_guard_log_dir' ) ) {
	define( 'JAI_SITE_HEALTH_GUARD_BOOTSTRAPPED', true );

if ( ! defined( 'JAI_SITE_HEALTH_GUARD_VERSION' ) ) {
	define( 'JAI_SITE_HEALTH_GUARD_VERSION', defined( 'UAIXLR_VERSION' ) ? UAIXLR_VERSION : '3.8.0' );
}

if ( ! defined( 'JAI_SITE_HEALTH_GUARD_STARTED_AT' ) ) {
	define( 'JAI_SITE_HEALTH_GUARD_STARTED_AT', microtime( true ) );
}

if ( ! defined( 'JAI_SITE_HEALTH_GUARD_RECOVERY_TTL' ) ) {
	define( 'JAI_SITE_HEALTH_GUARD_RECOVERY_TTL', 1800 );
}

function jai_site_health_guard_log_dir() {
	$dir = rtrim( WP_CONTENT_DIR, '/\\' ) . '/site-health-logs';

	if ( ! is_dir( $dir ) ) {
		if ( function_exists( 'wp_mkdir_p' ) ) {
			wp_mkdir_p( $dir );
		} else {
			@mkdir( $dir, 0755, true );
		}
	}

	if ( is_dir( $dir ) ) {
		$deny_file  = $dir . '/.htaccess';
		$index_file = $dir . '/index.html';

		if ( ! file_exists( $deny_file ) ) {
			@file_put_contents( $deny_file, "Require all denied\nDeny from all\n" );
		}

		if ( ! file_exists( $index_file ) ) {
			@file_put_contents( $index_file, '' );
		}
	}

	return $dir;
}

function jai_site_health_guard_state_path( $name ) {
	$name = preg_replace( '/[^a-z0-9\-]/i', '', (string) $name );
	return jai_site_health_guard_log_dir() . '/' . $name . '.php';
}

function jai_site_health_guard_clean_path( $path ) {
	$path = str_replace( '\\', '/', (string) $path );

	if ( defined( 'ABSPATH' ) ) {
		$root = str_replace( '\\', '/', ABSPATH );
		if ( 0 === strpos( $path, $root ) ) {
			return './' . ltrim( substr( $path, strlen( $root ) ), '/' );
		}
	}

	if ( defined( 'WP_CONTENT_DIR' ) ) {
		$content = str_replace( '\\', '/', WP_CONTENT_DIR );
		if ( 0 === strpos( $path, $content ) ) {
			return 'wp-content/' . ltrim( substr( $path, strlen( $content ) ), '/' );
		}
	}

	return $path;
}

function jai_site_health_guard_request_path() {
	$uri  = isset( $_SERVER['REQUEST_URI'] ) ? (string) $_SERVER['REQUEST_URI'] : '/';
	$path = parse_url( $uri, PHP_URL_PATH );

	return is_string( $path ) && '' !== $path ? $path : '/';
}

function jai_site_health_guard_request_method() {
	return isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( (string) $_SERVER['REQUEST_METHOD'] ) : 'GET';
}

function jai_site_health_guard_is_guard_rest_request() {
	$path = jai_site_health_guard_request_path();
	return 0 === strpos( $path, '/wp-json/site-health-guard/' ) || 0 === strpos( $path, '/index.php/wp-json/site-health-guard/' );
}

function jai_site_health_guard_is_frontend_request() {
	if ( PHP_SAPI === 'cli' ) {
		return false;
	}

	if ( ! in_array( jai_site_health_guard_request_method(), array( 'GET', 'HEAD' ), true ) ) {
		return false;
	}

	$path = jai_site_health_guard_request_path();

	foreach ( array( '/wp-admin', '/wp-login.php', '/wp-json', '/xmlrpc.php', '/wp-cron.php' ) as $prefix ) {
		if ( 0 === strpos( $path, $prefix ) ) {
			return false;
		}
	}

	if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) {
		return false;
	}

	return true;
}

function jai_site_health_guard_error_type_name( $type ) {
	$types = array(
		E_ERROR             => 'E_ERROR',
		E_PARSE             => 'E_PARSE',
		E_CORE_ERROR        => 'E_CORE_ERROR',
		E_COMPILE_ERROR     => 'E_COMPILE_ERROR',
		E_USER_ERROR        => 'E_USER_ERROR',
		E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
	);

	return isset( $types[ $type ] ) ? $types[ $type ] : (string) $type;
}

function jai_site_health_guard_error_hash( $error ) {
	$file = isset( $error['file'] ) ? jai_site_health_guard_clean_path( $error['file'] ) : '';
	$line = isset( $error['line'] ) ? (string) $error['line'] : '';
	$msg  = isset( $error['message'] ) ? preg_replace( '/\s+/', ' ', (string) $error['message'] ) : '';

	return substr( hash( 'sha256', $file . '|' . $line . '|' . $msg ), 0, 16 );
}

function jai_site_health_guard_is_fatal_error( $error ) {
	if ( ! is_array( $error ) || ! isset( $error['type'] ) ) {
		return false;
	}

	return in_array(
		(int) $error['type'],
		array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ),
		true
	);
}

function jai_site_health_guard_public_host() {
	$host = isset( $_SERVER['HTTP_HOST'] ) ? strtolower( (string) $_SERVER['HTTP_HOST'] ) : '';
	$host = preg_replace( '/:\d+$/', '', $host );
	$host = preg_replace( '/[^a-z0-9\.\-]/', '', $host );

	return '' !== $host ? $host : 'this site';
}

function jai_site_health_guard_site_label() {
	$host = jai_site_health_guard_public_host();
	$host = preg_replace( '/^www\./', '', $host );
	$label = str_replace( array( '.', '-' ), ' ', $host );
	$label = trim( preg_replace( '/\s+/', ' ', $label ) );

	return '' !== $label ? ucwords( $label ) : 'This Site';
}

function jai_site_health_guard_request_context() {
	$query_keys = array();
	if ( isset( $_SERVER['QUERY_STRING'] ) && '' !== $_SERVER['QUERY_STRING'] ) {
		parse_str( (string) $_SERVER['QUERY_STRING'], $query_values );
		$query_keys = array_values( array_map( 'strval', array_keys( (array) $query_values ) ) );
		sort( $query_keys );
	}

	$theme = '';
	if ( function_exists( 'wp_get_theme' ) ) {
		$wp_theme = wp_get_theme();
		if ( $wp_theme && method_exists( $wp_theme, 'get_stylesheet' ) ) {
			$theme = $wp_theme->get_stylesheet();
		}
	}

	$active_plugins = array();
	if ( function_exists( 'get_option' ) ) {
		$active_plugins = get_option( 'active_plugins', array() );
		if ( ! is_array( $active_plugins ) ) {
			$active_plugins = array();
		}
	}

	return array(
		'method'        => jai_site_health_guard_request_method(),
		'host'          => jai_site_health_guard_public_host(),
		'path'          => jai_site_health_guard_request_path(),
		'query_keys'    => $query_keys,
		'is_admin'      => function_exists( 'is_admin' ) ? (bool) is_admin() : false,
		'doing_ajax'    => function_exists( 'wp_doing_ajax' ) ? (bool) wp_doing_ajax() : false,
		'is_rest'       => defined( 'REST_REQUEST' ) && REST_REQUEST,
		'logged_in'     => function_exists( 'is_user_logged_in' ) ? (bool) is_user_logged_in() : false,
		'theme'         => $theme,
		'plugin_count'  => count( $active_plugins ),
		'plugins'       => array_values( array_map( 'strval', $active_plugins ) ),
		'wp_cache'      => defined( 'WP_CACHE' ) ? (bool) WP_CACHE : null,
		'memory_peak'   => function_exists( 'memory_get_peak_usage' ) ? memory_get_peak_usage( true ) : null,
		'php_version'   => PHP_VERSION,
		'guard_version' => JAI_SITE_HEALTH_GUARD_VERSION,
	);
}

function jai_site_health_guard_write_json( $file, $data ) {
	$json = function_exists( 'wp_json_encode' ) ? wp_json_encode( $data, JSON_UNESCAPED_SLASHES ) : json_encode( $data, JSON_UNESCAPED_SLASHES );
	if ( ! is_string( $json ) ) {
		return false;
	}

	if ( ! file_exists( $file ) ) {
		@file_put_contents( $file, "<?php exit; ?>\n" );
	}

	$written = @file_put_contents( $file, $json . "\n", FILE_APPEND | LOCK_EX );
	if ( false === $written ) {
		$written = @file_put_contents( $file, $json . "\n", FILE_APPEND );
	}

	return false !== $written;
}

function jai_site_health_guard_write_state( $name, $data ) {
	$dir = jai_site_health_guard_log_dir();
	if ( ! is_dir( $dir ) ) {
		return;
	}

	$data['updated_at']    = gmdate( 'c' );
	$data['updated_epoch'] = time();

	$json = function_exists( 'wp_json_encode' ) ? wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) : json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );
	if ( is_string( $json ) ) {
		$payload = "<?php exit; ?>\n" . $json . "\n";
		$written = @file_put_contents( jai_site_health_guard_state_path( $name ), $payload, LOCK_EX );
		if ( false === $written ) {
			@file_put_contents( jai_site_health_guard_state_path( $name ), $payload );
		}
	}
}

function jai_site_health_guard_read_state( $name ) {
	$file = jai_site_health_guard_state_path( $name );
	if ( ! is_readable( $file ) ) {
		return null;
	}

	$raw = (string) file_get_contents( $file );
	$raw = preg_replace( '/^<\?php exit; \?>\s*/', '', $raw );
	$data = json_decode( (string) $raw, true );

	return is_array( $data ) ? $data : null;
}

function jai_site_health_guard_log( $event, $data = array() ) {
	$dir = jai_site_health_guard_log_dir();
	if ( ! is_dir( $dir ) ) {
		return;
	}

	$record = array(
		'timestamp' => gmdate( 'c' ),
		'event'     => (string) $event,
		'request'   => jai_site_health_guard_request_context(),
		'data'      => $data,
	);

	jai_site_health_guard_write_json( $dir . '/events-' . gmdate( 'Y-m-d' ) . '.php', $record );
}