/{original.upload, normalized.csv, meta.json}. The directory name is * 128 bits from random_bytes(), the original file name is never used, and every directory carries index.php, .htaccess and * web.config deny rules. Nginx ignores those files: Site Health points that out. The failed-rows CSV of a job is kept in * uploads/catalogrelay/reports/ under a name with 128 bits of a keyed hash, so it outlives the file retention. */ final class FileStore { const DIR = 'catalogrelay'; const REPORTS = 'reports'; /** * Whether a string is a file reference. * * @param string $ref Reference. * @return bool */ public static function isRef( string $ref ): bool { return 1 === preg_match( '/^[a-f0-9]{32}$/', $ref ); } /** * Base directory (created with protection files). * * @return string|null Null when uploads are not writable. */ public static function baseDir(): ?string { $upload = wp_upload_dir( null, false ); if ( ! empty( $upload['error'] ) || empty( $upload['basedir'] ) ) { return null; } $dir = trailingslashit( $upload['basedir'] ) . self::DIR; if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) && ! is_dir( $dir ) ) { // Another request may have created it meanwhile. return null; } self::protect( $dir ); return $dir; } /** * Base directory path without creating it. * * @return string */ public static function basePath(): string { $upload = wp_upload_dir( null, false ); return trailingslashit( (string) ( $upload['basedir'] ?? '' ) ) . self::DIR; } /** * Create a new random directory. * * @return array {ref, dir} * @throws UploadException When the directory cannot be created. */ public static function create(): array { $base = self::baseDir(); if ( null === $base ) { throw new UploadException( 'not_writable', __( 'The uploads directory is not writable.', 'catalogrelay' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } $ref = bin2hex( random_bytes( 16 ) ); $dir = $base . '/' . $ref; if ( ! wp_mkdir_p( $dir ) ) { throw new UploadException( 'not_writable', __( 'The import directory could not be created.', 'catalogrelay' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } self::protect( $dir ); return array( 'ref' => $ref, 'dir' => $dir, ); } /** * Write the deny rules into a directory. * * @param string $dir Directory. * @return void */ public static function protect( string $dir ): void { $files = array( 'index.php' => " "\nRequire all denied\n\n\nOrder deny,allow\nDeny from all\n\n", 'web.config' => "\n\n", ); foreach ( $files as $name => $body ) { if ( ! is_file( $dir . '/' . $name ) ) { file_put_contents( $dir . '/' . $name, $body ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents } } } /** * Directory of a reference (null when the reference is malformed or the directory is gone). * * @param string $ref Reference. * @return string|null */ public static function dir( string $ref ): ?string { if ( ! self::isRef( $ref ) ) { return null; } $dir = self::basePath() . '/' . $ref; return is_dir( $dir ) ? $dir : null; } /** * Path of a file inside a reference directory. * * @param string $ref Reference. * @param string $name original.upload|normalized.csv|meta.json. * @return string */ public static function path( string $ref, string $name ): string { return self::basePath() . '/' . $ref . '/' . $name; } /** * Whether the normalized file still exists. * * @param string $ref Reference. * @return bool */ public static function hasSource( string $ref ): bool { return self::isRef( $ref ) && is_file( self::path( $ref, 'normalized.csv' ) ); } /** * Read meta.json. * * @param string $ref Reference. * @return array|null */ public static function meta( string $ref ): ?array { if ( ! self::isRef( $ref ) || ! is_file( self::path( $ref, 'meta.json' ) ) ) { return null; } $data = json_decode( (string) file_get_contents( self::path( $ref, 'meta.json' ) ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents return is_array( $data ) ? $data : null; } /** * Write meta.json (through a temporary file, so a reader never sees half of it). * * @param string $ref Reference. * @param array $meta Data. * @return void * @throws UploadException When it cannot be written. */ public static function writeMeta( string $ref, array $meta ): void { $path = self::path( $ref, 'meta.json' ); $json = (string) wp_json_encode( $meta ); if ( strlen( $json ) !== file_put_contents( $path . '.tmp', $json ) || ! rename( $path . '.tmp', $path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents,WordPress.WP.AlternativeFunctions.rename_rename if ( is_file( $path . '.tmp' ) ) { unlink( $path . '.tmp' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink } throw new UploadException( 'not_writable', __( 'The file could not be stored.', 'catalogrelay' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } } /** * Path of the saved failed-rows CSV of a root job (written when the job ends, kept after the import file is deleted). * * @param int $rootId Root job id. * @param bool $create Create the directory. * @return string|null Null when the directory cannot be created. */ public static function reportPath( int $rootId, bool $create = false ): ?string { $dir = self::basePath() . '/' . self::REPORTS; if ( $create ) { if ( null === self::baseDir() || ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) && ! is_dir( $dir ) ) ) { return null; } self::protect( $dir ); } return $dir . '/failed-rows-' . $rootId . '-' . substr( hash_hmac( 'sha256', 'failed-rows|' . $rootId, wp_salt( 'auth' ) ), 0, 32 ) . '.csv'; } /** * Saved failed-rows CSVs: path => mtime. * * @return array */ public static function reports(): array { $dir = self::basePath() . '/' . self::REPORTS; $out = array(); foreach ( is_dir( $dir ) ? (array) glob( $dir . '/failed-rows-*.csv' ) : array() as $file ) { $out[ (string) $file ] = (int) filemtime( (string) $file ); } return $out; } /** * Delete a reference directory with everything in it. * * @param string $ref Reference. * @return void */ public static function delete( string $ref ): void { $dir = self::dir( $ref ); if ( null === $dir ) { return; } foreach ( (array) scandir( $dir ) as $item ) { if ( '.' === $item || '..' === $item ) { continue; } $full = $dir . '/' . $item; if ( is_file( $full ) || is_link( $full ) ) { unlink( $full ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink } } rmdir( $dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir,WordPress.WP.AlternativeFunctions.rmdir_rmdir } /** * All reference directories on this site: ref => directory mtime. * * @return array */ public static function all(): array { $base = self::basePath(); $out = array(); if ( ! is_dir( $base ) ) { return $out; } foreach ( (array) scandir( $base ) as $item ) { if ( self::isRef( $item ) && is_dir( $base . '/' . $item ) ) { $out[ $item ] = (int) filemtime( $base . '/' . $item ); } } return $out; } }