POST.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * - key: User API key.
  12. *
  13. * Optional POST parameters are:
  14. * - name: User name.
  15. * - public: 1 To make the profile public, 0 for private.
  16. *
  17. * @author Iñigo Valentin <i@inigovalentin.com>
  18. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  19. * @package SWDB
  20. * @category API
  21. */
  22. try{
  23. header("Content-type: application/json; charset=utf-8");
  24. // Get put data
  25. parse_str(file_get_contents("php://input"), $_POST);
  26. // Get profile ID
  27. /** @var mixed $query Request parameters, from API_CONTROLLER*/
  28. if (count($query) > 0){
  29. $id = $query[0];
  30. }
  31. else{
  32. // ID is mandatory
  33. header("HTTP/1.1 400 User ID not received.");
  34. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User ID not received.");
  35. return 400;
  36. }
  37. // Check user mail.
  38. $mail = $_POST["email"];
  39. if ($mail == null || $mail == false){
  40. header("HTTP/1.1 400 User email not received.");
  41. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User email not received.");
  42. return 400;
  43. }
  44. if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
  45. header("HTTP/1.1 400 Invalid email.");
  46. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Invalid email.");
  47. return 400;
  48. }
  49. // Check user password.
  50. $password = $_POST["password"];
  51. if ($password == null || $password == false){
  52. header("HTTP/1.1 400 User password not received.");
  53. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User password not received.");
  54. return 400;
  55. }
  56. // Check user name.
  57. $name = $_POST["name"];
  58. if ($name == null || $name == false){
  59. $name = "player_" . $id;
  60. }
  61. // Check user name.
  62. $public = intval($_POST["public"]);
  63. if ($public != 1){
  64. $public = 0;
  65. }
  66. $db = get_context()->get_db();
  67. // Check if player exists
  68. $statement = $db->prepare("SELECT COUNT(id) AS c FROM data.player WHERE id = :id;");
  69. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  70. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  71. header("HTTP/1.1 400 Existing user.");
  72. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Existing user.");
  73. return 400;
  74. }
  75. // Get and validate API key
  76. $api_key = $_POST["key"];
  77. if ($api_key == null || $api_key == false){
  78. header("HTTP/1.1 400 API key not received.");
  79. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
  80. return 400;
  81. }
  82. $statement = $db->prepare("
  83. SELECT id
  84. FROM user
  85. WHERE api_key = :api_key;
  86. ");
  87. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  88. $statement->execute();
  89. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  90. if ($r == null){
  91. // No valid API key found
  92. header("HTTP/1.1 404 Player not found.");
  93. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid API key.");
  94. return 403;
  95. }
  96. $user_id = $r["id"];
  97. // Insert
  98. // TODO: Get user ID, check fields
  99. $statement = $db->prepare("
  100. INSERT INTO data.player (
  101. id,
  102. user,
  103. server,
  104. country,
  105. lang,
  106. name,
  107. mana,
  108. crystal,
  109. level,
  110. experience,
  111. energy,
  112. energy_max,
  113. energy_per_min,
  114. arena_energy,
  115. rep,
  116. social_point,
  117. honor_point,
  118. guild_point,
  119. darkportal_energy,
  120. dimension_energy,
  121. costume_point,
  122. honor_medal,
  123. honor_mark,
  124. event_coin,
  125. storage_slots,
  126. island,
  127. joined,
  128. top_rank_arena,
  129. top_rank_world_arena,
  130. top_rank_special_league,
  131. top_rank_gw,
  132. top_rank_siege,
  133. top_rank_wboss,
  134. top_rank_toan,
  135. top_rank_toah,
  136. top_rank_toal,
  137. public,
  138. mode,
  139. last_access
  140. ) VALUES (
  141. :id,
  142. :user,
  143. :server,
  144. :country,
  145. :lang,
  146. :name,
  147. :mana,
  148. :crystal,
  149. :level,
  150. :experience,
  151. :energy,
  152. :energy_max,
  153. :energy_per_min,
  154. :arena_energy,
  155. :rep,
  156. :social_point,
  157. :honor_point,
  158. :guild_point,
  159. :darkportal_energy,
  160. :dimension_energy,
  161. :costume_point,
  162. :honor_medal,
  163. :honor_mark,
  164. :event_coin,
  165. :storage_slots,
  166. :island,
  167. :joined,
  168. :top_rank_arena,
  169. :top_rank_world_arena,
  170. :top_rank_special_league,
  171. :top_rank_gw,
  172. :top_rank_siege,
  173. :top_rank_wboss,
  174. :top_rank_toan,
  175. :top_rank_toah,
  176. :top_rank_toal,
  177. :public,
  178. :mode,
  179. :last_access
  180. );
  181. ");
  182. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  183. $statement->bindValue(":user", $user_id, SQLITE3_INTEGER);
  184. $statement->bindValue(":server", null, SQLITE3_INTEGER);
  185. $statement->bindValue(":country", null, SQLITE3_INTEGER);
  186. $statement->bindValue(":lang", null, SQLITE3_INTEGER);
  187. $statement->bindValue(":name", $name, SQLITE3_INTEGER);
  188. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  189. $statement->bindValue(":crystal", 0, SQLIE3_INTTE3_INTEGER);
  190. $statement->bindValue(":level", 1, SQLITEGER);
  191. $statement->bindValue(":experience", 0, SQLITE3_INTEGER);
  192. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  193. $statement->bindValue(":energy_max", 0, SQLITE3_INTEGER);
  194. $statement->bindValue(":energy_per_min", 0, SQLITE3_INTEGER);
  195. $statement->bindValue(":arena_energy", 0, SQLITE3_INTEGER);
  196. $statement->bindValue(":rep", null, SQLITE3_INTEGER);
  197. $statement->bindValue(":social_point", 0, SQLITE3_INTEGER);
  198. $statement->bindValue(":honor_point", 0, SQLITE3_INTEGER);
  199. $statement->bindValue(":guild_point", 0, SQLITE3_INTEGER);
  200. $statement->bindValue(":darkportal_energy", 0, SQLITE3_INTEGER);
  201. $statement->bindValue(":dimension_energy", 0, SQLITE3_INTEGER);
  202. $statement->bindValue(":costume_point", 0, SQLITE3_INTEGER);
  203. $statement->bindValue(":honor_medal", 0, SQLITE3_INTEGER);
  204. $statement->bindValue(":honor_mark", 0, SQLITE3_INTEGER);
  205. $statement->bindValue(":event_coin", 0, SQLITE3_INTEGER);
  206. $statement->bindValue(":storage_slots", 0, SQLITE3_INTEGER);
  207. $statement->bindValue(":island", 1, SQLITE3_INTEGER);
  208. $statement->bindValue(":joined", null, SQLITE3_INTEGER);
  209. $statement->bindValue(":top_rank_arena", 0, SQLITE3_INTEGER);
  210. $statement->bindValue(":top_rank_world_arena", 0, SQLITE3_INTEGER);
  211. $statement->bindValue(":top_rank_special_league", 0, SQLITE3_INTEGER);
  212. $statement->bindValue(":top_rank_gw", 0, SQLITE3_INTEGER);
  213. $statement->bindValue(":top_rank_siege", 0, SQLITE3_INTEGER);
  214. $statement->bindValue(":top_rank_wboss", 0, SQLITE3_INTEGER);
  215. $statement->bindValue(":top_rank_toan", 0, SQLITE3_INTEGER);
  216. $statement->bindValue(":top_rank_toah", 0, SQLITE3_INTEGER);
  217. $statement->bindValue(":top_rank_toal", 0, SQLITE3_INTEGER);
  218. $statement->bindValue(":public", $public, SQLITE3_INTEGER);
  219. $statement->bindValue(":mode", 0, SQLITE3_INTEGER);
  220. $statement->bindValue(":last_access", null, SQLITE3_INTEGER);
  221. $statement->execute();
  222. // Build array
  223. $player = [
  224. "username" => $name,
  225. "public" => "1"
  226. ];
  227. echo(json_encode($player));
  228. header("HTTP/1.1 201 Created.");
  229. syslog(LOG_INFO, "[APIv1] HTTP/1.1 201 Created.");
  230. return 201;
  231. }
  232. catch(Exception $e) {
  233. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  234. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  235. return 500;
  236. }