<?php
$redirect_uri = 'https://pca.phlaym.net/pnutauth.php';
$client_id = 'RwHDh73PtU0It4DdKhwh2GEagBoO1ELD';
$client_secret = file_get_contents('../pca_settings/clientsecret');
if (isset($_GET['code'])) {
    $code = $_GET['code'];

    // set post fields
    $post = [
        'client_id'     => $client_id,
        'client_secret' => $client_secret,
        'code'            => $code,
        'redirect_uri'  => $redirect_uri,
        'grant_type'    => 'authorization_code'
    ];

    $ch = curl_init('https://api.pnut.io/v1/oauth/access_token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    // execute!
    $response = curl_exec($ch);

    // close the connection, release resources used
    curl_close($ch);
    $resp = json_decode($response, true);

    file_put_contents('../pca_settings/access_token', $resp['access_token']);
    header('Location: https://pca.phlaym.net/Check_PCA.php');
} else {
    header(
        'Location: https://pnut.io/oauth/authenticate?client_id='
        . $client_id
        . '&redirect_uri='
        . urlencode($redirect_uri)
        . '&scope=write_post,messages&response_type=code'
    );
}
?>