Skip to content

Csv Reader Allow Use of mimetype=text/html Files Without Extension #4040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- POWER Null/Bool Args. [PR #4031](https://github.com/PHPOffice/PhpSpreadsheet/pull/4031)
- Do Not Output Alignment and Protection for Conditional Format. [Issue #4025](https://github.com/PHPOffice/PhpSpreadsheet/issues/4025) [PR #4027](https://github.com/PHPOffice/PhpSpreadsheet/pull/4027)
- Xls Conditional Format Improvements. [PR #4030](https://github.com/PHPOffice/PhpSpreadsheet/pull/4030)
- Csv Reader allow use of html mimetype. [Issue #4036](https://github.com/PHPOffice/PhpSpreadsheet/issues/4036) [PR #4049](https://github.com/PHPOffice/PhpSpreadsheet/pull/4040)

## 2024-05-11 - 2.1.0

Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ public function canRead(string $filename): bool
'text/csv',
'text/plain',
'inode/x-empty',
'text/html',
];

return in_array($type, $supportedTypes, true);
Expand Down
1 change: 0 additions & 1 deletion src/PhpSpreadsheet/Writer/ZipStream3.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PhpOffice\PhpSpreadsheet\Writer;

use ZipStream\Option\Archive;
use ZipStream\ZipStream;

class ZipStream3
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpSpreadsheetTests/IOFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public static function providerIdentify(): array
//['samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
['samples/templates/46readHtml.html', 'Html', Reader\Html::class],
['tests/data/Reader/CSV/encoding.utf8bom.csv', 'Csv', Reader\Csv::class],
['tests/data/Reader/HTML/charset.UTF-16.lebom.html', 'Html', Reader\Html::class],
['tests/data/Reader/HTML/charset.UTF-8.bom.html', 'Html', Reader\Html::class],
];
}

Expand Down
83 changes: 83 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Csv/NotHtmlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Csv as CsvReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PHPUnit\Framework\TestCase;

class NotHtmlTest extends TestCase
{
private string $tempFile = '';

protected function tearDown(): void
{
if ($this->tempFile !== '') {
unlink($this->tempFile);
$this->tempFile = '';
}
}

public function testHtmlCantRead(): void
{
// This test has a file which IOFactory will identify as Csv.
// So file can be read using either Csv Reader or IOFactory.
$this->tempFile = $filename = File::temporaryFilename();
$cells = [
['1', '<a href="http://example.com">example</a>', '3'],
['4', '5', '6'],
];
$handle = fopen($filename, 'wb');
self::assertNotFalse($handle);
foreach ($cells as $row) {
fwrite($handle, "{$row[0]},{$row[1]},{$row[2]}\n");
}
fclose($handle);
// Php8.3- identify file as text/html.
// Php8.4+ identify file as text/csv, and this type of change
// has been known to be retrofitted to prior versions.
$mime = mime_content_type($filename);
if ($mime !== 'text/csv') {
self::assertSame('text/html', $mime);
}
self::assertSame('Csv', IOFactory::identify($filename));
$reader = new CsvReader();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame($cells, $sheet->toArray());
$spreadsheet->disconnectWorksheets();
}

public function testHtmlCanRead(): void
{
// This test has a file which IOFactory will identify as Html.
// So file has to be read using Csv Reader, not IOFactory.
$this->tempFile = $filename = File::temporaryFilename();
$cells = [
['<a href="http://example.com">example</a>', '<div>hello', '3'],
['4', '5', '</div>'],
];
$handle = fopen($filename, 'wb');
self::assertNotFalse($handle);
foreach ($cells as $row) {
fwrite($handle, "{$row[0]},{$row[1]},{$row[2]}\n");
}
fclose($handle);
// Php8.3- identify file as text/html.
// Php8.4+ identify file as text/csv, and this type of change
// has been known to be retrofitted to prior versions.
$mime = mime_content_type($filename);
if ($mime !== 'text/csv') {
self::assertSame('text/html', $mime);
}
self::assertSame('Html', IOFactory::identify($filename));
$reader = new CsvReader();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame($cells, $sheet->toArray());
$spreadsheet->disconnectWorksheets();
}
}
Loading