4096 ) { return null; } if ( false !== strpos( $path, "\0" ) || false !== strpos( $path, '\\' ) || '/' === $path[0] ) { return null; } foreach ( explode( '/', $path ) as $segment ) { if ( '' === $segment || '.' === $segment || '..' === $segment ) { return null; } } return $path; } /** * Whether an existing path's directory holds an entry with exactly this name. * * On disks that ignore upper/lower case (macOS, Windows), file_exists() also finds a * different file whose name differs only in case. Names are compared after Unicode * normalisation, since macOS may store accented letters decomposed. * * @param string $path Absolute path that exists. */ public static function exact_entry_exists( $path ) { $base = basename( $path ); $names = @scandir( dirname( $path ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! is_array( $names ) || in_array( $base, $names, true ) ) { return true; } if ( class_exists( 'Normalizer' ) ) { $want = \Normalizer::normalize( $base, \Normalizer::FORM_C ); foreach ( $names as $name ) { if ( \Normalizer::normalize( (string) $name, \Normalizer::FORM_C ) === $want ) { return true; } } } return false; } /** * Whether an absolute path is inside (or equal to) a root, after resolving symlinks. * * @param string $path Absolute path (must exist). * @param string $root Absolute root directory. */ public static function is_within( $path, $root ) { $real_path = realpath( $path ); $real_root = realpath( $root ); if ( false === $real_path || false === $real_root ) { return false; } $real_path = wp_normalize_path( $real_path ); $real_root = trailingslashit( wp_normalize_path( $real_root ) ); return $real_path . '/' === $real_root || 0 === strpos( $real_path, $real_root ); } /** * Recursively deletes a directory (does not follow symlinks). * * @param string $dir Absolute path. */ public static function rrmdir( $dir ) { if ( is_link( $dir ) || is_file( $dir ) ) { @unlink( $dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.unlink_unlink return; } if ( ! is_dir( $dir ) ) { return; } $items = @scandir( $dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged foreach ( (array) $items as $item ) { if ( '.' === $item || '..' === $item ) { continue; } self::rrmdir( $dir . '/' . $item ); } @rmdir( $dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_rmdir } /** * Total bytes of files under a directory (does not follow symlinks). * * @param string $dir Absolute path. */ public static function dir_size( $dir ) { $total = 0; if ( ! is_dir( $dir ) ) { return 0; } $it = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $file ) { if ( $file->isFile() && ! $file->isLink() ) { $total += $file->getSize(); } } return $total; } /** * Whether a file extension is worth compressing. * * @param string $path File path. */ public static function is_compressible( $path ) { $ext = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ); return in_array( $ext, array( 'php', 'js', 'css', 'html', 'htm', 'txt', 'json', 'xml', 'svg', 'sql', 'md', 'csv', 'po', 'pot', 'map', 'scss', 'less', 'log', 'ini', 'yml', 'yaml', 'twig', 'tpl', 'ttf', 'eot', 'otf', 'ico', 'bmp', 'tif', 'tiff', 'psd', 'mo' ), true ); } }