SHARE
TWEET

Untitled

a guest Oct 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. error_reporting(E_ALL);
  6. define('OAUTH2_CLIENT_ID', '631726889660055552');
  7. define('OAUTH2_CLIENT_SECRET', '69gFsaRDJ8feasZFrCGcmpGS5lfMfXxX');
  8. $authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
  9. $tokenURL = 'https://discordapp.com/api/oauth2/token';
  10. $apiURLBase = 'https://discordapp.com/api/users/@me';
  11. session_start();
  12. // Start the login process by sending the user to Discord's authorization page
  13. if(get('action') == 'login') {
  14.   $params = array(
  15.     'client_id' => OAUTH2_CLIENT_ID,
  16.     'redirect_uri' => 'http://playtogether.ml/dashboard.php?',
  17.     'response_type' => 'code',
  18.     'scope' => 'identify'
  19.   );
  20.   // Redirect the user to Discord's authorization page
  21.   header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
  22.   die();
  23. }
  24. // When Discord redirects the user back here, there will be a "code" and "state" parameter in the query string
  25. if(get('code')) {
  26.   // Exchange the auth code for a token
  27.   $token = apiRequest($tokenURL, array(
  28.     "grant_type" => "authorization_code",
  29.     'client_id' => OAUTH2_CLIENT_ID,
  30.     'client_secret' => OAUTH2_CLIENT_SECRET,
  31.     'redirect_uri' => 'http://playtogether.ml/dashboard.php?',
  32.     'code' => get('code')
  33.   ));
  34.   $logout_token = $token->access_token;
  35.   $_SESSION['access_token'] = $token->access_token;
  36.   header('Location: ' . $_SERVER['PHP_SELF']);
  37. }
  38.  
  39. $servername = "localhost";
  40. $username = "dovahkiin";
  41. $password = "Salahis13";
  42. $dbname = "skyrim_together";
  43. $conn = new mysqli($servername, $username, $password, $dbname);
  44. if ($conn->connect_error) {
  45.     die("Connection failed: " . $conn->connect_error);
  46. }
  47. if(session('access_token')) {
  48.   $user = apiRequest($apiURLBase);
  49.   $fulluser = $user->username . "#" . $user->discriminator;
  50.   $sql = "SELECT id FROM users WHERE email='" . $user->email . "';";
  51.   $result = $conn->query($sql);
  52.   if ($result->num_rows == 0) {
  53.     $sql = "INSERT into users(username,email) VALUES ('" . $fulluser . "','" . $user->email . "')";
  54.     $result = $conn->query($sql);
  55.   }
  56. } else {
  57.   header('Location: ?action=login');
  58.   die();
  59. }
  60.  
  61. $sql = "SELECT id, password, region FROM users WHERE email='" . $user->email . "';";
  62. $result = $conn->query($sql);
  63. $row = $result->fetch_assoc();
  64. $token=$row["password"];
  65. $region=$row["region"];
  66. $id = $row["id"];
  67. $port=10000+$id;
  68. $running=shell_exec('netstat -an | grep :' . $port . ' | wc -l');
  69.  
  70. if($running == 0 && isset($_POST['region'])) { //if the database says the instance isnt running but it should be
  71.     $name="SkyrimTogether.$id";
  72.     $token=$_POST["password"];
  73.     $region=$_POST["region"];
  74.     $filename=date("d-m-Y") . "_" . $name;
  75.     $logpath="/root/skyrim_together/logs/" . $filename . ".txt";
  76.     $output=shell_exec("sudo pm2 start /root/skyrim_together/start.sh --name $name --log $logpath --no-autorestart -- $name $port $token"); //will have to modify this to accept region arg, which a REDIRECTORY bash uses to start the START.SH bash on the correct server with the remaining bash line arguments
  77.     $sql = "UPDATE users SET username='" . $fulluser . "', password='" . $token . "', region='" . $region . "' WHERE id=" . $id . ";";
  78.     $result = $conn->query($sql);
  79.     while ($running == 0) {
  80.         $running=shell_exec('netstat -an | grep :' . $port . ' | wc -l');
  81.         sleep(0.5);
  82.     }
  83.     header('Location: ?');
  84.     die();
  85. }
  86.  
  87. if(get('action') == 'logout') {
  88.   // This must to logout you, but it didn't worked(
  89.   $params = array(
  90.     'access_token' => $logout_token
  91.   );
  92.   // Redirect the user to Discord's revoke page
  93.   header('Location: https://discordapp.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
  94.   die();
  95. }
  96.  
  97. function apiRequest($url, $post=FALSE, $headers=array()) {
  98.   $ch = curl_init($url);
  99.   curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  100.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  101.   $response = curl_exec($ch);
  102.   if($post)
  103.     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  104.   $headers[] = 'Accept: application/json';
  105.   if(session('access_token'))
  106.     $headers[] = 'Authorization: Bearer ' . session('access_token');
  107.   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  108.   $response = curl_exec($ch);
  109.   return json_decode($response);
  110. }
  111. function get($key, $default=NULL) {
  112.   return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
  113. }
  114. function session($key, $default=NULL) {
  115.   return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
  116. }
  117. ?>
  118.  
  119. <html lang="en">
  120.   <head>
  121.     <meta charset="utf-8">
  122.     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  123.     <title>PlayTogether | Home</title>
  124.     <link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon">
  125.     <link rel="icon" href="assets/favicon.ico" type="image/x-icon">
  126.     <meta name="description" content="For when you want to play on a private server, without the hassle of setting it up.">
  127.     <meta property="og:description" content="For when you want to play on a private server, without the hassle of setting it up.">
  128.     <meta name="keywords" content="PlayTogether, Skyrim, SkyrimTogether">
  129.     <meta name="author" content="Lord Kanelsnegle">
  130.  
  131.     <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700,800" rel="stylesheet">
  132.     <link rel="stylesheet" href="css/style.css">
  133.  
  134.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  135.     <script type="text/javascript">
  136.     $(window).load(function() {
  137.         $(".loader").fadeOut("slow");
  138.     });
  139.     </script>
  140.     </head>
  141.     <body>
  142.     <div class="loader"></div>
  143.     <div class="container">
  144.         <!--- MUSIC <audio autoplay loop>
  145.             <source src="assets/dovahkiin.mp3" type="audio/mpeg">
  146.         </audio> --->
  147.         <video autoplay muted loop>
  148.             <source src="assets/home.mp4" type="video/mp4">
  149.         </video>
  150.         <div class="centered">
  151.             <h1>Welcome, <?php echo $user->username ?></h1>
  152. <?php
  153. $instances=shell_exec("sudo docker ps | grep tiltedphoques/st-game-server | wc -l");
  154. if ($instances == 20 && $running == 0) {
  155.     echo '<p class="description">Our servers are currently running at maximum capacity. Come back later to launch your instance.</p>';
  156. }
  157. else
  158. {
  159. $sql = "SELECT id, password, region FROM users WHERE email='" . $user->email . "';";
  160. $result = $conn->query($sql);
  161. $row = $result->fetch_assoc();
  162. $token=$row["password"];
  163. $region=$row["region"];
  164. if($running == 0) { //if the instance isnt running
  165.     echo '<p class="description" style="margin-bottom:1em!important">Enter a password, select a region and press \'Launch\'. It\'s as easy as that!</p>';
  166.     echo '<form action="dashboard.php" method="post" onsubmit="submit.disabled=true; submit.value=\'Please Wait...\';">'; //mention that pass is 4-10 chars somewhere
  167.     echo '<input type="text" name="password" value="' . $token. '" minlength="4" maxlength="10" class="description" onfocus="if (this.value == \'' . $token. '\') {this.value = \'\';}" onblur="if (this.value == \'\') {this.value = \'' . $token. '\';}">';
  168.     echo '<select name="region">';
  169.     switch ($region) {
  170.         case "na":
  171.             echo '<option value="eu">EU</option>';
  172.             echo '<option value="na" selected>NA</option>';
  173.             break;
  174.         default: //"eu"
  175.             echo '<option value="eu" selected>EU</option>';
  176.             //echo '<option value="na">NA</option>';
  177.     }
  178.     echo '</select>';
  179.     echo '<input type="submit" name="submit" value="Launch Instance">';
  180.     echo '</form>';
  181. } else {
  182.     echo '<p class="description" style="margin-bottom:0px!important">Success! Your instance is now online at <b>playtogether.ml:' . $port . '</b> with password <b>' . $token . '</b>.</p>';
  183.     echo '<p>Note: The instance will terminate after 10 min of inactivity. Two or more players must be <b>playing</b>, not just connected, for the instance to be considered active.</p>';
  184.     shell_exec("sudo /root/skyrim_together/activity.sh SkyrimTogether." . $id . " " . $port . " > /dev/null 2>&1 &");
  185. }
  186. }
  187. ?>
  188.         </div>
  189.     </div>
  190.     <footer>
  191.       <div class="site-footer">
  192.             <center><p>Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved | Not affiliated with SkyrimTogether</p></center>
  193.       </div>
  194.     </footer>
  195.     </body>
  196. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top