2026-11-15 08:45:17 -08:00
|
|
|
<?php
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
|
|
namespace StreamGoals\Rest;
|
|
|
|
|
|
|
|
|
|
use StreamGoals\Core\Settings;
|
2026-11-15 13:41:18 -08:00
|
|
|
use StreamGoals\Overlay\Build;
|
2026-11-15 08:45:17 -08:00
|
|
|
use StreamGoals\Overlay\OverlayService;
|
|
|
|
|
use StreamGoals\Overlay\Payloads;
|
|
|
|
|
use StreamGoals\Overlay\Router;
|
|
|
|
|
use StreamGoals\Overlay\WpdbDataSource;
|
|
|
|
|
|
|
|
|
|
/** GET streamgoals/v1/overlay/{token}/{widget} — public-safe, ETag'd JSON for OBS polling. */
|
|
|
|
|
final class OverlayController {
|
|
|
|
|
|
|
|
|
|
public static function register(): void {
|
|
|
|
|
add_action( 'rest_api_init', array( self::class, 'routes' ) );
|
|
|
|
|
Router::register();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function routes(): void {
|
2026-11-15 12:36:28 -08:00
|
|
|
register_rest_route( 'streamgoals/v1', '/overlay/(?P<token>[A-Za-z0-9]{16,128})/(?P<widget>goal|timer|counter|alerts|actions)', array(
|
2026-11-15 08:45:17 -08:00
|
|
|
'methods' => 'GET',
|
|
|
|
|
'callback' => array( self::class, 'handle' ),
|
|
|
|
|
'permission_callback' => '__return_true', // capability is the unguessable token, checked in handle()
|
|
|
|
|
'args' => array(
|
|
|
|
|
'since' => array( 'type' => 'integer', 'minimum' => 0, 'sanitize_callback' => 'absint' ),
|
2026-11-15 12:26:03 -08:00
|
|
|
'recent' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 1, 'sanitize_callback' => 'absint' ),
|
2026-11-15 08:45:17 -08:00
|
|
|
),
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 14:07:44 -08:00
|
|
|
/** Read-only. The stored token, or '' when none is configured. Safe for public requests. */
|
|
|
|
|
public static function current_token(): string {
|
2026-11-15 08:45:17 -08:00
|
|
|
$token = (string) Settings::get( 'overlay_token', '' );
|
2026-11-15 14:07:44 -08:00
|
|
|
return strlen( $token ) >= 32 ? $token : '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Token for the admin screens; generates one (a DB write) if missing, but ONLY for a user who can manage options.
|
|
|
|
|
* Public requests must never reach this; they use verify_token(), which never writes.
|
|
|
|
|
*/
|
|
|
|
|
public static function ensure_token(): string {
|
|
|
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
|
|
|
return '';
|
2026-11-15 08:45:17 -08:00
|
|
|
}
|
2026-11-15 14:07:44 -08:00
|
|
|
return Settings::ensure_overlay_token();
|
2026-11-15 08:45:17 -08:00
|
|
|
}
|
|
|
|
|
|
2026-11-15 14:07:44 -08:00
|
|
|
/** Read-only compare; with no token configured nothing matches (callers answer 404). */
|
2026-11-15 08:45:17 -08:00
|
|
|
public static function verify_token( string $given ): bool {
|
2026-11-15 14:07:44 -08:00
|
|
|
$token = self::current_token();
|
|
|
|
|
return '' !== $token && hash_equals( $token, $given );
|
2026-11-15 08:45:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function handle( \WP_REST_Request $req ): \WP_REST_Response|\WP_Error {
|
|
|
|
|
if ( ! self::verify_token( (string) $req['token'] ) ) {
|
|
|
|
|
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
|
|
|
|
|
}
|
|
|
|
|
$since = $req->has_param( 'since' ) && '' !== (string) $req->get_param( 'since' ) ? absint( $req->get_param( 'since' ) ) : null;
|
2026-11-15 12:26:03 -08:00
|
|
|
$recent = $req->has_param( 'recent' ) && 1 === absint( $req->get_param( 'recent' ) );
|
2026-11-15 13:01:31 -08:00
|
|
|
$service = new OverlayService( new WpdbDataSource() );
|
|
|
|
|
$payload = $service->payload( (string) $req['widget'], time(), $since, $recent );
|
2026-11-15 08:45:17 -08:00
|
|
|
if ( null === $payload ) {
|
|
|
|
|
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 13:01:31 -08:00
|
|
|
$etag = Payloads::etag( $payload, $service->etagSalt( (string) $req['widget'] ) );
|
2026-11-15 13:41:18 -08:00
|
|
|
$headers = array( 'ETag' => $etag, 'Cache-Control' => 'private, max-age=1, must-revalidate', 'X-Robots-Tag' => 'noindex' ) + Build::headers( Build::current() );
|
2026-11-15 08:45:17 -08:00
|
|
|
$inm = (string) $req->get_header( 'if_none_match' );
|
|
|
|
|
if ( '' !== $inm && in_array( $etag, array_map( 'trim', explode( ',', str_replace( 'W/', '', $inm ) ) ), true ) ) {
|
|
|
|
|
$res = new \WP_REST_Response( null, 304 );
|
|
|
|
|
} else {
|
|
|
|
|
$res = new \WP_REST_Response( $payload, 200 );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $headers as $k => $v ) {
|
|
|
|
|
$res->header( $k, $v );
|
|
|
|
|
}
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
}
|