Add visitor-IP header setting, checkout-button toggles, and configurable timer maximum
- Core\ClientIp: trusted-header resolution for rate limiting behind Cloudflare/proxies - Settings: show/hide Pay Later and Debit/Credit Card (Pay Later off by default) - add_time rule: optional max_timer_seconds (uncapped by default), editable in rule form Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4f92cfdade
commit
e4d9042aba
14 files changed
+278
-16
No files matched your search
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Core;
|
||||
|
||||
/**
|
||||
* Works out the visitor's IP for rate limiting. Pure (takes the $_SERVER values as an array) so it is unit-testable.
|
||||
*
|
||||
* Behind a CDN or reverse proxy, PHP's REMOTE_ADDR is the proxy's address, so every visitor would share one rate
|
||||
* limit. The owner can name the header their proxy sets. A header is ONLY trusted when the owner selected it, and
|
||||
* only if it holds a valid IP address; otherwise we fall back to REMOTE_ADDR.
|
||||
*/
|
||||
final class ClientIp {
|
||||
|
||||
public const MODE_DIRECT = ''; // no proxy: use REMOTE_ADDR
|
||||
public const MODE_CLOUDFLARE = 'cloudflare'; // CF-Connecting-IP
|
||||
public const MODE_REAL_IP = 'real_ip'; // X-Real-IP (nginx and many hosts)
|
||||
public const MODE_FORWARDED = 'forwarded'; // last address in X-Forwarded-For (exactly one proxy in front)
|
||||
|
||||
public const MODES = array(
|
||||
self::MODE_DIRECT => 'Not behind a proxy or CDN (most sites)',
|
||||
self::MODE_CLOUDFLARE => 'Cloudflare (CF-Connecting-IP)',
|
||||
self::MODE_REAL_IP => 'X-Real-IP header',
|
||||
self::MODE_FORWARDED => 'X-Forwarded-For header (one proxy in front)',
|
||||
);
|
||||
|
||||
/** @param array<string,mixed> $server $_SERVER-style values (already unslashed). */
|
||||
public static function resolve( array $server, string $mode = self::MODE_DIRECT ): string {
|
||||
$remote = self::valid( $server['REMOTE_ADDR'] ?? null );
|
||||
$fromHeader = null;
|
||||
switch ( $mode ) {
|
||||
case self::MODE_CLOUDFLARE:
|
||||
$fromHeader = self::valid( $server['HTTP_CF_CONNECTING_IP'] ?? null );
|
||||
break;
|
||||
case self::MODE_REAL_IP:
|
||||
$fromHeader = self::valid( $server['HTTP_X_REAL_IP'] ?? null );
|
||||
break;
|
||||
case self::MODE_FORWARDED:
|
||||
$raw = $server['HTTP_X_FORWARDED_FOR'] ?? null;
|
||||
if ( is_string( $raw ) && '' !== trim( $raw ) ) {
|
||||
$parts = array_map( 'trim', explode( ',', $raw ) );
|
||||
$fromHeader = self::valid( end( $parts ) ); // the entry our proxy appended; earlier ones are client-supplied
|
||||
}
|
||||
break;
|
||||
}
|
||||
return $fromHeader ?? $remote ?? 'unknown';
|
||||
}
|
||||
|
||||
private static function valid( mixed $v ): ?string {
|
||||
if ( ! is_string( $v ) ) {
|
||||
return null;
|
||||
}
|
||||
$v = trim( $v );
|
||||
return false !== filter_var( $v, FILTER_VALIDATE_IP ) ? $v : null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user