checksum map (or false when unavailable) */ private static $plugins = array(); /** @var array|null */ private static $installed_plugins = null; /** * Core checksums for the installed version/locale. Cached one week. * * @return array|false rel path => md5 */ public static function core_checksums() { if ( null !== self::$core ) { return self::$core; } $version = Softvolt_Ai_Bodyguard_Helpers::wp_version(); $locale = get_locale(); $key = 'svabg_core_sums_' . md5( $version . '|' . $locale ); $cached = get_transient( $key ); if ( is_array( $cached ) && ! empty( $cached ) ) { self::$core = $cached; return self::$core; } $sums = self::fetch_core_checksums( $version, $locale ); if ( ! $sums && 'en_US' !== $locale ) { $sums = self::fetch_core_checksums( $version, 'en_US' ); } if ( $sums ) { set_transient( $key, $sums, WEEK_IN_SECONDS ); self::$core = $sums; } else { self::$core = false; } return self::$core; } private static function fetch_core_checksums( $version, $locale ) { $url = add_query_arg( array( 'version' => $version, 'locale' => $locale, ), 'https://api.wordpress.org/core/checksums/1.0/' ); $res = wp_remote_get( $url, array( 'timeout' => 30 ) ); if ( is_wp_error( $res ) || 200 !== (int) wp_remote_retrieve_response_code( $res ) ) { return false; } $data = json_decode( wp_remote_retrieve_body( $res ), true ); if ( empty( $data['checksums'] ) || ! is_array( $data['checksums'] ) ) { return false; } return $data['checksums']; } public static function refresh_core_checksums() { $version = Softvolt_Ai_Bodyguard_Helpers::wp_version(); $locale = get_locale(); delete_transient( 'svabg_core_sums_' . md5( $version . '|' . $locale ) ); self::$core = null; self::core_checksums(); } /** * Returns 'verified' | 'modified' | 'unknown' | 'unavailable' for a core-area path. */ public static function core_status( $rel, $md5 ) { $sums = self::core_checksums(); if ( ! $sums ) { return 'unavailable'; } if ( ! isset( $sums[ $rel ] ) ) { return 'unknown'; } return hash_equals( strtolower( $sums[ $rel ] ), strtolower( $md5 ) ) ? 'verified' : 'modified'; } public static function is_core_path( $rel ) { if ( 0 === strpos( $rel, 'wp-admin/' ) || 0 === strpos( $rel, 'wp-includes/' ) ) { return true; } if ( false === strpos( $rel, '/' ) ) { $sums = self::core_checksums(); if ( $sums && isset( $sums[ $rel ] ) ) { return true; } return in_array( $rel, self::root_core_files(), true ); } if ( 0 === strpos( $rel, 'wp-content/' ) ) { $sums = self::core_checksums(); return $sums && isset( $sums[ $rel ] ); // bundled themes/plugins e.g. twentytwentyfour, akismet, hello.php. } return false; } public static function root_core_files() { return array( 'index.php', 'wp-activate.php', 'wp-blog-header.php', 'wp-comments-post.php', 'wp-config-sample.php', 'wp-cron.php', 'wp-links-opml.php', 'wp-load.php', 'wp-login.php', 'wp-mail.php', 'wp-settings.php', 'wp-signup.php', 'wp-trackback.php', 'xmlrpc.php', 'license.txt', 'readme.html', 'composer.json' ); } /** * Missing core files (present in checksums, absent on disk). */ public static function missing_core_files() { $sums = self::core_checksums(); if ( ! $sums ) { return array(); } $missing = array(); foreach ( $sums as $rel => $md5 ) { if ( 0 === strpos( $rel, 'wp-content/' ) ) { continue; // Bundled themes are optional. } if ( ! file_exists( Softvolt_Ai_Bodyguard_Helpers::abs_path( $rel ) ) ) { $missing[] = $rel; } } return $missing; } /* ---------------------------- Plugins ---------------------------- */ public static function installed_plugins() { if ( null === self::$installed_plugins ) { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } self::$installed_plugins = get_plugins(); } return self::$installed_plugins; } /** * slug => ['version' => x, 'file' => main file, 'name' => ...] */ public static function plugin_index() { static $index = null; if ( null !== $index ) { return $index; } $index = array(); foreach ( self::installed_plugins() as $file => $data ) { $slug = dirname( $file ); if ( '.' === $slug ) { $slug = basename( $file, '.php' ); } $index[ $slug ] = array( 'version' => isset( $data['Version'] ) ? $data['Version'] : '', 'file' => $file, 'name' => isset( $data['Name'] ) ? $data['Name'] : $slug, 'single' => '.' === dirname( $file ), ); } return $index; } /** * Checksums for a plugin from downloads.wordpress.org. Returns false when the plugin is not in * the repository (premium/custom plugins) or the version is unknown. * * @return array|false rel file => ['md5' => .., 'sha256' => ..] */ public static function plugin_checksums( $slug ) { if ( array_key_exists( $slug, self::$plugins ) ) { return self::$plugins[ $slug ]; } $index = self::plugin_index(); if ( empty( $index[ $slug ]['version'] ) || ! empty( $index[ $slug ]['single'] ) ) { self::$plugins[ $slug ] = false; return false; } $version = $index[ $slug ]['version']; $key = 'svabg_plugin_sums_' . md5( $slug ); $cached = get_transient( $key ); if ( is_array( $cached ) && isset( $cached['version'] ) && $cached['version'] === $version ) { self::$plugins[ $slug ] = $cached['files']; return self::$plugins[ $slug ]; } $url = sprintf( 'https://downloads.wordpress.org/plugin-checksums/%s/%s.json', rawurlencode( $slug ), rawurlencode( $version ) ); $res = wp_remote_get( $url, array( 'timeout' => 20 ) ); $files = false; if ( ! is_wp_error( $res ) && 200 === (int) wp_remote_retrieve_response_code( $res ) ) { $data = json_decode( wp_remote_retrieve_body( $res ), true ); if ( ! empty( $data['files'] ) && is_array( $data['files'] ) ) { $files = $data['files']; } } set_transient( $key, array( 'version' => $version, 'files' => $files, ), $files ? WEEK_IN_SECONDS : DAY_IN_SECONDS ); self::$plugins[ $slug ] = $files; return $files; } /** * Returns 'verified' | 'modified' | 'unknown' | 'unavailable' */ public static function plugin_file_status( $slug, $inner_rel, $md5, $sha256 = '' ) { $sums = self::plugin_checksums( $slug ); if ( false === $sums ) { return 'unavailable'; } if ( ! isset( $sums[ $inner_rel ] ) ) { return 'unknown'; } $entry = $sums[ $inner_rel ]; // WordPress.org lists several hashes for a file that changed within one version (often readme.txt). if ( '' !== $sha256 && isset( $entry['sha256'] ) ) { foreach ( (array) $entry['sha256'] as $e ) { if ( is_string( $e ) && hash_equals( strtolower( $e ), strtolower( $sha256 ) ) ) { return 'verified'; } } return 'modified'; } if ( isset( $entry['md5'] ) ) { $expected = is_array( $entry['md5'] ) ? $entry['md5'] : array( $entry['md5'] ); foreach ( $expected as $e ) { if ( is_string( $e ) && hash_equals( strtolower( $e ), strtolower( $md5 ) ) ) { return 'verified'; } } return 'modified'; } return 'unknown'; } /** * Where WordPress.org keeps the official copy of a file ("" when it has none); the free edition only * uses it to tell whether a re-install brings the file back. */ public static function official_url( $rel ) { if ( self::is_core_path( $rel ) ) { return sprintf( 'https://core.svn.wordpress.org/tags/%s/%s', rawurlencode( Softvolt_Ai_Bodyguard_Helpers::wp_version() ), str_replace( '%2F', '/', rawurlencode( $rel ) ) ); } $slug = Softvolt_Ai_Bodyguard_Helpers::plugin_slug_from_rel( $rel ); if ( $slug ) { $index = self::plugin_index(); if ( ! empty( $index[ $slug ]['version'] ) ) { $inner = substr( $rel, strlen( 'wp-content/plugins/' . $slug . '/' ) ); return sprintf( 'https://plugins.svn.wordpress.org/%s/tags/%s/%s', rawurlencode( $slug ), rawurlencode( $index[ $slug ]['version'] ), str_replace( '%2F', '/', rawurlencode( $inner ) ) ); } } return ''; } }