<?php
include 'core/class/include.php';
function reloadString($length = 100) {
      $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZs';
      $charactersLength = strlen($characters);
      $randomString = '';
      for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
      }
    return $randomString;
}

function str_find($str, $target)
{
    if (strpos($str, $target) !== false) {
        return "true";
    }
    return "false";
}

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($str,$a) !== false) return true;
    }
    return false;
}

define('OAUTH2_CLIENT_ID', '716635785851437137');
define('OAUTH2_CLIENT_SECRET', 'nSsXcYViEQ_fvMuvvJAhnUpA9SDg286a');
define('OAUTH2_HOSTNAME', 'https://kpanel.cz');
$authorizeURL = 'https://discord.com/api/oauth2/authorize';
$tokenURL = 'https://discord.com/api/oauth2/token';
$apiURLBase = 'https://discord.com/api/users/@me';

if(get('code')) {
  if(strlen(get('code')) < 29) {
    die("Erreur dans la matrice, code \"".get('code')."\" invalide");
  }

  $token = apiRequest($tokenURL, array(
    "grant_type" => "authorization_code",
    'client_id' => OAUTH2_CLIENT_ID,
    'client_secret' => OAUTH2_CLIENT_SECRET,
    'redirect_uri' => OAUTH2_HOSTNAME.'/loginwithoutverif',
    'code' => get('code')
  ));
  if ($token->scope !== "identify guilds") {
    echo "Erreur dans votre requete: Un ou plusieurs scopes sont manquant, veuillez reessayez.";
    echo "<br>Si vous pensez qu'il s'agit d'une erreur, veuillez nous contactez sur <a href='https://discord.gg/Q6mJwR2'>https://discord.gg/Q6mJwR2</a>";
    session_unset();
    session_destroy();
    die();
  }



  $logout_token = $token->access_token;
  $_SESSION['access_token'] = $token->access_token;
  $_SESSION['client_id'] = $token->client_id;


//  header('Location: ' . $_SERVER['PHP_SELF']);
}

if(session('access_token')) {
  $user = apiRequest($apiURLBase);
  $_SESSION['discord_socket'] = $user;
  $test = apiRequest("https://discord.com/api/users/@me/guilds");
  $collabo =  json_encode($test);

  if (strpos($collabo, 'ID_To_BLACKLIST') !== false) {
    if (!isset($_SESSION['bad'])) {
      $_SESSION['bad'] = 'PanelName';
      header('Location: /home/error.php?discord='.$_SESSION['bad']);
    }

  }else{



       unset($_SESSION['bad']);

       $is_exist = $GLOBALS['DB']->Count("users",["discord_id" => $user->id]);

       if ($is_exist == 1){

        $GLOBALS['DB']->Update("users",[
            "discord_id" => $user->id
          ],[
            "discord_avatar" => $user->avatar,
            "username" => $user->username
        ]);

       	$_SESSION["id"] = $GLOBALS["DB"]->GetContent("users",["discord_id" => $user->id])[0]["id"];
       	header("Location: ./dashboard");

       }else{
          $GLOBALS['DB']->Insert("users",[
            "discord_id" => $user->id,
            "discord_avatar" => $user->avatar,
            "username" => $user->username,
            "role" => "0",
            "active" => "1",
            "ban" => "0",
            "fuckkey" => reloadString(10),
            "validationtoken" => reloadString(35)
          ]);

          $_SESSION["id"] = $GLOBALS["DB"]->GetContent("users",["discord_id" => $user->id])[0]["id"];
       	  header("Location: ./dashboard");

       }

  }



}else{
  $params = array(
    'client_id' => OAUTH2_CLIENT_ID,
    'redirect_uri' => OAUTH2_HOSTNAME.'/loginwithoutverif',
    'response_type' => 'code',
    'scope' => 'identify guilds'
  );
  header('Location: https://discord.com/api/oauth2/authorize' . '?' . http_build_query($params));
}

function apiRequest($url, $post=FALSE, $headers=array()) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $response = curl_exec($ch);
  if($post)
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  $headers[] = 'Accept: application/json';
  if(session('access_token'))
    $headers[] = 'Authorization: Bearer ' . session('access_token');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $response = curl_exec($ch);
  return json_decode($response);
}

function get($key, $default=NULL) {
  return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
}

function session($key, $default=NULL) {
  return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}
?>
