| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- /**
- * Profile creator script.
- *
- * Exposes an API to create a new profile.
- *
- * Mandatory query parameters are:
- * - id: Player ID.
- *
- * Mandatory POST parameters are:
- * - key: User API key.
- *
- * Optional POST parameters are:
- * - name: User name.
- * - public: 1 To make the profile public, 0 for private.
- *
- * @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
- 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;
- }
|