<?php

// The client is asking for authorization. Allow everything.
if (isset($_GET['redirect_uri'])) {
    $redirect = $_GET['redirect_uri'];
    if (strpos($_GET['redirect_uri'], '?') === false) {
        $redirect .= '?';
    } else {
        $redirect .= '&';
    }
    $parameters = [
        'code' => 'ok' . (isset($_GET['scope'])?$_GET['scope']:''),
        'me' => $_GET['fakeme'] . '?' . http_build_query([
            'endpoint' => $_GET['endpoint'],
            'token' => $_GET['token'],
            'fakeme' => $_GET['fakeme'],
        ]),
    ];
    if (isset($_GET['state'])) {
        $parameters['state'] = $_GET['state'];
    }
    $redirect .= http_build_query($parameters);
    header('Location: ' . $redirect, true, 302);
    exit();
}

// The client is asking for access token. Give them what they want.
if (isset($_POST['code'])) {
    header('Content-Type: application/json');
    exit(json_encode([
        'access_token' => $_GET['token'],
        'me' => $_GET['fakeme'] . '?' . http_build_query([
            'endpoint' => $_GET['endpoint'],
            'token' => $_GET['token'],
            'fakeme' => $_GET['fakeme'],
        ]),
        'scope' => substr($_POST['code'], 2),
    ]));
}

// Figure out current URL.
$ssl = false;
$url = 'http';
if (empty($_SERVER['HTTPS']) === false && $_SERVER['HTTPS'] === 'on') {
    $url .= 's';
    $ssl = true;
}
$url .= '://' . $_SERVER['HTTP_HOST'];
if ($ssl && $_SERVER['SERVER_PORT'] !== '443' || $ssl === false && $_SERVER['SERVER_PORT'] !== '80') {
    $url .= ':' . $_SERVER['SERVER_PORT'];
}
$url .= $_SERVER['REQUEST_URI'];
$url = htmlspecialchars($url, ENT_COMPAT | ENT_HTML5, 'UTF-8');

?><!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Generate Magic login URL for Micropub endpoints</title>
<?php if (isset($_GET['endpoint']) && isset($_GET['token'])) { ?>
        <link rel="authorization_endpoint" href="<?= $url ?>">
        <link rel="token_endpoint" href="<?= $url ?>">
        <link rel="micropub" href="<?= $_GET['endpoint'] ?>">
<?php } ?>
        <style>
            * {
                box-sizing: border-box;
            }
            form {
                text-align: center;
                padding-top: 1em;
            }
            label, input {
                width: 30em;
                display: block;
                margin: 0 auto;
                text-align: left;
                font-size: 100%;
                font-family: sans-serif;
                line-height: 1.4;
                padding: .3em .5em .2em;
            }
            input {
                margin-bottom: 1em;
                border: 1px solid currentColor;
                background-color: transparent;
            }
            input[type="submit"] {
                text-align: center;
            }
            p {
                font-size: 80%;
                font-family: sans-serif;
                width: 37.5em;
                margin: -.2em auto 0;
                text-align: left;
                padding: 0 .7em .4em;
                line-height: 1.4;
            }
        </style>
    </head>
    <body>
<?php if (isset($_GET['endpoint']) && isset($_GET['token'])) { ?>
        <form>
            <label for="url">Your magic URL</label>
            <input type="text" id="url" name="url" value="<?= $url ?>">
        </form>
<?php } else { ?>
        <form>
            <label for="endpoint"><a href="https://micropub.net">Micropub</a> endpoint URL</label>
            <input type="text" id="endpoint" name="endpoint" placeholder="https://example.com/micropub">
            <label for="token">Bearer token</label>
            <p>If you need a token from an actual <a href="https://indieauth.net">IndieAuth</a> flow, you can use the <a href="https://gimme-a-token.5eb.nl">Homebrew Access Token</a> service.</p>
            <input type="text" id="token" name="token" placeholder="token">
            <input type="hidden" name="fakeme" value="<?= $url ?>">
            <input type="submit" value="Create URL">
        </form>
<?php } ?>
    </body>
</html>