diff --git a/IntranetGeoIP.php b/IntranetGeoIP.php index f682b25..c7152e2 100644 --- a/IntranetGeoIP.php +++ b/IntranetGeoIP.php @@ -5,44 +5,33 @@ namespace Piwik\Plugins\IntranetGeoIP; use Piwik\Plugin; -use Piwik\Network; -use Piwik\Log; use Piwik\Notification; -use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; -use Piwik\Tracker\Request as TrackerRequest; -class IntranetGeoIP extends Plugin +// This is need, that this provider is detected in this plugin +// @see https://github.com/piwik/piwik/blob/b95837534c6fc4c9dd63eef2c2e9d8bb343ca23e/plugins/UserCountry/LocationProvider.php#L152 +require_once PIWIK_INCLUDE_PATH . '/plugins/IntranetGeoIP/Provider.php'; + +final class IntranetGeoIP extends Plugin { /** * * @return string */ - private function getDataExampleFilePath() + public static function getDataExampleFilePath() { return __DIR__ . '/data.example.php'; } - + /** * * @return string */ - private function getDataFilePath() + public static function getDataFilePath() { return PIWIK_INCLUDE_PATH . '/config/IntranetGeoIP.data.php'; } - /** - * - * @see Piwik\Plugin::getListHooksRegistered - */ - public function getListHooksRegistered() - { - return array( - 'Tracker.newVisitorInformation' => 'logIntranetSubNetworkInfo' - ); - } - /** * * @see \Piwik\Plugin::install() @@ -85,51 +74,4 @@ public function uninstall() unlink($this->getDataFilePath()); } } - - /** - * Called by event `Tracker.newVisitorInformation` - * - * @see getListHooksRegistered() - */ - public function logIntranetSubNetworkInfo(&$visitorInfo, TrackerRequest $request) - { - if (! file_exists($this->getDataFilePath())) { - Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath()); - return; - } - - $data = include $this->getDataFilePath(); - if ($data === false) { - // no data file found - // @todo ...inform the user/ log something - Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath()); - return; - } - - $privacyConfig = new PrivacyManagerConfig(); - - $ipBinary = $request->getIp(); - if ($privacyConfig->useAnonymizedIpForVisitEnrichment === true) { - $ipBinary = $visitorInfo['location_ip']; - } - - $ip = Network\IP::fromBinaryIP($ipBinary); - - foreach ($data as $value) { - if (isset($value['networks']) && $ip->isInRanges($value['networks']) === true) { - // values with the same key are not overwritten by right! - // http://www.php.net/manual/en/language.operators.array.php - if (isset($value['visitorInfo'])) { - $visitorInfo = $value['visitorInfo'] + $visitorInfo; - } - return; - } - } - - // if nothing was matched, you can define default values if you want to - if (isset($data['noMatch']) && isset($data['noMatch']['visitorInfo'])) { - $visitorInfo = $data['noMatch']['visitorInfo'] + $visitorInfo; - return; - } - } } diff --git a/Provider.php b/Provider.php new file mode 100644 index 0000000..5f5401b --- /dev/null +++ b/Provider.php @@ -0,0 +1,116 @@ + 'default', + * 'title' => '...', + * 'description' => '...' + * ); + * + * @return array + */ + public function getInfo() + { + $desc = Piwik::translate('UserCountry_DefaultLocationProviderDesc1') . ' ' . Piwik::translate('UserCountry_DefaultLocationProviderDesc2', array( + '', + '', + '', + '' + )) . '

' . Piwik::translate('UserCountry_HowToInstallGeoIPDatabases') . '

'; + + return array( + 'id' => self::ID, + 'title' => self::TITLE, + 'description' => $desc, + 'order' => 20 + ); + } + + /** + * This implementation is available, as soon as the plugin is installed! + * + * @return bool + */ + public function isAvailable() + { + return true; + } + + /** + * Test if the data file is around + * + * @return bool + */ + public function isWorking() + { + if(!file_exists(IntranetGeoIP::getDataFilePath())){ + return 'Configuration file is missing: ' . IntranetGeoIP::getDataFilePath(); + } + + $config = include IntranetGeoIP::getDataFilePath(); + + if(count($config) === 1){ + return 'Only default configuration given. Please edit the configuration file: ' . IntranetGeoIP::getDataFilePath(); + } + + return true; + } + + /** + * Returns an array describing the types of location information this provider will + * return. + * + * This provider supports the following types of location info: + * - continent code + * - continent name + * - country code + * - country name + * + * @return array + */ + public function getSupportedLocationInfo() + { + return array( + self::CONTINENT_CODE_KEY => true, + self::CONTINENT_NAME_KEY => true, + self::COUNTRY_CODE_KEY => true, + self::COUNTRY_NAME_KEY => true + ); + } + + /** + * Guesses a visitor's location using a visitor's browser language. + * + * @param array $info + * Contains 'ip' & 'lang' keys. + * @return array Contains the guessed country code mapped to LocationProvider::COUNTRY_CODE_KEY. + */ + public function getLocation($info) + { + // @todo this is only for testing! + $location = array( + parent::COUNTRY_CODE_KEY => 'at', + parent::ISP_KEY => 'github.com', + parent::ORG_KEY => 'OpenSource' + ); + $this->completeLocationResult($location); + + return $location; + } +} \ No newline at end of file