From ce123807279506d10f79fdf7214b1ea12b654648 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 11 Dec 2024 22:05:51 -0500 Subject: switch to POST for posting comments --- src/web/core/controller.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/web/core') diff --git a/src/web/core/controller.php b/src/web/core/controller.php index ac1e458..ca892e2 100644 --- a/src/web/core/controller.php +++ b/src/web/core/controller.php @@ -39,4 +39,27 @@ abstract class Controller extends Component { die(); } + /** + * Returns HTTP POST information if POST request. + * Returns 405 Method Not Allowed if not. + * + * If $key is specified, returns only that key. otherwise + * returns HTTP 400 Bad Request; + */ + protected function post_data(?string $key = NULL): array|string + { + // only post requests allowed + if ($_SERVER['REQUEST_METHOD'] != 'POST') + $this->error(405); + + // return entire $_POST array + if (!$key) + return $_POST; + + if (!isset($_POST[$key])) + $this->error(400); + + return $_POST[$key]; + } + } -- cgit v1.2.3-freya