Ejemplo n.º 1
0
/**
 * Returns the name of a city + the name of its region + the name of its country using
 * the label of a Visits by City report.
 * 
 * @param string $label A label containing a city name, region code + country code,
 *                      separated by two '|' chars: 'Paris|A8|FR'
 * @return string|false eg. 'Paris, Ile de France, France' or false if $label ==
 *                      Piwik_DataTable::LABEL_SUMMARY_ROW.
 */
function Piwik_UserCountry_getPrettyCityName($label)
{
    if ($label == Piwik_DataTable::LABEL_SUMMARY_ROW) {
        return $label;
    }
    if ($label == '') {
        return Piwik_Translate('General_Unknown');
    }
    // get city name, region code & country code
    $parts = explode(Piwik_UserCountry::LOCATION_SEPARATOR, $label);
    $cityName = $parts[0];
    $regionCode = $parts[1];
    $countryCode = $parts[2];
    if ($cityName == Piwik_Tracker_Visit::UNKNOWN_CODE || $cityName == '') {
        $cityName = Piwik_Translate('General_Unknown');
    }
    $result = $cityName;
    if ($countryCode != Piwik_Tracker_Visit::UNKNOWN_CODE && $countryCode != '') {
        if ($regionCode != '' && $regionCode != Piwik_Tracker_Visit::UNKNOWN_CODE) {
            $regionName = Piwik_UserCountry_LocationProvider_GeoIp::getRegionNameFromCodes($countryCode, $regionCode);
            $result .= ', ' . $regionName;
        }
        $result .= ', ' . Piwik_CountryTranslate($countryCode);
    }
    return $result;
}
Ejemplo n.º 2
0
/**
 *  Proxy to normal piwik.php, but in testing mode
 *  
 *  - Use the tests database to record Tracking data
 *  - Allows to overwrite the Visitor IP, and Server datetime 
 *  
 * @see Main.test.php
 * 
 */
// Wrapping the request inside ob_start() calls to ensure that the Test
// calling us waits for the full request to process before unblocking
ob_start();
define('PIWIK_INCLUDE_PATH', '../../..');
define('PIWIK_USER_PATH', PIWIK_INCLUDE_PATH);
require_once PIWIK_INCLUDE_PATH . '/libs/upgradephp/upgrade.php';
require_once PIWIK_INCLUDE_PATH . '/core/Loader.php';
// Config files forced to use the test database
// Note that this also provides security for Piwik installs containing tests files:
// this proxy will not record any data in the production database.
Piwik::createConfigObject();
Piwik_Config::getInstance()->setTestEnvironment();
Piwik_Config::getInstance()->PluginsInstalled['PluginsInstalled'] = array();
Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
Piwik_Tracker::setTestEnvironment();
Piwik_DataTable_Manager::getInstance()->deleteAll();
Piwik_Option::getInstance()->clearCache();
Piwik_Site::clearCache();
Piwik_Common::deleteTrackerCache();
include PIWIK_INCLUDE_PATH . '/piwik.php';
ob_end_flush();
Ejemplo n.º 3
0
 /**
  * Returns a GeoIP instance. Creates it if necessary.
  * 
  * @param string $key 'loc', 'isp' or 'org'. Determines the type of GeoIP database
  *                    to load.
  * @return object|false
  */
 private function getGeoIpInstance($key)
 {
     if (empty($this->geoIpCache[$key])) {
         // make sure region names are loaded & saved first
         parent::getRegionNames();
         require_once PIWIK_INCLUDE_PATH . '/libs/MaxMindGeoIP/geoipcity.inc';
         $pathToDb = self::getPathToGeoIpDatabase(parent::$dbNames[$key]);
         if ($pathToDb !== false) {
             $this->geoIpCache[$key] = geoip_open($pathToDb, GEOIP_STANDARD);
             // TODO support shared memory
         }
     }
     return empty($this->geoIpCache[$key]) ? false : $this->geoIpCache[$key];
 }
Ejemplo n.º 4
0
 function getPrettyLocation()
 {
     $parts = array();
     // add city if it's known
     if ($this->details['location_city'] != '') {
         $parts[] = $this->details['location_city'];
     }
     // add region if it's known
     $region = $this->details['location_region'];
     if ($region != '' && $region != Piwik_Tracker_Visit::UNKNOWN_CODE) {
         $parts[] = Piwik_UserCountry_LocationProvider_GeoIp::getRegionNameFromCodes($this->details['location_country'], $region);
     }
     // add country & return concatenated result
     $parts[] = $this->getCountryName();
     return implode(', ', $parts);
 }
Ejemplo n.º 5
0
 /**
  * Returns an array of region names mapped by country code & region code.
  * 
  * @return array
  */
 public static function getRegionNames()
 {
     if (is_null(self::$regionNames)) {
         require_once PIWIK_INCLUDE_PATH . '/libs/MaxMindGeoIP/geoipregionvars.php';
         self::$regionNames = $GEOIP_REGION_NAME;
     }
     return self::$regionNames;
 }
Ejemplo n.º 6
0
 /**
  * Returns true if the PECL module that is installed can be successfully used
  * to get the location of an IP address.
  * 
  * @return bool
  */
 public function isWorking()
 {
     // if no no location database is available, this implementation is not setup correctly
     if (!self::isLocationDatabaseAvailable()) {
         $dbDir = dirname(geoip_db_filename(GEOIP_COUNTRY_EDITION)) . '/';
         $quotedDir = "'{$dbDir}'";
         // check if the directory the PECL module is looking for exists
         if (!is_dir($dbDir)) {
             return Piwik_Translate('UserCountry_PeclGeoIPNoDBDir', array($quotedDir, "'geoip.custom_directory'"));
         }
         // check if the user named the city database GeoLiteCity.dat
         if (file_exists($dbDir . 'GeoLiteCity.dat')) {
             return Piwik_Translate('UserCountry_PeclGeoLiteError', array($quotedDir, "'GeoLiteCity.dat'", "'GeoIPCity.dat'"));
         }
         return Piwik_Translate('UserCountry_CannotFindPeclGeoIPDb', array($quotedDir, "'GeoIP.dat'", "'GeoIPCity.dat'"));
     }
     return parent::isWorking();
 }
 public static function setLocationProvider($file)
 {
     Piwik_UserCountry_LocationProvider_GeoIp::$dbNames['loc'] = array($file);
     Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
     Piwik_UserCountry_LocationProvider::$providers = null;
     Piwik_UserCountry_LocationProvider::setCurrentProvider(self::GEOIP_IMPL_TO_TEST);
 }
Ejemplo n.º 8
0
 public static function tearDownAfterClass()
 {
     Piwik_UserCountry_LocationProvider::$providers = null;
     Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
     Piwik_UserCountry_LocationProvider::setCurrentProvider('default');
 }