list of array( url, dir ). */ public static function roots() { $uploads = wp_upload_dir( null, false ); $roots = array( array( (string) $uploads['baseurl'], (string) $uploads['basedir'] ), array( get_theme_root_uri(), get_theme_root() ), array( plugins_url(), self::plugins_dir() ), array( home_url( '/' ), self::home_dir() ), ); $out = array(); foreach ( $roots as $root ) { if ( '' !== $root[0] && '' !== $root[1] && is_dir( $root[1] ) ) { $out[] = array( untrailingslashit( $root[0] ), untrailingslashit( str_replace( '\\', '/', $root[1] ) ) ); } } return $out; } /** Real paths of the folders files may be read from. */ public static function allowed_bases() { $bases = array(); foreach ( self::roots() as $root ) { $real = realpath( $root[1] ); if ( $real ) { $bases[] = $real; } } return array_values( array_unique( $bases ) ); } /** True when $real (a realpath) is inside one of the allowed folders. */ public static function is_allowed( $real ) { foreach ( self::allowed_bases() as $base ) { if ( $real === $base || 0 === strpos( $real, rtrim( $base, '/\\' ) . DIRECTORY_SEPARATOR ) || 0 === strpos( $real, rtrim( $base, '/\\' ) . '/' ) ) { return true; } } return false; } /** A server path shown relative to the site folder, for messages in the admin. */ public static function display( $path ) { $path = str_replace( '\\', '/', (string) $path ); $home = self::home_dir(); return 0 === strpos( $path, $home ) ? '/' . substr( $path, strlen( $home ) ) : $path; } }