Diff
14 years ago
geshi
14 years ago
.htaccess
14 years ago
Diff.php
14 years ago
IPTraf.php
14 years ago
diffResult.php
14 years ago
dropAll.php
14 years ago
email_genericAlert.php
14 years ago
email_newIssues.php
14 years ago
geshi.php
14 years ago
menu_activity.php
14 years ago
menu_blockedIPs.php
14 years ago
menu_config.php
14 years ago
menu_options.php
14 years ago
menu_scan.php
14 years ago
sysinfo.php
14 years ago
wf503.php
14 years ago
wfAPI.php
14 years ago
wfAction.php
14 years ago
wfBrowscap.php
14 years ago
wfBrowscapCache.php
14 years ago
wfConfig.php
14 years ago
wfCrawl.php
14 years ago
wfDB.php
14 years ago
wfDict.php
14 years ago
wfIssues.php
14 years ago
wfLockedOut.php
14 years ago
wfLog.php
14 years ago
wfModTracker.php
14 years ago
wfRate.php
14 years ago
wfScanEngine.php
14 years ago
wfSchema.php
14 years ago
wfUtils.php
14 years ago
wfViewResult.php
14 years ago
wordfenceClass.php
14 years ago
wordfenceConstants.php
14 years ago
wordfenceHash.php
14 years ago
wordfenceScanner.php
14 years ago
wordfenceURLHoover.php
14 years ago
wfUtils.php
150 lines
| 1 | <?php |
| 2 | class wfUtils { |
| 3 | private static $reverseLookupCache = array(); |
| 4 | public static function makeTimeAgo($secs, $noSeconds = false) { |
| 5 | if($secs < 1){ |
| 6 | return "a moment"; |
| 7 | } |
| 8 | $months = floor($secs / (86400 * 30)); |
| 9 | $days = floor($secs / 86400); |
| 10 | $hours = floor($secs / 3600); |
| 11 | $minutes = floor($secs / 60); |
| 12 | if($months) { |
| 13 | $days -= $months * 30; |
| 14 | return self::pluralize($months, 'month', $days, 'day'); |
| 15 | } else if($days) { |
| 16 | $hours -= $days * 24; |
| 17 | return self::pluralize($days, 'day', $hours, 'hour'); |
| 18 | } else if($hours) { |
| 19 | $minutes -= $hours * 60; |
| 20 | return self::pluralize($hours, 'hour', $minutes, 'min'); |
| 21 | } else if($minutes) { |
| 22 | $secs -= $minutes * 60; |
| 23 | return self::pluralize($minutes, 'min'); |
| 24 | } else { |
| 25 | if($noSeconds){ |
| 26 | return "less than a minute"; |
| 27 | } else { |
| 28 | return floor($secs) . " secs"; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | public static function pluralize($m1, $t1, $m2 = false, $t2 = false) { |
| 33 | if($m1 != 1) { |
| 34 | $t1 = $t1 . 's'; |
| 35 | } |
| 36 | if($m2 != 1) { |
| 37 | $t2 = $t2 . 's'; |
| 38 | } |
| 39 | if($m1 && $m2){ |
| 40 | return "$m1 $t1 $m2 $t2"; |
| 41 | } else { |
| 42 | return "$m1 $t1"; |
| 43 | } |
| 44 | } |
| 45 | public static function formatBytes($bytes, $precision = 2) { |
| 46 | $units = array('B', 'KB', 'MB', 'GB', 'TB'); |
| 47 | |
| 48 | $bytes = max($bytes, 0); |
| 49 | $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
| 50 | $pow = min($pow, count($units) - 1); |
| 51 | |
| 52 | // Uncomment one of the following alternatives |
| 53 | $bytes /= pow(1024, $pow); |
| 54 | // $bytes /= (1 << (10 * $pow)); |
| 55 | |
| 56 | return round($bytes, $precision) . ' ' . $units[$pow]; |
| 57 | } |
| 58 | public static function inet_ntoa($ip){ |
| 59 | $long = 4294967295 - ($ip - 1); |
| 60 | return long2ip(-$long); |
| 61 | } |
| 62 | public static function inet_aton($ip){ |
| 63 | return sprintf("%u", ip2long($ip)); |
| 64 | } |
| 65 | public static function getBaseURL(){ |
| 66 | return plugins_url() . '/wordfence/'; |
| 67 | } |
| 68 | public static function getPluginBaseDir(){ |
| 69 | return ABSPATH . 'wp-content/plugins/'; |
| 70 | } |
| 71 | public static function getIP(){ |
| 72 | $ip = 0; |
| 73 | if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ |
| 74 | $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 75 | } |
| 76 | if((! $ip) && isset($_SERVER['REMOTE_ADDR'])){ |
| 77 | $ip = $_SERVER['REMOTE_ADDR']; |
| 78 | } |
| 79 | return $ip; |
| 80 | } |
| 81 | public static function getRequestedURL(){ |
| 82 | return ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; |
| 83 | } |
| 84 | public static function reverseLookup($IP){ |
| 85 | if(! isset(self::$reverseLookupCache[$IP])){ |
| 86 | $ptr = implode(".", array_reverse(explode(".",$IP))) . ".in-addr.arpa"; |
| 87 | $host = dns_get_record($ptr, DNS_PTR); |
| 88 | if($host == null){ |
| 89 | self::$reverseLookupCache[$IP] = ''; |
| 90 | } else { |
| 91 | self::$reverseLookupCache[$IP] = $host[0]['target']; |
| 92 | } |
| 93 | } |
| 94 | return self::$reverseLookupCache[$IP]; |
| 95 | } |
| 96 | public static function editUserLink($userID){ |
| 97 | return get_admin_url() . 'user-edit.php?user_id=' . $userID; |
| 98 | } |
| 99 | public static function wdie($err){ |
| 100 | $trace=debug_backtrace(); $caller=array_shift($trace); |
| 101 | error_log("Wordfence error in " . $caller['file'] . " line " . $caller['line'] . ": $err"); |
| 102 | exit(); |
| 103 | } |
| 104 | public static function tmpl($file, $data){ |
| 105 | extract($data); |
| 106 | ob_start(); |
| 107 | include $file; |
| 108 | return ob_get_contents() . (ob_end_clean() ? "" : ""); |
| 109 | } |
| 110 | public static function bigRandomHex(){ |
| 111 | return dechex(rand(0, 2147483647)) . dechex(rand(0, 2147483647)) . dechex(rand(0, 2147483647)); |
| 112 | } |
| 113 | public static function encrypt($str){ |
| 114 | $key = wfConfig::get('encKey'); |
| 115 | if(! $key){ |
| 116 | error_log("Wordfence error: No encryption key found!"); |
| 117 | exit(); |
| 118 | } |
| 119 | $db = new wfDB(); |
| 120 | return $db->querySingle("select HEX(AES_ENCRYPT('%s', '%s')) as val", $str, $key); |
| 121 | } |
| 122 | public static function decrypt($str){ |
| 123 | $key = wfConfig::get('encKey'); |
| 124 | if(! $key){ |
| 125 | error_log("Wordfence error: No encryption key found!"); |
| 126 | exit(); |
| 127 | } |
| 128 | $db = new wfDB(); |
| 129 | return $db->querySingle("select AES_DECRYPT(UNHEX('%s'), '%s') as val", $str, $key); |
| 130 | } |
| 131 | public static function logCaller(){ |
| 132 | $trace=debug_backtrace(); |
| 133 | $caller=array_shift($trace); |
| 134 | $c2 = array_shift($trace); |
| 135 | error_log("Caller for " . $caller['file'] . " line " . $caller['line'] . " is " . $c2['file'] . ' line ' . $c2['line']); |
| 136 | } |
| 137 | public static function getWPVersion(){ |
| 138 | global $wp_version; |
| 139 | global $wordfence_wp_version; |
| 140 | if(isset($wordfence_wp_version)){ |
| 141 | return $wordfence_wp_version; |
| 142 | } else { |
| 143 | return $wp_version; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | ?> |
| 150 |