$expected Pfad => erwarteter Hash oder Liste gueltiger Hashes. * @param string $path Pfad der vorhandenen Datei. * @param string|null $hash Hash der vorhandenen Datei; null, wenn sie nicht lesbar war. * @return string `ok`, `modified`, `unreadable` oder `unknown`. */ public static function file_state( array $expected, string $path, ?string $hash ): string { if ( ! isset( $expected[ $path ] ) ) { return 'unknown'; } if ( null === $hash ) { return 'unreadable'; } foreach ( (array) $expected[ $path ] as $candidate ) { if ( is_string( $candidate ) && hash_equals( strtolower( $candidate ), strtolower( $hash ) ) ) { return 'ok'; } } return 'modified'; } /** * Dateien der Soll-Liste, die im Paket nicht vorhanden sind. * * @param array $expected Pfad => erwarteter Hash. * @param string[] $present Pfade aller vorhandenen Dateien. * @return string[] Sortiert. */ public static function missing( array $expected, array $present ): array { $missing = array_values( array_diff( array_keys( $expected ), $present ) ); sort( $missing, SORT_STRING ); return $missing; } /** * Soll-Liste des Kerns auf den pruefbaren Teil kuerzen. * * @param array $checksums Antwort des Dienstes (Pfad => MD5). * @return array */ public static function core_expected( array $checksums ): array { $out = array(); foreach ( $checksums as $path => $hash ) { if ( is_string( $path ) && is_string( $hash ) && self::is_core_path( $path ) ) { $out[ $path ] = $hash; } } return $out; } /** * Soll-Liste eines Plugins aus der Antwort von * `downloads.wordpress.org/plugin-checksums/`. Ein Eintrag kann mehrere * gueltige Hashes haben (der Dienst liefert dann eine Liste). * * @param array $files Wert von `files` der Antwort. * @return array Pfad => gueltige SHA-256-Werte. */ public static function plugin_expected( array $files ): array { $out = array(); foreach ( $files as $path => $entry ) { $hashes = self::alternatives( $entry ); if ( is_string( $path ) && $hashes ) { $out[ $path ] = $hashes; } } return $out; } /** * Alle gueltigen SHA-256-Werte eines Eintrags der Plugin-Pruefsummen. * * @param mixed $entry Eintrag aus `files`. * @return string[] */ public static function alternatives( $entry ): array { if ( ! is_array( $entry ) || ! isset( $entry['sha256'] ) ) { return array(); } $hashes = is_array( $entry['sha256'] ) ? $entry['sha256'] : array( $entry['sha256'] ); return array_values( array_filter( $hashes, 'is_string' ) ); } /** * Zaehlt die Befunde eines Pakets fuer Anzeige und Digest. * * @param array $package Ergebnis eines Pakets mit `modified`, `missing`, `unknown`. * @return int */ public static function finding_count( array $package ): int { $count = 0; foreach ( array( 'modified', 'missing', 'unknown' ) as $key ) { $count += isset( $package[ $key ] ) && is_array( $package[ $key ] ) ? count( $package[ $key ] ) : 0; } return $count; } }