diff --git a/core/stdlib_ex.php b/core/stdlib_ex.php index 0122d3fd..516d0ea0 100644 --- a/core/stdlib_ex.php +++ b/core/stdlib_ex.php @@ -5,10 +5,14 @@ * @param T|false $x * @return T */ -function false_throws(mixed $x): mixed +function false_throws(mixed $x, ?callable $errorgen = null): mixed { if($x === false) { - throw new \Exception("Unexpected false"); + $msg = "Unexpected false"; + if($errorgen) { + $msg = $errorgen(); + } + throw new \Exception($msg); } return $x; } @@ -18,10 +22,14 @@ function false_throws(mixed $x): mixed * @param T|null $x * @return T */ -function null_throws(mixed $x): mixed +function null_throws(mixed $x, ?callable $errorgen = null): mixed { if($x === null) { - throw new \Exception("Unexpected null"); + $msg = "Unexpected null"; + if($errorgen) { + $msg = $errorgen(); + } + throw new \Exception($msg); } return $x; } @@ -31,7 +39,7 @@ function null_throws(mixed $x): mixed */ function json_encode_ex(mixed $value, int|null $flags = 0, int $depth = 512): string { - return false_throws(json_encode($value, $flags, $depth)); + return false_throws(json_encode($value, $flags, $depth), "json_last_error_msg"); } function strtotime_ex(string $time, int|null $now = null): int diff --git a/core/sys_config.php b/core/sys_config.php index 9ee7e518..2af3ac69 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -31,7 +31,7 @@ _d("DEBUG", false); // boolean print various debugging details _d("COOKIE_PREFIX", 'shm'); // string if you run multiple galleries with non-shared logins, give them different prefixes _d("SPEED_HAX", false); // boolean do some questionable things in the name of performance _d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse -_d("VERSION", "2.10.3"); // string shimmie version +_d("VERSION", "2.11.0-alpha"); // string shimmie version _d("TIMEZONE", null); // string timezone _d("EXTRA_EXTS", ""); // string optional extra extensions _d("BASE_HREF", null); // string force a specific base URL (default is auto-detect) diff --git a/core/tests/StdLibExTest.php b/core/tests/StdLibExTest.php new file mode 100644 index 00000000..c6a3476c --- /dev/null +++ b/core/tests/StdLibExTest.php @@ -0,0 +1,27 @@ +assertEquals( + '{"a":1,"b":2,"c":3,"d":4,"e":5}', + json_encode_ex(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]) + ); + } + + public function testJsonEncodeError(): void + { + $e = $this->assertException(\Exception::class, function () { + json_encode_ex("\xB1\x31"); + }); + $this->assertEquals( + "Malformed UTF-8 characters, possibly incorrectly encoded", + $e->getMessage() + ); + } +} diff --git a/ext/comment/theme.php b/ext/comment/theme.php index f5ac2030..14ad0ec8 100644 --- a/ext/comment/theme.php +++ b/ext/comment/theme.php @@ -255,7 +255,7 @@ class CommentListTheme extends Themelet $h_del = ""; if ($user->can(Permissions::DELETE_COMMENT)) { $comment_preview = substr(html_unescape($tfe->stripped), 0, 50); - $j_delete_confirm_message = json_encode_ex("Delete comment by {$comment->owner_name}:\n$comment_preview"); + $j_delete_confirm_message = json_encode("Delete comment by {$comment->owner_name}:\n$comment_preview") ?: "Delete "; $h_delete_script = html_escape("return confirm($j_delete_confirm_message);"); $h_delete_link = make_link("comment/delete/$i_comment_id/$i_image_id"); $h_del = " - Del";