nextcloud / files_texteditor

:page_facing_up: Text editor for plaintext files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File's content transformed in to "false" content

ivarsg opened this issue · comments

commented

When I try to open file for editing with "Edit in Plain Text editor", whatever is the actual content of the file, the content in text editor is just one word - "false".

After some research I came to these lines of source code, and there is, in my opinion something wrong:

$encoding = mb_detect_encoding($fileContents . 'a', 'UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII', true);
if ($encoding === '') {
// set default encoding if it couldn't be detected
$encoding = 'ISO-8859-15';
}
$fileContents = iconv($encoding, 'UTF-8', $fileContents);
return new DataResponse(
[
'filecontents' => $fileContents,

  1. since in line No 99 it is strict mode used, mb_detect_encoding() function will return either one of the encodings given in the input list, or FALSE; in case of FALSE or any valid result, the condition on line No 100 will NEVER evaluate to TRUE (some times in history, here was simple comparison, and in this case it evaluated to TRUE in case if $encoding holds false value);
  2. in case if iconv() function fails on line No 104 (as it is in may case; I can read error logs in NC logs), according to iconv() documentation it returns FALSE and this is how on line No 107 file's content is converted to single word "false".
  3. as per this answer on stackoverflow.com, most probably on line No 99 the passed in encoding list is not correct/supported by mb_detect_encoding() (here I agree that PHP documentation is a bit awkward - encodings currently implemented in mb_detect_encoding() are documented under mb_detect_order())

In my case valid UTF-8 content is detected as WINDOWS-1257, and then iconv() fails to convert actual UTF8 content treated as WINDOWS-1257 to UTF8. There is an error log entry in NC logs: Error: iconv(): Detected an illegal character in input string at /var/www/{******}/apps/files_texteditor/lib/Controller/FileHandlingController.php#104

Same problem here:

  1. The file was created with files_texteditor, I added some text, including some accentuated chars, euro symbol, etc.
    At this point, I still could open it without any error.
  2. I then added some pipe symbols (from CP437 encoding) and from then I couldn't open the file again, just the "false" at the begining of the file.
  3. I checked on the filesystem, the content of the file is complete, the reported encoding is UTF-8.

Actually, adding some traces to files_texteditor/lib/Controller/FileHandlingController.php,
I saw that my file is detected as Windows-1252 by mb_detect_encoding instead of UTF-8
I also tried to force UTF-8, and in this case, the file is displayed correctly:

$encoding = 'UTF-8';
$fileContents = iconv($encoding, 'UTF-8', $fileContents);

Same here. UTF-8 is detected as Windows-1257.
When removing Windows-1257 from the list, mb_detect_encoding() detects correct UTF-8.