$data ) { $single = false === strpos( $file, '/' ); $slug = $single ? basename( $file, '.php' ) : dirname( $file ); $list[] = array( 'file' => $file, 'slug' => $slug, 'version' => (string) $data['Version'], 'name' => (string) $data['Name'], 'dir' => $single ? WP_PLUGIN_DIR : WP_PLUGIN_DIR . '/' . $slug, 'single' => $single, ); } $ctx->progress = array( 'done' => 0, 'total' => count( $list ), 'label' => __( 'Plugins', 'safetybis-security' ), ); return array( 'list' => $list, 'p' => 0, 'pkg' => null, ); } /** * Step. * * @param array $state State. * @param Context $ctx Context. * @return bool */ public function step( array &$state, Context $ctx ) { $total = count( $state['list'] ); while ( $state['p'] < $total ) { $plugin = $state['list'][ $state['p'] ]; $ctx->progress = array( 'done' => $state['p'], 'total' => $total, 'label' => $plugin['name'], ); if ( null === $state['pkg'] ) { $state['pkg'] = $plugin + array( 'phase' => 'verify', 'i' => 0, 'changed' => 0, ); } $ref = $this->reference( $plugin ); if ( null === $ref ) { $ctx->add( 'unverifiable', 'low', $plugin['slug'], '', $plugin['version'], array( 'kind' => 'plugin', 'name' => $plugin['name'], 'version' => $plugin['version'] ) ); $ctx->count( 'unverifiable' ); $done = true; } else { $pkg = $state['pkg']; $done = PackageCheck::run( $pkg, $ref, $ctx, 'plugin' ); $state['pkg'] = $pkg; } if ( ! $done ) { return false; } $ctx->count( 'checked' ); $state['p']++; $state['pkg'] = null; if ( $ctx->out_of_time() ) { return $state['p'] >= $total; } } return true; } /** * Reference list for one plugin; single-file plugins compare only their file. * * @param array $plugin Plugin. * @return array|null */ private function reference( array $plugin ) { if ( $plugin['single'] && 'hello' === $plugin['slug'] ) { // Hello Dolly ships with WordPress: its reference is in the core list. list( $version, $locale ) = CoreModule::installed(); $core = Reference::core( $version, $locale ); if ( ! $core && 'en_US' !== $locale ) { $core = Reference::core( $version, 'en_US' ); } $md5 = is_array( $core ) && isset( $core['wp-content/plugins/hello.php'] ) ? $core['wp-content/plugins/hello.php'] : ''; // An updated Hello Dolly from the directory differs from the bundled copy. $dir = Reference::plugin( 'hello-dolly', $plugin['version'] ); $alt = is_array( $dir ) && isset( $dir['hello.php'] ) ? $dir['hello.php'] : array(); // WordPress updates never replace hello.php: accept every copy any WordPress release shipped // (data/hello-dolly-md5.json, built by tools/hello-dolly-hashes.py from the official checksums). $known = json_decode( (string) file_get_contents( SAFETYBIS_SECURITY_DIR . 'data/hello-dolly-md5.json' ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $old = is_array( $known ) && isset( $known['md5'] ) && is_array( $known['md5'] ) ? array_keys( $known['md5'] ) : array(); $all = array_values( array_unique( array_filter( array_merge( array( $md5 ), $alt, $old ) ) ) ); return $all ? array( 'hello.php' => $all ) : null; } $ref = Reference::plugin( $plugin['slug'], $plugin['version'] ); if ( ! $plugin['single'] ) { // Only versions that exist in the directory get the bundled copy as an alternative. return null === $ref ? null : Reference::with_bundled( $ref, 'plugins', $plugin['slug'] ); } if ( null === $ref ) { return null; } $file = basename( $plugin['file'] ); return isset( $ref[ $file ] ) ? array( $file => $ref[ $file ] ) : null; } }