Advertisement
4epB9Ik

Untitled

Feb 26th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. ini_set('max_execution_time', 300); //300 seconds = 5 minutes. In case if your CURL is slow and is loading too much (Can be IPv6 problem)
  5.  
  6. error_reporting(E_ALL);
  7.  
  8. define('OAUTH2_CLIENT_ID', '681515469500579914');
  9. define('OAUTH2_CLIENT_SECRET', 'tkrcvO0dNv8HMQpUiTVafweygOJmTa8T');
  10.  
  11. $authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
  12. $tokenURL = 'https://discordapp.com/api/oauth2/token';
  13. $apiURLBase = 'https://discordapp.com/api/users/@me';
  14.  
  15. session_start();
  16.  
  17. // Start the login process by sending the user to Discord's authorization page
  18. if(get('action') == 'login') {
  19.  
  20. $params = array(
  21. 'client_id' => OAUTH2_CLIENT_ID,
  22. 'redirect_uri' => 'https://4epb9ik.ru/test.php',
  23. 'response_type' => 'code',
  24. 'scope' => 'identify guilds'
  25. );
  26.  
  27. // Redirect the user to Discord's authorization page
  28. header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
  29. die();
  30. }
  31.  
  32.  
  33. // When Discord redirects the user back here, there will be a "code" and "state" parameter in the query string
  34. if(get('code')) {
  35.  
  36. // Exchange the auth code for a token
  37. $token = apiRequest($tokenURL, array(
  38. "grant_type" => "authorization_code",
  39. 'client_id' => OAUTH2_CLIENT_ID,
  40. 'client_secret' => OAUTH2_CLIENT_SECRET,
  41. 'redirect_uri' => 'https://4epb9ik.ru/test.php',
  42. 'code' => get('code')
  43. ));
  44. $logout_token = $token->access_token;
  45. $_SESSION['access_token'] = $token->access_token;
  46.  
  47.  
  48. header('Location: ' . $_SERVER['PHP_SELF']);
  49. }
  50.  
  51. if(session('access_token')) {
  52. $user = apiRequest($apiURLBase);
  53.  
  54. echo '<h3>Logged In</h3>';
  55. echo '<h4>Welcome, ' . $user->username . '</h4>';
  56. echo '<pre>';
  57. print_r($user);
  58. echo '</pre>';
  59.  
  60. } else {
  61. echo '<h3>Not logged in</h3>';
  62. echo '<p><a href="?action=login">Log In</a></p>';
  63. }
  64.  
  65.  
  66. if(get('action') == 'logout') {
  67. // This must to logout you, but it didn't worked(
  68.  
  69. $params = array(
  70. 'access_token' => $logout_token
  71. );
  72.  
  73. // Redirect the user to Discord's revoke page
  74. header('Location: https://discordapp.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
  75. die();
  76. }
  77.  
  78. function apiRequest($url, $post=FALSE, $headers=array()) {
  79. $ch = curl_init($url);
  80. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  81. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  82.  
  83. $response = curl_exec($ch);
  84.  
  85.  
  86. if($post)
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  88.  
  89. $headers[] = 'Accept: application/json';
  90.  
  91. if(session('access_token'))
  92. $headers[] = 'Authorization: Bearer ' . session('access_token');
  93.  
  94. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  95.  
  96. $response = curl_exec($ch);
  97. return json_decode($response);
  98. }
  99.  
  100. function get($key, $default=NULL) {
  101. return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
  102. }
  103.  
  104. function session($key, $default=NULL) {
  105. return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
  106. }
  107.  
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement