1 <?php |
1 <?php |
2 |
2 |
3 include_once 'common.php'; |
3 include_once 'common.php'; |
|
4 |
4 use Abraham\TwitterOAuth\TwitterOAuth; |
5 use Abraham\TwitterOAuth\TwitterOAuth; |
|
6 use GuzzleHttp\Client; |
5 |
7 |
6 /** |
|
7 * Check for a POSTed status message to send to Twitter |
|
8 */ |
|
9 if (!empty($_POST) && isset($_POST['status']) |
|
10 && isset($_SESSION['SOCIAL_ACCESS_TOKEN'])) { |
|
11 |
8 |
|
9 function sendTwitter() |
|
10 { |
12 $token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']); |
11 $token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']); |
13 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']); |
12 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']); |
14 |
13 |
15 $data = $connection->post("statuses/update", ["status" => $_POST['status']]); |
14 $data = $connection->post("statuses/update", ["status" => $_POST['status']]); |
16 |
15 |
34 } |
33 } |
35 /** |
34 /** |
36 * Tweet sent (hopefully), redirect back home... |
35 * Tweet sent (hopefully), redirect back home... |
37 */ |
36 */ |
38 //header('Location: ' . URL_ROOT . '?result=' . $result); |
37 //header('Location: ' . URL_ROOT . '?result=' . $result); |
39 echo($result); |
38 echo ($result); |
|
39 } |
|
40 |
|
41 function addHashtagMastodon($status, $hashtag, $group) : string { |
|
42 $res = $status; |
|
43 $to_add = ""; |
|
44 |
|
45 if(!str_contains($status, "#$hashtag")) { |
|
46 $to_add .= " #$hashtag"; |
|
47 } |
|
48 if(!str_contains($status, $group)) { |
|
49 $to_add .= " $group"; |
|
50 } |
|
51 |
|
52 return mb_substr($status,0, 512-mb_strlen($to_add)).$to_add; |
|
53 } |
|
54 |
|
55 function sendMastodon() |
|
56 { |
|
57 $token = unserialize($_SESSION['SOCIAL_ACCESS_TOKEN']); |
|
58 $domain = $_SESSION['SOCIAL_LOGIN_DOMAIN']; |
|
59 $status = addHashtagMastodon($_POST['status'], ltrim($_POST['hashtag'], '#'), $_POST['group']); |
|
60 |
|
61 $client = new Client([ |
|
62 'base_uri' => "https://$domain", |
|
63 'timeout' => 2.0, |
|
64 ]); |
|
65 $resp = $client->post("/api/v1/statuses", [ |
|
66 'headers' => [ |
|
67 'Authorization' => 'Bearer ' . $token, |
|
68 'Accept' => 'application/json', |
|
69 'Idempotency-Key' => hash('sha256', $status), |
|
70 ], |
|
71 'form_params' => [ 'status' => $status ], |
|
72 ]); |
|
73 echo('true'); |
|
74 } |
|
75 |
|
76 |
|
77 /** |
|
78 * Check for a POSTed status message to send to Twitter |
|
79 */ |
|
80 if ( |
|
81 !empty($_POST) && isset($_POST['status']) |
|
82 && isset($_SESSION['SOCIAL_ACCESS_TOKEN']) |
|
83 ) { |
|
84 |
|
85 $socialNetwork = isset($_POST['social_network']) ? $_POST['social_network'] : "Twitter"; |
|
86 |
|
87 if ($socialNetwork == "Twitter") { |
|
88 sendTwitter(); |
|
89 } else { |
|
90 sendMastodon(); |
|
91 } |
40 } else { |
92 } else { |
41 /** |
93 /** |
42 * Mistaken request? Some malfeasant trying something? |
94 * Mistaken request? Some malfeasant trying something? |
43 */ |
95 */ |
44 exit('Invalid tweet request. Oops. Sorry.'); |
96 exit('Invalid tweet request. Oops. Sorry.'); |