2026-11-15 13:01:31 -08:00
|
|
|
<?php
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
|
|
namespace StreamGoals\Alerts;
|
|
|
|
|
|
|
|
|
|
/** Pure helpers: MIME whitelist, type classification and duration clamping for alert media. */
|
|
|
|
|
final class Media {
|
|
|
|
|
|
|
|
|
|
/** Exact MIME => kind. Everything else (svg, html, ...) is rejected. */
|
|
|
|
|
public const MIMES = array(
|
|
|
|
|
'image/png' => 'image',
|
|
|
|
|
'image/jpeg' => 'image',
|
|
|
|
|
'image/gif' => 'image',
|
|
|
|
|
'image/webp' => 'image',
|
|
|
|
|
'video/mp4' => 'video',
|
|
|
|
|
'video/webm' => 'video',
|
|
|
|
|
'audio/mpeg' => 'audio',
|
|
|
|
|
'audio/ogg' => 'audio',
|
|
|
|
|
'audio/wav' => 'audio',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
public const WARN_BYTES = 26214400; // 25 MB
|
|
|
|
|
public const MIN_DURATION_MS = 1000;
|
|
|
|
|
public const MAX_DURATION_MS = 60000;
|
|
|
|
|
public const DEFAULT_MS = 6000;
|
|
|
|
|
public const VIDEO_CAP_MS = 30000;
|
|
|
|
|
|
|
|
|
|
/** 'image' | 'video' | 'audio' for an allowed MIME, else null. Strict: no parameters, no case games. */
|
|
|
|
|
public static function kind( mixed $mime ): ?string {
|
|
|
|
|
if ( ! is_string( $mime ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return self::MIMES[ strtolower( $mime ) ] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function isAllowed( mixed $mime ): bool {
|
|
|
|
|
return null !== self::kind( $mime );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 'image' | 'video' for a visual attachment MIME, else null (audio is not a visual). */
|
|
|
|
|
public static function visualType( mixed $mime ): ?string {
|
|
|
|
|
$k = self::kind( $mime );
|
|
|
|
|
return 'image' === $k || 'video' === $k ? $k : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function isSound( mixed $mime ): bool {
|
|
|
|
|
return 'audio' === self::kind( $mime );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function clampDurationMs( mixed $ms, int $default = self::DEFAULT_MS ): int {
|
|
|
|
|
if ( ! is_numeric( $ms ) ) {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
return max( self::MIN_DURATION_MS, min( self::MAX_DURATION_MS, (int) $ms ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Whole seconds (1-60) to clamped milliseconds; null when the input is not a number. */
|
|
|
|
|
public static function secondsToMs( mixed $s ): ?int {
|
|
|
|
|
if ( ! is_scalar( $s ) || ! preg_match( '/^\s*\d{1,4}(\.\d+)?\s*$/', (string) $s ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return self::clampDurationMs( (int) round( (float) $s * 1000 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function clampVolume( mixed $v, int $default = 80 ): int {
|
|
|
|
|
return is_numeric( $v ) ? max( 0, min( 100, (int) $v ) ) : $default;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 13:41:18 -08:00
|
|
|
/**
|
|
|
|
|
* Appends ?v=<timestamp> so a replaced file with the same name is fetched again. The timestamp comes from the
|
|
|
|
|
* server (file mtime, else the attachment's post_modified_gmt), never from input. Unknown time = URL unchanged.
|
|
|
|
|
*/
|
|
|
|
|
public static function withVersion( string $url, int $fileMtime, ?string $postModifiedGmt = null ): string {
|
|
|
|
|
$v = $fileMtime;
|
|
|
|
|
if ( $v <= 0 && null !== $postModifiedGmt && preg_match( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $postModifiedGmt ) ) {
|
|
|
|
|
$t = strtotime( $postModifiedGmt . ' UTC' );
|
|
|
|
|
$v = false === $t ? 0 : $t;
|
|
|
|
|
}
|
|
|
|
|
if ( $v <= 0 || str_contains( $url, '#' ) ) {
|
|
|
|
|
return $url;
|
|
|
|
|
}
|
|
|
|
|
$base = preg_replace( '/([?&])v=[^&]*(&|$)/', '$1', $url ) ?? $url;
|
|
|
|
|
$base = rtrim( $base, '?&' );
|
|
|
|
|
return $base . ( str_contains( $base, '?' ) ? '&' : '?' ) . 'v=' . $v;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 13:01:31 -08:00
|
|
|
/** Only plain http(s) URLs are ever handed to the overlay. */
|
|
|
|
|
public static function safeUrl( mixed $url ): ?string {
|
|
|
|
|
if ( ! is_string( $url ) || '' === $url || strlen( $url ) > 2000 || preg_match( '/[\x00-\x20\x7F"\'<>\\\\]/', $url ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return preg_match( '#^https?://[^/]#i', $url ) ? $url : null;
|
|
|
|
|
}
|
|
|
|
|
}
|