|
|
|
@ -10,68 +10,106 @@ |
|
|
|
|
if(isset($data["action"])){ |
|
|
|
|
switch($data["action"]){ |
|
|
|
|
case "load_polls": |
|
|
|
|
$this->response(200, $this->get_polls($this->load_database())); |
|
|
|
|
self::response(200, [ |
|
|
|
|
"polls" => self::get_polls(self::load_database()), |
|
|
|
|
"ip" => self::get_ip() |
|
|
|
|
]); |
|
|
|
|
break; |
|
|
|
|
case "add_poll": |
|
|
|
|
$database = $this->load_database(); |
|
|
|
|
$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" => [] |
|
|
|
|
"options" => [], |
|
|
|
|
"ip" => self::get_ip(), |
|
|
|
|
"date" => time(), |
|
|
|
|
"deleted" => false |
|
|
|
|
]; |
|
|
|
|
$this->save_database($database); |
|
|
|
|
$this->response(200, $this->get_polls($database)); |
|
|
|
|
self::save_database($database); |
|
|
|
|
self::response(200, self::get_polls($database)); |
|
|
|
|
break; |
|
|
|
|
case "get_poll": |
|
|
|
|
$database = $this->load_database(); |
|
|
|
|
$database = self::load_database(); |
|
|
|
|
$poll = isset($database["polls"]) && isset($database["polls"][$data["i"]]) ? $database["polls"][$data["i"]] : null; |
|
|
|
|
$this->response($poll ? 200 : 404, $poll ?? [ |
|
|
|
|
self::response($poll ? 200 : 404, $poll ?? [ |
|
|
|
|
"message" => "poll_not_exists", |
|
|
|
|
"i" => $data["i"] |
|
|
|
|
]); |
|
|
|
|
break; |
|
|
|
|
case "add_option": |
|
|
|
|
$database = $this->load_database(); |
|
|
|
|
$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" => 0 |
|
|
|
|
"points" => [], |
|
|
|
|
"ip" => self::get_ip(), |
|
|
|
|
"date" => time(), |
|
|
|
|
"deleted" => false |
|
|
|
|
]; |
|
|
|
|
$this->save_database($database); |
|
|
|
|
$this->response(200, $database["polls"][$data["i"]]["options"]); |
|
|
|
|
self::save_database($database); |
|
|
|
|
self::response(200, $database["polls"][$data["i"]]["options"]); |
|
|
|
|
}else |
|
|
|
|
$this->response(404, [ |
|
|
|
|
self::response(404, [ |
|
|
|
|
"message" => "poll_not_exists", |
|
|
|
|
"name" => $data["name"] |
|
|
|
|
]); |
|
|
|
|
break; |
|
|
|
|
case "set_option": |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
$database = $this->load_database(); |
|
|
|
|
if(isset($database["polls"]) && isset($database["polls"][$data["name"]])){ |
|
|
|
|
$database["polls"][$data["name"]][$data["i"]]["points"] ++; |
|
|
|
|
$this->save_database($database); |
|
|
|
|
$this->response(200, null); |
|
|
|
|
$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 |
|
|
|
|
$this->response(404, [ |
|
|
|
|
self::response(404, [ |
|
|
|
|
"message" => "poll_not_exists", |
|
|
|
|
"name" => $data["name"] |
|
|
|
|
"i" => $data["i"], |
|
|
|
|
"j" => $data["j"] |
|
|
|
|
]); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
self::response(404, ["message" => "action_unknown"]); |
|
|
|
|
break; |
|
|
|
|
}; |
|
|
|
|
}else |
|
|
|
|
$this->response(405, ["message" => "no_action"]); |
|
|
|
|
self::response(405, ["message" => "no_action"]); |
|
|
|
|
}else |
|
|
|
|
$this->response(405, ["message" => "no_data"]); |
|
|
|
|
self::response(405, ["message" => "no_data"]); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function get_polls($database){ |
|
|
|
|
private static function get_polls($database){ |
|
|
|
|
return isset($database["polls"]) ? array_map(fn($poll) => ["name" => $poll["name"]], $database["polls"]) : []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function response($code, $data){ |
|
|
|
|
private static function response($code, $data){ |
|
|
|
|
|
|
|
|
|
echo json_encode([ |
|
|
|
|
"ok" => $code == 200, |
|
|
|
@ -82,12 +120,22 @@ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function load_database(){ |
|
|
|
|
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 function save_database($database){ |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}; |