0){ $id = $query[0]; } else{ // ID is mandatory header("HTTP/1.1 400 User ID not received."); syslog(LOG_INFO, "[APIv3] 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, "[APIv3] 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, "[APIv3] 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, "[APIv3] 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; } // Check if player exists $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid;"); $statement->bindValue(":uid", $id, SQLITE3_INTEGER); if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){ header("HTTP/1.1 400 Existing user."); syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Existing user."); return 400; } // Prepare inputs $password = hash("sha256", $password); $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $charactersLength = 16; $api_key = ""; for ($i = 0; $i < $charactersLength; $i++) { $api_key .= $characters[rand(0, $charactersLength - 1)]; } // Insert $statement = $db->prepare(" INSERT INTO player ( uid, name, mail, password, api_key, mana, crystal, country, lang, level, experience, energy, energy_max, energy_per_min, arena_energy, arena_energy_max, rep, social_point, honor_point, guild_point, darkportal_energy, darkportal_energy_max, dimension_energy, dimension_energy_max, costume_point, costume_point_max, honor_medal, honor_mark, event_coin, storage_slots, island, public ) VALUES ( :uid, :name, :mail, :password, :api_key, :mana, :crystal, :country, :lang, :level, :experience, :energy, :energy_max, :energy_per_min, :arena_energy, :arena_energy_max, :rep, :social_point, :honor_point, :guild_point, :darkportal_energy, :darkportal_energy_max, :dimension_energy, :dimension_energy_max, :costume_point, :costume_point_max, :honor_medal, :honor_mark, :event_coin, :storage_slots, :island, :public ); "); $statement->bindValue(":uid", $id, SQLITE3_INTEGER); $statement->bindValue(":name", $name, SQLITE3_TEXT); $statement->bindValue(":mail", $mail, SQLITE3_TEXT); $statement->bindValue(":password", $password, SQLITE3_TEXT); $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT); $statement->bindValue(":mana", 0, SQLITE3_INTEGER); $statement->bindValue(":crystal", 0, SQLITE3_INTEGER); $statement->bindValue(":country", null, SQLITE3_TEXT); $statement->bindValue(":lang", null, SQLITE3_TEXT); $statement->bindValue(":level", 1, SQLITE3_INTEGER); $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(":arena_energy_max", 10, 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(":darkportal_energy_max", 10, SQLITE3_INTEGER); $statement->bindValue(":dimension_energy", 0, SQLITE3_INTEGER); $statement->bindValue(":dimension_energy_max", 100, SQLITE3_INTEGER); $statement->bindValue(":costume_point", 0, SQLITE3_INTEGER); $statement->bindValue(":costume_point_max", 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_upgrade", 0, SQLITE3_INTEGER); $statement->bindValue(":public", $public, SQLITE3_INTEGER); $statement->execute(); // Build array $player = [ "username" => $name, "email" => $mail, "api_key" => $api_key, "public" => "1" ]; echo(json_encode($player)); header("HTTP/1.1 201 Created."); syslog(LOG_INFO, "[APIv3] HTTP/1.1 201 Created."); return 201; } catch(Exception $e) { header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage()); syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage()); return 500; } ?>