POST.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Profile creator script.
  4. *
  5. * Exposes an API to create a new profile.
  6. *
  7. * Mandatory query parameters are:
  8. * - id: Player ID.
  9. *
  10. * Mandatory POST parameters are:
  11. * - email: User email.
  12. * - password: User password.
  13. *
  14. * Optional POST parameters are:
  15. * - name: User name.
  16. * - public: 1 To make the profile public, 0 for private.
  17. *
  18. * @category API
  19. */
  20. global $db;
  21. try{
  22. header("Content-type: application/json; charset=utf-8");
  23. // Get put data
  24. parse_str(file_get_contents("php://input"), $_POST);
  25. // Get profile ID
  26. /** @var mixed $query Request parameters, from API_CONTROLLER*/
  27. if (count($query) > 0){
  28. $id = $query[0];
  29. }
  30. else{
  31. // ID is mandatory
  32. header("HTTP/1.1 400 User ID not received.");
  33. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User ID not received.");
  34. return 400;
  35. }
  36. // Check user mail.
  37. $mail = $_POST["email"];
  38. if ($mail == null || $mail == false){
  39. header("HTTP/1.1 400 User email not received.");
  40. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User email not received.");
  41. return 400;
  42. }
  43. if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
  44. header("HTTP/1.1 400 Invalid email.");
  45. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Invalid email.");
  46. return 400;
  47. }
  48. // Check user password.
  49. $password = $_POST["password"];
  50. if ($password == null || $password == false){
  51. header("HTTP/1.1 400 User password not received.");
  52. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User password not received.");
  53. return 400;
  54. }
  55. // Check user name.
  56. $name = $_POST["name"];
  57. if ($name == null || $name == false){
  58. $name = "player_" . $id;
  59. }
  60. // Check user name.
  61. $public = intval($_POST["public"]);
  62. if ($public != 1){
  63. $public = 0;
  64. }
  65. // Check if player exists
  66. $statement = $db->prepare("SELECT COUNT(uid) AS c FROM data.player WHERE id = :id;");
  67. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  68. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  69. header("HTTP/1.1 400 Existing user.");
  70. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Existing user.");
  71. return 400;
  72. }
  73. // Prepare inputs
  74. $password = hash("sha256", $password);
  75. $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  76. $charactersLength = 16;
  77. $api_key = "";
  78. for ($i = 0; $i < $charactersLength; $i++) {
  79. $api_key .= $characters[rand(0, $charactersLength - 1)];
  80. }
  81. // Insert
  82. // TODO: Get user ID, check fields
  83. $statement = $db->prepare("
  84. INSERT INTO data.player (
  85. id,
  86. user,
  87. name,
  88. mail,
  89. password,
  90. api_key,
  91. mana,
  92. crystal,
  93. country,
  94. lang,
  95. level,
  96. experience,
  97. energy,
  98. energy_max,
  99. energy_per_min,
  100. arena_energy,
  101. arena_energy_max,
  102. rep,
  103. social_point,
  104. honor_point,
  105. guild_point,
  106. darkportal_energy,
  107. darkportal_energy_max,
  108. dimension_energy,
  109. dimension_energy_max,
  110. costume_point,
  111. costume_point_max,
  112. honor_medal,
  113. honor_mark,
  114. event_coin,
  115. storage_slots,
  116. island,
  117. public
  118. ) VALUES (
  119. :uid,
  120. :name,
  121. :mail,
  122. :password,
  123. :api_key,
  124. :mana,
  125. :crystal,
  126. :country,
  127. :lang,
  128. :level,
  129. :experience,
  130. :energy,
  131. :energy_max,
  132. :energy_per_min,
  133. :arena_energy,
  134. :arena_energy_max,
  135. :rep,
  136. :social_point,
  137. :honor_point,
  138. :guild_point,
  139. :darkportal_energy,
  140. :darkportal_energy_max,
  141. :dimension_energy,
  142. :dimension_energy_max,
  143. :costume_point,
  144. :costume_point_max,
  145. :honor_medal,
  146. :honor_mark,
  147. :event_coin,
  148. :storage_slots,
  149. :island,
  150. :public
  151. );
  152. ");
  153. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  154. $statement->bindValue(":name", $name, SQLITE3_TEXT);
  155. $statement->bindValue(":mail", $mail, SQLITE3_TEXT);
  156. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  157. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  158. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  159. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  160. $statement->bindValue(":country", null, SQLITE3_TEXT);
  161. $statement->bindValue(":lang", null, SQLITE3_TEXT);
  162. $statement->bindValue(":level", 1, SQLITE3_INTEGER);
  163. $statement->bindValue(":experience", 0, SQLITE3_INTEGER);
  164. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  165. $statement->bindValue(":energy_max", 0, SQLITE3_INTEGER);
  166. $statement->bindValue(":energy_per_min", 0, SQLITE3_INTEGER);
  167. $statement->bindValue(":arena_energy", 0, SQLITE3_INTEGER);
  168. $statement->bindValue(":arena_energy_max", 10, SQLITE3_INTEGER);
  169. $statement->bindValue(":rep", null, SQLITE3_INTEGER);
  170. $statement->bindValue(":social_point", 0, SQLITE3_INTEGER);
  171. $statement->bindValue(":honor_point", 0, SQLITE3_INTEGER);
  172. $statement->bindValue(":guild_point", 0, SQLITE3_INTEGER);
  173. $statement->bindValue(":darkportal_energy", 0, SQLITE3_INTEGER);
  174. $statement->bindValue(":darkportal_energy_max", 10, SQLITE3_INTEGER);
  175. $statement->bindValue(":dimension_energy", 0, SQLITE3_INTEGER);
  176. $statement->bindValue(":dimension_energy_max", 100, SQLITE3_INTEGER);
  177. $statement->bindValue(":costume_point", 0, SQLITE3_INTEGER);
  178. $statement->bindValue(":costume_point_max", 0, SQLITE3_INTEGER);
  179. $statement->bindValue(":honor_medal", 0, SQLITE3_INTEGER);
  180. $statement->bindValue(":honor_mark", 0, SQLITE3_INTEGER);
  181. $statement->bindValue(":event_coin", 0, SQLITE3_INTEGER);
  182. $statement->bindValue(":storage_slots", 0, SQLITE3_INTEGER);
  183. $statement->bindValue(":island_upgrade", 0, SQLITE3_INTEGER);
  184. $statement->bindValue(":public", $public, SQLITE3_INTEGER);
  185. $statement->execute();
  186. // Build array
  187. $player = [
  188. "username" => $name,
  189. "public" => "1"
  190. ];
  191. echo(json_encode($player));
  192. header("HTTP/1.1 201 Created.");
  193. syslog(LOG_INFO, "[APIv1] HTTP/1.1 201 Created.");
  194. return 201;
  195. }
  196. catch(Exception $e) {
  197. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  198. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  199. return 500;
  200. }
  201. ?>