get( 'archive_path' );
if ( is_string( $source ) && '' !== $source ) {
if ( ! self::contains( self::archives(), $source ) || is_link( $source ) ) {
throw new \RuntimeException( 'The backup file is outside the backups folder.' );
}
return $source;
}
return self::job( $job->id() ) . '/upload.part';
}
/**
* The wp-content directory being exported or restored into. Filterable so
* tests and unusual layouts can redirect it.
*/
public static function content(): string {
return untrailingslashit( wp_normalize_path( (string) apply_filters( 'migratex_content_dir', WP_CONTENT_DIR ) ) );
}
/**
* Creates the storage tree and drops web-server deny rules into it.
*
* .htaccess (Apache) and web.config (IIS) deny every direct request. Nginx reads neither, so an
* nginx.conf snippet with the matching rule is written for the site owner to include, and Exposure
* warns in the admin when the folder is still reachable. Whatever the server does, nothing here is
* guessable: every job lives in a folder named with 128 random bits (JobStore::create()) and every
* finished archive ends in a 128-bit random token (Export InitStage), and archives are only ever
* handed out through the authenticated download endpoint, never linked.
*/
public static function ensureStorage(): bool {
foreach ( array( self::root(), self::archives(), self::jobs() ) as $dir ) {
if ( ! wp_mkdir_p( $dir ) ) {
return false;
}
}
$root = self::root();
$files = array(
'.htaccess' => "Options -Indexes\n\nRequire all denied\n\n\nOrder deny,allow\nDeny from all\n\n",
'index.php' => " Exposure::nginxSnippet(),
'web.config' => "\n\n",
);
foreach ( $files as $name => $contents ) {
$path = $root . '/' . $name;
if ( ! is_file( $path ) && false === file_put_contents( $path, $contents ) ) {
return false;
}
}
// The deny rules above cover Apache and IIS (and Nginx once the snippet is included). An index file in
// every folder also stops a listing on servers that ignore them (a server that lists a folder with no index file shows its contents).
foreach ( array( self::archives(), self::jobs() ) as $dir ) {
if ( ! is_file( $dir . '/index.php' ) && false === file_put_contents( $dir . '/index.php', $files['index.php'] ) ) {
return false;
}
}
return true;
}
/**
* True when $path resolves to a location inside $base.
*
* Resolves symlinks and `..` through realpath. A path that does not exist yet
* is judged by its parent directory, so this works for files about to be written.
*/
public static function contains( string $base, string $path ): bool {
if ( false !== strpos( $path, "\0" ) ) {
return false;
}
$real_base = realpath( $base );
if ( false === $real_base ) {
return false;
}
$real = realpath( $path );
if ( false === $real ) {
$name = basename( $path );
if ( '..' === $name || '.' === $name || '' === $name ) {
return false;
}
$parent = realpath( dirname( $path ) );
if ( false === $parent ) {
return false;
}
$real = rtrim( $parent, '/\\' ) . DIRECTORY_SEPARATOR . $name;
}
$real_base = rtrim( $real_base, '/\\' ) . DIRECTORY_SEPARATOR;
return 0 === strpos( rtrim( $real, '/\\' ) . DIRECTORY_SEPARATOR, $real_base );
}
}