p0wny-shell icon indicating copy to clipboard operation
p0wny-shell copied to clipboard

An error is thrown if a file contains non UTF-8 characters

Open matjazpotocnik opened this issue 5 years ago • 1 comments

File and directory names on Windows might not be UTF-8 encoded and json_encode() fails. This is what I have come up with (around line 100):

    $encoded = json_encode($response);
    if(!$encoded) {
        //replace "unknown" charaters with ?
        $i = 0;
        foreach($response['stdout'] as $v) {
            $response['stdout'][$i] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '?', $v);
            $i++;
        }
      $encoded = json_encode($response);
    }

    header("Content-Type: application/json");
    echo $encoded;
    die();

matjazpotocnik avatar Mar 23 '19 03:03 matjazpotocnik

Do not hesitate to make a PR to fix the encoding on Windows :)

flozz avatar Feb 02 '21 12:02 flozz

A possible workaround to this problem would be to encode the stdout result with base64 or hex to ensure that no invalid characters are encoded with json. The client side would have to decode the json field stdout with base64 or hex and could then output it again. The same procedure is already performed with the files.

cli-ish avatar May 11 '23 16:05 cli-ish

@flozz Would this be a desired function as I described?

cli-ish avatar May 22 '23 17:05 cli-ish

Yeah probably encoding stdout/stderr to base64 could save us here... Do you think paths (cwd) should be encoded too? :)

flozz avatar May 23 '23 11:05 flozz

Not sure, could be a potential problem. Would this be worth a PR from my side?

cli-ish avatar May 25 '23 12:05 cli-ish

Yes please make a PR if you want to implement a fix for this issue :)

flozz avatar May 26 '23 12:05 flozz

Fix released in v2023.05.28 thanks to @cli-ish :)

flozz avatar May 28 '23 12:05 flozz