web/tweet_ajax.php
author ymh <ymh.work@gmail.com>
Fri, 15 Nov 2024 01:37:34 +0100
changeset 1577 96a6eada7982
parent 1575 ce1d5b0d1479
child 1581 2d713fdd532f
permissions -rwxr-xr-x
Correct some details again
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
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     2
229
74c9ddc3640b a lot of changes in the web file organization.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 0
diff changeset
     3
include_once 'common.php';
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
     4
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
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;
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     7
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
     8
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
     9
function sendTwitter()
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    10
{
1557
7c67caaafdeb enmi 22
ymh <ymh.work@gmail.com>
parents: 1427
diff changeset
    11
    $token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']);
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    12
    $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: 1050
diff changeset
    13
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    14
    $data = $connection->post("statuses/update", ["status" => $_POST['status']]);
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    15
1557
7c67caaafdeb enmi 22
ymh <ymh.work@gmail.com>
parents: 1427
diff changeset
    16
    // $token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']);
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    17
    // $client = $token->getHttpClient($configuration);
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    18
    // $client->setUri('https://api.twitter.com/1.1/statuses/update.json');
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    19
    // $client->setMethod(Zend_Http_Client::POST);
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    20
    // $client->setParameterPost('status', $_POST['status']);
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    21
    // $response = $client->request();
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    22
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    23
    // /**
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    24
    //  * Check if the json response refers to our tweet details (assume it
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    25
    //  * means it was successfully posted). API gurus can correct me.
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    26
    //  */
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    27
    // $data = json_decode($response->getBody());
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    28
    // $result = $response->getBody();
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    29
    if (isset($data->text)) {
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    30
        $result = 'true';
1427
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    31
    } else {
8b3d57a519eb remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents: 1050
diff changeset
    32
        $result = json_encode($data);
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    33
    }
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
     * Tweet sent (hopefully), redirect back home...
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    36
     */
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    37
    //header('Location: ' . URL_ROOT . '?result=' . $result);
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    38
    echo ($result);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    39
}
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    40
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    41
function addHashtagMastodon($status, $hashtag, $group) : string {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    42
    $res = $status;
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    43
    $to_add = "";
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
    if(!str_contains($status, "#$hashtag")) {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    46
        $to_add .= " #$hashtag";
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    47
    }
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    48
    if(!str_contains($status, $group)) {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    49
        $to_add .= " $group";
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    50
    }
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
    return mb_substr($status,0, 512-mb_strlen($to_add)).$to_add;
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    53
}
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    54
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    55
function sendMastodon()
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    56
{
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    57
    $token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    58
    $domain = $_SESSION['SOCIAL_LOGIN_DOMAIN'];
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    59
    $status = addHashtagMastodon($_POST['status'], ltrim($_POST['hashtag'], '#'), $_POST['group']);
1577
96a6eada7982 Correct some details again
ymh <ymh.work@gmail.com>
parents: 1575
diff changeset
    60
1558
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    61
    $client = new Client([
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    62
        'base_uri' => "https://$domain",
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    63
        'timeout'  => 2.0,
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    64
    ]);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    65
    $resp = $client->post("/api/v1/statuses", [
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    66
        'headers' => [
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    67
            'Authorization'   => 'Bearer ' . $token,
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    68
            'Accept'          => 'application/json',
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    69
            'Idempotency-Key' => hash('sha256', $status),
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    70
        ],
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    71
        'form_params' => [ 'status' => $status ],
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    72
    ]);
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    73
    echo('true');
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    74
}
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    75
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
/**
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    78
 * Check for a POSTed status message to send to Twitter
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    79
 */
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    80
if (
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    81
    !empty($_POST) && isset($_POST['status'])
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    82
    && isset($_SESSION['SOCIAL_ACCESS_TOKEN'])
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    83
) {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    84
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    85
    $socialNetwork = isset($_POST['social_network']) ? $_POST['social_network'] : "Twitter";
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    86
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    87
    if ($socialNetwork == "Twitter") {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    88
        sendTwitter();
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    89
    } else {
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    90
        sendMastodon();
761ba7426984 upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents: 1557
diff changeset
    91
    }
0
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    92
} else {
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    93
    /**
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    94
     * Mistaken request? Some malfeasant trying something?
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    95
     */
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    96
    exit('Invalid tweet request. Oops. Sorry.');
4eba9c11703f first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    97
}