From face65de2254eb40ddcfd93f8d12dc1c0582a130 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Fri, 23 Sep 2022 16:42:06 +0200 Subject: utf8-safe versions of typical php input functions --- it.class | 21 +++++++++++++++++++-- test/it.t | 5 ++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/it.class b/it.class index 148b08f..2079e84 100644 --- a/it.class +++ b/it.class @@ -1335,7 +1335,7 @@ static function params2utf8() foreach (['PHP_SELF', 'SCRIPT_NAME', 'SCRIPT_URL', 'SCRIPT_URI', 'HTTP_USER_AGENT'] as $var) $_SERVER[$var] = it::any2utf8($_SERVER[$var]); - $urlfix = function($m) { return urlencode(it::any2utf8(urldecode($m[0]))); }; + $urlfix = function($m) { return urlencode(it::any2utf8(urldecode($m[0]))); }; # NOPHPLINT foreach (['QUERY_STRING', 'REQUEST_URI', 'HTTP_REFERER'] as $var) { $_SERVER[$var.'_RAW'] = $_SERVER[$var]; @@ -1343,7 +1343,7 @@ static function params2utf8() if (strpos($_SERVER[$var], '%') !== false) { - if (grapheme_strlen(urldecode($_SERVER[$var])) === null) # handle latin (double encodes correct utf8) + if (grapheme_strlen(urldecode($_SERVER[$var])) === null) # NOPHPLINT handle latin (double encodes correct utf8) $_SERVER[$var] = preg_replace_callback('/%[89A-F][A-Z0-9]/i', $urlfix, $_SERVER[$var]); while (preg_match('/%C3%8[23]%C2%[89ab][0-9a-f]/i', $_SERVER[$var]) && $iterations++ < 3) # handle doubly encoded utf8, UTF8SAFE $_SERVER[$var] = preg_replace_callback('/%C3%8[23]%C2%[89ab][0-9a-f]/i', $urlfix, $_SERVER[$var]); @@ -1366,4 +1366,21 @@ static function setcookie($name, $value, $p = []) : @setcookie($name, $value, $p); } +static function urldecode($str) +{ + return it::any2utf8(urldecode($str)); # NOPHPLINT +} + +static function parse_str($str) # note different usage +{ + parse_str($str, $request); # NOPHPLINT + + return it::any2utf8($request); +} + +static function request_body() +{ + return it::any2utf8(it::file_get_contents('php://input')); +} + } diff --git a/test/it.t b/test/it.t index 19f445f..0aee958 100755 --- a/test/it.t +++ b/test/it.t @@ -539,7 +539,7 @@ function requesturi($teststring, $expect) { $_SERVER['REQUEST_URI'] = urlencode($teststring); it::params2utf8(); - is(urldecode($_SERVER['REQUEST_URI']), $expect, "parms2utf8('$expect')"); + is(urldecode($_SERVER['REQUEST_URI']), $expect, "parms2utf8('$expect')"); # NOPHPLINT } _getopt(["val"], ['foo' => "val"]); @@ -567,3 +567,6 @@ function _getopt($in, $expect) is($opts, ['args' => $expect['args'] ?? []] + $expect); } + +is(it::urldecode("a%61%E4"), "aaä"); +is(it::parse_str("%E4[%E4][%E4]=%E4"), ['ä' => ['ä' => ['ä' => 'ä']]]); -- cgit v1.2.3