<?php
	// oAuth2

	$get = $_REQUEST;
	$query_string = '';
	foreach ($get as $key => $value) {
		$query_string .=  $key . '=' . $value . '&';
	}
	echo($query_string);
	parse_str($query_string);

	$client_id = 'f4c59081e488ac326f32f8ab52620023e996dd82d7d01163';
	$client_secret = 'e65b9127acac254c5b9bfbbe68b9a6ca07b0d6df6dbb0023e996dd82d7d01163';
	$redirect_uri = 'https://romfast.ro/oauth2/';

	// REFRESH TOKEN se apeleaza din ROA cu parametrul ?refresh_token=REFRESH_TOKEN pentru prelungirea valabilitatii tokenului
	if (!empty($refresh_token)){
		$url = 'https://logincert.anaf.ro/anaf-oauth2/v1/token';

		//The data you want to send via POST
		$fields = [
			'client_id'     => $client_id,
			'client_secret' => $client_secret,
			'refresh_token' => $refresh_token,
			'redirect_uri'	=> $redirect_uri,
			'grant_type' => 'refresh_token'
		];

		//url-ify the data for the POST
		$fields_string = http_build_query($fields);

		//open connection
		$ch = curl_init();

		//set the url, number of POST vars, POST data
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_POST, true);
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

		//So that curl_exec returns the contents of the cURL; rather than echoing it
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

		//execute post
		$jsonobj = curl_exec($ch);
	
		// $arr = json_decode($jsonobj, true);		

/* 		$json = json_encode($jsonobj, JSON_UNESCAPED_SLASHES);
		if ($json === false) {
			// Avoid echo of empty string (which is invalid JSON), and
			// JSONify the error message instead:
			$json = json_encode(["error" => json_last_error_msg()]);
			if ($json === false) {
				// This should not happen, but we go all the way now:
				$json = '{"error":"unknown"}';
			}
			// Set HTTP response status code to: 500 - Internal Server Error
			http_response_code(500);
		}
		 */
		header("Content-Type: application/json");
		echo $jsonobj;

		// Close handle
		//curl_close($ch);

		exit();
	}
	
	// COD AUTORIZARE se apeleaza din ROA fara parametri pentru obtinerea codului de autorizare de la ANAF. ANAF apeleaza aceasta pagina (callback) cu codul de autorizare
	if (empty($code)) {
		// Obtinere cod autorizare
		$url = 'https://logincert.anaf.ro/anaf-oauth2/v1/authorize';
		$url .='?client_id='.$client_id;
		$url .='&client_secret='.$client_secret;
		$url .='&response_type=code';
		$url .='&redirect_uri='.$redirect_uri;
		
		// Redirectionez la pagina de autorizare ANAF pentru obtinerea codului de autorizare
		echo $url;
		//header('Location: '.$url);
		exit();
	}
	else {
		// NEW TOKEN 
		// callback ANAF: https://romfast.ro/oauth2/?code=COD_AUTORIZARE
		
		// Obtinere token pe baza codului de autorizare
		// print_r('Codul de autorizare este: '.$code);
		
		$url = 'https://logincert.anaf.ro/anaf-oauth2/v1/token';

		//The data you want to send via POST
		$fields = [
			'client_id'      => $client_id,
			'client_secret' => $client_secret,
			'code'         => $code,
			'redirect_uri'	=> $redirect_uri,
			'grant_type' => 'authorization_code'
		];

		//url-ify the data for the POST
		$fields_string = http_build_query($fields);

		//open connection
		$ch = curl_init();

		//set the url, number of POST vars, POST data
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_POST, true);
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

		//So that curl_exec returns the contents of the cURL; rather than echoing it
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

		//execute post
		$jsonobj = curl_exec($ch);
		//echo $jsonobj;
		
		$arr = json_decode($jsonobj, true);
		
		echo '<p>Copiati ANAF Access Token si Refresh Token de pe liniile urmatoare si completati-le in aplicatie</p>';
		echo '<p><b>'.$arr["access_token"].'</b></p>';
		
		echo 'refresh_token: '.$arr["refresh_token"];

		// Close handle
		curl_close($ch);
				
	}


?>