-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
501 lines (428 loc) · 13.2 KB
/
Client.php
File metadata and controls
501 lines (428 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/**
* ProxyCheck.io Client Class
*
* @package ArrayPress\ProxyCheck
* @copyright Copyright (c) 2024, ArrayPress Limited
* @license GPL2+
* @version 1.0.0
* @author David Sherlock
*/
declare( strict_types=1 );
namespace ArrayPress\ProxyCheck;
use ArrayPress\ProxyCheck\Traits\Parameters;
use ArrayPress\ProxyCheck\Traits\Dashboard;
use ArrayPress\ProxyCheck\Response\Client\IP;
use ArrayPress\ProxyCheck\Response\Client\DisposableEmail;
use Exception;
use WP_Error;
class Client {
use Parameters;
use Dashboard;
/**
* Base URL for the ProxyCheck API endpoints
*
* @var string
*/
private const API_BASE = 'https://proxycheck.io/v2/';
/**
* Maximum number of IPs per batch request for free users
*
* @var int
*/
private const BATCH_MAX_SIZE = 100;
/**
* Maximum number of IPs per batch request for registered users
*
* @var int
*/
private const BATCH_MAX_SIZE_REGISTERED = 1000;
/**
* Initialize the ProxyCheck client
*
* @param string $api_key The API key for ProxyCheck.io service
* @param bool $enable_cache Whether to enable response caching
* @param int $cache_expiration Cache expiration time in seconds
* @param string $cache_prefix Prefix for cache keys
*/
public function __construct(
string $api_key = '',
bool $enable_cache = true,
int $cache_expiration = 600,
string $cache_prefix = 'proxycheck_'
) {
$this->set_api_key( $api_key );
$this->set_cache_enabled( $enable_cache );
$this->set_cache_expiration( $cache_expiration );
$this->set_cache_prefix( $cache_prefix );
}
/**
* Process email address for privacy
*
* @param string $email Email address to process
* @param bool $mask Whether to mask the email
*
* @return string Processed email address
*/
private function process_email( string $email, bool $mask = false ): string {
if ( $mask && strpos( $email, '@' ) !== false ) {
return 'anonymous@' . explode( '@', $email )[1];
}
return $email;
}
/**
* Build query parameters from options
*
* @param array $options Additional options to override current params
*
* @return array
*/
private function build_query_params( array $options = [] ): array {
return array_merge( $this->get_parameters(), $options );
}
/**
* Make a GET request to the ProxyCheck API
*
* @param string $endpoint API endpoint
* @param array $params Query parameters
* @param array $args Additional request arguments
*
* @return array|WP_Error Response array or WP_Error on failure
*/
private function make_get_request( string $endpoint, array $params = [], array $args = [] ) {
if ( ! empty( $this->get_api_key() ) ) {
$params['key'] = $this->get_api_key();
}
$url = self::API_BASE . $endpoint;
if ( ! empty( $params ) ) {
$url .= '?' . http_build_query( $params );
}
$default_args = [
'timeout' => 15,
'headers' => [
'Accept' => 'application/json',
],
];
$args = wp_parse_args( $args, $default_args );
$response = wp_remote_get( $url, $args );
return $this->handle_response( $response );
}
/**
* Make a POST request to the ProxyCheck API
*
* @param string $endpoint API endpoint
* @param array $params Query parameters
* @param array $data POST data
* @param array $args Additional request arguments
*
* @return array|WP_Error Response array or WP_Error on failure
*/
private function make_post_request( string $endpoint, array $params = [], array $data = [], array $args = [] ) {
if ( ! empty( $this->get_api_key() ) ) {
$params['key'] = $this->get_api_key();
}
$url = self::API_BASE . $endpoint;
if ( ! empty( $params ) ) {
$url .= '?' . http_build_query( $params );
}
$default_args = [
'timeout' => 15,
'method' => 'POST',
'body' => $data,
'headers' => [
'Accept' => 'application/json',
],
];
$args = wp_parse_args( $args, $default_args );
$response = wp_remote_post( $url, $args );
return $this->handle_response( $response );
}
/**
* Handle API response
*
* @param array|WP_Error $response API response
*
* @return array|WP_Error Processed response or WP_Error
*/
private function handle_response( $response ) {
if ( is_wp_error( $response ) ) {
return new WP_Error(
'api_error',
sprintf(
__( 'ProxyCheck API request failed: %s', 'arraypress' ),
$response->get_error_message()
)
);
}
$status_code = wp_remote_retrieve_response_code( $response );
if ( $status_code < 200 || $status_code >= 300 ) {
return new WP_Error(
'http_error',
sprintf(
__( 'HTTP request failed with status code: %d', 'arraypress' ),
$status_code
)
);
}
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
return new WP_Error(
'json_error',
__( 'Failed to parse ProxyCheck API response', 'arraypress' )
);
}
if ( isset( $data['status'] ) && $data['status'] === 'error' ) {
return new WP_Error(
'api_error',
$data['message'] ?? __( 'Unknown API error', 'arraypress' )
);
}
return $data;
}
/**
* Check a single IP address
*
* @param string $ip IP address to check
* @param array $options Additional options for the check
*
* @return IP|WP_Error Response object or WP_Error on failure
*/
public function check_ip( string $ip, array $options = [] ) {
if ( ! $this->is_valid_ip( $ip ) ) {
return new WP_Error(
'invalid_ip',
sprintf( __( 'Invalid IP address: %s', 'arraypress' ), $ip )
);
}
$cache_key = $this->get_cache_key( $ip, $options );
if ( $this->is_cache_enabled() ) {
$cached_data = get_transient( $cache_key );
if ( false !== $cached_data ) {
return new IP( $cached_data );
}
}
$params = $this->build_query_params( $options );
$response = $this->make_get_request( $ip, $params );
if ( is_wp_error( $response ) ) {
return $response;
}
// Add block status and apply country rules
$response = $this->add_block_status( $response, $ip );
if ( $this->is_cache_enabled() ) {
set_transient( $cache_key, $response, $this->get_cache_expiration() );
}
return new IP( $response );
}
/**
* Check multiple IP addresses in a batch
*
* @param array $ips Array of IP addresses to check
* @param array $options Additional options for the check
*
* @return array|WP_Error Array of Response objects or WP_Error on failure
*/
public function check_ips( array $ips, array $options = [] ) {
$valid_ips = array_filter( $ips, [ $this, 'is_valid_ip' ] );
if ( empty( $valid_ips ) ) {
return new WP_Error(
'invalid_ips',
__( 'No valid IPs provided for check', 'arraypress' )
);
}
$batch_size = empty( $this->get_api_key() ) ? self::BATCH_MAX_SIZE : self::BATCH_MAX_SIZE_REGISTERED;
$batches = array_chunk( $valid_ips, $batch_size );
$results = [];
foreach ( $batches as $batch ) {
$params = $this->build_query_params( $options );
$data = [ 'ips' => implode( ',', $batch ) ];
$response = $this->make_post_request( '', $params, $data );
if ( is_wp_error( $response ) ) {
return $response;
}
foreach ( $batch as $ip ) {
if ( isset( $response[ $ip ] ) ) {
try {
$single_response = [
'status' => $response['status'] ?? 'ok',
'node' => $response['node'] ?? null,
'query time' => $response['query time'] ?? null,
$ip => $response[ $ip ]
];
// Add block status for each IP
$single_response = $this->add_block_status( $single_response, $ip );
$results[ $ip ] = new IP( $single_response );
} catch ( Exception $e ) {
return new WP_Error(
'response_error',
sprintf( __( 'Error processing response for IP %s: %s', 'arraypress' ),
$ip,
$e->getMessage() )
);
}
}
}
}
return $results;
}
/**
* Add block status to response
*
* @param array $response API response
* @param string $address IP or email being checked
*
* @return array Modified response
*/
private function add_block_status( array $response, string $address ): array {
// Handle email disposable check
if ( strpos( $address, '@' ) !== false && isset( $response[ $address ]['disposable'] ) ) {
$response['block'] = $response[ $address ]['disposable'] === 'yes' ? 'yes' : 'no';
$response['block_reason'] = $response[ $address ]['disposable'] === 'yes' ? 'disposable' : 'na';
return $response;
}
// Initialize block status
$response['block'] = 'no';
$response['block_reason'] = 'na';
$ip_data = $response[ $address ];
// Check for proxy/VPN status with operator details
if ( isset( $ip_data['proxy'] ) && $ip_data['proxy'] === 'yes' ) {
$response['block'] = 'yes';
$response['block_reason'] = isset( $ip_data['type'] ) &&
$ip_data['type'] === 'VPN' ? 'vpn' : 'proxy';
// Add operator details if available
if ( isset( $ip_data['operator'] ) ) {
$response['block_details'] = [
'name' => $ip_data['operator']['name'] ?? '',
'anonymity' => $ip_data['operator']['anonymity'] ?? '',
'popularity' => $ip_data['operator']['popularity'] ?? ''
];
}
}
// Check risk score
if ( isset( $ip_data['risk'] ) && $ip_data['risk'] > 70 ) {
$response['block'] = 'yes';
// Only update block reason if not already blocked for proxy/VPN
if ( $response['block_reason'] === 'na' ) {
$response['block_reason'] = 'high_risk';
}
}
// Apply country rules
return $this->apply_country_rules( $response, $address );
}
/**
* Apply country blocking rules to response
*
* @param array $response API response
* @param string $address IP address being checked
*
* @return array Modified response
*/
private function apply_country_rules( array $response, string $address ): array {
// Skip if no country info available
if ( ! isset( $response[ $address ]['country'] ) ) {
$response['block'] = 'na';
$response['block_reason'] = 'na';
return $response;
}
// Get country info
$country = strtoupper( $response[ $address ]['country'] );
$isocode = strtoupper( $response[ $address ]['isocode'] ?? '' );
// First check if country is blocked
if ( $response['block'] === 'no' && ! empty( $this->get_blocked_countries() ) ) {
if ( in_array( $country, $this->get_blocked_countries() ) ||
in_array( $isocode, $this->get_blocked_countries() ) ) {
$response['block'] = 'yes';
$response['block_reason'] = 'country';
}
}
// Then check if country is explicitly allowed
if ( $response['block'] === 'yes' && ! empty( $this->get_allowed_countries() ) ) {
if ( in_array( $country, $this->get_allowed_countries() ) ||
in_array( $isocode, $this->get_allowed_countries() ) ) {
$response['block'] = 'no';
$response['block_reason'] = 'na';
}
}
return $response;
}
/**
* Check if an email is disposable
*
* @param string $email Email address to check
* @param bool $mask Whether to mask the email address
*
* @return DisposableEmail|WP_Error Response object or WP_Error on failure
*/
public function check_email( string $email, bool $mask = false ) {
if ( ! is_email( $email ) ) {
return new WP_Error(
'invalid_email',
sprintf( __( 'Invalid email address: %s', 'arraypress' ), $email )
);
}
// Process email masking
$processed_email = $this->process_email( $email, $mask );
$cache_key = $this->get_cache_key( $processed_email );
if ( $this->is_cache_enabled() ) {
$cached_data = get_transient( $cache_key );
if ( false !== $cached_data ) {
return new DisposableEmail( $cached_data );
}
}
$response = $this->make_get_request( $processed_email );
if ( is_wp_error( $response ) ) {
return $response;
}
// Add block status for email check
$response = $this->add_block_status( $response, $processed_email );
if ( $this->is_cache_enabled() ) {
set_transient( $cache_key, $response, $this->get_cache_expiration() );
}
return new DisposableEmail( $response );
}
/**
* Generate cache key
*
* @param string $value Value to generate key for
* @param array $params Additional parameters to include in key
*
* @return string Generated cache key
*/
private function get_cache_key( string $value, array $params = [] ): string {
$key_parts = [ $value ];
if ( ! empty( $params ) ) {
$key_parts[] = md5( serialize( $params ) );
}
return $this->get_cache_prefix() . md5( implode( '_', $key_parts ) );
}
/**
* Clear cached data
*
* @param string|null $value Optional specific value to clear cache for
*
* @return bool True on success, false on failure
*/
public function clear_cache( ?string $value = null ): bool {
if ( $value !== null ) {
return delete_transient( $this->get_cache_key( $value ) );
}
global $wpdb;
$pattern = $wpdb->esc_like( '_transient_' . $this->get_cache_prefix() ) . '%';
return $wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
$pattern
)
) !== false;
}
/**
* Validate an IP address
*
* @param string $ip IP address to validate
*
* @return bool True if valid, false otherwise
*/
private function is_valid_ip( string $ip ): bool {
return filter_var( $ip, FILTER_VALIDATE_IP ) !== false;
}
}