PluginProbe
Loginizer / 2.1.0
Loginizer v2.1.0
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / common.php

common.php in Loginizer 2.1.0, at common.php

165 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * NOTE:
5 * Functions in this file must NOT depend on any WordPress functions.
6 *
7 * Some of these functions are executed before WordPress is fully initialized.
8 * Adding WordPress-dependent logic here may result in fatal errors.
9 *
10 * When modifying or adding functions, always verify that
11 * country-blocking, brute force or any pre-init functionality remain unaffected.
12 */
13
14 if(!defined('ABSPATH') && !defined('LOGINIZER_FIREWALL')){
15 die('HACKING ATTEMPT!');
16 }
17
18 // Get the client IP
19 function _lz_getip(){
20 if(isset($_SERVER["REMOTE_ADDR"])){
21 return $_SERVER["REMOTE_ADDR"];
22 }elseif(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
23 return $_SERVER["HTTP_X_FORWARDED_FOR"];
24 }elseif(isset($_SERVER["HTTP_CLIENT_IP"])){
25 return $_SERVER["HTTP_CLIENT_IP"];
26 }
27 }
28
29 // Get the client IP
30 function lz_getip(){
31
32 global $loginizer;
33
34 // Just so that we have something
35 $ip = _lz_getip();
36
37 $loginizer['ip_method'] = (int) @$loginizer['ip_method'];
38
39 if(isset($_SERVER["REMOTE_ADDR"])){
40 $ip = $_SERVER["REMOTE_ADDR"];
41 }
42
43 if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && @$loginizer['ip_method'] == 1){
44 if(strpos($_SERVER["HTTP_X_FORWARDED_FOR"], ',')){
45 $temp_ip = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]);
46 $ip = trim($temp_ip[0]);
47 }else{
48 $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
49 }
50 }
51
52 if(isset($_SERVER["HTTP_CLIENT_IP"]) && @$loginizer['ip_method'] == 2){
53 $ip = $_SERVER["HTTP_CLIENT_IP"];
54 }
55
56 if(@$loginizer['ip_method'] == 3 && isset($_SERVER[@$loginizer['custom_ip_method']])){
57 $ip = $_SERVER[@$loginizer['custom_ip_method']];
58 }
59
60 // Hacking fix for X-Forwarded-For
61 if(!lz_valid_ip($ip)){
62 return '';
63 }
64
65 return $ip;
66
67 }
68
69 // Check if an IP is valid
70 function lz_valid_ip($ip){
71
72 if(empty($ip)){
73 return false;
74 }
75
76 // IPv6
77 if(lz_valid_ipv6($ip)){
78 return true;
79 }
80
81 // IPv4
82 if(!ip2long($ip) || !lz_valid_ipv4($ip)){
83 return false;
84 }
85
86 return true;
87 }
88
89 function lz_valid_ipv4($ip){
90 if(!preg_match('/^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}$/is', $ip) || substr_count($ip, '.') != 3){
91 return false;
92 }
93
94 $r = explode('.', $ip);
95
96 foreach($r as $v){
97 $v = (int) $v;
98 if($v > 255 || $v < 0){
99 return false;
100 }
101 }
102
103 return true;
104
105 }
106
107 function lz_valid_ipv6($ip){
108
109 $pattern = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/';
110
111 if(!preg_match($pattern, $ip)){
112 return false;
113 }
114
115 return true;
116
117 }
118
119 function loginizer_is_whitelisted(){
120
121 global $loginizer;
122
123 $whitelist = $loginizer['whitelist'];
124
125 if(empty($whitelist)){
126 return false;
127 }
128
129 $current_ip_inet = inet_ptoi($loginizer['current_ip']);
130
131 foreach($whitelist as $k => $v){
132 $start_inet = inet_ptoi($v['start']);
133 $end_inet = inet_ptoi($v['end']);
134
135 // Is the IP in the blacklist ?
136 if($start_inet <= $current_ip_inet && $current_ip_inet <= $end_inet){
137 $result = 1;
138 break;
139 }
140
141 // Is it in a wider range ?
142 if($start_inet >= 0 && $end_inet < 0){
143
144 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
145 // if the current IP is <= than the start of the range, it is within the range
146 // OR
147 // if the current IP is <= than the end of the range, it is within the range
148 if($start_inet <= $current_ip_inet
149 || $current_ip_inet <= $end_inet){
150 $result = 1;
151 break;
152 }
153
154 }
155
156 }
157
158 // You are whitelisted
159 if(!empty($result)){
160 return true;
161 }
162
163 return false;
164
165 }