false, 'error' => 'empty_file', ); } if ( self::unclosedQuote( $path, $delimiter, self::MAX_RECORD_BYTES ) ) { return array( 'ok' => false, 'error' => 'unclosed_quote', ); } $reader = new CsvReader( $path, $delimiter ); $header = null; $header_end = 0; $rows = 0; $records = 0; while ( true ) { $rec = $reader->read(); if ( null === $rec ) { break; } if ( $rec['next'] - $rec['off'] > self::MAX_RECORD_BYTES ) { return array( 'ok' => false, 'error' => 'unclosed_quote', ); } if ( null === $header ) { if ( $rec['blank'] ) { continue; } $header = $rec['cells']; $header_end = $rec['next']; continue; } ++$records; if ( ! $rec['blank'] ) { ++$rows; } } if ( null === $header ) { return array( 'ok' => false, 'error' => 'empty_file', ); } if ( 0 === $rows ) { return array( 'ok' => false, 'error' => 'header_only', ); } return array( 'ok' => true, 'rows' => $rows, 'records' => $records, 'header' => array_map( 'strval', $header ), 'header_end' => $header_end, ); } /** * Whether a quoted field is never closed. A quote opens a field only at the start of a field (after a delimiter, * a line break or the file start), a doubled quote inside a quoted field is an escaped quote. With $maxRecord, a * record that grows past that many bytes counts as broken too, so it is refused before fgetcsv() buffers it whole. * * @param string $path Path. * @param string $delimiter Delimiter. * @param int $maxRecord Largest record in bytes (0: no limit). * @return bool */ public static function unclosedQuote( string $path, string $delimiter = ',', int $maxRecord = 0 ): bool { $fh = fopen( $path, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( ! $fh ) { return false; } $in_quote = false; $pending = false; // Saw a quote inside a quoted field at the end of the previous block. $prev = "\n"; // Last character of the previous blocks that is not a space or a tab used as padding. $base = 0; // File offset of the block. $rec = 0; // File offset where the current record started. while ( ! feof( $fh ) ) { $buf = fread( $fh, 65536 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread if ( false === $buf || '' === $buf ) { break; } $len = strlen( $buf ); $pos = 0; if ( $pending ) { $pending = false; if ( '"' === $buf[0] ) { $pos = 1; // Escaped quote. } else { $in_quote = false; // The quote closed the field. } } while ( $pos < $len ) { $q = strpos( $buf, '"', $pos ); if ( $maxRecord > 0 && ! $in_quote ) { // A line break outside a quoted field ends the record. $seg = (string) substr( $buf, $pos, ( false === $q ? $len : $q ) - $pos ); $nl = strrpos( $seg, "\n" ); if ( false !== $nl ) { $rec = $base + $pos + $nl + 1; } } if ( false === $q ) { break; } if ( $in_quote ) { if ( $q + 1 >= $len ) { $pending = true; $pos = $len; break; } if ( '"' === $buf[ $q + 1 ] ) { $pos = $q + 2; continue; } $in_quote = false; $pos = $q + 1; continue; } // Outside a quoted field: is this quote at the start of a field? Skip spaces and tabs before it (but not // the delimiter itself when it is a tab). $j = $q - 1; while ( $j >= 0 && self::padding( $buf[ $j ], $delimiter ) ) { --$j; } $before = $j >= 0 ? $buf[ $j ] : $prev; if ( "\n" === $before || "\r" === $before || $delimiter === $before ) { $in_quote = true; } $pos = $q + 1; } for ( $j = $len - 1; $j >= 0; $j-- ) { if ( ! self::padding( $buf[ $j ], $delimiter ) ) { $prev = $buf[ $j ]; break; } } $base += $len; if ( $maxRecord > 0 && $base - $rec > $maxRecord ) { fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return true; } } fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose // A quote in a quoted field at the very end of the file closes that field (a file without a final line break). return $in_quote && ! $pending; } /** * Whether a byte is padding before a field: a space, or a tab when the tab is not the delimiter. * * @param string $c Byte. * @param string $delimiter Delimiter. * @return bool */ private static function padding( string $c, string $delimiter ): bool { return $c !== $delimiter && ( ' ' === $c || "\t" === $c ); } }