2026-11-15 13:01:31 -08:00
<? php
declare ( strict_types = 1 );
namespace StreamGoals\Alerts ;
use StreamGoals\Admin\Input ;
/** Pure validation/sanitizing for the alert definition form. */
final class DefInput {
/**
* @param int[] $validGoalIds Existing goal ids (0 = all goals always allowed).
* @param int[] $enabledActionIds Enabled rewards that may be chosen as a trigger.
* @param callable(int):?array{mime:string,size?:int} $attachment Lookup of an attachment by id (null = missing).
* @return array{data:?array,error:?string,warnings:string[]}
*/
public static function validate ( array $post , int $minMinor , string $currency , array $validGoalIds , array $enabledActionIds , callable $attachment , int $defaultSort = 0 ) : array {
$fail = static fn ( string $m ) : array => array ( 'data' => null , 'error' => $m , 'warnings' => array () );
$title = Input :: text ( $post [ 'title' ] ?? '' , 200 );
if ( '' === $title ) {
return $fail ( 'Please give this alert a name, for example "Big donation fireworks".' );
}
if ( mb_strlen ( $title ) > 120 ) {
return $fail ( 'The name can be at most 120 characters.' );
}
$goal = Input :: int ( $post [ 'goal_id' ] ?? '0' , 0 , PHP_INT_MAX >> 1 );
if ( null === $goal || ( 0 !== $goal && ! in_array ( $goal , array_map ( 'intval' , $validGoalIds ), true ) ) ) {
return $fail ( 'Please choose all goals or one of your goals.' );
}
$trigger = is_scalar ( $post [ 'trigger_type' ] ?? null ) ? ( string ) $post [ 'trigger_type' ] : '' ;
$min = 0 ;
$action = 0 ;
if ( 'amount' === $trigger ) {
$min = Input :: amountToMinor ( $post [ 'amount' ] ?? '' , $currency );
if ( null === $min || $min <= 0 ) {
return $fail ( 'Please enter the smallest donation that should trigger this alert, for example 10.00.' );
}
if ( $min < $minMinor ) {
return $fail ( 'The amount must be at least the minimum donation (' . \StreamGoals\Admin\Format :: money ( $minMinor , $currency ) . ').' );
}
} elseif ( 'reward' === $trigger ) {
$action = Input :: int ( $post [ 'action_id' ] ?? '' , 1 , PHP_INT_MAX >> 1 );
if ( null === $action || ! in_array ( $action , array_map ( 'intval' , $enabledActionIds ), true ) ) {
return $fail ( 'Please choose one of your turned-on rewards.' );
}
} else {
return $fail ( 'Please choose when this alert plays.' );
}
$warnings = array ();
$visualId = 0 ;
$visual = '' ;
$sound = 0 ;
$vRaw = Input :: int ( $post [ 'visual_id' ] ?? '0' , 0 , PHP_INT_MAX >> 1 );
$sRaw = Input :: int ( $post [ 'sound_id' ] ?? '0' , 0 , PHP_INT_MAX >> 1 );
if ( null === $vRaw || null === $sRaw ) {
return $fail ( 'The chosen media file is not valid. Please pick it again.' );
}
if ( $vRaw > 0 ) {
$a = $attachment ( $vRaw );
if ( ! is_array ( $a ) ) {
return $fail ( 'The picture or video you chose no longer exists in your Media Library. Please pick it again.' );
}
$type = Media :: visualType ( $a [ 'mime' ] ?? null );
if ( null === $type ) {
return $fail ( 'The picture or video must be PNG, JPEG, GIF, WebP, MP4 or WebM. Other file types are not allowed.' );
}
$visualId = $vRaw ;
$visual = $type ;
self :: sizeWarning ( $a , 'picture or video' , 'video' === $type , $warnings );
}
if ( $sRaw > 0 ) {
$a = $attachment ( $sRaw );
if ( ! is_array ( $a ) ) {
return $fail ( 'The sound you chose no longer exists in your Media Library. Please pick it again.' );
}
if ( ! Media :: isSound ( $a [ 'mime' ] ?? null ) ) {
return $fail ( 'The sound must be an MP3, OGG or WAV file.' );
}
$sound = $sRaw ;
self :: sizeWarning ( $a , 'sound' , false , $warnings );
}
$vol = Input :: int ( $post [ 'volume' ] ?? '80' , 0 , 100 );
if ( null === $vol ) {
return $fail ( 'Volume must be a whole number from 0 to 100.' );
}
$dur = Media :: secondsToMs ( $post [ 'duration' ] ?? '6' );
if ( null === $dur || ( int ) round ( ( float ) $post [ 'duration' ] * 1000 ) !== $dur ) {
return $fail ( 'How long the picture stays must be between 1 and 60 seconds.' );
}
$message = Input :: text ( $post [ 'message' ] ?? '' , 400 );
if ( mb_strlen ( $message ) > 255 ) {
return $fail ( 'The message can be at most 255 characters.' );
}
$sortRaw = $post [ 'sort' ] ?? '' ;
$sort = ( is_scalar ( $sortRaw ) && '' === trim ( ( string ) $sortRaw ) ) ? $defaultSort : Input :: int ( $sortRaw , 0 , 99999 );
if ( null === $sort ) {
return $fail ( 'The sort order must be a whole number from 0 to 99999.' );
}
2026-11-15 13:41:18 -08:00
$frameless = self :: checkbox ( $post [ 'frameless' ] ?? null ) ? 1 : 0 ;
2026-11-15 13:01:31 -08:00
return array (
'data' => array (
'goal_id' => $goal ,
'trigger_type' => $trigger ,
'min_minor' => $min ,
'action_id' => $action ,
'title' => $title ,
'visual_id' => $visualId ,
'visual_type' => $visual ,
'sound_id' => $sound ,
'volume' => $vol ,
'duration_ms' => $dur ,
'message' => $message ,
'sort' => $sort ,
2026-11-15 13:41:18 -08:00
'frameless' => $frameless ,
2026-11-15 13:01:31 -08:00
),
'error' => null ,
'warnings' => $warnings ,
);
}
2026-11-15 13:41:18 -08:00
/** HTML checkbox: present with a truthy value = on; absent or anything odd = off. */
public static function checkbox ( mixed $v ) : bool {
return is_scalar ( $v ) && in_array ( strtolower ( trim ( ( string ) $v ) ), array ( '1' , 'on' , 'true' , 'yes' ), true );
}
2026-11-15 13:01:31 -08:00
/** @param string[] $warnings */
private static function sizeWarning ( array $a , string $what , bool $video , array & $warnings ) : void {
if ( ( int ) ( $a [ 'size' ] ?? 0 ) > Media :: WARN_BYTES ) {
$warnings [] = 'The ' . $what . ' is over 25 MB.' . ( $video ? ' Big videos can make OBS stutter or load slowly; a short, compressed clip works much better.' : ' Large files take longer to load, so the alert may start late.' );
}
}
}