$maxLen ) { throw new ValidationException( 'message_too_long', "Message must be at most {$maxLen} characters." ); } $name = self::clean( $input['display_name'] ?? '' ); $wantsAnon = filter_var( $input['anonymous'] ?? false, FILTER_VALIDATE_BOOLEAN ); $policy = (string) ( $settings['anonymity_policy'] ?? 'allow' ); if ( mb_strlen( $name ) > self::NAME_MAX ) { throw new ValidationException( 'name_too_long', 'Display name is too long.' ); } switch ( $policy ) { case 'force_anonymous': $anon = true; break; case 'require_name': if ( '' === $name ) { throw new ValidationException( 'name_required', 'Please enter a display name.' ); } $anon = false; break; default: $anon = $wantsAnon; } if ( $anon ) { $name = ''; // never persist the name of an anonymous donor } // Mask profanity before anything is stored (same-length masking, so length checks above still hold). $filter = new ProfanityFilter( $settings ); $name = $filter->filter( $name ); $message = $filter->filter( $message ); return array( 'goal_id' => (int) $goal['id'], 'currency' => $currency, 'base_minor' => $base, 'fee_minor' => $fee, 'total_minor' => $base + $fee, 'donor_name' => $name, 'is_anonymous' => $anon, 'message' => $message, 'cover_fees' => $cover, 'moderation' => ( ! empty( $settings['moderate_messages'] ) && '' !== $message ) ? 'pending' : 'approved', ); } /** * Goal-open check, amount parsing/minimum and fee cover only (used by the live fee preview). * * @return array{currency:string,base_minor:int,fee_minor:int,total_minor:int,cover_fees:bool} * @throws ValidationException */ public static function quote( array $input, array $goal, array $settings, FeeCalculator $fees, int $nowTs ): array { if ( 'complete' === ( $goal['status'] ?? '' ) ) { throw new ValidationException( 'goal_inactive', 'This goal is closed and no longer accepting donations. Thank you for your support!', 409 ); } if ( ! self::goalIsOpen( $goal, $nowTs ) ) { throw new ValidationException( 'goal_inactive', 'This goal is not accepting donations.', 409 ); } $currency = strtoupper( (string) $goal['currency'] ); $rawAmount = is_scalar( $input['amount'] ?? null ) ? trim( (string) $input['amount'] ) : ''; try { $base = Money::fromDecimalString( $rawAmount, $currency )->minor; } catch ( \InvalidArgumentException $e ) { throw new ValidationException( 'invalid_amount', 'Please enter a valid amount.' ); } $min = \StreamGoals\Core\Settings::effectiveMin( (int) ( $settings['min_donation_minor'] ?? 0 ) ); if ( $base <= 0 || $base < $min ) { throw new ValidationException( 'amount_too_small', 'The minimum donation is ' . ( new Money( $min, $currency ) )->toDecimalString() . ' ' . $currency . '.' ); } if ( $base > 99999999 ) { // sanity cap (~$999,999.99) throw new ValidationException( 'amount_too_large', 'The amount is too large.' ); } $cover = filter_var( $input['cover_fees'] ?? false, FILTER_VALIDATE_BOOLEAN ); $fee = $cover ? $fees->feeFor( $base ) : 0; return array( 'currency' => $currency, 'base_minor' => $base, 'fee_minor' => $fee, 'total_minor' => $base + $fee, 'cover_fees' => $cover, ); } /** Active status and inside the optional [starts_at, ends_at) window (UTC datetimes). */ public static function goalIsOpen( array $goal, int $nowTs ): bool { if ( 'active' !== ( $goal['status'] ?? '' ) ) { return false; } if ( ! empty( $goal['starts_at'] ) && strtotime( $goal['starts_at'] . ' UTC' ) > $nowTs ) { return false; } if ( ! empty( $goal['ends_at'] ) && strtotime( $goal['ends_at'] . ' UTC' ) <= $nowTs ) { return false; } return true; } private static function clean( mixed $v ): string { if ( ! is_scalar( $v ) ) { return ''; } $s = strip_tags( (string) $v ); $s = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $s ) ?? ''; return trim( $s ); } }