Skip to content

Commit

Permalink
Merge pull request #29 from ThaDafinser/feature/piwik3
Browse files Browse the repository at this point in the history
piwik3
  • Loading branch information
ThaDafinser authored Mar 8, 2017
2 parents 84f247a + 360b17c commit 36d2320
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 36 deletions.
36 changes: 36 additions & 0 deletions Columns/AbstractBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IntranetGeoIP\Columns;

use Piwik\Tracker\Visitor;
use Piwik\Tracker\Request;
use Piwik\Plugins\UserCountry\Columns\Base as UserCountryBase;
use Piwik\Plugins\IntranetGeoIP\IntranetGeoIP;

abstract class AbstractBase extends UserCountryBase
{

public function onNewVisit(Request $request, Visitor $visitor, $action)
{
$userInfo = $this->getUserInfo($request, $visitor);

return $this->getLocationIntranetColumn($userInfo, $this->columnName);
}

protected function getLocationIntranetColumn(array $userInfo, $locationKey)
{
$result = IntranetGeoIP::getResult($userInfo);

if (isset($result[$locationKey])) {
return $result[$locationKey];
}

return;
}
}
9 changes: 9 additions & 0 deletions Columns/OverwriteCity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP\Columns;

class OverwriteCity extends AbstractBase
{

protected $columnName = 'location_city';
// protected $columnType = 'varchar(255) DEFAULT NULL';
}
9 changes: 9 additions & 0 deletions Columns/OverwriteCountry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP\Columns;

class OverwriteCountry extends AbstractBase
{

protected $columnName = 'location_country';
// protected $columnType = 'CHAR(3) NULL';
}
9 changes: 9 additions & 0 deletions Columns/OverwriteLatitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP\Columns;

class OverwriteLatitude extends AbstractBase
{

protected $columnName = 'location_latitude';
// protected $columnType = 'decimal(9, 6) DEFAULT NULL';
}
9 changes: 9 additions & 0 deletions Columns/OverwriteLongitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP\Columns;

class OverwriteLongitude extends AbstractBase
{

protected $columnName = 'location_longitude';
// protected $columnType = 'decimal(9, 6) DEFAULT NULL';
}
22 changes: 22 additions & 0 deletions Columns/OverwriteProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP\Columns;

use Piwik\Plugin\Manager;
use Piwik\Tracker\Visitor;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;

class OverwriteProvider extends AbstractBase
{

protected $columnName = 'location_provider';

public function onNewVisit(Request $request, Visitor $visitor, $action)
{
if (! Manager::getInstance()->isPluginInstalled('Provider')) {
return false;
}

return parent::onNewVisit($request, $visitor, $action);
}
}
9 changes: 9 additions & 0 deletions Columns/OverwriteRegion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Piwik\Plugins\IntranetGeoIP\Columns;

