-
Notifications
You must be signed in to change notification settings - Fork 14
/
class-get-public-ip.php
78 lines (60 loc) · 1.75 KB
/
class-get-public-ip.php
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
<?php
/**
* Work around local DNS by asking public web sites to look up the IP of a domain
*/
class UBP_Get_Public_IP {
var $domain;
var $transient;
var $expire = 86400;
var $ip;
var $ip_pattern = '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/';
function __construct( $domain ) {
$this->domain = $domain;
$this->transient = 'dbp_'.$domain;
// Filter to set remote IP programmatically
$ip = apply_filters( 'ubp_remote_ip', false );
if ( empty( $ip ) ) {
$ip = get_transient( $this->transient );
}
if ( empty( $ip ) ) {
// Loading and serving from localhost
$custom_url = apply_filters('ubp_ip_url', false, $this->domain );
$custom_args = apply_filters('ubp_ip_args', array() );
if ( $custom_url ) $ip = $this->get_ip( $custom_url, $custom_args );
if ( !$ip ) $ip = $this->get_ip( "http://aruljohn.com/cgi-bin/hostname2ip.pl?host=$domain", array( 'referer'=>'http://aruljohn.com/hostname2ip.html' ) );
if ( $ip ) {
set_transient( $this->transient, $ip, $this->expire );
$this->ip = $ip;
}
}
if ( empty($ip) ){ $ip = $domain; }
$this->ip = $ip;
}
function __toString() {
return $this->ip;
}
public function get_ip( $url, $args=array() ) {
$defaults = array(
'method' => 'GET',
'referer'=> $domain,
'body' => '',
'index' => 0,
);
$args = wp_parse_args($args, $defaults);
extract( $args );
$query_args = array(
'method' => $method,
'headers' => array('Referer'=>$referer),
'body' => $body,
);
$response = wp_remote_get( $url, $query_args );
if ( ! is_wp_error( $response ) ) {
$body = strip_tags($response['body']);
preg_match_all( $this->ip_pattern, $body, $matches );
return !empty( $matches[0][$index] )
? $matches[0][$index]
: false;
}
return false;
}
}