Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/provider #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 8 additions & 66 deletions IntranetGeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}
}
116 changes: 116 additions & 0 deletions Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP;

use Piwik\Common;
use Piwik\Config;
use Piwik\Piwik;
use Piwik\Plugins\UserCountry\LocationProvider;

final class Provder extends LocationProvider
{

const ID = 'IntranetGeoIP';

const TITLE = 'IntranetGeoIP';

/**
* Returns information about this location provider.
* Contains an id, title & description:
*
* array(
* 'id' => 'default',
* 'title' => '...',
* 'description' => '...'
* );
*
* @return array
*/
public function getInfo()
{
$desc = Piwik::translate('UserCountry_DefaultLocationProviderDesc1') . ' ' . Piwik::translate('UserCountry_DefaultLocationProviderDesc2', array(
'<strong>',
'<em>',
'</em>',
'</strong>'
)) . '<p><em><a href="http://piwik.org/faq/how-to/#faq_163" rel="noreferrer" target="_blank">' . Piwik::translate('UserCountry_HowToInstallGeoIPDatabases') . '</em></a></p>';

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;
}
}