Diff
1 year ago
audit-log
1 year ago
dashboard
1 year ago
rest-api
1 year ago
.htaccess
7 years ago
Diff.php
6 years ago
GeoLite2-Country.mmdb
1 year ago
IPTraf.php
1 year ago
IPTrafList.php
1 year ago
WFLSPHP52Compatability.php
6 years ago
compat.php
8 years ago
diffResult.php
1 year ago
email_genericAlert.php
5 years ago
email_newIssues.php
3 years ago
email_unlockRequest.php
5 years ago
email_unsubscribeRequest.php
4 years ago
flags.php
7 years ago
live_activity.php
4 years ago
menu_dashboard.php
3 years ago
menu_dashboard_options.php
3 years ago
menu_firewall.php
3 years ago
menu_firewall_blocking.php
4 years ago
menu_firewall_blocking_options.php
3 years ago
menu_firewall_waf.php
4 years ago
menu_firewall_waf_options.php
3 years ago
menu_install.php
3 years ago
menu_options.php
1 year ago
menu_scanner.php
2 years ago
menu_scanner_credentials.php
1 year ago
menu_scanner_options.php
3 years ago
menu_support.php
1 year ago
menu_tools.php
1 year ago
menu_tools_auditlog.php
1 year ago
menu_tools_diagnostic.php
1 year ago
menu_tools_importExport.php
4 years ago
menu_tools_livetraffic.php
2 years ago
menu_tools_twoFactor.php
4 years ago
menu_tools_whois.php
3 years ago
menu_wordfence_central.php
3 years ago
noc1.key
7 years ago
sodium_compat_fast.php
2 years ago
sysinfo.php
1 year ago
viewFullActivityLog.php
4 years ago
wf503.php
2 years ago
wfAPI.php
1 year ago
wfActivityReport.php
1 year ago
wfAdminNoticeQueue.php
4 years ago
wfAlerts.php
5 years ago
wfArray.php
3 years ago
wfAuditLog.php
1 year ago
wfBrowscap.php
3 years ago
wfBrowscapCache.php
7 years ago
wfBulkCountries.php
2 years ago
wfCache.php
3 years ago
wfCentralAPI.php
1 year ago
wfConfig.php
1 year ago
wfCrawl.php
1 year ago
wfCredentialsController.php
1 year ago
wfCrypt.php
6 years ago
wfCurlInterceptor.php
3 years ago
wfDB.php
1 year ago
wfDashboard.php
1 year ago
wfDateLocalization.php
2 years ago
wfDeactivationOption.php
3 years ago
wfDiagnostic.php
1 year ago
wfDict.php
8 years ago
wfDirectoryIterator.php
7 years ago
wfFileUtils.php
2 years ago
wfHelperBin.php
11 years ago
wfHelperString.php
1 year ago
wfIPWhitelist.php
5 years ago
wfImportExportController.php
5 years ago
wfInaccessibleDirectoryException.php
2 years ago
wfInvalidPathException.php
3 years ago
wfIpLocation.php
3 years ago
wfIpLocator.php
3 years ago
wfIssues.php
1 year ago
wfJWT.php
7 years ago
wfLicense.php
3 years ago
wfLockedOut.php
2 years ago
wfLog.php
1 year ago
wfMD5BloomFilter.php
8 years ago
wfModuleController.php
7 years ago
wfNotification.php
8 years ago
wfOnboardingController.php
1 year ago
wfPersistenceController.php
1 year ago
wfRESTAPI.php
7 years ago
wfScan.php
2 years ago
wfScanEngine.php
1 year ago
wfScanEntrypoint.php
3 years ago
wfScanFile.php
1 year ago
wfScanFileLink.php
3 years ago
wfScanFileListItem.php
1 year ago
wfScanFileProperties.php
1 year ago
wfScanMonitor.php
2 years ago
wfScanPath.php
3 years ago
wfSchema.php
1 year ago
wfStyle.php
1 year ago
wfSupportController.php
1 year ago
wfUnlockMsg.php
5 years ago
wfUpdateCheck.php
1 year ago
wfUtils.php
1 year ago
wfVersionCheckController.php
3 years ago
wfVersionSupport.php
1 year ago
wfView.php
5 years ago
wfViewResult.php
1 year ago
wfWebsite.php
3 years ago
wordfenceClass.php
1 year ago
wordfenceConstants.php
1 year ago
wordfenceHash.php
1 year ago
wordfenceScanner.php
1 year ago
wordfenceURLHoover.php
2 years ago
wfIpLocator.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../vendor/autoload.php'; |
| 4 | |
| 5 | require_once __DIR__ . '/wfIpLocation.php'; |
| 6 | |
| 7 | use Wordfence\MmdbReader\Database; |
| 8 | use Wordfence\MmdbReader\Exception\MmdbThrowable; |
| 9 | |
| 10 | class wfIpLocator { |
| 11 | |
| 12 | const SOURCE_BUNDLED = 0; |
| 13 | const SOURCE_WFLOGS = 1; |
| 14 | |
| 15 | const DATABASE_FILE_NAME = 'GeoLite2-Country.mmdb'; |
| 16 | |
| 17 | private static $instances = array(); |
| 18 | |
| 19 | private $database; |
| 20 | private $preferred; |
| 21 | |
| 22 | private function __construct($database, $preferred) { |
| 23 | $this->database = $database; |
| 24 | $this->preferred = $preferred; |
| 25 | } |
| 26 | |
| 27 | public function isPreferred() { |
| 28 | return $this->preferred; |
| 29 | } |
| 30 | |
| 31 | private static function logError($message) { |
| 32 | if (class_exists('wfUtils')) |
| 33 | wfUtils::check_and_log_last_error('ip_locator', 'IP Location Error:', $message, 0); |
| 34 | } |
| 35 | |
| 36 | public function locate($ip) { |
| 37 | if ($this->database !== null) { |
| 38 | try { |
| 39 | $record = $this->database->search($ip); |
| 40 | if ($record !== null) |
| 41 | return new wfIpLocation($record); |
| 42 | } |
| 43 | catch (MmdbThrowable $t) { |
| 44 | self::logError('Failed to locate IP address: ' . $t->getMessage()); |
| 45 | } |
| 46 | } |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | public function getCountryCode($ip, $default = '') { |
| 51 | $record = $this->locate($ip); |
| 52 | if ($record !== null) |
| 53 | return $record->getCountryCode(); |
| 54 | return $default; |
| 55 | } |
| 56 | |
| 57 | public function getDatabaseVersion() { |
| 58 | if ($this->database !== null) { |
| 59 | try { |
| 60 | return $this->database->getMetadata()->getBuildEpoch(); |
| 61 | } |
| 62 | catch (MmdbThrowable $t) { |
| 63 | self::logError('Failed to retrieve database version: ' . $t->getMessage()); |
| 64 | } |
| 65 | } |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | private static function getDatabaseDirectory($source) { |
| 70 | switch ($source) { |
| 71 | case self::SOURCE_BUNDLED: |
| 72 | return WFWAF_LOG_PATH; |
| 73 | case self::SOURCE_BUNDLED: |
| 74 | default: |
| 75 | return __DIR__; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private static function initializeDatabase($preferredSource, &$isPreferred) { |
| 80 | $sources = array(); |
| 81 | if ($preferredSource !== self::SOURCE_BUNDLED) |
| 82 | $sources[] = $preferredSource; |
| 83 | $sources[] = self::SOURCE_BUNDLED; |
| 84 | $isPreferred = true; |
| 85 | foreach ($sources as $source) { |
| 86 | $directory = self::getDatabaseDirectory($source); |
| 87 | try { |
| 88 | $path = $directory . '/' . self::DATABASE_FILE_NAME; |
| 89 | if (file_exists($path)) //Preemptive check to prevent warnings |
| 90 | return Database::open($path); |
| 91 | } |
| 92 | catch (MmdbThrowable $t) { |
| 93 | self::logError('Failed to initialize IP location database: ' . $t->getMessage()); |
| 94 | } |
| 95 | $preferred = false; |
| 96 | } |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | public static function getInstance($preferredSource = null) { |
| 101 | if ($preferredSource === null) |
| 102 | $preferredSource = self::SOURCE_WFLOGS; |
| 103 | if (!array_key_exists($preferredSource, self::$instances)) { |
| 104 | $database = self::initializeDatabase($preferredSource, $isPreferred); |
| 105 | self::$instances[$preferredSource] = new wfIpLocator($database, $isPreferred); |
| 106 | } |
| 107 | return self::$instances[$preferredSource]; |
| 108 | } |
| 109 | |
| 110 | } |