class OverwriteRegion extends AbstractBase
{

protected $columnName = 'location_region';
// protected $columnType = 'char(2) DEFAULT NULL';
}
85 changes: 66 additions & 19 deletions IntranetGeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
class IntranetGeoIP extends Plugin
{

const DATA_EXAMPLE_FILE_PATH = __DIR__ . '/data.example.php';

const DATA_FILE_PATH = PIWIK_INCLUDE_PATH . '/config/IntranetGeoIP.data.php';

private static $result = [];

/**
*
* @return string
*/
private function getDataExampleFilePath()
{
return __DIR__ . '/data.example.php';
return self::DATA_EXAMPLE_FILE_PATH;
}

/**
Expand All @@ -29,27 +35,27 @@ private function getDataExampleFilePath()
*/
private function getDataFilePath()
{
return PIWIK_INCLUDE_PATH . '/config/IntranetGeoIP.data.php';
return self::DATA_FILE_PATH;
}

/**
*
* @see Piwik\Plugin::getListHooksRegistered
* @see \Piwik\Plugin::install()
*/
public function getListHooksRegistered()
public function install()
{
return array(
'Tracker.newVisitorInformation' => 'logIntranetSubNetworkInfo'
);
return $this->copyDataFile();
}

/**
*
* @see \Piwik\Plugin::install()
* @see \Piwik\Plugin::uninstall()
*/
public function install()
public function uninstall()
{
return $this->copyDataFile();
if (file_exists($this->getDataFilePath())) {
unlink($this->getDataFilePath());
}
}

/**
Expand All @@ -75,21 +81,62 @@ private function copyDataFile()
return;
}

/**
*
* @see \Piwik\Plugin::uninstall()
*/
public function uninstall()
public static function getResult(array $userInfo)
{
if (file_exists($this->getDataFilePath())) {
unlink($this->getDataFilePath());
if (!array_key_exists($userInfo['ip'], self::$result)) {

self::$result[$userInfo['ip']] = self::getNewResult($userInfo);
}

return self::$result[$userInfo['ip']];
}

private static function getNewResult(array $userInfo)
{
if (! file_exists(IntranetGeoIP::DATA_FILE_PATH)) {
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . IntranetGeoIP::DATA_FILE_PATH);
return [];
}

$data = include IntranetGeoIP::DATA_FILE_PATH;
if ($data === false) {
// no data file found
// @todo ...inform the user/ log something
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . IntranetGeoIP::DATA_FILE_PATH);
return [];
}
if (! is_array($data)) {
Log::error('Your data file seems to be not valid. The content is: ' . print_r($data, true) . ', File used: ' . IntranetGeoIP::DATA_FILE_PATH);
return [];
}

$ip = Network\IP::fromStringIP($userInfo['ip']);

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'])) {
return $value['visitorInfo'];
}

return [];
}
}

// if nothing was matched, you can define default values if you want to
if (isset($data['noMatch']) && isset($data['noMatch']['visitorInfo'])) {
return $data['noMatch']['visitorInfo'];
}

return [];
}

/**
* Called by event `Tracker.newVisitorInformation`
*
* @see getListHooksRegistered()
* @see registerEvents()
*/
public function logIntranetSubNetworkInfo(&$visitorInfo, TrackerRequest $request)
{
Expand All @@ -105,7 +152,7 @@ public function logIntranetSubNetworkInfo(&$visitorInfo, TrackerRequest $request
Log::error('Plugin IntranetGeoIP does not work. File is missing: ' . $this->getDataFilePath());
return;
}
if(!is_array($data)){
if (! is_array($data)) {
Log::error('Your data file seems to be not valid. The content is: ' . print_r($data, true) . ', File used: ' . $this->getDataFilePath());
return;
}
Expand Down
2 changes: 1 addition & 1 deletion data.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'noMatch' => [
'visitorInfo' => [
// Provider requires the "Provider" Plugin to be active. (Disabled by default in Version 2.15 and above)
//'location_provider' => 'unknown'
//'location_country' => 'at',
]
],

Expand Down
32 changes: 16 additions & 16 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "IntranetGeoIP",
"version": "2.3.2",
"description": "Piwik plugin to locate all locale data of a user based on the IP address/subnetwork (country, region, city, latitude, longitude, provider, ...)",
"keywords": ["Subnet", "Intranet", "IPv4", "IPv6", "GeoIP", "ThaDafinser"],
"license": "GPL v3",
"homepage": "https://github.com/ThaDafinser/Piwik-IntranetGeoIP",
"authors": [
{"name": "Martin Keckeis", "homepage": "https://github.com/thadafinser"}
],
"theme": false,
"require": {
"piwik": ">=2.15.0,<3.0.0-b1",
"php": ">=5.6.0"
}
}
{
"name": "IntranetGeoIP",
"version": "3.0.0",
"description": "Piwik plugin to locate all locale data of a user based on the IP address/subnetwork (country, region, city, latitude, longitude, provider, ...)",
"keywords": ["Subnet", "Intranet", "IPv4", "IPv6", "GeoIP", "ThaDafinser"],
"license": "GPL v3",
"homepage": "https://github.com/ThaDafinser/Piwik-IntranetGeoIP",
"authors": [
{"name": "Martin Keckeis", "homepage": "https://github.com/thadafinser"}
],
"theme": false,
"require": {
"piwik": ">=2.16.0,<4.0.0-b1",
"php": ">=5.6.0"
}
}

0 comments on commit 36d2320

Please sign in to comment.