self::get_polls(self::load_database()), "ip" => self::get_ip() ]); break; case "add_poll": $database = self::load_database(); isset($database["polls"]) || ($database["polls"] = []); foreach($database["polls"] as $poll) $poll["name"] == $data["name"] && self::response(400, [ "message" => "poll_already_exists", "name" => $data["name"] ]); $database["polls"][] = [ "name" => $data["name"], "options" => [], "ip" => self::get_ip(), "date" => time(), "deleted" => false ]; self::save_database($database); self::response(200, self::get_polls($database)); break; case "get_poll": $database = self::load_database(); $poll = isset($database["polls"]) && isset($database["polls"][$data["i"]]) ? $database["polls"][$data["i"]] : null; self::response($poll ? 200 : 404, $poll ?? [ "message" => "poll_not_exists", "i" => $data["i"] ]); break; case "add_option": $database = self::load_database(); if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){ foreach($database["polls"][$data["i"]]["options"] as $option) $option["text"] == $data["text"] && self::response(400, [ "message" => "poll_option_already_exists", "text" => $data["text"] ]); $database["polls"][$data["i"]]["options"][] = [ "text" => $data["text"], "points" => [], "ip" => self::get_ip(), "date" => time(), "deleted" => false ]; self::save_database($database); self::response(200, $database["polls"][$data["i"]]["options"]); }else self::response(404, [ "message" => "poll_not_exists", "name" => $data["name"] ]); break; case "set_option": $database = self::load_database(); if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){ $ip = self::get_ip(); $done = false; $date = time(); foreach($database["polls"][$data["i"]]["options"][$data["j"]]["points"] as $i => $option) if($done = $option["ip"] == $ip){ $database["polls"][$data["i"]]["options"][$data["j"]]["points"][$i]["selected"] = $data["selected"]; $database["polls"][$data["i"]]["options"][$data["j"]]["points"][$i]["changes"] ++; $database["polls"][$data["i"]]["options"][$data["j"]]["points"][$i]["date_last"] = $date; break; }; !$done && ($database["polls"][$data["i"]]["options"][$data["j"]]["points"][] = [ "ip" => $ip, "selected" => $data["selected"], "date_in" => $date, "date_last" => $date, "changes" => 1, "deleted" => false ]); self::save_database($database); self::response(200, $database["polls"][$data["i"]]["options"]); }else self::response(404, [ "message" => "poll_not_exists", "i" => $data["i"], "j" => $data["j"] ]); break; default: self::response(404, ["message" => "action_unknown"]); break; }; }else self::response(405, ["message" => "no_action"]); }else self::response(405, ["message" => "no_data"]); } private static function get_polls($database){ return isset($database["polls"]) ? array_map(fn($poll) => ["name" => $poll["name"]], $database["polls"]) : []; } private static function response($code, $data){ echo json_encode([ "ok" => $code == 200, "code" => $code, "content" => $data ]); exit(0); } private static function load_database(){ return file_exists(\GamUsino\Secrets::settings["database_path"]) ? json_decode(file_get_contents(\GamUsino\Secrets::settings["database_path"]), true) : []; } private static function save_database($database){ file_put_contents(\GamUsino\Secrets::settings["database_path"], json_encode($database)); } public static function get_ip(){ foreach(["HTTP_X_REAL_IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR", "REMOTE_ADDR"] as $key){ if(!empty($_SERVER[$key])) return explode(",", $_SERVER[$key])[0]; if($ips = getenv($key)) return explode(",", $ips)[0]; }; return null; } };