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, "[APIv3] 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, "[APIv3] 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, "[APIv3] 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, "[APIv3] 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 player WHERE uid = :uid;");
  67. $statement->bindValue(":uid", $id, SQLITE3_INTEGER);
  68. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  69. header("HTTP/1.1 400 Existing user.");
  70. syslog(LOG_INFO, "[APIv3] 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. $statement = $db->prepare("
  83. INSERT INTO player (
  84. uid,
  85. name,
  86. mail,
  87. password,
  88. api_key,
  89. mana,
  90. crystal,
  91. country,
  92. lang,
  93. level,
  94. experience,
  95. energy,
  96. energy_max,
  97. energy_per_min,
  98. arena_energy,
  99. arena_energy_max,
  100. rep,
  101. social_point,
  102. honor_point,
  103. guild_point,
  104. darkportal_energy,
  105. darkportal_energy_max,
  106. dimension_energy,
  107. dimension_energy_max,
  108. costume_point,
  109. costume_point_max,
  110. honor_medal,
  111. honor_mark,
  112. event_coin,
  113. storage_slots,
  114. island,
  115. public
  116. ) VALUES (
  117. :uid,
  118. :name,
  119. :mail,
  120. :password,
  121. :api_key,
  122. :mana,
  123. :crystal,
  124. :country,
  125. :lang,
  126. :level,
  127. :experience,
  128. :energy,
  129. :energy_max,
  130. :energy_per_min,
  131. :arena_energy,
  132. :arena_energy_max,
  133. :rep,
  134. :social_point,
  135. :honor_point,
  136. :guild_point,
  137. :darkportal_energy,
  138. :darkportal_energy_max,
  139. :dimension_energy,
  140. :dimension_energy_max,
  141. :costume_point,
  142. :costume_point_max,
  143. :honor_medal,
  144. :honor_mark,
  145. :event_coin,
  146. :storage_slots,
  147. :island,
  148. :public
  149. );
  150. ");
  151. $statement->bindValue(":uid", $id, SQLITE3_INTEGER);
  152. $statement->bindValue(":name", $name, SQLITE3_TEXT);
  153. $statement->bindValue(":mail", $mail, SQLITE3_TEXT);
  154. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  155. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  156. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  157. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  158. $statement->bindValue(":country", null, SQLITE3_TEXT);
  159. $statement->bindValue(":lang", null, SQLITE3_TEXT);
  160. $statement->bindValue(":level", 1, SQLITE3_INTEGER);
  161. $statement->bindValue(":experience", 0, SQLITE3_INTEGER);
  162. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  163. $statement->bindValue(":energy_max", 0, SQLITE3_INTEGER);
  164. $statement->bindValue(":energy_per_min", 0, SQLITE3_INTEGER);
  165. $statement->bindValue(":arena_energy", 0, SQLITE3_INTEGER);
  166. $statement->bindValue(":arena_energy_max", 10, SQLITE3_INTEGER);
  167. $statement->bindValue(":rep", null, SQLITE3_INTEGER);
  168. $statement->bindValue(":social_point", 0, SQLITE3_INTEGER);
  169. $statement->bindValue(":honor_point", 0, SQLITE3_INTEGER);
  170. $statement->bindValue(":guild_point", 0, SQLITE3_INTEGER);
  171. $statement->bindValue(":darkportal_energy", 0, SQLITE3_INTEGER);
  172. $statement->bindValue(":darkportal_energy_max", 10, SQLITE3_INTEGER);
  173. $statement->bindValue(":dimension_energy", 0, SQLITE3_INTEGER);
  174. $statement->bindValue(":dimension_energy_max", 100, SQLITE3_INTEGER);
  175. $statement->bindValue(":costume_point", 0, SQLITE3_INTEGER);
  176. $statement->bindValue(":costume_point_max", 0, SQLITE3_INTEGER);
  177. $statement->bindValue(":honor_medal", 0, SQLITE3_INTEGER);
  178. $statement->bindValue(":honor_mark", 0, SQLITE3_INTEGER);
  179. $statement->bindValue(":event_coin", 0, SQLITE3_INTEGER);
  180. $statement->bindValue(":storage_slots", 0, SQLITE3_INTEGER);
  181. $statement->bindValue(":island_upgrade", 0, SQLITE3_INTEGER);
  182. $statement->bindValue(":public", $public, SQLITE3_INTEGER);
  183. $statement->execute();
  184. // Build array
  185. $player = [
  186. "username" => $name,
  187. "email" => $mail,
  188. "api_key" => $api_key,
  189. "public" => "1"
  190. ];
  191. echo(json_encode($player));
  192. header("HTTP/1.1 201 Created.");
  193. syslog(LOG_INFO, "[APIv3] 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, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  199. return 500;
  200. }
  201. ?>