| author | ymh <ymh.work@gmail.com> |
| Fri, 15 Nov 2024 17:17:09 +0100 | |
| changeset 1581 | 2d713fdd532f |
| parent 1577 | 96a6eada7982 |
| permissions | -rwxr-xr-x |
|
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 | 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 |
|
|
1581
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
14 |
$status = addHashtagTwitter($_POST['status'], ltrim($_POST['hashtag'], '#')); |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
15 |
|
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
16 |
$data = $connection->post("statuses/update", ["status" => $status]); |
|
0
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
17 |
|
| 1557 | 18 |
// $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
|
19 |
// $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
|
20 |
// $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
|
21 |
// $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
|
22 |
// $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
|
23 |
// $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
|
24 |
|
|
8b3d57a519eb
remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents:
1050
diff
changeset
|
25 |
// /** |
|
8b3d57a519eb
remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents:
1050
diff
changeset
|
26 |
// * 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
|
27 |
// * 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
|
28 |
// */ |
|
8b3d57a519eb
remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents:
1050
diff
changeset
|
29 |
// $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
|
30 |
// $result = $response->getBody(); |
|
0
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
31 |
if (isset($data->text)) { |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
32 |
$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
|
33 |
} else { |
|
8b3d57a519eb
remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents:
1050
diff
changeset
|
34 |
$result = json_encode($data); |
|
0
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 |
/** |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
37 |
* Tweet sent (hopefully), redirect back home... |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
38 |
*/ |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
39 |
//header('Location: ' . URL_ROOT . '?result=' . $result); |
|
1558
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
40 |
echo ($result); |
|
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 |
|
|
1581
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
43 |
function addHashtagTwitter($status, $hashtag) : string { |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
44 |
$res = $status; |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
45 |
$to_add = ""; |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
46 |
|
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
47 |
if(!str_contains($status, "#$hashtag")) { |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
48 |
$to_add .= " #$hashtag"; |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
49 |
} |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
50 |
|
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
51 |
return mb_substr($status,0, 280-mb_strlen($to_add)).$to_add; |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
52 |
} |
|
2d713fdd532f
remove default hashtag and improve reactivity
ymh <ymh.work@gmail.com>
parents:
1577
diff
changeset
|
53 |
|
|
1558
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
54 |
function addHashtagMastodon($status, $hashtag, $group) : string { |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
55 |
$res = $status; |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
56 |
$to_add = ""; |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
57 |
|
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
58 |
if(!str_contains($status, "#$hashtag")) { |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
59 |
$to_add .= " #$hashtag"; |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
60 |
} |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
61 |
if(!str_contains($status, $group)) { |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
62 |
$to_add .= " $group"; |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
63 |
} |
|
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 |
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
|
66 |
} |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
67 |
|
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
68 |
function sendMastodon() |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
69 |
{ |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
70 |
$token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']); |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
71 |
$domain = $_SESSION['SOCIAL_LOGIN_DOMAIN']; |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
72 |
$status = addHashtagMastodon($_POST['status'], ltrim($_POST['hashtag'], '#'), $_POST['group']); |
| 1577 | 73 |
|
|
1558
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
74 |
$client = new Client([ |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
75 |
'base_uri' => "https://$domain", |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
76 |
'timeout' => 2.0, |
|
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 |
$resp = $client->post("/api/v1/statuses", [ |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
79 |
'headers' => [ |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
80 |
'Authorization' => 'Bearer ' . $token, |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
81 |
'Accept' => 'application/json', |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
82 |
'Idempotency-Key' => hash('sha256', $status), |
|
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 |
'form_params' => [ 'status' => $status ], |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
85 |
]); |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
86 |
echo('true'); |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
87 |
} |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
88 |
|
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
89 |
|
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
90 |
/** |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
91 |
* 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
|
92 |
*/ |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
93 |
if ( |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
94 |
!empty($_POST) && isset($_POST['status']) |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
95 |
&& isset($_SESSION['SOCIAL_ACCESS_TOKEN']) |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
96 |
) { |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
97 |
|
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
98 |
$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
|
99 |
|
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
100 |
if ($socialNetwork == "Twitter") { |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
101 |
sendTwitter(); |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
102 |
} else { |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
103 |
sendMastodon(); |
|
761ba7426984
upgrade metadataplayer and add a sitemap
ymh <ymh.work@gmail.com>
parents:
1557
diff
changeset
|
104 |
} |
|
0
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
105 |
} else { |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
106 |
/** |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
107 |
* Mistaken request? Some malfeasant trying something? |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
108 |
*/ |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
109 |
exit('Invalid tweet request. Oops. Sorry.'); |
|
4eba9c11703f
first import
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
110 |
} |