isset( $overrides['delimiter'] ) && in_array( $overrides['delimiter'], DelimiterSniffer::choices(), true ) ? $overrides['delimiter'] : $meta['delimiter'] ); $insp = FileInspector::inspect( FileStore::path( $ref, 'normalized.csv' ), (string) $overrides['delimiter'] ); if ( empty( $insp['ok'] ) ) { throw new UploadException( (string) $insp['error'], self::message( (string) $insp['error'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } $meta = array_merge( $meta, self::fromInspection( $insp, (string) $overrides['delimiter'] ) ); FileStore::writeMeta( $ref, $meta ); return self::publicInfo( $meta ); } $overrides['encoding'] = $enc; $new = array_merge( $meta, self::normalize( $ref, $overrides ) ); self::commit( $ref, $new, $meta ); return self::publicInfo( $new ); } /** * Info of an existing upload. * * @param string $ref Reference. * @return array|null */ public static function info( string $ref ): ?array { $meta = FileStore::meta( $ref ); return $meta ? self::publicInfo( $meta ) : null; } /** * Transcode and inspect into normalized.csv.tmp; commit() puts it in place. * * @param string $ref Reference. * @param array $overrides Overrides. * @return array Meta fields. * @throws UploadException On any refusal (the temporary file is deleted). */ private static function normalize( string $ref, array $overrides ): array { $orig = FileStore::path( $ref, 'original.upload' ); $norm = FileStore::path( $ref, 'normalized.csv.tmp' ); $head = (string) file_get_contents( $orig, false, null, 0, 65536 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $auto = ! ( isset( $overrides['encoding'] ) && in_array( $overrides['encoding'], EncodingDetector::choices(), true ) ); $enc = $auto ? EncodingDetector::detect( $head, (int) filesize( $orig ) <= strlen( $head ) ) : (string) $overrides['encoding']; if ( EncodingDetector::UNSUPPORTED === $enc ) { throw new UploadException( 'unsupported_encoding', __( 'The text encoding is not supported. Save the file as UTF-8, UTF-16 (with BOM) or Windows-1252.', 'catalogrelay' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } try { $res = StreamTranscoder::run( $orig, $norm, $enc ); if ( $auto && EncodingDetector::UTF8 === $enc && ! empty( $res['replaced'] ) && 1 !== preg_match( '/[\x80-\xFF]/', $head ) ) { // The first 64 KB were plain ASCII, so they could not tell UTF-8 from Windows-1252, and the rest of the // file is not valid UTF-8: read it as Windows-1252 (design 6.1 "otherwise Windows-1252"). $enc = EncodingDetector::CP1252; $res = StreamTranscoder::run( $orig, $norm, $enc ); } if ( isset( $res['error'] ) ) { throw new UploadException( 'binary' === $res['error'] ? 'unsupported_format' : 'not_writable', 'binary' === $res['error'] ? __( 'The file contains binary data and is not a text CSV.', 'catalogrelay' ) : __( 'The file could not be converted.', 'catalogrelay' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } $sample = (string) file_get_contents( $norm, false, null, 0, 65536 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $delimiter = isset( $overrides['delimiter'] ) && in_array( $overrides['delimiter'], DelimiterSniffer::choices(), true ) ? (string) $overrides['delimiter'] : DelimiterSniffer::detect( $sample ); $insp = FileInspector::inspect( $norm, $delimiter ); if ( empty( $insp['ok'] ) ) { throw new UploadException( (string) $insp['error'], self::message( (string) $insp['error'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } } catch ( UploadException $e ) { if ( is_file( $norm ) ) { wp_delete_file( $norm ); } throw $e; } return array_merge( array( 'encoding' => $enc, 'sha256_src' => $res['sha256_src'], 'sha256_norm' => $res['sha256_norm'], ), self::fromInspection( $insp, $delimiter ) ); } /** * Put a new normalized file and its meta in place: meta first (the step that can fail on a full disk), then the * rename of normalized.csv.tmp, which needs no space. When the rename fails the previous meta is written back. * * @param string $ref Reference. * @param array $meta New meta. * @param array|null $old Previous meta (null for a new upload: the caller deletes the directory on failure). * @return void * @throws UploadException When a file cannot be written. */ private static function commit( string $ref, array $meta, ?array $old ): void { $tmp = FileStore::path( $ref, 'normalized.csv.tmp' ); try { FileStore::writeMeta( $ref, $meta ); if ( ! rename( $tmp, FileStore::path( $ref, 'normalized.csv' ) ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename if ( null !== $old ) { FileStore::writeMeta( $ref, $old ); } throw new UploadException( 'not_writable', __( 'The file could not be converted.', 'catalogrelay' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- the message is returned as JSON or escaped on render, never echoed raw. } } finally { if ( is_file( $tmp ) ) { wp_delete_file( $tmp ); } } } /** * Copy a source file through one open handle. A file of the incoming directory must still be the plain file that * was checked (not a link swapped in after IncomingDir::resolve()) and have no second name (a hard link to a file * elsewhere). A file that changes while it is copied (still being written) is refused. * * @param string $path Source. * @param string $dst Destination. * @param bool $incoming Whether the source is in the incoming directory. * @return string '' when copied; missing|changing|not_writable. */ public static function copyFrom( string $path, string $dst, bool $incoming ): string { if ( ! is_file( $path ) ) { return 'missing'; } $in = fopen( $path, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( ! $in ) { return 'missing'; } $before = fstat( $in ); $ok = is_array( $before ) && 0100000 === ( $before['mode'] & 0170000 ); if ( $ok && $incoming ) { $name = lstat( $path ); $ok = is_array( $name ) && 0100000 === ( $name['mode'] & 0170000 ) && $name['dev'] === $before['dev'] && $name['ino'] === $before['ino'] && $before['nlink'] <= 1; } if ( ! $ok ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return 'missing'; } $out = fopen( $dst, 'wb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $copied = $out ? stream_copy_to_stream( $in, $out ) : false; $closed = $out && fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose $after = fstat( $in ); fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose if ( false === $copied || ! $closed ) { return 'not_writable'; } if ( $copied !== $before['size'] || ! is_array( $after ) || $after['size'] !== $before['size'] || $after['mtime'] !== $before['mtime'] ) { return 'changing'; } return ''; } /** * Meta fields from an inspection. * * @param array $insp Inspection result. * @param string $delimiter Delimiter. * @return array */ private static function fromInspection( array $insp, string $delimiter ): array { return array( 'delimiter' => $delimiter, 'rows' => (int) $insp['rows'], 'header' => $insp['header'], 'header_end' => (int) $insp['header_end'], ); } /** * Message of an inspection error. * * @param string $code Code. * @return string */ public static function message( string $code ): string { switch ( $code ) { case 'empty_file': return __( 'The file is empty (0 bytes).', 'catalogrelay' ); case 'header_only': return __( 'The file has a header row but no data rows.', 'catalogrelay' ); case 'unclosed_quote': return __( 'The file has a quoted field that is never closed, so the rows after it cannot be read. Fix the quotes and upload again.', 'catalogrelay' ); } return __( 'The file could not be read.', 'catalogrelay' ); } /** * Fields exposed to the client: no paths, hashes of the source only. * * @param array $meta Meta. * @return array */ private static function publicInfo( array $meta ): array { $ref = (string) $meta['ref']; $delim = (string) $meta['delimiter']; $rows = array(); $path = FileStore::path( $ref, 'normalized.csv' ); if ( is_file( $path ) ) { $reader = new CsvReader( $path, $delim ); $reader->seek( 0 ); for ( $i = 0; $i < 6; $i++ ) { $rec = $reader->read(); if ( null === $rec ) { break; } if ( $rec['blank'] ) { continue; } $rows[] = array_map( 'strval', $rec['cells'] ); } } return array( 'upload_ref' => $ref, 'name' => (string) ( $meta['name'] ?? '' ), 'encoding' => (string) $meta['encoding'], 'delimiter' => $delim, 'rows' => (int) $meta['rows'], 'header' => array_map( 'strval', (array) $meta['header'] ), 'preview' => array_slice( $rows, 1, 5 ), 'sha256' => (string) $meta['sha256_norm'], 'size' => (int) ( $meta['size'] ?? 0 ), 'created' => (string) ( $meta['created'] ?? '' ), ); } }