PluginProbe ʕ •ᴥ•ʔ
Wordfence Security – Firewall, Malware Scan, and Login Security / 5.2.7
Wordfence Security – Firewall, Malware Scan, and Login Security v5.2.7
8.2.2 8.2.1 8.2.0 3.7.1 3.7.2 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.9.1 4.0.1 4.0.2 4.0.3 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.1 5.1.2 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.1 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.1 5.3.10 5.3.11 5.3.12 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.3.8 5.3.9 6.0.1 6.0.10 6.0.11 6.0.12 6.0.14 6.0.15 6.0.16 6.0.17 6.0.18 6.0.19 6.0.2 6.0.20 6.0.21 6.0.22 6.0.23 6.0.24 6.0.25 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.1.1 6.1.10 6.1.11 6.1.12 6.1.14 6.1.15 6.1.16 6.1.17 6.1.2 6.1.3 6.1.4 6.1.5 6.1.6 6.1.7 6.1.8 6.1.9 6.2.0 6.2.1 6.2.10 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.7 6.2.8 6.2.9 6.3.0 6.3.1 6.3.10 6.3.11 6.3.12 6.3.14 6.3.15 6.3.16 6.3.17 6.3.18 6.3.19 6.3.2 6.3.20 6.3.21 6.3.22 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1.0 7.1.1 7.1.10 7.1.11 7.1.12 7.1.14 7.1.15 7.1.16 7.1.17 7.1.18 7.1.19 7.1.2 7.1.20 7.1.3 7.1.4 7.1.5 7.1.6 7.1.7 7.1.8 7.1.9 7.10.0 7.10.1 7.10.2 7.10.3 7.10.4 7.10.5 7.10.6 7.10.7 7.11.0 7.11.1 7.11.2 7.11.3 7.11.4 7.11.5 7.11.6 7.11.7 7.2.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.4.0 7.4.1 7.4.10 7.4.11 7.4.12 7.4.14 7.4.2 7.4.3 trunk 7.4.4 1.1 7.4.5 1.2 7.4.6 1.3 7.4.7 1.3.1 7.4.8 1.3.2 7.4.9 1.3.3 7.5.0 1.4.2 7.5.1 1.4.3 7.5.10 1.4.4 7.5.11 1.4.5 7.5.2 1.4.6 7.5.3 1.4.7 7.5.4 1.4.8 7.5.5 1.5.1 7.5.6 1.5.2 7.5.7 1.5.3 7.5.8 1.5.4 7.5.9 1.5.5 7.6.0 1.5.6 7.6.1 2.0.1 7.6.2 2.0.2 7.7.0 2.0.3 7.7.1 2.0.5 7.8.0 2.0.6 7.8.1 2.0.7 7.8.2 2.1.0 7.9.0 2.1.1 7.9.1 2.1.2 7.9.2 2.1.3 7.9.3 2.1.4 8.0.0 2.1.5 8.0.1 3.0.2 8.0.2 3.0.3 8.0.3 3.0.4 8.0.4 3.0.5 8.0.5 3.0.6 8.1.0 3.0.7 8.1.1 3.0.8 8.1.2 3.0.9 8.1.3 3.1.0 8.1.4 3.1.1 v1.4.1 3.1.2 3.1.4 3.1.6 3.2.1 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.1 3.4.4 3.4.5 3.5.1 3.5.2 3.6.1 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
wordfence / lib / wfUtils.php
wordfence / lib Last commit date
Diff 14 years ago whois 12 years ago .htaccess 14 years ago Diff.php 14 years ago GeoIP.dat 11 years ago IPTraf.php 11 years ago conntest.php 11 years ago dashboard.php 11 years ago diffResult.php 11 years ago email_genericAlert.php 11 years ago email_newIssues.php 11 years ago email_unlockRequest.php 11 years ago menuHeader.php 11 years ago menu_activity.php 11 years ago menu_blockedIPs.php 11 years ago menu_countryBlocking.php 11 years ago menu_options.php 11 years ago menu_rangeBlocking.php 11 years ago menu_scan.php 11 years ago menu_scanSchedule.php 11 years ago menu_sitePerf.php 11 years ago menu_sitePerfStats.php 11 years ago menu_twoFactor.php 11 years ago menu_whois.php 11 years ago pageTitle.php 13 years ago schedWeekEntry.php 12 years ago sysinfo.php 11 years ago unknownFiles.php 11 years ago viewFullActivityLog.php 11 years ago wf503.php 12 years ago wfAPI.php 11 years ago wfAction.php 14 years ago wfArray.php 13 years ago wfBrowscap.php 11 years ago wfBrowscapCache.php 11 years ago wfBulkCountries.php 13 years ago wfCache.php 11 years ago wfConfig.php 11 years ago wfCountryMap.php 13 years ago wfCrawl.php 11 years ago wfDB.php 11 years ago wfDict.php 14 years ago wfGeoIP.php 13 years ago wfIssues.php 11 years ago wfLockedOut.php 13 years ago wfLog.php 11 years ago wfRate.php 14 years ago wfScan.php 11 years ago wfScanEngine.php 11 years ago wfSchema.php 11 years ago wfUnlockMsg.php 11 years ago wfUtils.php 11 years ago wfViewResult.php 11 years ago wordfenceClass.php 11 years ago wordfenceConstants.php 11 years ago wordfenceHash.php 11 years ago wordfenceScanner.php 11 years ago wordfenceURLHoover.php 11 years ago
wfUtils.php
651 lines
1 <?php
2 require_once('wfConfig.php');
3 require_once('wfCountryMap.php');
4 class wfUtils {
5 private static $privateAddrs = array(
6 array('0.0.0.0/8',0,16777215),
7 array('10.0.0.0/8',167772160,184549375),
8 array('100.64.0.0/10',1681915904,1686110207),
9 array('127.0.0.0/8',2130706432,2147483647),
10 array('169.254.0.0/16',2851995648,2852061183),
11 array('172.16.0.0/12',2886729728,2887778303),
12 array('192.0.0.0/29',3221225472,3221225479),
13 array('192.0.2.0/24',3221225984,3221226239),
14 array('192.88.99.0/24',3227017984,3227018239),
15 array('192.168.0.0/16',3232235520,3232301055),
16 array('198.18.0.0/15',3323068416,3323199487),
17 array('198.51.100.0/24',3325256704,3325256959),
18 array('203.0.113.0/24',3405803776,3405804031),
19 array('224.0.0.0/4',3758096384,4026531839),
20 array('240.0.0.0/4',4026531840,4294967295),
21 array('255.255.255.255/32',4294967295,4294967295)
22 );
23 private static $isWindows = false;
24 public static $scanLockFH = false;
25 private static $lastErrorReporting = false;
26 private static $lastDisplayErrors = false;
27 public static function makeTimeAgo($secs, $noSeconds = false) {
28 if($secs < 1){
29 return "a moment";
30 }
31 $months = floor($secs / (86400 * 30));
32 $days = floor($secs / 86400);
33 $hours = floor($secs / 3600);
34 $minutes = floor($secs / 60);
35 if($months) {
36 $days -= $months * 30;
37 return self::pluralize($months, 'month', $days, 'day');
38 } else if($days) {
39 $hours -= $days * 24;
40 return self::pluralize($days, 'day', $hours, 'hour');
41 } else if($hours) {
42 $minutes -= $hours * 60;
43 return self::pluralize($hours, 'hour', $minutes, 'min');
44 } else if($minutes) {
45 $secs -= $minutes * 60;
46 return self::pluralize($minutes, 'min');
47 } else {
48 if($noSeconds){
49 return "less than a minute";
50 } else {
51 return floor($secs) . " secs";
52 }
53 }
54 }
55 public static function pluralize($m1, $t1, $m2 = false, $t2 = false) {
56 if($m1 != 1) {
57 $t1 = $t1 . 's';
58 }
59 if($m2 != 1) {
60 $t2 = $t2 . 's';
61 }
62 if($m1 && $m2){
63 return "$m1 $t1 $m2 $t2";
64 } else {
65 return "$m1 $t1";
66 }
67 }
68 public static function formatBytes($bytes, $precision = 2) {
69 $units = array('B', 'KB', 'MB', 'GB', 'TB');
70
71 $bytes = max($bytes, 0);
72 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
73 $pow = min($pow, count($units) - 1);
74
75 // Uncomment one of the following alternatives
76 $bytes /= pow(1024, $pow);
77 // $bytes /= (1 << (10 * $pow));
78
79 return round($bytes, $precision) . ' ' . $units[$pow];
80 }
81 public static function inet_ntoa($ip){
82 $long = 4294967295 - ($ip - 1);
83 return long2ip(-$long);
84 }
85 public static function inet_aton($ip){
86 $ip = preg_replace('/(?<=^|\.)0+([1-9])/', '$1', $ip);
87 return sprintf("%u", ip2long($ip));
88 }
89 public static function hasLoginCookie(){
90 if(isset($_COOKIE)){
91 if(is_array($_COOKIE)){
92 foreach($_COOKIE as $key => $val){
93 if(strpos($key, 'wordpress_logged_in') == 0){
94 return true;
95 }
96 }
97 }
98 }
99 return false;
100 }
101 public static function getBaseURL(){
102 return plugins_url() . '/wordfence/';
103 }
104 public static function getPluginBaseDir(){
105 return WP_CONTENT_DIR . '/plugins/';
106 //return ABSPATH . 'wp-content/plugins/';
107 }
108 public static function defaultGetIP(){
109 return $IP;
110 }
111 public static function makeRandomIP(){
112 return rand(11,230) . '.' . rand(0,255) . '.' . rand(0,255) . '.' . rand(0,255);
113 }
114 public static function isPrivateAddress($addr){
115 $num = self::inet_aton($addr);
116 foreach(self::$privateAddrs as $a){
117 if($num >= $a[1] && $num <= $a[2]){
118 return true;
119 }
120 }
121 return false;
122 }
123 private static function getCleanIP($arr){ //Expects an array of items. The items are either IP's or IP's separated by comma, space or tab. Or an array of IP's.
124 // We then examine all IP's looking for a public IP and storing private IP's in an array. If we find no public IPs we return the first private addr we found.
125 $privates = array(); //Store private addrs until end as last resort.
126 for($i = 0; $i < count($arr); $i++){
127 $item = $arr[$i];
128 if(is_array($item)){
129 foreach($item as $j){
130 $j = preg_replace('/:\d+$/', '', $j); //Strip off port
131 if(self::isValidIP($j)){
132 if(self::isPrivateAddress($j)){
133 $privates[] = $j;
134 } else {
135 return $j;
136 }
137 }
138 }
139 continue; //This was an array so we can skip to the next item
140 }
141 $skipToNext = false;
142 foreach(array(',', ' ', "\t") as $char){
143 if(strpos($item, $char) !== false){
144 $sp = explode($char, $item);
145 foreach($sp as $j){
146 $j = preg_replace('/:\d+$/', '', $j); //Strip off port
147 if(self::isValidIP($j)){
148 if(self::isPrivateAddress($j)){
149 $privates[] = $j;
150 } else {
151 return $j;
152 }
153 }
154 }
155 $skipToNext = true;
156 break;
157 }
158 }
159 if($skipToNext){ continue; } //Skip to next item because this one had a comma, space or tab so was delimited and we didn't find anything.
160
161 $item = preg_replace('/:\d+$/', '', $item); //Strip off port
162 if(self::isValidIP($item)){
163 if(self::isPrivateAddress($item)){
164 $privates[] = $item;
165 } else {
166 return $item;
167 }
168 }
169 }
170 if(sizeof($privates) > 0){
171 return $privates[0]; //Return the first private we found so that we respect the order the IP's were passed to this function.
172 } else {
173 return false;
174 }
175 }
176 public static function extractHostname($str){
177 if(preg_match('/https?:\/\/([a-zA-Z0-9\.\-]+)(?:\/|$)/i', $str, $matches)){
178 return strtolower($matches[1]);
179 } else {
180 return false;
181 }
182 }
183 public static function getIP(){
184 //For debugging.
185 //return '54.232.205.132';
186 //return self::makeRandomIP();
187 $howGet = wfConfig::get('howGetIPs', false);
188 if($howGet){
189 if($howGet == 'REMOTE_ADDR'){
190 $IP = self::getCleanIP(array($_SERVER['REMOTE_ADDR']));
191 } else {
192 $IP = self::getCleanIP(array($_SERVER[$howGet], $_SERVER['REMOTE_ADDR']));
193 }
194 } else {
195 $IPs = array($_SERVER['REMOTE_ADDR']);
196 if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $IPs[] = $_SERVER['HTTP_X_FORWARDED_FOR']; }
197 if(isset($_SERVER['HTTP_X_REAL_IP'])){ $IPs[] = $_SERVER['HTTP_X_REAL_IP']; }
198 $IP = self::getCleanIP($IPs);
199 }
200 return $IP; //Returns a valid IP or false.
201 }
202 public static function isValidIP($IP){
203 if(preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $IP, $m)){
204 if(
205 $m[1] >= 0 && $m[1] <= 255 &&
206 $m[2] >= 0 && $m[2] <= 255 &&
207 $m[3] >= 0 && $m[3] <= 255 &&
208 $m[4] >= 0 && $m[4] <= 255
209 ){
210 return true;
211 }
212 }
213 return false;
214 }
215 public static function getRequestedURL(){
216 if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']){
217 $host = $_SERVER['HTTP_HOST'];
218 } else {
219 $host = $_SERVER['SERVER_NAME'];
220 }
221 $prefix = 'http';
222 if( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ){
223 $prefix = 'https';
224 }
225 return $prefix . '://' . $host . $_SERVER['REQUEST_URI'];
226 }
227
228 public static function editUserLink($userID){
229 return get_admin_url() . 'user-edit.php?user_id=' . $userID;
230 }
231 public static function tmpl($file, $data){
232 extract($data);
233 ob_start();
234 include $file;
235 return ob_get_contents() . (ob_end_clean() ? "" : "");
236 }
237 public static function bigRandomHex(){
238 return dechex(rand(0, 2147483647)) . dechex(rand(0, 2147483647)) . dechex(rand(0, 2147483647));
239 }
240 public static function encrypt($str){
241 $key = wfConfig::get('encKey');
242 if(! $key){
243 wordfence::status(1, 'error', "Wordfence error: No encryption key found!");
244 return false;
245 }
246 $db = new wfDB();
247 return $db->querySingle("select HEX(AES_ENCRYPT('%s', '%s')) as val", $str, $key);
248 }
249 public static function decrypt($str){
250 $key = wfConfig::get('encKey');
251 if(! $key){
252 wordfence::status(1, 'error', "Wordfence error: No encryption key found!");
253 return false;
254 }
255 $db = new wfDB();
256 return $db->querySingle("select AES_DECRYPT(UNHEX('%s'), '%s') as val", $str, $key);
257 }
258 public static function lcmem(){
259 $trace=debug_backtrace();
260 $caller=array_shift($trace);
261 $c2 = array_shift($trace);
262 $mem = memory_get_usage(true);
263 error_log("$mem at " . $caller['file'] . " line " . $caller['line']);
264 }
265 public static function logCaller(){
266 $trace=debug_backtrace();
267 $caller=array_shift($trace);
268 $c2 = array_shift($trace);
269 error_log("Caller for " . $caller['file'] . " line " . $caller['line'] . " is " . $c2['file'] . ' line ' . $c2['line']);
270 }
271 public static function getWPVersion(){
272 if(wordfence::$wordfence_wp_version){
273 return wordfence::$wordfence_wp_version;
274 } else {
275 global $wp_version;
276 return $wp_version;
277 }
278 }
279 public static function isAdminPageMU(){
280 if(preg_match('/^[\/a-zA-Z0-9\-\_\s\+\~\!\^\.]*\/wp-admin\/network\//', $_SERVER['REQUEST_URI'])){
281 return true;
282 }
283 return false;
284 }
285 public static function getSiteBaseURL(){
286 return rtrim(site_url(), '/') . '/';
287 }
288 public static function longestLine($data){
289 $lines = preg_split('/[\r\n]+/', $data);
290 $max = 0;
291 foreach($lines as $line){
292 $len = strlen($line);
293 if($len > $max){
294 $max = $len;
295 }
296 }
297 return $max;
298 }
299 public static function longestNospace($data){
300 $lines = preg_split('/[\r\n\s\t]+/', $data);
301 $max = 0;
302 foreach($lines as $line){
303 $len = strlen($line);
304 if($len > $max){
305 $max = $len;
306 }
307 }
308 return $max;
309 }
310 public static function requestMaxMemory(){
311 if(wfConfig::get('maxMem', false) && (int) wfConfig::get('maxMem') > 0){
312 $maxMem = (int) wfConfig::get('maxMem');
313 } else {
314 $maxMem = 256;
315 }
316 if( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < $maxMem ) ){
317 self::iniSet('memory_limit', $maxMem . 'M');
318 }
319 }
320 public static function isAdmin($user = false){
321 if($user){
322 if(is_multisite()){
323 if(user_can($user, 'manage_network')){
324 return true;
325 }
326 } else {
327 if(user_can($user, 'manage_options')){
328 return true;
329 }
330 }
331 } else {
332 if(is_multisite()){
333 if(current_user_can('manage_network')){
334 return true;
335 }
336 } else {
337 if(current_user_can('manage_options')){
338 return true;
339 }
340 }
341 }
342 return false;
343 }
344 public static function isWindows(){
345 if(! self::$isWindows){
346 if(preg_match('/^win/i', PHP_OS)){
347 self::$isWindows = 'yes';
348 } else {
349 self::$isWindows = 'no';
350 }
351 }
352 return self::$isWindows == 'yes' ? true : false;
353 }
354 public static function getScanLock(){
355 //Windows does not support non-blocking flock, so we use time.
356 $scanRunning = wfConfig::get('wf_scanRunning');
357 if($scanRunning && time() - $scanRunning < WORDFENCE_MAX_SCAN_TIME){
358 return false;
359 }
360 wfConfig::set('wf_scanRunning', time());
361 return true;
362 }
363 public static function clearScanLock(){
364 wfConfig::set('wf_scanRunning', '');
365 }
366 public static function isScanRunning(){
367 $scanRunning = wfConfig::get('wf_scanRunning');
368 if($scanRunning && time() - $scanRunning < WORDFENCE_MAX_SCAN_TIME){
369 return true;
370 } else {
371 return false;
372 }
373 }
374 public static function getIPGeo($IP){ //Works with int or dotted
375
376 $locs = self::getIPsGeo(array($IP));
377 if(isset($locs[$IP])){
378 return $locs[$IP];
379 } else {
380 return false;
381 }
382 }
383 public static function getIPsGeo($IPs){ //works with int or dotted. Outputs same format it receives.
384 $IPs = array_unique($IPs);
385 $isInt = false;
386 if(strpos($IPs[0], '.') === false){
387 $isInt = true;
388 }
389 $toResolve = array();
390 $db = new wfDB();
391 global $wpdb;
392 $locsTable = $wpdb->base_prefix . 'wfLocs';
393 $IPLocs = array();
394 foreach($IPs as $IP){
395 $row = $db->querySingleRec("select IP, ctime, failed, city, region, countryName, countryCode, lat, lon, unix_timestamp() - ctime as age from " . $locsTable . " where IP=%s", ($isInt ? $IP : self::inet_aton($IP)) );
396 if($row){
397 if($row['age'] > WORDFENCE_MAX_IPLOC_AGE){
398 $db->queryWrite("delete from " . $locsTable . " where IP=%s", $row['IP']);
399 } else {
400 if($row['failed'] == 1){
401 $IPLocs[$IP] = false;
402 } else {
403 if(! $isInt){
404 $row['IP'] = self::inet_ntoa($row['IP']);
405 }
406 $IPLocs[$IP] = $row;
407 }
408 }
409 }
410 if(! isset($IPLocs[$IP])){
411 $toResolve[] = $IP;
412 }
413 }
414 if(sizeof($toResolve) > 0){
415 $api = new wfAPI(wfConfig::get('apiKey'), wfUtils::getWPVersion());
416 try {
417 $freshIPs = $api->call('resolve_ips', array(), array(
418 'ips' => implode(',', $toResolve)
419 ));
420 if(is_array($freshIPs)){
421 foreach($freshIPs as $IP => $value){
422 if($value == 'failed'){
423 $db->queryWrite("insert IGNORE into " . $locsTable . " (IP, ctime, failed) values (%s, unix_timestamp(), 1)", ($isInt ? $IP : self::inet_aton($IP)) );
424 $IPLocs[$IP] = false;
425 } else if(is_array($value)){
426 for($i = 0; $i <= 5; $i++){
427 //Prevent warnings in debug mode about uninitialized values
428 if(! isset($value[$i])){ $value[$i] = ''; }
429 }
430 $db->queryWrite("insert IGNORE into " . $locsTable . " (IP, ctime, failed, city, region, countryName, countryCode, lat, lon) values (%s, unix_timestamp(), 0, '%s', '%s', '%s', '%s', %s, %s)",
431 ($isInt ? $IP : self::inet_aton($IP)),
432 $value[3], //city
433 $value[2], //region
434 $value[1], //countryName
435 $value[0],//countryCode
436 $value[4],//lat
437 $value[5]//lon
438 );
439 $IPLocs[$IP] = array(
440 'IP' => $IP,
441 'city' => $value[3],
442 'region' => $value[2],
443 'countryName' => $value[1],
444 'countryCode' => $value[0],
445 'lat' => $value[4],
446 'lon' => $value[5]
447 );
448 }
449 }
450 }
451 } catch(Exception $e){
452 wordfence::status(2, 'error', "Call to Wordfence API to resolve IPs failed: " . $e->getMessage());
453 return array();
454 }
455 }
456 return $IPLocs;
457 }
458 public static function reverseLookup($IP){
459 $db = new wfDB();
460 global $wpdb;
461 $reverseTable = $wpdb->base_prefix . 'wfReverseCache';
462 $IPn = wfUtils::inet_aton($IP);
463 $host = $db->querySingle("select host from " . $reverseTable . " where IP=%s and unix_timestamp() - lastUpdate < %d", $IPn, WORDFENCE_REVERSE_LOOKUP_CACHE_TIME);
464 if(! $host){
465 $ptr = implode(".", array_reverse(explode(".",$IP))) . ".in-addr.arpa";
466 $host = @dns_get_record($ptr, DNS_PTR);
467 if($host == null){
468 $host = 'NONE';
469 } else {
470 $host = $host[0]['target'];
471 }
472 $db->queryWrite("insert into " . $reverseTable . " (IP, host, lastUpdate) values (%s, '%s', unix_timestamp()) ON DUPLICATE KEY UPDATE host='%s', lastUpdate=unix_timestamp()", $IPn, $host, $host);
473 }
474 if($host == 'NONE'){
475 return '';
476 } else {
477 return $host;
478 }
479 }
480 public static function errorsOff(){
481 self::$lastErrorReporting = @ini_get('error_reporting');
482 @error_reporting(0);
483 self::$lastDisplayErrors = @ini_get('display_errors');
484 self::iniSet('display_errors', 0);
485 if(class_exists('wfScan')){ wfScan::$errorHandlingOn = false; }
486 }
487 public static function errorsOn(){
488 @error_reporting(self::$lastErrorReporting);
489 self::iniSet('display_errors', self::$lastDisplayErrors);
490 if(class_exists('wfScan')){ wfScan::$errorHandlingOn = true; }
491 }
492 //Note this function may report files that are too big which actually are not too big but are unseekable and throw an error on fseek(). But that's intentional
493 public static function fileTooBig($file){ //Deals with files > 2 gigs on 32 bit systems which are reported with the wrong size due to integer overflow
494 wfUtils::errorsOff();
495 $fh = @fopen($file, 'r');
496 wfUtils::errorsOn();
497 if(! $fh){ return false; }
498 $offset = WORDFENCE_MAX_FILE_SIZE_TO_PROCESS + 1;
499 $tooBig = false;
500 try {
501 if(@fseek($fh, $offset, SEEK_SET) === 0){
502 if(strlen(fread($fh, 1)) === 1){
503 $tooBig = true;
504 }
505 } //Otherwise we couldn't seek there so it must be smaller
506 fclose($fh);
507 return $tooBig;
508 } catch(Exception $e){ return true; } //If we get an error don't scan this file, report it's too big.
509 }
510 public static function fileOver2Gigs($file){ //Surround calls to this func with try/catch because fseek may throw error.
511 $fh = @fopen($file, 'r');
512 if(! $fh){ return false; }
513 $offset = 2147483647;
514 $tooBig = false;
515 //My throw an error so surround calls to this func with try/catch
516 if(@fseek($fh, $offset, SEEK_SET) === 0){
517 if(strlen(fread($fh, 1)) === 1){
518 $tooBig = true;
519 }
520 } //Otherwise we couldn't seek there so it must be smaller
521 @fclose($fh);
522 return $tooBig;
523 }
524 public static function countryCode2Name($code){
525 if(isset(wfCountryMap::$map[$code])){
526 return wfCountryMap::$map[$code];
527 } else {
528 return '';
529 }
530 }
531 public static function extractBareURI($URL){
532 $URL = preg_replace('/^https?:\/\/[^\/]+/i', '', $URL); //strip of method and host
533 $URL = preg_replace('/\#.*$/', '', $URL); //strip off fragment
534 $URL = preg_replace('/\?.*$/', '', $URL); //strip off query string
535 return $URL;
536 }
537 public static function IP2Country($IP){
538 if(! (function_exists('geoip_open') && function_exists('geoip_country_code_by_addr'))){
539 require_once('wfGeoIP.php');
540 }
541 $gi = geoip_open(dirname(__FILE__) . "/GeoIP.dat",GEOIP_STANDARD);
542 $country = geoip_country_code_by_addr($gi, $IP);
543 geoip_close($gi);
544 return $country ? $country : '';
545 }
546 public static function siteURLRelative(){
547 if(is_multisite()){
548 $URL = network_site_url();
549 } else {
550 $URL = site_url();
551 }
552 $URL = preg_replace('/^https?:\/\/[^\/]+/i', '', $URL);
553 $URL = rtrim($URL, '/') . '/';
554 return $URL;
555 }
556 public static function localHumanDate(){
557 return date('l jS \of F Y \a\t h:i:s A', time() + (3600 * get_option('gmt_offset')));
558 }
559 public static function localHumanDateShort(){
560 return date('D jS F \@ h:i:sA', time() + (3600 * get_option('gmt_offset')));
561 }
562 public static function funcEnabled($func){
563 if(! function_exists($func)){ return false; }
564 $disabled = explode(',', ini_get('disable_functions'));
565 foreach($disabled as $f){
566 if($func == $f){ return false; }
567 }
568 return true;
569 }
570 public static function iniSet($key, $val){
571 if(self::funcEnabled('ini_set')){
572 @ini_set($key, $val);
573 }
574 }
575 public static function doNotCache(){
576 header("Cache-Control: no-cache, must-revalidate");
577 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); //In the past
578 if(! defined('DONOTCACHEPAGE')){ define('DONOTCACHEPAGE', true); }
579 if(! defined('DONOTCACHEDB')){ define('DONOTCACHEDB', true); }
580 if(! defined('DONOTCDN')){ define('DONOTCDN', true); }
581 if(! defined('DONOTCACHEOBJECT')){ define('DONOTCACHEOBJECT', true); }
582 wfCache::doNotCache();
583 }
584 public static function isUABlocked($uaPattern){ // takes a pattern using asterisks as wildcards, turns it into regex and checks it against the visitor UA returning true if blocked
585 return fnmatch($uaPattern, $_SERVER['HTTP_USER_AGENT'], FNM_CASEFOLD);
586 }
587 public static function rangeToCIDRs($startIP, $endIP){
588 $startIPBin = sprintf('%032b', $startIP);
589 $endIPBin = sprintf('%032b', $endIP);
590 $IPIncBin = $startIPBin;
591 $CIDRs = array();
592 while(strcmp($IPIncBin, $endIPBin) <= 0){
593 $longNetwork = 32;
594 $IPNetBin = $IPIncBin;
595 while(($IPIncBin[$longNetwork - 1] == '0') && (strcmp(substr_replace($IPNetBin, '1', $longNetwork - 1, 1), $endIPBin) <= 0)){
596 $IPNetBin[$longNetwork - 1] = '1';
597 $longNetwork--;
598 }
599 $CIDRs[] = long2ip(bindec($IPIncBin)) . ($longNetwork < 32 ? '/' . $longNetwork : '');
600 $IPIncBin = sprintf('%032b', bindec($IPNetBin) + 1);
601 }
602 return $CIDRs;
603 }
604 public static function setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly){
605 if(version_compare(PHP_VERSION, '5.2.0') >= 0){
606 @setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
607 } else {
608 @setcookie($name, $value, $expire, $path);
609 }
610 }
611 public static function isNginx(){
612 $sapi = php_sapi_name();
613 $serverSoft = $_SERVER['SERVER_SOFTWARE'];
614 if($sapi == 'fpm-fcgi' || stripos($serverSoft, 'nginx') !== false){
615 return true;
616 }
617 }
618 public static function getLastError(){
619 $err = error_get_last();
620 if(is_array($err)){
621 return $err['message'];
622 }
623 return '';
624 }
625 public static function hostNotExcludedFromProxy($url){
626 if(! defined('WP_PROXY_BYPASS_HOSTS')){
627 return true; //No hosts are excluded
628 }
629 $hosts = explode(',', WP_PROXY_BYPASS_HOSTS);
630 $url = preg_replace('/^https?:\/\//i', '', $url);
631 $url = preg_replace('/\/.*$/', '', $url);
632 $url = strtolower($url);
633 foreach($hosts as $h){
634 if(strtolower(trim($h)) == $url){
635 return false;
636 }
637 }
638 return true;
639 }
640 public static function hasXSS($URL){
641 if(! preg_match('/^https?:\/\/[a-z0-9\.\-]+\/[^\':<>\"\\\]*$/i', $URL)){
642 return true;
643 } else {
644 return false;
645 }
646 }
647 }
648
649
650 ?>
651