<?php

$con=mysqli_connect("localhost","user_jibler","ba897ad272231711fdcd05a964d34cb2","Jibler");

  if (mysqli_connect_errno($con))
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//$ip_server = $_SERVER['SERVER_ADDR'];
  
// Printing the stored address
//echo "Server IP Address is: $ip_server";

$DomainNamee ="https://jibler.app/jibler/UserDriverApi/";
  
$serviceAccountFile = 'jibler-37339-63535a118af8.json';

// Read the service account JSON file
$serviceAccount = json_decode(file_get_contents($serviceAccountFile), true);

// Google's OAuth 2.0 token endpoint
$googleOAuthUrl = 'https://oauth2.googleapis.com/token';

// Time constants
$now = time();
$tokenExpiry = $now + 3600; // Token valid for 1 hour

// Service account credentials
$privateKey = $serviceAccount['private_key'];
$clientEmail = $serviceAccount['client_email'];

// JWT header
$jwtHeader = [
    'alg' => 'RS256',
    'typ' => 'JWT'
];

// JWT claim set (payload)
$jwtClaimSet = [
    'iss' => $clientEmail,                      // Issuer: the service account email
    'scope' => 'https://www.googleapis.com/auth/cloud-platform',  // OAuth scope for FCM
    'aud' => $googleOAuthUrl,                   // Audience: OAuth token URL
    'exp' => $tokenExpiry,                      // Expiration time
    'iat' => $now                               // Issued at time
];

// Base64 URL encode a string
function base64UrlEncode($data) {
    return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
}

// Create the JWT
$jwtHeaderEncoded = base64UrlEncode(json_encode($jwtHeader));
$jwtClaimSetEncoded = base64UrlEncode(json_encode($jwtClaimSet));
$jwtSignatureInput = $jwtHeaderEncoded . '.' . $jwtClaimSetEncoded;

// Sign the JWT using the private key (RS256)
$signature = '';
openssl_sign($jwtSignatureInput, $signature, $privateKey, 'sha256WithRSAEncryption');
$jwtSignatureEncoded = base64UrlEncode($signature);

// Final JWT token
$jwtToken = $jwtSignatureInput . '.' . $jwtSignatureEncoded;

// Prepare the POST fields to get an access token
$postFields = http_build_query([
    'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion' => $jwtToken
]);

// Send the request to get an access token
$ch = curl_init($googleOAuthUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded'
]);

// Execute the request and get the response
$response = curl_exec($ch);
curl_close($ch);

// Decode the JSON response
$tokenResponse = json_decode($response, true);

if (isset($tokenResponse['access_token'])) {
    // Access token received successfully
    $accessToken = $tokenResponse['access_token'];
    $ProgID = "jibler-37339";
  //  echo "Access Token: " . $accessToken . "\n";
} else {
    // Handle error
//    echo "Error: " . $response . "\n";
}
  
?>
