| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * Profile deleter script.
- *
- * Exposes an API to update an existing profile.
- *
- * It also saves the data to a JSON file in the data directory.
- *
- * Mandatory query parameters are:
- * - id: Player ID.
- *
- * Mandatory DELETE parameters are:
- * - key: User API key.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @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
- $_DELETE = [];
- parse_str(file_get_contents("php://input"), $_DELETE);
- // Get profile ID
- /** @var mixed $query Request parameters, from API_CONTROLLER*/
- if (count($query) > 0){
- $player_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 API key.
- $api_key = $_DELETE["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;
- }
- $db = get_context()->get_db();
- // Authenticate
- $statement = $db->prepare("
- SELECT COUNT(player.id) AS c
- FROM
- user,
- player
- WHERE
- user.id = player.user AND
- player.id = :id AND
- user.api_key = :api_key;
- ");
- $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
- $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- // Delete all player data
- $db->query("PRAGMA foreign_keys = OFF;");
- $statement = $db->prepare(
- "DELETE FROM unit_skill
- WHERE unit IN (SELECT id FROM unit WHERE player = :player);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM unit_rune
- WHERE unit IN (SELECT id FROM unit WHERE player = :player);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM rune_stat
- WHERE rune IN (SELECT id FROM rune WHERE player = :player);
- ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM unit_artifact
- WHERE unit IN (SELECT id FROM unit WHERE player = :player);n ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM artifact_stat
- WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $statement = $db->prepare("
- DELETE FROM artifact_effect
- WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- $tables_to_clear = [
- "defense",
- "unit",
- "rune",
- "artifact",
- "building",
- "decoration",
- "item",
- "enchantment"
- ];
- foreach ($tables_to_clear as $table){
- $statement = $db->prepare("DELETE FROM $table WHERE player = :player;");
- $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- }
- $statement = $db->prepare("
- DELETE FROM player
- WHERE id = :id
- ");
- $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
- $statement->execute();
- header("HTTP/1.1 204 Profile deleted.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 204 Profile deleted.");
- return 204;
- }
- 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;
- }
|