siteguard-admin-filter.php
5 years ago
siteguard-base.php
5 years ago
siteguard-captcha.php
8 years ago
siteguard-config.php
9 years ago
siteguard-disable-author-query.php
5 years ago
siteguard-disable-pingback.php
9 years ago
siteguard-disable-xmlrpc.php
5 years ago
siteguard-htaccess.php
5 years ago
siteguard-login-alert.php
8 years ago
siteguard-login-history.php
8 years ago
siteguard-login-lock.php
8 years ago
siteguard-rename-login.php
6 years ago
siteguard-updates-notify.php
8 years ago
siteguard-waf-exclude-rule.php
5 years ago
siteguard-base.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | function siteguard_error_log( $message ) { |
| 4 | $logfile = SITEGUARD_PATH . 'error.log'; |
| 5 | $f = @fopen( $logfile, 'a+' ); |
| 6 | if ( false != $f ) { |
| 7 | fwrite( $f, date_i18n( 'Y/m/d H:i:s:' ) . $message . "\n" ); |
| 8 | fclose( $f ); |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | function siteguard_error_dump( $title, $obj ) { |
| 13 | ob_start(); |
| 14 | var_dump( $obj ); |
| 15 | $msg = ob_get_contents( ); |
| 16 | ob_end_clean( ); |
| 17 | siteguard_error_log( "$title: $msg" ); |
| 18 | } |
| 19 | |
| 20 | function siteguard_check_multisite( ) { |
| 21 | if ( ! is_multisite() ) { |
| 22 | return true; |
| 23 | } |
| 24 | $message = esc_html__( 'It does not support the multisite function of WordPress.', 'siteguard' ); |
| 25 | $error = new WP_Error( 'siteguard', $message ); |
| 26 | return $error; |
| 27 | } |
| 28 | |
| 29 | class SiteGuard_Base { |
| 30 | public static $ip_mode_items = array( '0', '1', '2', '3' ); |
| 31 | function __construct() { |
| 32 | } |
| 33 | function is_switch_value( $value ) { |
| 34 | if ( '0' === $value || '1' === $value ) { |
| 35 | return true; |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | function cvt_camma2ret( $value ) { |
| 40 | $result = str_replace( ' ', '', $value ); |
| 41 | return str_replace( ',', "\r\n", $result ); |
| 42 | } |
| 43 | function cvt_ret2camma( $exclude ) { |
| 44 | $result = str_replace( ' ', '', $exclude ); |
| 45 | $result = str_replace( ',', '', $result ); |
| 46 | $result = preg_replace( '/(\r\n){2,}/', "\r\n", $result ); |
| 47 | $result = preg_replace( '/\r\n$/', '', $result ); |
| 48 | $result = str_replace( "\r\n", ',', $result ); |
| 49 | $result = str_replace( "\r", ',', $result ); |
| 50 | return str_replace( "\n", ',', $result ); |
| 51 | } |
| 52 | function check_module( $name, $default = false ) { |
| 53 | return true; |
| 54 | # It does not work WP-CLI |
| 55 | #if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) { |
| 56 | # return ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false); |
| 57 | #} else { |
| 58 | # return $default; |
| 59 | #} |
| 60 | |
| 61 | # It does not work in FastCGI well. |
| 62 | #$module = 'mod_' . $name; |
| 63 | #return apache_mod_loaded( $module, $default ); |
| 64 | #if ( function_exists('phpinfo') ) { |
| 65 | # ob_start( ); |
| 66 | # phpinfo(8); |
| 67 | # $phpinfo = ob_get_clean( ); |
| 68 | # if ( false !== strpos( $phpinfo, $module ) ) { |
| 69 | # return true; |
| 70 | # } |
| 71 | #} |
| 72 | #return $default; |
| 73 | } |
| 74 | function is_private_ip( $ip ) { |
| 75 | $private_ips = array( |
| 76 | '10.0.0.0,10.255.255.255', |
| 77 | '172.16.0.0,172.31.255.255', |
| 78 | '192.168.0.0,192.168.255.255' |
| 79 | ); |
| 80 | |
| 81 | $long_ip = ip2long( $ip ); |
| 82 | if ( -1 !== $long_ip && false !== $long_ip ) { |
| 83 | $long_ip = sprintf( '%u', $long_ip ); |
| 84 | foreach( $private_ips as $private_ip ) { |
| 85 | list( $start, $end ) = explode( ',', $private_ip ); |
| 86 | $long_start = ip2long( $start ); |
| 87 | $long_start = sprintf( '%u', $long_start ); |
| 88 | $long_end = ip2long( $end ); |
| 89 | $long_end = sprintf( '%u', $long_end ); |
| 90 | if ( $long_ip >= $long_start && $long_ip <= $long_end ) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | function get_server_ip( ) { |
| 98 | if ( isset( $_SERVER['SERVER_ADDR'] ) ) { |
| 99 | $ip = $_SERVER['SERVER_ADDR']; |
| 100 | if ( false === $this->is_private_ip( $ip ) ) { |
| 101 | if ( preg_match( '/[0-9.:]+/', $ip ) ) { |
| 102 | return $ip; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $url = 'http://inet-ip.info/ip'; |
| 108 | $options = array( |
| 109 | 'http' => array( |
| 110 | 'method' => 'GET', |
| 111 | 'timeout' => 2, |
| 112 | ) |
| 113 | ); |
| 114 | $ip = file_get_contents( $url, false, stream_context_create( $options ) ); |
| 115 | if ( false !== $ip ) { |
| 116 | if ( preg_match( '/[0-9.:]+/', $ip ) ) { |
| 117 | return $ip; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $host = parse_url( home_url( ), PHP_URL_HOST ); |
| 122 | if ( false !== $host && null !== $host ) { |
| 123 | putenv( 'RES_OPTIONS=retrans:1 retry:1 timeout:2 attempts:1' ); |
| 124 | $ip = gethostbyname( $host ); |
| 125 | if ( $ip !== $host ) { |
| 126 | if ( '127.0.0.1' !== $ip && '::1' !== $ip ) { |
| 127 | if ( preg_match( '/[0-9.:]+/', $ip ) ) { |
| 128 | return $ip; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | function get_ip( ) { |
| 136 | global $siteguard_config; |
| 137 | $ip_mode = $siteguard_config->get( 'ip_mode' ); |
| 138 | if ( ! in_array( $ip_mode, SiteGuard_Base::$ip_mode_items ) ) { |
| 139 | $ip_mode = '0'; |
| 140 | $siteguard_config->set( 'ip_mode', $ip_mode ); |
| 141 | $siteguard_config->update( ); |
| 142 | } |
| 143 | $ip_mode_num = intval( $ip_mode ); |
| 144 | $remote_addr = '127.0.0.1'; |
| 145 | if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 146 | $remote_addr = $_SERVER['REMOTE_ADDR']; |
| 147 | } |
| 148 | if ( '0' === $ip_mode ) { |
| 149 | return $remote_addr; |
| 150 | } |
| 151 | if ( ! isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 152 | return $remote_addr; |
| 153 | } |
| 154 | $xff = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 155 | if ( empty( $xff ) ) { |
| 156 | return $remote_addr; |
| 157 | } |
| 158 | $ips = explode( ',', $xff ); |
| 159 | $count = count( $ips ); |
| 160 | $idx = $count - $ip_mode_num; |
| 161 | if ( $idx < 0 ) { |
| 162 | return $remote_addr; |
| 163 | } |
| 164 | $ip = $ips[ $idx ]; |
| 165 | if ( ! filter_var($ip, FILTER_VALIDATE_IP ) ) { |
| 166 | return $remote_addr; |
| 167 | } |
| 168 | return $ip; |
| 169 | } |
| 170 | } |
| 171 |