return json_encode($result); }; /* POST request: Add a new topic * Parameters: * posted_by * title * text * * Returns: * success / failure */ $addTopic = function ($sql) { $_POST = json_decode(file_get_contents('php://input'), true); /* Parameters */ $expected = array("title", "text"); $params = fetchPostParams($expected); /* Check all parameters were included and set */ foreach ($expected as $expect) { if (!isset($params[$expect])) { die("Error: {$expect} parameter was not set."); } } $cookie = validateCookie(); if (!$cookie) { header('HTTP/1.1 401 Unauthorized'); die("Invalid cookie"); } $posted_by = $cookie["id"]; $title = $params["title"]; $text = $params["text"]; /* Sql escaping */
function checkAndGetParams($expected, $optional) { $params = fetchPostParams($expected); $optional_params = fetchPostParams($optional); //Check all parameters were included and set foreach ($expected as $expect) { if (!isset($params[$expect])) { die("Error: {$expect} parameter was not set."); } } return [$params, $optional_params]; }