absolute directory (no trailing slash). * * @return array */ public static function all() { $content = Paths::content_dir(); $extra = array( 'uploads' => Paths::uploads_dir(), 'plugins' => Paths::plugins_dir(), 'mu-plugins' => Paths::mu_plugins_dir(), 'themes' => Paths::themes_dir(), ); $roots = array( 'content' => $content ); foreach ( $extra as $key => $dir ) { $dir = untrailingslashit( wp_normalize_path( (string) $dir ) ); if ( '' !== $dir && is_dir( $dir ) && 0 !== strpos( $dir . '/', $content . '/' ) ) { $roots[ $key ] = $dir; } } return $roots; } /** * Maps root + relative path to a safe absolute path on this site, or null. * * The file must exist, must not be a symlink and must resolve inside the root. * * @param string $root_key Root key. * @param string $rel Relative path (untrusted). * @return string|null */ public static function resolve_existing( $root_key, $rel ) { $roots = self::all(); $rel = Fs::clean_relative( $rel ); if ( null === $rel || ! isset( $roots[ $root_key ] ) ) { return null; } $path = $roots[ $root_key ] . '/' . $rel; if ( is_link( $path ) || ! file_exists( $path ) || ! Fs::is_within( $path, $roots[ $root_key ] ) ) { return null; } $exclusions = new Exclusions( $root_key, $roots[ $root_key ] ); $parts = explode( '/', $rel ); $so_far = ''; foreach ( $parts as $part ) { $so_far = '' === $so_far ? $part : $so_far . '/' . $part; if ( $exclusions->excluded( $so_far, $part ) ) { return null; } } return $path; } }