Exemplo n.º 1
0
 public function index()
 {
     Piwik::checkUserHasSuperUserAccess();
     $view = new View('@IP2Location/index');
     $view->language = LanguagesManager::getLanguageCodeForCurrentUser();
     $this->setBasicVariablesView($view);
     $view->adminMenu = MenuAdmin::getInstance()->getMenu();
     $view->topMenu = MenuTop::getInstance()->getMenu();
     $view->notifications = NotificationManager::getAllNotificationsToDisplay();
     $view->userMenu = MenuUser::getInstance()->getMenu();
     $view->phpVersion = phpversion();
     $view->phpIsNewEnough = version_compare($view->phpVersion, '5.3.0', '>=');
     $view->assign('userMenu', 'IP2Location');
     $view->assign('dbNotFound', false);
     $view->assign('dbOutDated', false);
     $view->assign('showResults', false);
     $view->assign('fileName', '-');
     $view->assign('date', '-');
     $view->assign('country', '');
     $view->assign('regionName', '');
     $view->assign('cityName', '');
     $view->assign('position', '');
     $ipAddress = trim(Common::getRequestVar('ipAddress', $_SERVER['REMOTE_ADDR']));
     $view->assign('ipAddress', $ipAddress);
     $dbPath = PIWIK_INCLUDE_PATH . '/plugins/IP2Location/data/';
     $dbFile = '';
     if ($handle = opendir($dbPath)) {
         while (false !== ($file = readdir($handle))) {
             if (strtoupper(substr($file, -4)) == '.BIN') {
                 $dbFile = $dbPath . $file;
                 break;
             }
         }
         closedir($handle);
     }
     if (!$dbFile) {
         $view->assign('dbNotFound', true);
     }
     if ($dbFile) {
         $view->assign('fileName', $file);
         if (filemtime($dbFile) < strtotime('-2 months')) {
             $view->assign('dbOutDated', true);
         } else {
             $view->assign('date', date('d M, Y', filemtime($dbFile)));
         }
         if (!empty($_POST)) {
             $view->assign('showResults', true);
             $result = IP2LocationAPI::lookup($ipAddress, $dbFile);
             $view->assign('country', $result['countryCode'] != '-' ? $result['countryName'] . ' (' . $result['countryCode'] . ')' : '-');
             $view->assign('regionName', !preg_match('/not supported/', $result['regionName']) ? $result['regionName'] : '-');
             $view->assign('cityName', !preg_match('/not supported/', $result['cityName']) ? $result['cityName'] : '-');
             $view->assign('position', !preg_match('/not supported/', $result['latitude']) && $result['latitude'] != '-' ? $result['latitude'] . ', ' . $result['longitude'] : '-');
         }
     }
     echo $view->render();
 }
Exemplo n.º 2
0
 public function insertVisitorLocation(&$visitorInfo, \Piwik\Tracker\Request $request)
 {
     $dbPath = PIWIK_INCLUDE_PATH . '/plugins/IP2Location/data/';
     $dbFile = '';
     if ($handle = opendir($dbPath)) {
         while (false !== ($file = readdir($handle))) {
             if (strtoupper(substr($file, -4)) == '.BIN') {
                 $dbFile = $dbPath . $file;
                 break;
             }
         }
         closedir($handle);
     }
     if ($dbFile) {
         $privacyConfig = new PrivacyManagerConfig();
         $ipAddress = IPUtils::binaryToStringIP($privacyConfig->useAnonymizedIpForVisitEnrichment ? $visitorInfo['location_ip'] : $request->getIp());
         $result = IP2LocationAPI::lookup($ipAddress, $dbFile);
         $countryCode = $result['countryCode'] != '-' ? strtolower($result['countryCode']) : null;
         $regionName = $result['regionName'] != '-' ? $result['regionName'] : null;
         $regionCode = $this->getRegionCode(strtoupper($countryCode), $regionName);
         $cityName = !preg_match('/not supported/', $result['cityName']) && $result['cityName'] != '-' ? $result['cityName'] : null;
         $latitude = !preg_match('/not supported/', $result['latitude']) && $result['latitude'] != '-' ? $result['latitude'] : null;
         $longitude = !preg_match('/not supported/', $result['longitude']) && $result['longitude'] != '-' ? $result['longitude'] : null;
         if ($countryCode) {
             $visitorInfo['location_country'] = $countryCode;
         }
         if ($regionCode) {
             $visitorInfo['location_region'] = $regionCode;
         }
         if ($cityName) {
             $visitorInfo['location_city'] = $cityName;
         }
         if ($latitude) {
             $visitorInfo['location_latitude'] = $latitude;
         }
         if ($longitude) {
             $visitorInfo['location_longitude'] = $longitude;
         }
     }
 }