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