Advertisement
kamiakze

Untitled

May 29th, 2023
880
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.20 KB | None | 0 0
  1. <?php
  2.  
  3.  include_once("config.php");
  4.  //---Author : [Clinche]
  5.  //---Date 14/10/2022
  6.  //TODO: wait for 42 to fix client_credentials
  7.  function        api42_get_bot_token(){
  8.          global $ft_uid, $ft_secret;
  9.  
  10.          $url = "https://api.intra.42.fr/oauth/token";
  11.          $postParams = [
  12.                  'grant_type' => "client_credentials",
  13.                  'client_id' => $ft_uid,
  14.                  'client_secret' => $ft_secret
  15.          ];
  16.  
  17.          $ch = curl_init();
  18.  
  19.          curl_setopt($ch, CURLOPT_URL, $url);
  20.          curl_setopt($ch, CURLOPT_POST, 1);
  21.          curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
  22.          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23.          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
  24.  
  25.          $reponse = curl_exec($ch);
  26.          curl_close($ch);
  27.  
  28.          $data = json_decode($reponse);
  29.          var_dump($data);
  30.          $token = $data->access_token;
  31.          return ($token);
  32.  }
  33.  
  34.  function        api42_get_tokens($code, $discorduid){
  35.          global $ft_uid, $ft_secret, $ft_url;
  36.  
  37.          $ft_url = $ft_url."?uid=".$discorduid;
  38.          $url = "https://api.intra.42.fr/oauth/token";
  39.  
  40.          $postParams = [
  41.                  'grant_type' => "authorization_code",
  42.                  'client_id' => $ft_uid,
  43.                  'client_secret' => $ft_secret,
  44.                  'code' => $code,
  45.                  'redirect_uri'=> $ft_url
  46.          ];
  47.  
  48.          $ch = curl_init();
  49.  
  50.          curl_setopt($ch, CURLOPT_URL, $url);
  51.          curl_setopt($ch, CURLOPT_POST, 1);
  52.          curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
  53.          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54.          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
  55.  
  56.          $reponse = curl_exec($ch);
  57.          curl_close($ch);
  58.  
  59.          $data = json_decode($reponse);
  60.  
  61.          $token = $data->access_token;
  62.          $sectoken = $data->refresh_token;
  63.  
  64.          $tokens = array( 'token' => $token, 'refresh_token' => $sectoken );
  65.          return ($tokens);
  66.  }
  67.  
  68.  function        api42_ref_tokens($reftoken, $discorduid){
  69.          global $ft_uid, $ft_secret, $ft_url;
  70.  
  71.          $ft_url = $ft_url."?uid=".$discorduid;
  72.          $url = "https://api.intra.42.fr/oauth/token";
  73.  
  74.          $postParams = [
  75.                  'grant_type' => "refresh_token",
  76.                  'client_id' => $ft_uid,
  77.                  'client_secret' => $ft_secret,
  78.                  'refresh_token' => $reftoken,
  79.                  'redirect_uri'=> $ft_url
  80.          ];
  81.  
  82.          $ch = curl_init();
  83.  
  84.          curl_setopt($ch, CURLOPT_URL, $url);
  85.          curl_setopt($ch, CURLOPT_POST, 1);
  86.          curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
  87.          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  88.          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
  89.  
  90.          $reponse = curl_exec($ch);
  91.          curl_close($ch);
  92.  
  93.          $data = json_decode($reponse);
  94.  
  95.          $token = $data->access_token;
  96.          $sectoken = $data->refresh_token;
  97.  
  98.          $tokens = array( 'token' => $token, 'refresh_token' => $sectoken );
  99.          return ($tokens);
  100.  }
  101.  
  102.  function        api42_do_request($endpoint, $token)
  103.  {
  104.          $url = "https://api.intra.42.fr{$endpoint}";
  105.  
  106.          $curl = curl_init($url);
  107.          curl_setopt($curl, CURLOPT_URL, $url);
  108.          curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  109.  
  110.          $headers = array(
  111.                          "Authorization: Bearer ".$token,
  112.                          );
  113.  
  114.          curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  115.          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  116.          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  117.  
  118.          $resp = curl_exec($curl);
  119.          curl_close($curl);
  120.  
  121.          $data = json_decode($resp);
  122.          
  123.          return ($data);
  124.  }
  125.  
  126.  function        extract_cursus_from_api($apireq){
  127.          
  128.          foreach ($apireq->cursus_users as $item)
  129.          {
  130.                  if ($item->cursus->id == 21)
  131.                          return $item;
  132.          }
  133.          error_log('error while fetching cursuses');
  134.          echo('oops');
  135.          die();
  136.  }
  137.  ?>
Advertisement
Comments
  • hduaj
    347 days
    Comment was deleted
Add Comment
Please, Sign In to add comment
Advertisement