isFile() ) { continue; } $out[] = substr( wp_normalize_path( $file->getPathname() ), strlen( $dir ) + 1 ); if ( count( $out ) >= $limit ) { break; } } } catch ( \UnexpectedValueException $e ) { return $out; } sort( $out, SORT_STRING ); return $out; } /** * md5 of the file with Windows line endings (CRLF) turned into LF, or '' when the file * has no CRLF, is larger than 10 MB or cannot be read. A file that differs from the * original only in line endings (FTP text mode, packages built on Windows) is not changed. * * @param string $file Absolute path. * @return string */ public static function md5_lf( $file ) { if ( ! is_readable( $file ) || filesize( $file ) > 10485760 ) { return ''; } $data = (string) file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents return false === strpos( $data, "\r\n" ) ? '' : md5( str_replace( "\r\n", "\n", $data ) ); } /** * Whether a file matches one of the reference hashes, ignoring line endings. * * @param string $file Absolute path. * @param string $md5 md5 of the file as it is. * @param string[] $ref Accepted hashes. * @return bool */ public static function matches( $file, $md5, array $ref ) { if ( in_array( $md5, $ref, true ) ) { return true; } $lf = self::md5_lf( $file ); return '' !== $lf && in_array( $lf, $ref, true ); } /** * md5 of a file, or '' when it cannot be read. * * @param string $file Absolute path. * @return string */ public static function md5( $file ) { $hash = is_readable( $file ) ? md5_file( $file ) : false; return false === $hash ? '' : $hash; } }