!" * * If category does not correspond to one of the available categories on the server, * returns a 400 response. */ if (isset($_GET["mode"])) { $mode = strtolower($_GET["mode"]); $result; if ($mode == "categories") { $result = get_categories(); } else if ($mode == "category") { $result = get_category(); } if ($result) { header("Content-type: application/json"); echo $result; } else { print_error("Invalid mode passed. Please pass mode as 'categories' or 'category'."); } } else if (isset($_POST["category"]) && isset($_POST["question"]) && isset($_POST["answer"])) { add_new_qa(); } else { print_error("Missing required GET or POST parameters."); } /** * Returns a JSON object containing all categories in the trivia folder path * mapping each category name to the number of files of trivia data for that category. * @return {object} - JSON-formatted object with schema: * { * "categories" : [ * { : }, * { : }, * ... * { : } * ] * } */ function get_categories() { $triviafiles = "trivia/"; $categories = array_diff(scandir($triviafiles), array('..', '.')); $result["categories"] = []; foreach ($categories as $category) { $filecount = count(glob($triviafiles . $category . "/*.txt")); $result["categories"][$category] = $filecount; } return json_encode($result); } /** * Returns a JSON object for a trivia category containing a random question/answer. * If name is passed as a query parameter with a value, returns a question/answer * for a random trivia file with the category name. If the name does not correspond to * the category name passed, outputs an 400 error. If no name query parameter is passed, * returns a random question/answer from any of the trivia categories on the server. * * @return {object} - JSON-formatted object with schema: * { * "question" : , * "answer" : * } */ function get_category() { $triviafiles = "trivia/"; if (!isset($_GET["name"])) { $categories = scandir($triviafiles); $category_name = $categories[array_rand($categories)]; } else { $category_name = strtolower($_GET["name"]); } $trivia = glob("trivia/" . $category_name . "/*.txt"); if (count($trivia) == 0) { print_error("No trivia found for the requested category."); } $index = array_rand($trivia); $random_trivia = $trivia[$index]; $qfile = file_get_contents($random_trivia); $lines = explode("\n", $qfile); $question = $lines[0]; $answer = $lines[1]; return json_encode(array("question" => $question, "answer" => $answer)); } /** * Adds a new Q/A entry for the category name passed as a POST parameter (required). * Outputs plain text response upon success, or 400 error if unknown category is passed. */ function add_new_qa() { $category = strtolower($_POST["category"]); if (file_exists("trivia/{$category}")) { $newq = $_POST["question"]; $newa = $_POST["answer"]; $trivia = glob($triviafiles . $category . "/*.txt"); $trivia_num = count($trivia_files); $new_trivia_file = "trivia/{$category}/" . strtolower($category) . $trivia_num . ".txt"; file_put_contents($new_trivia_file, "Q: {$newq}\nA: {$newa}"); header("Content-type: text/plain"); echo "New entry added for {$category} category!"; } else { print_error("Unknown category: {$category}"); } } /** * Outputs a 400 error with the given $msg text (plain text output). * @param $msg - error message output. */ function print_error($msg) { header("HTTP/1.1 400 Invalid Request"); header("Content-type: text/plain"); die($msg); } ?>