* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB * @category API */ try{ header("Content-type: application/json; charset=utf-8"); // Get put data parse_str(file_get_contents("php://input"), $_POST); // Get profile ID /** @var mixed $query Request parameters, from API_CONTROLLER*/ if (count($query) > 0){ $id = $query[0]; } else{ // ID is mandatory header("HTTP/1.1 400 User ID not received."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User ID not received."); return 400; } // Check user mail. $mail = $_POST["email"]; if ($mail == null || $mail == false){ header("HTTP/1.1 400 User email not received."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User email not received."); return 400; } if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) { header("HTTP/1.1 400 Invalid email."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Invalid email."); return 400; } // Check user password. $password = $_POST["password"]; if ($password == null || $password == false){ header("HTTP/1.1 400 User password not received."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User password not received."); return 400; } // Check user name. $name = $_POST["name"]; if ($name == null || $name == false){ $name = "player_" . $id; } // Check user name. $public = intval($_POST["public"]); if ($public != 1){ $public = 0; } $db = get_context()->get_db(); // Check if player exists $statement = $db->prepare("SELECT COUNT(id) AS c FROM data.player WHERE id = :id;"); $statement->bindValue(":id", $id, SQLITE3_INTEGER); if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){ header("HTTP/1.1 400 Existing user."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Existing user."); return 400; } // Get and validate API key $api_key = $_POST["key"]; if ($api_key == null || $api_key == false){ header("HTTP/1.1 400 API key not received."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received."); return 400; } $statement = $db->prepare(" SELECT id FROM user WHERE api_key = :api_key; "); $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT); $statement->execute(); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r == null){ // No valid API key found header("HTTP/1.1 404 Player not found."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid API key."); return 403; } $user_id = $r["id"]; // Insert // TODO: Get user ID, check fields $statement = $db->prepare(" INSERT INTO data.player ( id, user, server, country, lang, name, mana, crystal, level, experience, energy, energy_max, energy_per_min, arena_energy, rep, social_point, honor_point, guild_point, darkportal_energy, dimension_energy, costume_point, honor_medal, honor_mark, event_coin, storage_slots, island, joined, top_rank_arena, top_rank_world_arena, top_rank_special_league, top_rank_gw, top_rank_siege, top_rank_wboss, top_rank_toan, top_rank_toah, top_rank_toal, public, mode, last_access ) VALUES ( :id, :user, :server, :country, :lang, :name, :mana, :crystal, :level, :experience, :energy, :energy_max, :energy_per_min, :arena_energy, :rep, :social_point, :honor_point, :guild_point, :darkportal_energy, :dimension_energy, :costume_point, :honor_medal, :honor_mark, :event_coin, :storage_slots, :island, :joined, :top_rank_arena, :top_rank_world_arena, :top_rank_special_league, :top_rank_gw, :top_rank_siege, :top_rank_wboss, :top_rank_toan, :top_rank_toah, :top_rank_toal, :public, :mode, :last_access ); "); $statement->bindValue(":id", $id, SQLITE3_INTEGER); $statement->bindValue(":user", $user_id, SQLITE3_INTEGER); $statement->bindValue(":server", null, SQLITE3_INTEGER); $statement->bindValue(":country", null, SQLITE3_INTEGER); $statement->bindValue(":lang", null, SQLITE3_INTEGER); $statement->bindValue(":name", $name, SQLITE3_INTEGER); $statement->bindValue(":mana", 0, SQLITE3_INTEGER); $statement->bindValue(":crystal", 0, SQLIE3_INTTE3_INTEGER); $statement->bindValue(":level", 1, SQLITEGER); $statement->bindValue(":experience", 0, SQLITE3_INTEGER); $statement->bindValue(":energy", 0, SQLITE3_INTEGER); $statement->bindValue(":energy_max", 0, SQLITE3_INTEGER); $statement->bindValue(":energy_per_min", 0, SQLITE3_INTEGER); $statement->bindValue(":arena_energy", 0, SQLITE3_INTEGER); $statement->bindValue(":rep", null, SQLITE3_INTEGER); $statement->bindValue(":social_point", 0, SQLITE3_INTEGER); $statement->bindValue(":honor_point", 0, SQLITE3_INTEGER); $statement->bindValue(":guild_point", 0, SQLITE3_INTEGER); $statement->bindValue(":darkportal_energy", 0, SQLITE3_INTEGER); $statement->bindValue(":dimension_energy", 0, SQLITE3_INTEGER); $statement->bindValue(":costume_point", 0, SQLITE3_INTEGER); $statement->bindValue(":honor_medal", 0, SQLITE3_INTEGER); $statement->bindValue(":honor_mark", 0, SQLITE3_INTEGER); $statement->bindValue(":event_coin", 0, SQLITE3_INTEGER); $statement->bindValue(":storage_slots", 0, SQLITE3_INTEGER); $statement->bindValue(":island", 1, SQLITE3_INTEGER); $statement->bindValue(":joined", null, SQLITE3_INTEGER); $statement->bindValue(":top_rank_arena", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_world_arena", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_special_league", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_gw", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_siege", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_wboss", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_toan", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_toah", 0, SQLITE3_INTEGER); $statement->bindValue(":top_rank_toal", 0, SQLITE3_INTEGER); $statement->bindValue(":public", $public, SQLITE3_INTEGER); $statement->bindValue(":mode", 0, SQLITE3_INTEGER); $statement->bindValue(":last_access", null, SQLITE3_INTEGER); $statement->execute(); // Build array $player = [ "username" => $name, "public" => "1" ]; echo(json_encode($player)); header("HTTP/1.1 201 Created."); syslog(LOG_INFO, "[APIv1] HTTP/1.1 201 Created."); return 201; } catch(Exception $e) { header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage()); syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage()); return 500; }