progress = array( 'done' => 0, 'total' => count( self::filter( $ref ) ), 'label' => __( 'WordPress core files', 'safetybis-security' ), ); return array( 'version' => $version, 'locale' => $locale, 'alt' => $alt, 'phase' => 'verify', 'i' => 0, ); } /** * Step. * * @param array $state State. * @param Context $ctx Context. * @return bool */ public function step( array &$state, Context $ctx ) { $ref = self::filter( (array) Reference::core( $state['version'], $state['locale'] ) ); $paths = array_keys( $ref ); $total = count( $paths ); $root = trailingslashit( wp_normalize_path( ABSPATH ) ); $ctx->progress = array( 'done' => $state['i'], 'total' => $total, 'label' => __( 'WordPress core files', 'safetybis-security' ), ); if ( 'verify' === $state['phase'] ) { while ( $state['i'] < $total ) { $path = $paths[ $state['i'] ]; $file = $root . $path; $state['i']++; $ctx->count( 'checked' ); if ( ! is_file( $file ) ) { $soft = in_array( $path, self::SOFT_MISSING, true ); $ctx->add( 'missing', $soft ? 'low' : 'medium', 'core', $path, 'missing', array( 'version' => $state['version'] ) ); } else { $md5 = Files::md5( $file ); $ok = Files::matches( $file, $md5, array_filter( array( $ref[ $path ], 'wp-includes/version.php' === $path ? $state['alt'] : '' ) ) ); if ( ! $ok ) { $sev = in_array( $path, self::SOFT_MISSING, true ) ? 'low' : Files::severity( $path ); $ctx->add( 'modified', $sev, 'core', $path, $md5, array( 'version' => $state['version'] ) ); } } if ( $ctx->out_of_time() ) { $ctx->progress['done'] = $state['i']; return false; } } $state['phase'] = 'extra'; $ctx->progress['done'] = $total; if ( $ctx->out_of_time() ) { return false; } } // Unexpected files (one pass; listing these folders takes well under a second). $old = self::old_files( $root ); foreach ( array( 'wp-admin', 'wp-includes' ) as $dir ) { foreach ( Files::walk( $root . $dir ) as $rel ) { $path = $dir . '/' . $rel; if ( isset( $ref[ $path ] ) ) { continue; } $this->unexpected( $ctx, $root, $path, $old ); } } if ( Settings::get( 'check_root' ) ) { foreach ( (array) scandir( $root ) as $name ) { $path = (string) $name; if ( '' === $path || '.' === $path[0] || isset( $ref[ $path ] ) || in_array( $path, self::ROOT_SKIP, true ) ) { continue; } if ( is_file( $root . $path ) && Files::is_executable( $path ) ) { $this->unexpected( $ctx, $root, $path, $old, 'medium' ); } } } return true; } /** * Report an unexpected file. * * @param Context $ctx Context. * @param string $root Site root. * @param string $path Relative path. * @param array $old Old core files. * @param string|null $severity Fixed severity or null for the extension rule. */ private function unexpected( Context $ctx, $root, $path, array $old, $severity = null ) { $md5 = Files::md5( $root . $path ); if ( self::is_old( $path, $old ) ) { $ctx->add( 'old-file', 'low', 'core', $path, $md5 ); return; } $ctx->add( 'unexpected', null === $severity ? Files::severity( $path ) : $severity, 'core', $path, $md5, array( 'size' => (int) @filesize( $root . $path ) ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged } /** * Installed version and package locale. * * @return array{0: string, 1: string} */ public static function installed() { // Read the file as text, without running it: the globals can be changed by other code. $source = (string) file_get_contents( ABSPATH . WPINC . '/version.php' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local file. $values = array( 'wp_version' => '', 'wp_local_package' => '', ); foreach ( array_keys( $values ) as $name ) { if ( preg_match( '/^\s*\$' . $name . '\s*=\s*([\'"])([A-Za-z0-9._+-]+)\1\s*;/m', $source, $m ) ) { $values[ $name ] = $m[2]; } } if ( '' === $values['wp_version'] ) { $values['wp_version'] = (string) get_bloginfo( 'version' ); } return array( $values['wp_version'], '' !== $values['wp_local_package'] ? $values['wp_local_package'] : 'en_US' ); } /** * Keep only files this module checks. * * @param array $ref Reference list. * @return array */ private static function filter( array $ref ) { $out = array(); foreach ( $ref as $path => $md5 ) { if ( 0 === strpos( $path, 'wp-content/' ) ) { continue; } $out[ $path ] = (string) $md5; } ksort( $out, SORT_STRING ); return $out; } /** * Files that WordPress updates delete ($_old_files in update-core.php), read as text. * * @param string $root Site root. * @return string[] */ private static function old_files( $root ) { $file = $root . 'wp-admin/includes/update-core.php'; $text = is_readable( $file ) ? (string) file_get_contents( $file ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $from = strpos( $text, '$_old_files = array(' ); if ( false === $from ) { return array(); } $to = strpos( $text, ');', $from ); if ( false === $to ) { return array(); } preg_match_all( "/'([^']+)'/", substr( $text, $from, $to - $from ), $m ); return $m[1]; } /** * Whether a path is (inside) an old core file or folder. * * @param string $path Relative path. * @param string[] $old Old entries. * @return bool */ private static function is_old( $path, array $old ) { foreach ( $old as $entry ) { $entry = rtrim( $entry, '/' ); if ( $path === $entry || 0 === strpos( $path, $entry . '/' ) ) { return true; } } return false; } }