| @@ -56,49 +56,24 @@ | ||
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * Collect IP from request. |
| 59 | 59 | * |
| 60 | - * Prefers REMOTE_ADDR since it cannot be spoofed by the client. When it is | |
| 61 | - * a private/reserved address (reverse proxy, Docker bridge gateway like | |
| 62 | - * 192.168.65.1, local dev), the forwarded headers are scanned for the first | |
| 63 | - * public IP. If nothing public is found, the request is local: 127.0.0.1. | |
| 64 | - * | |
| 65 | 60 | * @return string |
| 66 | 61 | */ |
| 67 | 62 | public static function get_ip() { |
| 68 | - $remote_addr = ! empty($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : ''; | |
| 69 | - | |
| 70 | - if (self::is_public_ip($remote_addr)) { | |
| 71 | - return $remote_addr; | |
| 63 | + $ip = '127.0.0.1'; // Local IP | |
| 64 | + if (! empty($_SERVER['HTTP_CLIENT_IP'])) { | |
| 65 | + $ip = $_SERVER['HTTP_CLIENT_IP']; | |
| 66 | + } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
| 67 | + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
| 68 | + } else { | |
| 69 | + $ip = ! empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $ip; | |
| 72 | 70 | } |
| 73 | 71 | |
| 74 | - foreach (['HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP'] as $header) { | |
| 75 | - if (empty($_SERVER[$header])) { | |
| 76 | - continue; | |
| 77 | - } | |
| 78 | - $candidates = explode(',', sanitize_text_field($_SERVER[$header])); | |
| 79 | - foreach ($candidates as $candidate) { | |
| 80 | - $candidate = trim($candidate); | |
| 81 | - if (self::is_public_ip($candidate)) { | |
| 82 | - return $candidate; | |
| 83 | - } | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - return '127.0.0.1'; | |
| 72 | + return sanitize_text_field($ip); | |
| 88 | 73 | } |
| 89 | 74 | |
| 90 | 75 | /** |
| 91 | - * Check whether a string is a valid public (non-private, non-reserved) IP. | |
| 92 | - * | |
| 93 | - * @param string $ip | |
| 94 | - * @return bool | |
| 95 | - */ | |
| 96 | - private static function is_public_ip($ip): bool { | |
| 97 | - return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | 76 | * Get views for front-end display |
| 102 | 77 | * |
| 103 | 78 | * @param string $name it will be file name only from the view's folder. |
| 104 | 79 | * @param array $data |
| @@ -158,15 +133,8 @@ | ||
| 158 | 133 | if (strtoupper($method) === 'POST') { |
| 159 | 134 | $headers['Content-Type'] = 'application/json'; |
| 160 | 135 | } |
| 161 | 136 | |
| 162 | - // Resolve requested platform: $_REQUEST wins (frontend-supplied), then caller's extra_headers, then default. | |
| 163 | - if ( isset( $_REQUEST['requested_platform'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 164 | - $extra_headers['x-templately-requested-platform'] = sanitize_text_field( wp_unslash( $_REQUEST['requested_platform'] ) ); | |
| 165 | - } elseif ( ! isset( $extra_headers['x-templately-requested-platform'] ) ) { | |
| 166 | - $extra_headers['x-templately-requested-platform'] = 'templately'; | |
| 167 | - } | |
| 168 | - | |
| 169 | 137 | // Merge additional headers |
| 170 | 138 | $headers = array_merge($headers, $extra_headers); |
| 171 | 139 | |
| 172 | 140 | $args = [ |
| @@ -251,46 +219,8 @@ | ||
| 251 | 219 | break; |
| 252 | 220 | } |
| 253 | 221 | |
| 254 | 222 | return $sanitized_value; |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Escape a string for safe embedding inside a GraphQL or JSON string literal. | |
| 259 | - * | |
| 260 | - * GraphQL string escaping rules are identical to JSON string escaping (per the | |
| 261 | - * GraphQL spec), so wp_json_encode() is the authoritative escaper. We strip the | |
| 262 | - * outer quotes it adds and return only the escaped inner content, ready to be | |
| 263 | - * wrapped in your own quote pair. | |
| 264 | - * | |
| 265 | - * Handles pre-encoded JSON: when the caller has already run json_encode() + | |
| 266 | - * wp_slash() on a value (e.g. categories, dependencies in Items.php), the | |
| 267 | - * quotes are already escaped as \" and the string is ready to embed. Calling | |
| 268 | - * wp_json_encode() again would double-escape those backslashes. We detect this | |
| 269 | - * case by checking whether wp_unslash() produces valid JSON, and if so, return | |
| 270 | - * the value directly without further encoding. | |
| 271 | - * | |
| 272 | - * @param string $value Raw string or wp_slash(json_encode()) output. | |
| 273 | - * @return string Escaped string, safe to place between double quotes in GraphQL/JSON. | |
| 274 | - */ | |
| 275 | - public static function esc_json_string( $value ) { | |
| 276 | - $value = (string) $value; | |
| 277 | - | |
| 278 | - // If wp_slash() was applied to a JSON string upstream, the quotes are | |
| 279 | - // already escaped (e.g. {\"key\":\"val\"}). Detect this by unslashing and | |
| 280 | - // checking for valid JSON — if it matches, the value is already suitable | |
| 281 | - // for embedding in a string literal; return it as-is to avoid doubling backslashes. | |
| 282 | - $unslashed = wp_unslash( $value ); | |
| 283 | - if ( $unslashed !== $value ) { | |
| 284 | - $decoded = json_decode( $unslashed, true ); | |
| 285 | - if ( json_last_error() === JSON_ERROR_NONE && null !== $decoded ) { | |
| 286 | - return $value; | |
| 287 | - } | |
| 288 | - } | |
| 289 | - | |
| 290 | - $encoded = wp_json_encode( $value ); | |
| 291 | - // wp_json_encode wraps the value in "...", strip those outer quotes. | |
| 292 | - return substr( $encoded, 1, -1 ); | |
| 293 | 223 | } |
| 294 | 224 | |
| 295 | 225 | /** |
| 296 | 226 | * Check for X-Templately-Verified header and update user verification status |