Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?php
  2. if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
  3. echo "Error: Could not show credentials";
  4. exit();
  5. }
  6.  
  7. $postdata = json_decode(file_get_contents('php://input'));
  8.  
  9. if ($postdata->challenge_name == NULL) {
  10. echo "Error: No challenge name specified";
  11. exit();
  12. }
  13.  
  14. if ($postdata->id == NULL) {
  15. echo "Error: No user id specified";
  16. exit();
  17. }
  18.  
  19. if ($postdata->time == NULL) {
  20. echo "Error: No time specified";
  21. exit();
  22. }
  23.  
  24. if ($postdata->key != '---') {
  25. echo "Error: Invalid server key";
  26. exit();
  27. }
  28.  
  29. $servername = "localhost";
  30. $username = "super";
  31. $password = "---";
  32. $dbname = "SpeedBlocks";
  33.  
  34. $ch_name = $postdata->challenge_name;
  35. $user_id = $postdata->id;
  36. $time = $postdata->time;
  37. unset($postdata->challenge_name);
  38. unset($postdata->id);
  39. unset($postdata->time);
  40. unset($postdata->key);
  41.  
  42. $conn = new mysqli($servername, $username, $password, $dbname);
  43. if ($conn->connect_error) {
  44. die("Connection failed: " . $conn->connect_error);
  45. }
  46.  
  47. $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'SpeedBlocks' AND table_name = '$ch_name';";
  48.  
  49. $result = $conn->query($sql);
  50.  
  51. if ($result->num_rows == 0) { // Create new table
  52. $sql = "create table $ch_name (user_id int(10) PRIMARY KEY, time int(10) NOT NULL, ";
  53. foreach ($postdata as $key => $value) {
  54. $sql .= "$key varchar(10), ";
  55. }
  56. $sql = substr($sql, 0, -2);
  57. $sql .= ");";
  58.  
  59. if ($conn->query($sql) === TRUE) {
  60. echo "New table created successfully";
  61. } else {
  62. echo "Error: " . $sql . "<br>" . $conn->error;
  63. exit();
  64. }
  65.  
  66. $sql = "INSERT INTO challenge_list (name) VALUES ($ch_name);";
  67.  
  68. if ($conn->query($sql) === TRUE) {
  69. echo "Updated challenge_list succesfully";
  70. } else {
  71. echo "Error: " . $sql . "<br>" . $conn->error;
  72. exit();
  73. }
  74. }
  75.  
  76. $sql = "INSERT INTO $ch_name (user_id, time, ";
  77.  
  78. foreach ($postdata as $key => $value)
  79. $sql .= "$key, ";
  80.  
  81. $sql = substr($sql, 0, -2) . ") VALUES ($user_id, $time, ";
  82.  
  83. foreach ($postdata as $key => $value)
  84. $sql .= "$value, ";
  85.  
  86. $sql = substr($sql, 0, -2) . ") ON DUPLICATE KEY UPDATE user_id=$user_id, time=$time, ";
  87.  
  88. foreach ($postdata as $key => $value)
  89. $sql .= "$key='$value', ";
  90.  
  91. $sql = substr($sql, 0, -2) . ";";
  92.  
  93. if ($conn->query($sql) === TRUE) {
  94. echo "New records created successfully";
  95. } else {
  96. echo "Error: " . $sql . "<br>" . $conn->error;
  97. }
  98.  
  99. $conn->close();
  100. ?>
  101.  
  102. // Example input
  103. // {"challenge_name":"test","id":412,"time":376,"blocks":"12","highest_combo":"57"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement