munzee.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. require 'required.php';
  3. require 'onlyloggedin.php';
  4. header("Content-Type: text/html");
  5. if (!is_empty($_GET['code'])) {
  6. //file_put_contents("munzee.log", "User " . $_SESSION['uuid'] . " is attempting OAuth.\n", FILE_APPEND);
  7. $code = $_GET['code'];
  8. $url = 'https://api.munzee.com/oauth/login';
  9. $fields = array(
  10. 'client_id' => urlencode(MUNZEE_KEY),
  11. 'client_secret' => urlencode(MUNZEE_SECRET),
  12. 'grant_type' => 'authorization_code',
  13. 'code' => urlencode($code),
  14. 'redirect_uri' => urlencode("http://gs.terranquest.net/munzee.php")
  15. );
  16. //url-ify the data for the POST
  17. foreach ($fields as $key => $value) {
  18. $fields_string .= $key . '=' . $value . '&';
  19. }
  20. rtrim($fields_string, '&');
  21. //open connection
  22. $ch = curl_init();
  23. $options = array(
  24. CURLOPT_URL => $url,
  25. CURLOPT_POST => 1,
  26. CURLOPT_POSTFIELDS => $fields_string,
  27. CURLOPT_RETURNTRANSFER => true, // return web page
  28. CURLOPT_HEADER => false, // don't return headers
  29. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  30. CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
  31. CURLOPT_ENCODING => "", // handle compressed
  32. CURLOPT_USERAGENT => "TerranQuest Game Server (terranquest.net; Ubuntu; Linux x86_64; PHP 7)", // name of client
  33. CURLOPT_AUTOREFERER => true, // set referrer on redirect
  34. CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
  35. CURLOPT_TIMEOUT => 120, // time-out on response
  36. );
  37. curl_setopt_array($ch, $options);
  38. //execute post
  39. $result = curl_exec($ch);
  40. //close connection
  41. curl_close($ch);
  42. $jsonresult = json_decode($result, TRUE);
  43. $data = $jsonresult['data'];
  44. //file_put_contents("munzee.log", "User " . $_SESSION['uuid'] . " OAuth result:\n", FILE_APPEND);
  45. //file_put_contents("munzee.log", " Result: $result\n\n", FILE_APPEND);
  46. if ($jsonresult['status_code'] == 200) {
  47. if ($database->has('munzee', ['player_uuid' => $_SESSION['uuid']])) {
  48. $database->update('munzee', ['bearertoken' => $data['token']['access_token'], 'refreshtoken' => $data['token']['refresh_token'], 'expires' => $data['token']['expires']], ['player_uuid' => $_SESSION['uuid']]);
  49. } else {
  50. $database->insert('munzee', ['bearertoken' => $data['token']['access_token'], 'refreshtoken' => $data['token']['refresh_token'], 'expires' => $data['token']['expires'], 'player_uuid' => $_SESSION['uuid']]);
  51. }
  52. echo MUNZEE_LINKED_HTML;
  53. die();
  54. } else {
  55. echo MUNZEE_FAILED_HTML;
  56. die();
  57. }
  58. }