<?php
/**
 * @package Hafenstudios\Wellenbrecher
 */

namespace Hafenstudios\Wellenbrecher\Rules;

use Hafenstudios\Wellenbrecher\Engine\Context;
use Hafenstudios\Wellenbrecher\Engine\RuleResult;

defined( 'ABSPATH' ) || exit;

/**
 * SERVER_PROTOCOL HTTP/1.0 bei modernem Browser-Claim: kein aktueller
 * Browser spricht noch HTTP/1.0, primitive Bots schon.
 */
class ProtocolRule extends AbstractRule {

	public const ID             = 'http10';
	public const DEFAULT_POINTS = 10;

	public function __construct( int $points = self::DEFAULT_POINTS ) {
		parent::__construct( $points );
	}

	public function id(): string {
		return self::ID;
	}

	public function stage(): int {
		return 1;
	}

	public function evaluate( Context $context ): RuleResult {
		if ( 'HTTP/1.0' !== strtoupper( trim( $context->server( 'protocol' ) ) ) ) {
			return $this->miss();
		}
		$ua = $context->server( 'user_agent' );
		// Moderner Browser-Claim: Mozilla/5.0 mit einem Engine-Token.
		if ( false !== stripos( $ua, 'mozilla/5.0' )
			&& preg_match( '/(chrome|chromium|firefox|safari|edg)\//i', $ua ) ) {
			return $this->hit();
		}
		return $this->miss();
	}
}
