web/callback.php
author ymh <ymh.work@gmail.com>
Thu, 07 Nov 2024 22:38:14 +0100
changeset 1571 4a1e6952afe5
parent 1558 761ba7426984
permissions -rwxr-xr-x
Improve on mastodon management
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     1
<?php
563
0dd745105665 Tests for IE9 cookie issues
Raphael Velt <raph.velt@gmail.com>
parents: 557
diff changeset
     2
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
     3
233
10d6fd6ce9ab - put all in a registry
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 229
diff changeset
     4
include_once 'common.php';
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
     5
use Abraham\TwitterOAuth\TwitterOAuth;
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
     6
use GuzzleHttp\Client;
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
     7
233
10d6fd6ce9ab - put all in a registry
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 229
diff changeset
     8
if(!isset($_REQUEST['rep'])) {
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
     9
    $rep = $C_default_rep;
233
10d6fd6ce9ab - put all in a registry
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 229
diff changeset
    10
}
10d6fd6ce9ab - put all in a registry
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 229
diff changeset
    11
else {
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    12
    $rep = $_REQUEST['rep'];
233
10d6fd6ce9ab - put all in a registry
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 229
diff changeset
    13
}
10d6fd6ce9ab - put all in a registry
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 229
diff changeset
    14
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    15
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    16
if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN'])) {
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    17
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    18
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    19
    $token = unserialize($_SESSION['TWITTER_REQUEST_TOKEN']);
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    20
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    21
    if (isset($_REQUEST['oauth_token']) && $token['oauth_token'] !== $_REQUEST['oauth_token']) {
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    22
        exit('Invalid callback request. Oops. Sorry.');
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    23
    }
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    24
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    25
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']);
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    26
    $access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_REQUEST['oauth_verifier']]);
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    27
1557
7c67caaafdeb enmi 22
ymh <ymh.work@gmail.com>
parents: 1427
diff changeset
    28
    $_SESSION['SOCIAL_ACCESS_TOKEN'] = serialize($access_token);
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    29
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    30
    /**
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    31
     * Now that we have an Access Token, we can discard the Request Token
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    32
     */
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    33
    $_SESSION['TWITTER_REQUEST_TOKEN'] = null;
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    34
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    35
    /**
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    36
     * With Access Token in hand, let's try accessing the client again
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    37
     */
849
973a136cfbd0 Bugfixes
Raphael Velt <raph.velt@gmail.com>
parents: 847
diff changeset
    38
    header('Location: ' . ( isset($_SESSION['TWITTER_REDIRECT_URL']) ? $_SESSION['TWITTER_REDIRECT_URL'] : ( URL_ROOT . "$rep/client.php" ) ) );
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 849
diff changeset
    39
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    40
} elseif (!empty($_GET) && isset($_SESSION['SOCIAL_AUTH_STATE']) && isset($_SESSION['SOCIAL_LOGIN_DOMAIN'])) {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    41
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    42
    if($_GET['state'] != $_SESSION['SOCIAL_AUTH_STATE']) {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    43
        exit("Invalid state, state mismatch. Aborting.");
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    44
    }
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    45
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    46
    $login_domain = $_SESSION['SOCIAL_LOGIN_DOMAIN'];
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    47
    $base_uri = "https://$login_domain";
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    48
    $client = new Client([
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    49
        'base_uri' => $base_uri,
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    50
        'timeout'  => 2.0,
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    51
    ]);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    52
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    53
    //get token
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    54
    $client_ids = get_cached_app_ids($login_domain, $rep, $appCacheHandle);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    55
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    56
    $client_id = $client_ids["client_id"];
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    57
    $client_secret = $client_ids["client_secret"];
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    58
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    59
    if(!$client_id || !$client_secret) {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    60
        exit("Invalid app cache. Aborting.");
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    61
    }
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    62
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    63
    $token_resp = $client->post("/oauth/token", ['form_params' => [
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    64
        'client_id' => $client_id,
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    65
        'client_secret' => $client_secret,
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    66
        'redirect_uri' => URL_ROOT . "callback.php?rep=$rep",
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    67
        'grant_type' => 'authorization_code',
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    68
        'code' => $_GET['code'],
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    69
        'scope' => 'read write push',
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    70
    ]]);
1571
4a1e6952afe5 Improve on mastodon management
ymh <ymh.work@gmail.com>
parents: 1558
diff changeset
    71
    $body = $token_resp->getBody();
4a1e6952afe5 Improve on mastodon management
ymh <ymh.work@gmail.com>
parents: 1558
diff changeset
    72
    $token_ent = json_decode($body, true);
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    73
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    74
    $_SESSION['SOCIAL_ACCESS_TOKEN'] = serialize($token_ent['access_token']);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    75
    $_SESSION['SOCIAL_AUTH_STATE'] = null;
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    76
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    77
    header('Location: ' . ( URL_ROOT . "$rep/client.php" ) );
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    78
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    79
} else {
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    80
    /**
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    81
     * Mistaken request? Some malfeasant trying something?
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    82
     */
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    83
    exit('Invalid callback request. Oops. Sorry.');
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    84
}