203 lines
6.5 KiB
PHP
203 lines
6.5 KiB
PHP
<?php
|
|
|
|
class DPTW{
|
|
|
|
private static $default_settings = [
|
|
"dictionary_urls" => "https://inciclopedia.org/wiki/Frikipedia",
|
|
"request_root" => "/api"
|
|
];
|
|
private $inputs = [];
|
|
private $request_variables = [];
|
|
|
|
public $dictionary;
|
|
|
|
public function __construct($inputs = null){
|
|
|
|
is_array($inputs) && ($this->inputs = $inputs);
|
|
|
|
class_exists('\DPTW\Dictionary') && ($this->dictionary = new \DPTW\Dictionary($this));
|
|
class_exists('\DPTW\MySQL') && ($this->mysql = new \DPTW\MySQL($this));
|
|
|
|
$route = substr($_SERVER["REQUEST_URI"], strlen($this->settings("request_root")));
|
|
($uri_variables_i = strpos($route, "?")) && ($route = substr($route, 0, $uri_variables_i));
|
|
|
|
isset(($_GET["v"])) && ($this->request_variables = json_decode(base64_decode($_GET["v"]), true));
|
|
|
|
switch($route){
|
|
case "/dictionary/update":
|
|
$this->dictionary->update();
|
|
break;
|
|
case "/sessions/update":
|
|
$this->session_update(true);
|
|
break;
|
|
case "/scores/set":
|
|
$this->set_score();
|
|
break;
|
|
case "/test/1":
|
|
$this->test_1();
|
|
break;
|
|
case "/scores/list":
|
|
$this->list_scores();
|
|
break;
|
|
default:
|
|
self::response(404, "unknown_route");
|
|
break;
|
|
};
|
|
|
|
}
|
|
|
|
public function request($key){
|
|
return isset($this->request_variables[$key]) ? $this->request_variables[$key] : null;
|
|
}
|
|
|
|
private static function save_file($path, $data){
|
|
|
|
$directory = "";
|
|
$slash = strpos($path, "/") ? "/" : "\\";
|
|
|
|
if(preg_match('/^[a-z]\:/', $path)){
|
|
$directory = substr($path, 0, 2);
|
|
$path = substr($path, 2);
|
|
};
|
|
|
|
$directory = preg_replace('/^(.+)[\/\\\\][^\/\\\\]+$/', "$1", $path);
|
|
!file_exists($directory) && mkdir($directory, 0777, true);
|
|
|
|
file_put_contents($path, $data);
|
|
|
|
}
|
|
|
|
public function settings($keys, $inputs = null){
|
|
|
|
if(is_string($keys))
|
|
$keys = [$keys];
|
|
elseif(!is_array($keys))
|
|
$keys = [];
|
|
|
|
foreach([$inputs, $this->inputs, \DPTW\Secrets::settings, self::$default_settings] as $subinputs)
|
|
if(is_array($subinputs))
|
|
foreach($keys as $key)
|
|
if(isset($subinputs[$key]))
|
|
return $subinputs[$key];
|
|
return null;
|
|
}
|
|
|
|
public static function string_variables($string, $variables){
|
|
return preg_replace_callback('/\{([^\{\}]+)\}/', function($values) use($variables){
|
|
return isset($variables[$values[1]]) ? $variables[$values[1]] : $values[0];
|
|
}, $string);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function session_get(){
|
|
return isset($_SESSION["dptw_session_id"]) ? $_SESSION["dptw_session_id"] : null;
|
|
}
|
|
|
|
public static function response($code, $data){
|
|
|
|
header("content-type: application/json");
|
|
echo json_encode([
|
|
"ok" => true,
|
|
"code" => $code,
|
|
"content" => $data
|
|
]);
|
|
|
|
}
|
|
|
|
private function session_update($response = false){
|
|
|
|
$results = $this->mysql->query("call sessions_update({session}, @error, @id);");
|
|
$data = [
|
|
"error" => $results[0]["variables"]["error"],
|
|
"id" => $results[0]["variables"]["id"]
|
|
];
|
|
|
|
if(!isset($_SESSION["dptw_session_id"]) || $_SESSION["dptw_session_id"] != $results[0]["variables"]["id"])
|
|
$_SESSION["dptw_session_id"] = $results[0]["variables"]["id"];
|
|
|
|
$response && $this->response(200, $data);
|
|
|
|
return [$data["id"], $data["error"]];
|
|
}
|
|
|
|
private function test_1(){
|
|
|
|
self::response(200, base64_encode(json_encode([
|
|
"mode" => "testing_game",
|
|
"page" => 1,
|
|
"items_per_page" => 20,
|
|
"nicks" => ""
|
|
])));
|
|
|
|
}
|
|
|
|
private function set_score(){
|
|
|
|
list($session_id, $error) = $this->session_update();
|
|
$id = null;
|
|
|
|
if(!$error){
|
|
|
|
$results = $this->mysql->query("call scores_set({session}, '{mode}', '{nick}', {score}, @error, @id);", [
|
|
"mode" => $this->request("mode"),
|
|
"nick" => $this->request("nick"),
|
|
"score" => $this->request("score")
|
|
]);
|
|
|
|
$error |= $results[0]["variables"]["error"];
|
|
$id = $results[0]["variables"]["id"];
|
|
|
|
};
|
|
|
|
self::response(200, [
|
|
"error" => $error,
|
|
"id" => $id
|
|
]);
|
|
|
|
return [$id, $error];
|
|
}
|
|
|
|
private function list_scores(){
|
|
|
|
list($session_id, $error) = $this->session_update();
|
|
$pages = null;
|
|
$items = null;
|
|
$scores = null;
|
|
|
|
if(!$error){
|
|
|
|
$results = $this->mysql->query("call scores_list({session}, '{mode}', {page}, {items_per_page}, '{nicks}', @error, @pages, @items);", [
|
|
"mode" => $this->request("mode"),
|
|
"page" => $this->request("page"),
|
|
"items_per_page" => $this->request("items_per_page"),
|
|
"nicks" => $this->request("nicks")
|
|
]);
|
|
|
|
$error |= $results[0]["variables"]["error"];
|
|
$pages = $results[0]["variables"]["pages"];
|
|
$items = $results[0]["variables"]["items"];
|
|
$scores = $results[0]["tables"][0];
|
|
|
|
};
|
|
|
|
self::response(200, [
|
|
"error" => $error,
|
|
"pages" => $pages,
|
|
"items" => $items,
|
|
"scores" => $scores
|
|
]);
|
|
|
|
return [$pages, $items, $scores, $error];
|
|
}
|
|
|
|
};
|