-
Notifications
You must be signed in to change notification settings - Fork 0
/
jolt-twitch-plugin.php
executable file
·510 lines (422 loc) · 14.2 KB
/
jolt-twitch-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
/*
Plugin Name: Jolt Twitch
Plugin URI: http://joltradio.org
Description: Pull twitch status and serve through API
Version: 0.3
Author: Rafa
Author URI: rrdesign.us
Text Domain: jolt-twitch
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Copyright © 2021 Rafa
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Get an Oauth token from Twitch to subscribe to events.
* @return string $token is our token from Twitch, encoded in base64.
*/
function jolt_twitch_getToken() {
$token = get_transient('jolt_twitch_token');
if (false !== $token) {
return $token;
}
$twitchClientId = get_option('jolt_twitchClientId', '0');
$twitchSecret = get_option('jolt_twitchSecret', 'change this secret key');
$oauthUrl = 'https://id.twitch.tv/oauth2/token';
$payload = [
'client_id' => $twitchClientId,
'client_secret' => $twitchSecret,
'grant_type' => 'client_credentials'
];
$headers = [
'Content-Type' => 'application/json'
];
$response = wp_remote_post($oauthUrl, [
'headers' => $headers,
'body' => wp_json_encode($payload),
'timeout' => 10
]);
if (is_wp_error($response)) {
error_log('Attempt to get token has failed.');
return false;
}
$body = wp_remote_retrieve_body($response);
$body = json_decode($body, true);
if ($body === false || !isset($body['access_token'])) {
return false;
}
$token = base64_encode($body['access_token']);
set_transient( 'jolt_twitch_token', $token, $body['expires_in'] - 30 );
return $token;
}
/**
* Subscribe to the Twitch events we want: when we start broadcasting
* and when we stop, checking to see we're not already subscribed.
* @return void
*/
function jolt_twitch_subscribe_flow() {
if ( get_option('jolt_twitchTesting') === 'on') {
return false;
}
$webhookUrl = 'https://api.twitch.tv/helix/eventsub/subscriptions';
$token = jolt_twitch_getToken();
if (false === $token)
return null;
//Check if we're already subscribed
$currentSubs = jolt_twitch_getSubs();
if ( sizeof($currentSubs) === 2 ) {
return null;
}
$twitchClientId = get_option('jolt_twitchClientId', '0');
$twitchSubSecret = get_option('jolt_twitchSubSecret', 'change this secret key');
jolt_twitch_subEvent('stream.online', $twitchClientId, $twitchSubSecret, $token, $webhookUrl);
jolt_twitch_subEvent('stream.offline', $twitchClientId, $twitchSubSecret, $token, $webhookUrl);
}
/**
* Check if we are subscribed to our events, so as not to subscribe again.
* @return bool|array false if there are no subs, otherwise an array of $allSubs.
*/
function jolt_twitch_getSubs() {
$token = jolt_twitch_getToken();
if (false === $token)
return false;
$subscriptionsUrl = 'https://api.twitch.tv/helix/eventsub/subscriptions?status=enabled';
$twitchClientId = get_option('jolt_twitchClientId', '0');
$headers = [
'Client-ID' => $twitchClientId,
'Authorization' => 'Bearer ' . base64_decode($token)
];
$response = wp_remote_get($subscriptionsUrl, [
'headers' => $headers
]);
$body = wp_remote_retrieve_body($response);
$body = json_decode($body, true);
if (isset($body['data'])) {
$allSubs = [];
foreach ($body['data'] as $sub) {
$type = $sub['type'];
$id = $sub['id'];
$allSubs[$type] = $id;
}
return $allSubs;
}
return false;
}
/**
* Subscribe to a Twitch event.
* @param string $type of Twitch event (https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types)
* @param int $id Your Twitch Client ID
* @param string $secret Your Twitch secret key
* @param string $token retrieved by jolt_twitch_getToken()
* @param string $url for Twicth EventSubs
* @return bool False if the subscription fails, else true.
*/
function jolt_twitch_subEvent($type, $id, $secret, $token, $url) {
$broadcasterUserId = get_option('jolt_twitchUserId', '0');
$headers = [
'Client-ID' => $id,
'Authorization' => 'Bearer ' . base64_decode($token),
'Content-Type' => 'application/json',
];
$payload = [
'type' => $type,
'version' => 1,
'condition' => [
'broadcaster_user_id' => $broadcasterUserId
],
'transport' => [
'method' => 'webhook',
'callback' => get_site_url() . '/wp-json/wp/v2/jolt-twitch/event-callback',
'secret' => $secret
]
];
$response = wp_remote_post($url, [
'headers' => $headers,
'body' => json_encode($payload),
'timeout' => 10
]);
if (is_wp_error($response)) {
error_log('Attempt to subscribe has failed: ' . $type);
return false;
}
$body = wp_remote_retrieve_body($response);
$body = json_decode($body, true);
return true;
}
/**
* Takes any current subscriptions as an input and ubsubscribes them.
* @param array $subscriptions returned from jolt_twitch_getSubs()
* @return void
*/
function jolt_twitch_unsubEvents($subscriptions) {
if (empty($subscriptions))
return false;
$token = jolt_twitch_getToken();
$twitchClientId = get_option('jolt_twitchClientId', '0');
$headers = [
'Client-ID' => $twitchClientId,
'Authorize' => 'Bearer ' . base64_decode($token)
];
$url = 'https://api.twitch.tv/helix/eventsub/subscriptions?id=';
foreach ($subscriptions as $sub) {
$deleteUrl = $url . $sub;
wp_remote_request($deleteUrl, ['method' => 'DELETE']);
}
}
/**
* Checks our Twitch subscriptions, unsubscribes from all of them, and
* then deletes our current Twitch token.
* @return void
*/
function jolt_twitch_unsubAllEvents() {
$subs = jolt_twitch_getSubs();
jolt_twitch_unsubEvents($subs);
delete_transient('jolt_twitch_token');
}
/**
* Handles our response to Twitch after subscribing to an event, and if the
* subscription is succesful will update our endpoint with our subscription status.
*
* Twitch will immediately send a POST request back to us after subscribing.
* In it there will be data that needs to be combined and hashed together
* with our own Twitch secret key and served as a response to the POST request. This
* confirmation also prevents fradulent subscriptions.
*
* If our subscription is valid, we go ahead and call jolt_twitch_setStatus() to update
* our endpoint.
* @return void
*/
function jolt_twitch_eventResponse() {
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
$isVerified = false;
$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_LOWER);
if (isset($headers['twitch-eventsub-message-signature'])) {
$signature = $headers['twitch-eventsub-message-signature'];
$messageId = $headers['twitch-eventsub-message-id'];
$timeStamp = $headers['twitch-eventsub-message-timestamp'];
} else {
status_header(403);
error_log('No signature header.');
return;
}
if (isset($signature)) {
$twitchSubSecret = get_option('jolt_twitchSubSecret', '0');
$data = file_get_contents("php://input");
$hmacMessage = $messageId . $timeStamp . $data;
$hash = 'sha256=' . hash_hmac('sha256', $hmacMessage, $twitchSubSecret);
$isVerified = $hash === $signature;
}
if (!$isVerified) {
status_header(403);
error_log('isVerified is false. returning error.');
return;
}
$data = json_decode($data, true);
$challenge = isset($data['challenge']) ? $data['challenge'] : null;
$statusLive = isset($data['subscription']['type']) ? $data['subscription']['type'] :null;
if (isset($challenge)) {
echo $challenge;
return;
}
if (isset($statusLive)) {
if ($statusLive === 'stream.online') {
jolt_twitch_setStatus(true);
} elseif ($statusLive === 'stream.offline') {
jolt_twitch_setStatus(false);
}
}
return;
}
}
/**
* Update the live status of our Twitch stream.
*
* Using WP's update_option we store our live status, recieved from
* our subscriptions, in the WP database. This way we can serve it later
* as an API response.
* @param bool $liveStatus
* @return bool true if the status was updated, otherwise false.
*/
function jolt_twitch_setStatus($liveStatus) {
$status = [
'live' => $liveStatus
];
return update_option('jolt_twitchStatus', $status);
}
function jolt_twitch_getStatus() {
if ( get_option('jolt_twitchForceLive') === 'on') {
return ['live' => true];
}
return get_option('jolt_twitchStatus');
}
/**
* Create the plugin settings menu and page in WP backend
* @return void
*/
function jolt_twitch_addSettingsPage() {
add_options_page(
'Jolt Twitch',
'Jolt Twitch',
'manage_options',
'jolt-twitch',
'jolt_twitch_createAdminPage'
);
}
/**
* Populate the plugin settings page in WP backend
* @return void
*/
function jolt_twitch_createAdminPage() {
if (!current_user_can('manage_options')) {
wp_die('Unauthorized user');
}
$twitchClientId = get_option('jolt_twitchClientId', 'none');
$twitchUserId = get_option('jolt_twitchUserId', 'none');
$twitchSecret = get_option('jolt_twitchSecret', 'change this secret key');
$twitchSubSecret = get_option('jolt_twitchSubSecret', 'change this secret key');
$twitchForceLive = get_option('jolt_twitchForceLive');
if ( !isset( $_POST['jolt_twitch_settings_noncer'] )
|| !wp_verify_nonce( $_POST['jolt_twitch_settings_noncer'], 'jolt_twitch' )
) {
print 'Sorry, your nonce did not verify.';
} else {
if ( isset($_POST['jolt_twitchClientId']) ) {
$twitchClientId = $_POST['jolt_twitchClientId'];
$twitchClientId= trim($twitchClientId);
$twitchClientId = strip_tags($twitchClientId);
update_option('jolt_twitchClientId', $twitchClientId);
}
if ( isset($_POST['jolt_twitchUserId']) ) {
$twitchUserId = $_POST['jolt_twitchUserId'];
$twitchUserId = trim($twitchUserId);
$twitchUserId = strip_tags($twitchUserId);
update_option('jolt_twitchUserId', $twitchUserId);
}
if ( isset($_POST['jolt_twitchSecret']) ) {
$twitchSecret = $_POST['jolt_twitchSecret'];
$twitchSecret = trim($twitchSecret);
$twitchSecret = strip_tags($twitchSecret);
update_option('jolt_twitchSecret', $twitchSecret);
}
if ( isset($_POST['jolt_twitchSubSecret']) ) {
$twitchSubSecret = $_POST['jolt_twitchSubSecret'];
$twitchSubSecret = trim($twitchSubSecret);
$twitchSubSecret = strip_tags($twitchSubSecret);
update_option('jolt_twitchSubSecret', $twitchSubSecret);
}
if ( isset($_POST['jolt_twitchForceLive']) ) {
update_option('jolt_twitchForceLive', 'on');
$twitchForceLive = 'on';
} else {
update_option('jolt_twitchForceLive', 'off');
$twitchForceLive = 'off';
}
if ( isset($_POST['jolt_twitchUnsubscribeAllEvents']) ) {
jolt_twitch_unsubAllEvents();
}
if ( isset($_POST['jolt_twitchSubscribeAllEvents']) ) {
jolt_twitch_subscribe_flow();
}
}
?>
<div>
<h2>Jolt Twitch</h2>
<h3>Credentials</h3>
<form method="post">
<p>
<label>
Twitch Client ID
<input name="jolt_twitchClientId" type="text" value="<?= $twitchClientId ?>" />
</label>
</p>
<p>
<label>
User ID for stream
<input name="jolt_twitchUserId" type="text" value="<?= $twitchUserId ?>" />
</label>
</p>
<p>
<label>
Secret Key
<input name="jolt_twitchSecret" type="text" value="<?= $twitchSecret ?>" />
</label>
</p>
<p>
<label>
Sub Secret
<input name="jolt_twitchSubSecret" type="text" value="<?= $twitchSubSecret ?>" />
</label>
</p>
<p>
<label>
Set status to 'live' for testing.
<input name="jolt_twitchForceLive" type="checkbox" <?= $twitchForceLive === 'on' ? 'checked' : ''?> />
</label>
</p>
<?php wp_nonce_field( 'jolt_twitch', 'jolt_twitch_settings_noncer' ); ?>
<input type="Submit" value="Save" class="button button-primary button-large">
</form>
<form method="post">
<?php wp_nonce_field( 'jolt_twitch', 'jolt_twitch_settings_noncer' ); ?>
<input type="hidden" name="jolt_twitchUnsubscribeAllEvents" value="true" />
<input type="Submit" value="Unsubscribe All Events" class="button button-primary button-large">
</form>
<form method="post">
<?php wp_nonce_field( 'jolt_twitch', 'jolt_twitch_settings_noncer' ); ?>
<input type="hidden" name="jolt_twitchSubscribeAllEvents" value="true" />
<input type="Submit" value="Subscribe All Events" class="button button-primary button-large">
</form>
</div>
<?php }
/**
* WP plugin activation routine.
*
* We set our testing status off, the live status off in case either
* of these were left on due to unforseen circumstances, and we schedule
* our WP cronjob to check our subscriptions hourly.
* @return void
*/
function jolt_twitch_activation() {
update_option('jolt_twitchTesting', 'off');
update_option('jolt_twitchStatus', ['live' => false]);
if ( !wp_next_scheduled('jolt_twitch_update') ) {
wp_schedule_event(time(), 'hourly', 'jolt_twitch_update');
}
}
/**
* WP plugin deactivation routine.
*
* Clears the WP cronjob and ubsubscribes from any Twitch events still
* outstanding.
* @return void
*/
function jolt_twitch_deactivation() {
wp_clear_scheduled_hook('jolt_twitch_update');
jolt_twitch_unsubAllEvents();
}
add_action('admin_menu', 'jolt_twitch_addSettingsPage');
add_action( 'rest_api_init', function () {
register_rest_route('wp/v2', '/jolt-twitch', [
'methods' => 'GET',
'callback' => 'jolt_twitch_getStatus',
]);
register_rest_route('wp/v2', '/jolt-twitch/event-callback', [
'methods' => 'POST',
'callback' => 'jolt_twitch_eventResponse',
]);
} );
add_action('jolt_twitch_update', 'jolt_twitch_subscribe_flow', 10, 2);
register_activation_hook(__FILE__, 'jolt_twitch_activation');
register_deactivation_hook(__FILE__, 'jolt_twitch_deactivation');