$headers Header row. * @param array $rows Data rows. * @return string */ public static function generate_csv( array $headers, array $rows ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $output = fopen( 'php://temp', 'r+' ); if ( false === $output ) { return ''; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite fwrite( $output, self::BOM ); fputcsv( $output, $headers, ',', '"', '\\' ); foreach ( $rows as $row ) { fputcsv( $output, array_map( array( __CLASS__, 'escape_formula' ), array_values( (array) $row ) ), ',', '"', '\\' ); } rewind( $output ); $csv = stream_get_contents( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $output ); return false === $csv ? '' : $csv; } /** * Send a CSV download and exit. * * @param string $filename File name. * @param array $headers Header row. * @param array $rows Data rows. * @return void */ public static function send_download( $filename, array $headers, array $rows ) { nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $filename ) . '"' ); header( 'X-Content-Type-Options: nosniff' ); echo self::generate_csv( $headers, $rows ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV body. exit; } /** * Validate an uploaded file from $_FILES. * * @param array $file $_FILES entry. * @return string|\WP_Error Temporary file path. */ public static function validate_upload( $file ) { if ( ! is_array( $file ) || empty( $file['tmp_name'] ) || ! isset( $file['error'] ) ) { return new \WP_Error( 'no_file', __( 'Choose a CSV file to upload.', 'slugh' ) ); } if ( UPLOAD_ERR_OK !== (int) $file['error'] ) { return new \WP_Error( 'upload_error', __( 'The upload failed. The file may be larger than the server allows.', 'slugh' ) ); } if ( (int) $file['size'] > self::MAX_SIZE ) { return new \WP_Error( 'too_large', __( 'The file is larger than 5 MB.', 'slugh' ) ); } if ( ! is_uploaded_file( $file['tmp_name'] ) ) { return new \WP_Error( 'invalid_upload', __( 'Invalid upload.', 'slugh' ) ); } $name = isset( $file['name'] ) ? (string) $file['name'] : ''; if ( 'csv' !== strtolower( pathinfo( $name, PATHINFO_EXTENSION ) ) ) { return new \WP_Error( 'not_csv', __( 'Only .csv files are accepted.', 'slugh' ) ); } $check = wp_check_filetype_and_ext( $file['tmp_name'], $name, array( 'csv' => 'text/csv' ) ); if ( empty( $check['ext'] ) || 'csv' !== $check['ext'] ) { return new \WP_Error( 'not_csv', __( 'The file does not look like a CSV file.', 'slugh' ) ); } return $file['tmp_name']; } /** * Parse a CSV file with fgetcsv(). The temporary file is deleted afterwards. * * Header names are trimmed and lowercased; unknown columns are kept but callers ignore them. * * @param string $path File path. * @param bool $delete_file Delete the file after parsing. * @return array{headers: string[], rows: array}>}|\WP_Error */ public static function parse_file( $path, $delete_file = true ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $handle = fopen( $path, 'r' ); if ( false === $handle ) { return new \WP_Error( 'read_error', __( 'The file could not be read.', 'slugh' ) ); } $headers = array(); $rows = array(); $line = 0; while ( false !== ( $cells = fgetcsv( $handle, 0, ',', '"', '\\' ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition ++$line; if ( 1 === $line && isset( $cells[0] ) ) { $cells[0] = preg_replace( '/^\xEF\xBB\xBF/', '', (string) $cells[0] ); } if ( array( null ) === $cells || ( 1 === count( $cells ) && '' === trim( (string) $cells[0] ) ) ) { continue; } if ( empty( $headers ) ) { $headers = array_map( function ( $header ) { return strtolower( trim( (string) $header ) ); }, $cells ); continue; } $data = array(); foreach ( $headers as $index => $header ) { $data[ $header ] = isset( $cells[ $index ] ) ? trim( (string) $cells[ $index ] ) : ''; } if ( ! \SlugH\Redirects\Normalizer::is_utf8( implode( '', $data ) ) ) { $data['__invalid_encoding'] = '1'; } $rows[] = array( 'line' => $line, 'data' => $data, ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $handle ); if ( $delete_file ) { wp_delete_file( $path ); } if ( empty( $headers ) ) { return new \WP_Error( 'empty_file', __( 'The file is empty or has no header row.', 'slugh' ) ); } return array( 'headers' => $headers, 'rows' => $rows, ); } }