CSV imports keep accented letters: csvTextToUtf8 reads an upload as UTF-8 whether it was saved as UTF-8, UTF-16, Windows-1252 or Mac Roman, used by every import window kind, bank statements on Expenses and Income, and the network IP and interface imports; tab separated files import; the import answer survives a bad byte; 26.12.6 changelog and CONTRIBUTING rule

This commit is contained in:
johnnyq
2026-12-04 11:55:35 -05:00
parent 76c572c67e
commit c4e486a652
7 changed files with 92 additions and 18 deletions

View File

@@ -69,9 +69,9 @@ function importHeadingKey($heading) {
/**
* The uploaded file ($_FILES[$field]) - [$headings, $rows, $error]; $rows
* are [line in the file, cells]. Comma or semicolon separated, whichever its
* heading line has more of; a byte-order mark is skipped, and so is a row with
* nothing in it.
* are [line in the file, cells]. Read as UTF-8 whatever the spreadsheet saved
* it as (csvTextToUtf8). Comma, semicolon or tab separated, whichever its
* heading line has most of; a row with nothing in it is skipped.
*/
function importFile($field = 'file') {
$upload = $_FILES[$field] ?? null;
@@ -84,17 +84,23 @@ function importFile($field = 'file') {
if (intval($upload['size']) > IMPORT_MAX_BYTES) {
return [[], [], 'The file is over ' . (IMPORT_MAX_BYTES / 1048576) . ' MB - split it and import each part.'];
}
$handle = fopen($upload['tmp_name'], 'r');
$first = $handle ? fgets($handle) : false;
if ($first === false || trim($first) === '') {
$text = str_replace(["\r\n", "\r"], "\n", csvTextToUtf8((string) @file_get_contents($upload['tmp_name'])));
$first = strstr($text . "\n", "\n", true);
if (trim($first) === '') {
return [[], [], 'The file is empty.'];
}
$delimiter = substr_count($first, ';') > substr_count($first, ',') ? ';' : ',';
$delimiter = ',';
foreach ([';', "\t"] as $candidate) {
if (substr_count($first, $candidate) > substr_count($first, $delimiter)) {
$delimiter = $candidate;
}
}
$handle = fopen('php://temp', 'r+');
fwrite($handle, $text);
rewind($handle);
$headings = array_map(function ($heading) {
return trim((string) $heading);
}, (array) fgetcsv($handle, 0, $delimiter, '"', ''));
$headings[0] = trim(preg_replace('/^\xEF\xBB\xBF/', '', $headings[0] ?? ''));
$rows = [];
$line = 1;
while (($cells = fgetcsv($handle, 0, $delimiter, '"', '')) !== false) {
@@ -118,6 +124,63 @@ function importFile($field = 'file') {
return [$headings, $rows, ''];
}
/**
* A CSV file's text as UTF-8, whatever the spreadsheet or bank saved it as:
* UTF-8 as it is (its byte order mark dropped), UTF-16 by its byte order mark
* (Excel's "Unicode Text"), and otherwise Windows-1252 (Excel's plain "CSV" on
* Windows, Outlook) or Mac Roman (Excel's plain "CSV" on a Mac), whichever
* shows fewer signs of a wrong reading (csvTextMisreads) - Windows-1252 when
* it's a tie.
*/
function csvTextToUtf8($text) {
$text = (string) $text;
if (strncmp($text, "\xEF\xBB\xBF", 3) === 0) {
$text = substr($text, 3);
} elseif (strncmp($text, "\xFF\xFE", 2) === 0 || strncmp($text, "\xFE\xFF", 2) === 0) {
return (string) mb_convert_encoding(substr($text, 2), 'UTF-8', $text[0] === "\xFF" ? 'UTF-16LE' : 'UTF-16BE');
}
if (mb_check_encoding($text, 'UTF-8')) {
return $text;
}
static $mac_roman = null;
if ($mac_roman === null) {
$mac_roman = [];
foreach ([
0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8,
0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC,
0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8,
0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8,
0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153,
0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02,
0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4,
0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7,
] as $i => $codepoint) {
$mac_roman[chr(0x80 + $i)] = mb_chr($codepoint, 'UTF-8');
}
}
$windows = (string) mb_convert_encoding($text, 'UTF-8', 'Windows-1252');
$mac = strtr($text, $mac_roman);
return csvTextMisreads($mac) < csvTextMisreads($windows) ? $mac : $windows;
}
/**
* How many signs $text shows of having been read in the wrong encoding: a
* control character (a byte Windows-1252 doesn't have), a capital in the
* middle of a word ("MŸller", "FranÁois"), symbols in the middle of a word
* ("J¸rgen", "S¿ren", "Pe–a" - but not an apostrophe, em dash, ellipsis or
* trademark, which real text has there), an opening quote or a letter Western
* words don't have right after a letter ("Zo‘", "Malmš", "Strafle"), and an
* "œ" ending a word.
*/
function csvTextMisreads($text) {
$odd_symbol = '[^\x00-\x7F\p{L}\x{A0}\x{A9}\x{AE}\x{2014}\x{2019}\x{2026}\x{2122}]';
return intval(preg_match_all('/[\x{80}-\x{9F}]'
. '|(?<=\p{Ll})[^\x00-\x7F\P{Lu}]|(?<=\p{L})[^\x00-\x7F\P{Lu}](?=\p{Ll})'
. '|(?<=\p{L})(?!\x{2013}\p{L})' . $odd_symbol . '+(?=\p{L})|(?<=\p{Ll})\x{2013}(?=\p{Ll})'
. '|(?<=\p{L})[\x{2018}\x{201C}\x{0161}\x{017E}\x{0192}\x{02C6}\x{0131}\x{FB01}\x{FB02}]'
. '|(?<=\p{L})\x{0153}(?!\p{L})/u', (string) $text));
}
/** Which field each of the file's columns is, by its heading - column => field key. The first column for a field takes it. */
function importAutoMap(array $def, array $headings) {
$lookup = [];