diff options
author | sgiehl <stefan@matomo.org> | 2020-02-24 11:18:50 +0300 |
---|---|---|
committer | sgiehl <stefan@matomo.org> | 2020-02-24 11:18:50 +0300 |
commit | 31dcb9dbb01460bf9a68dc8b7bc826a05c367f74 (patch) | |
tree | e7dbaf14fb8cf8e34f421df53e22fc88b1b7b0c2 /plugins | |
parent | 92e1b8bc5b8baa7683e4aa7a6abdd40064cd448b (diff) | |
parent | 7c517de3c74b8417230d813ae35e6b5eba06605e (diff) |
Merge branch '3.x-dev' into 4.x-dev
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/CoreVisualizations/Visualizations/HtmlTable.php | 8 | ||||
-rw-r--r-- | plugins/CustomJsTracker/TrackingCode/PluginTrackerFiles.php | 48 | ||||
-rw-r--r-- | plugins/CustomJsTracker/tests/Integration/PluginTrackerFilesTest.php | 87 | ||||
-rw-r--r-- | plugins/ExampleTracker/Columns/ExampleVisitDimension.php | 11 | ||||
-rw-r--r-- | plugins/ExampleTracker/ExampleTracker.php | 19 | ||||
-rw-r--r-- | plugins/ExampleTracker/VisitorDetails.php | 27 | ||||
-rw-r--r-- | plugins/ExampleTracker/templates/_visitorLogIcons.twig | 1 | ||||
-rw-r--r-- | plugins/ExampleTracker/tracker.js | 50 | ||||
-rw-r--r-- | plugins/GeoIp2/GeoIP2AutoUpdater.php | 38 | ||||
-rw-r--r-- | plugins/GeoIp2/LocationProvider/GeoIp2/Php.php | 27 | ||||
-rw-r--r-- | plugins/GeoIp2/tests/Integration/LocationProviderTest.php | 20 | ||||
-rw-r--r-- | plugins/GeoIp2/tests/Unit/GeoIP2AutoUpdaterTest.php | 149 | ||||
-rw-r--r-- | plugins/Overlay/Controller.php | 7 |
13 files changed, 375 insertions, 117 deletions
diff --git a/plugins/CoreVisualizations/Visualizations/HtmlTable.php b/plugins/CoreVisualizations/Visualizations/HtmlTable.php index 42c0798fbf..ba3bb6b98a 100644 --- a/plugins/CoreVisualizations/Visualizations/HtmlTable.php +++ b/plugins/CoreVisualizations/Visualizations/HtmlTable.php @@ -235,9 +235,11 @@ class HtmlTable extends Visualization $reportTotal = isset($totals[$column]) ? $totals[$column] : 0; - $percentageColumnName = $column . '_row_percentage'; - $rowPercentage = $formatter->formatPercent(Piwik::getPercentageSafe($value, $reportTotal, $precision = 1), $precision); - $row->setMetadata($percentageColumnName, $rowPercentage); + if (is_numeric($value)) { + $percentageColumnName = $column . '_row_percentage'; + $rowPercentage = $formatter->formatPercent(Piwik::getPercentageSafe($value, $reportTotal, $precision = 1), $precision); + $row->setMetadata($percentageColumnName, $rowPercentage); + } if ($siteTotalRow) { $siteTotal = $siteTotalRow->getColumn($column) ?: 0; diff --git a/plugins/CustomJsTracker/TrackingCode/PluginTrackerFiles.php b/plugins/CustomJsTracker/TrackingCode/PluginTrackerFiles.php index e062a73516..b35f89364b 100644 --- a/plugins/CustomJsTracker/TrackingCode/PluginTrackerFiles.php +++ b/plugins/CustomJsTracker/TrackingCode/PluginTrackerFiles.php @@ -17,11 +17,6 @@ class PluginTrackerFiles const MIN_TRACKER_FILE = 'tracker.min.js'; /** - * @var string - */ - protected $dir; - - /** * @var Plugin\Manager */ private $pluginManager; @@ -33,7 +28,6 @@ class PluginTrackerFiles public function __construct() { - $this->dir = PIWIK_DOCUMENT_ROOT . '/plugins/'; $this->pluginManager = Plugin\Manager::getInstance(); } @@ -42,6 +36,18 @@ class PluginTrackerFiles $this->ignoreMinified = true; } + protected function getDirectoriesToLook() + { + $dirs = array(); + $manager = Plugin\Manager::getInstance(); + foreach ($manager->getPluginsLoadedAndActivated() as $pluginName => $plugin) { + if ($plugin->isTrackerPlugin()) { + $dirs[$pluginName] = rtrim(Plugin\Manager::getPluginDirectory($pluginName), '/') . '/'; + } + } + return $dirs; + } + /** * @return File[] */ @@ -49,25 +55,11 @@ class PluginTrackerFiles { $jsFiles = array(); - if (!$this->ignoreMinified) { - $trackerFiles = \_glob($this->dir . '*/' . self::MIN_TRACKER_FILE); - - foreach ($trackerFiles as $trackerFile) { - $plugin = $this->getPluginNameFromFile($trackerFile); - if ($this->isPluginActivated($plugin)) { - $jsFiles[$plugin] = new File($trackerFile); - } - } - } - - $trackerFiles = \_glob($this->dir . '*/' . self::TRACKER_FILE); - - foreach ($trackerFiles as $trackerFile) { - $plugin = $this->getPluginNameFromFile($trackerFile); - if (!isset($jsFiles[$plugin])) { - if ($this->isPluginActivated($plugin)) { - $jsFiles[$plugin] = new File($trackerFile); - } + foreach ($this->getDirectoriesToLook() as $pluginName => $pluginDir) { + if (!$this->ignoreMinified && file_exists($pluginDir . self::MIN_TRACKER_FILE)) { + $jsFiles[$pluginName] = new File($pluginDir . self::MIN_TRACKER_FILE); + } elseif (file_exists($pluginDir . self::TRACKER_FILE)) { + $jsFiles[$pluginName] = new File($pluginDir . self::TRACKER_FILE); } } @@ -101,10 +93,4 @@ class PluginTrackerFiles { return $this->pluginManager->isPluginActivated($pluginName); } - - protected function getPluginNameFromFile($file) - { - $file = str_replace(array($this->dir, self::TRACKER_FILE, self::MIN_TRACKER_FILE), '', $file); - return trim($file, '/'); - } } diff --git a/plugins/CustomJsTracker/tests/Integration/PluginTrackerFilesTest.php b/plugins/CustomJsTracker/tests/Integration/PluginTrackerFilesTest.php index b063fbab28..7dab1b6032 100644 --- a/plugins/CustomJsTracker/tests/Integration/PluginTrackerFilesTest.php +++ b/plugins/CustomJsTracker/tests/Integration/PluginTrackerFilesTest.php @@ -14,33 +14,12 @@ use Piwik\Tests\Framework\TestCase\IntegrationTestCase; class CustomPluginTrackerFiles extends PluginTrackerFiles { - private $pluginNamesForFile = array(); - public function __construct($pluginNameForRegularTrackerFile = 'CustomJsTracker', $pluginNameForMinifiedTracker = 'CustomJsTracker') - { - parent::__construct(); - - $this->dir = PIWIK_DOCUMENT_ROOT . '/plugins/CustomJsTracker/tests/'; - - $this->pluginNamesForFile = array( - 'tracker.js' => $pluginNameForRegularTrackerFile, - 'tracker.min.js' => $pluginNameForMinifiedTracker + protected function getDirectoriesToLook() { + return array( + 'CustomJsTracker' => PIWIK_DOCUMENT_ROOT . '/plugins/CustomJsTracker/tests/resources/' ); } - - protected function getPluginNameFromFile($file) - { - $fileName = basename($file); - return $this->pluginNamesForFile[$fileName]; - } -} - -class CustomPluginTrackerFiles2 extends PluginTrackerFiles { - - public function getPluginNameFromFile($file) - { - return parent::getPluginNameFromFile($file); - } } /** @@ -72,72 +51,20 @@ class PluginTrackerFilesTest extends IntegrationTestCase $this->assertEquals('tracker.js', $foundFiles['CustomJsTracker']->getName()); } - public function test_find_ifMultiplePluginsImplementATracker_ShouldReturnEachOfThem() - { - $trackerFiles = new CustomPluginTrackerFiles('CustomJsTracker', 'Goals'); - $foundFiles = $trackerFiles->find(); - - $this->assertCount(2, $foundFiles); - $this->assertTrue(isset($foundFiles['CustomJsTracker'])); - $this->assertTrue(isset($foundFiles['Goals'])); - $this->assertEquals('tracker.js', $foundFiles['CustomJsTracker']->getName()); - $this->assertEquals('tracker.min.js', $foundFiles['Goals']->getName()); - } - public function test_find_EventsCanIgnoreFiles() { - $trackerFiles = new CustomPluginTrackerFiles('CustomJsTracker', 'Goals'); + $trackerFiles = new CustomPluginTrackerFiles(); $foundFiles = $trackerFiles->find(); - $this->assertCount(2, $foundFiles); + $this->assertCount(1, $foundFiles); Piwik::addAction('CustomJsTracker.shouldAddTrackerFile', function (&$shouldAdd, $pluginName) { - if ($pluginName === 'Goals') { + if ($pluginName === 'CustomJsTracker') { $shouldAdd = false; } }); $foundFiles = $trackerFiles->find(); - $this->assertCount(1, $foundFiles); - $this->assertTrue(isset($foundFiles['CustomJsTracker'])); - $this->assertFalse(isset($foundFiles['Goals'])); - } - - public function test_find_shouldNotReturnATrackerFile_IfPluginIsNotActivatedOrLoaded() - { - $trackerFiles = new CustomPluginTrackerFiles('MyNotExistingPlugin', 'Goals'); - $foundFiles = $trackerFiles->find(); - - $this->assertCount(1, $foundFiles); - $this->assertTrue(isset($foundFiles['Goals'])); - $this->assertEquals('tracker.min.js', $foundFiles['Goals']->getName()); - - $trackerFiles = new CustomPluginTrackerFiles('Goals', 'MyNotExistingPlugin'); - $foundFiles = $trackerFiles->find(); - - $this->assertCount(1, $foundFiles); - $this->assertTrue(isset($foundFiles['Goals'])); - $this->assertEquals('tracker.js', $foundFiles['Goals']->getName()); - } - - public function test_find_shouldNotReturnFileIfNoPluginActivated() - { - $trackerFiles = new CustomPluginTrackerFiles('MyNotExistingPlugin', 'MyNotExistingPlugin2'); - $foundFiles = $trackerFiles->find(); - - $this->assertSame(array(), $foundFiles); - } - - public function test_getPluginNameFromFile_shouldDetectPluginName() - { - $trackerFiles = new CustomPluginTrackerFiles2(); - $pluginName = $trackerFiles->getPluginNameFromFile(PIWIK_DOCUMENT_ROOT . '/plugins/MyFooBarPlugin/tracker.js'); - $this->assertSame('MyFooBarPlugin', $pluginName); - - $pluginName = $trackerFiles->getPluginNameFromFile(PIWIK_DOCUMENT_ROOT . '/plugins//MyFooBarPlugin//tracker.js'); - $this->assertSame('MyFooBarPlugin', $pluginName); - - $pluginName = $trackerFiles->getPluginNameFromFile(PIWIK_DOCUMENT_ROOT . '/plugins//MyFooBarPlugin//tracker.min.js'); - $this->assertSame('MyFooBarPlugin', $pluginName); + $this->assertCount(0, $foundFiles); } } diff --git a/plugins/ExampleTracker/Columns/ExampleVisitDimension.php b/plugins/ExampleTracker/Columns/ExampleVisitDimension.php index f153e4faca..be294660d5 100644 --- a/plugins/ExampleTracker/Columns/ExampleVisitDimension.php +++ b/plugins/ExampleTracker/Columns/ExampleVisitDimension.php @@ -8,6 +8,7 @@ */ namespace Piwik\Plugins\ExampleTracker\Columns; +use Piwik\Common; use Piwik\Piwik; use Piwik\Plugin\Dimension\VisitDimension; use Piwik\Plugin\Segment; @@ -76,6 +77,11 @@ class ExampleVisitDimension extends VisitDimension */ public function onNewVisit(Request $request, Visitor $visitor, $action) { + $paramValue = Common::getRequestVar('myCustomVisitParam', '', 'string', $request->getParams()); + if (!empty($paramValue)) { + return $paramValue; + } + if (empty($action)) { return 0; } @@ -100,6 +106,11 @@ class ExampleVisitDimension extends VisitDimension */ public function onExistingVisit(Request $request, Visitor $visitor, $action) { + $paramValue = Common::getRequestVar('myCustomVisitParam', '', 'string', $request->getParams()); + if (!empty($paramValue)) { + return $paramValue; + } + if (empty($action)) { return false; // Do not change an already persisted value } diff --git a/plugins/ExampleTracker/ExampleTracker.php b/plugins/ExampleTracker/ExampleTracker.php index 22af517aaa..1eb40ce15d 100644 --- a/plugins/ExampleTracker/ExampleTracker.php +++ b/plugins/ExampleTracker/ExampleTracker.php @@ -8,6 +8,25 @@ */ namespace Piwik\Plugins\ExampleTracker; +use Piwik\Common; +use Piwik\Plugins\Live\Visitor; + class ExampleTracker extends \Piwik\Plugin { + public function getListHooksRegistered() + { + return [ + 'Live.getAllVisitorDetails' => 'getAllVisitorDetails', + ]; + } + + public function isTrackerPlugin() + { + return true; + } + + public function getAllVisitorDetails(&$visitor, $visitorRawData) + { + $visitor['myCustomVisitParam'] = isset($visitorRawData['example_visit_dimension']) ? $visitorRawData['example_visit_dimension'] : 'no-value'; + } } diff --git a/plugins/ExampleTracker/VisitorDetails.php b/plugins/ExampleTracker/VisitorDetails.php new file mode 100644 index 0000000000..530fbf0287 --- /dev/null +++ b/plugins/ExampleTracker/VisitorDetails.php @@ -0,0 +1,27 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\ExampleTracker; + +use Piwik\Plugins\Live\VisitorDetailsAbstract; +use Piwik\View; + +class VisitorDetails extends VisitorDetailsAbstract +{ + public function renderIcons($visitorDetails) + { + if (empty($visitorDetails['myCustomVisitParam'])) { + return ''; + } + + $view = new View('@ExampleTracker/_visitorLogIcons'); + $view->myCustomVisitParam = $visitorDetails['myCustomVisitParam']; + return $view->render(); + } +}
\ No newline at end of file diff --git a/plugins/ExampleTracker/templates/_visitorLogIcons.twig b/plugins/ExampleTracker/templates/_visitorLogIcons.twig new file mode 100644 index 0000000000..c582382f9a --- /dev/null +++ b/plugins/ExampleTracker/templates/_visitorLogIcons.twig @@ -0,0 +1 @@ +<span>{{ myCustomVisitParam }}</span>
\ No newline at end of file diff --git a/plugins/ExampleTracker/tracker.js b/plugins/ExampleTracker/tracker.js new file mode 100644 index 0000000000..0383efc9e1 --- /dev/null +++ b/plugins/ExampleTracker/tracker.js @@ -0,0 +1,50 @@ +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +(function () { + + var configs = {}; + + function init() { + if ('object' === typeof window && 'object' === typeof window.Piwik && 'object' === typeof window.Piwik.ExampleTracker) { + // do not initialize twice + return; + } + + if ('object' === typeof window && !window.Piwik) { + // piwik is not defined yet + return; + } + + Piwik.ExampleTracker = { + // empty + }; + + Piwik.addPlugin('ExampleTracker', { + log: function (eventParams) { + if (!eventParams || !eventParams.tracker) { + return ''; + } + + return '&myCustomVisitParam=' + 500 + eventParams.tracker.getSiteId(); + }, + }); + } + + if ('object' === typeof window.Piwik) { + init(); + } else { + // tracker is loaded separately for sure + if ('object' !== typeof window.piwikPluginAsyncInit) { + window.piwikPluginAsyncInit = []; + } + + window.piwikPluginAsyncInit.push(init); + } + +})();
\ No newline at end of file diff --git a/plugins/GeoIp2/GeoIP2AutoUpdater.php b/plugins/GeoIp2/GeoIP2AutoUpdater.php index 4af0e8c4b2..423256be83 100644 --- a/plugins/GeoIp2/GeoIP2AutoUpdater.php +++ b/plugins/GeoIp2/GeoIP2AutoUpdater.php @@ -130,7 +130,10 @@ class GeoIP2AutoUpdater extends Task $logger = StaticContainer::get(LoggerInterface::class); $url = trim($url); - if ($this->isDbIpUrl($url)) { + + if (self::isPaidDbIpUrl($url)) { + $url = $this->fetchPaidDbIpUrl($url); + } else if (self::isDbIpUrl($url)) { $url = $this->getDbIpUrlWithLatestDate($url); } @@ -731,7 +734,7 @@ class GeoIP2AutoUpdater extends Task return LocationProviderGeoIp2::$dbNames[$dbType][0] . '.' . $ext; } - private function getDbIpUrlWithLatestDate($url) + protected function getDbIpUrlWithLatestDate($url) { $today = Date::today(); return preg_replace('/-\d{4}-\d{2}\./', '-' . $today->toString('Y-m') . '.', $url); @@ -741,4 +744,35 @@ class GeoIP2AutoUpdater extends Task { return !! preg_match('/db-ip\.com/', $url); } + + protected static function isPaidDbIpUrl($url) + { + return !! preg_match('/db-ip\.com\/account\/[0-9a-z]+\/db/', $url); + } + + protected function fetchPaidDbIpUrl($url) + { + $content = trim($this->fetchUrl($url)); + + if (0 === strpos($content, 'http')) { + return $content; + } + + $content = json_decode($content, true); + + if (!empty($content['mmdb']['url'])) { + return $content['mmdb']['url']; + } + + if (!empty($content['url'])) { + return $content['url']; + } + + throw new Exception('Unable to determine download url'); + } + + protected function fetchUrl($url) + { + return Http::fetchRemoteFile($url); + } } diff --git a/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php b/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php index f2c145e35e..310a273dc4 100644 --- a/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php +++ b/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php @@ -11,6 +11,7 @@ namespace Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2; use GeoIp2\Database\Reader; use GeoIp2\Exception\AddressNotFoundException; use MaxMind\Db\Reader\InvalidDatabaseException; +use Piwik\Common; use Piwik\Log; use Piwik\Piwik; use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2; @@ -238,11 +239,35 @@ class Php extends GeoIp2 if (is_array($lookupResult->subdivisions) && count($lookupResult->subdivisions) > 0) { $subdivisions = $lookupResult->subdivisions; $subdivision = $this->determinSubdivision($subdivisions, $result[self::COUNTRY_CODE_KEY]); - $result[self::REGION_CODE_KEY] = strtoupper($subdivision->isoCode); + $result[self::REGION_CODE_KEY] = strtoupper($subdivision->isoCode) ?: $this->determineRegionIsoCodeByNameAndCountryCode($subdivision->name, $result[self::COUNTRY_CODE_KEY]); $result[self::REGION_NAME_KEY] = $subdivision->name; } } + /** + * Try to determine the ISO region code based on the region name and country code + * + * @param string $regionName + * @param string $countryCode + * @return string + */ + protected function determineRegionIsoCodeByNameAndCountryCode($regionName, $countryCode) + { + $regionNames = self::getRegionNames(); + + if (empty($regionNames[$countryCode])) { + return ''; + } + + foreach ($regionNames[$countryCode] as $isoCode => $name) { + if (Common::mb_strtolower($name) === Common::mb_strtolower($regionName)) { + return $isoCode; + } + } + + return ''; + } + protected function determinSubdivision($subdivisions, $countryCode) { if (in_array($countryCode, ['GB'])) { diff --git a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php index bb681f7704..86d9ae5529 100644 --- a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php +++ b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php @@ -34,6 +34,26 @@ class LocationProviderTest extends \PHPUnit\Framework\TestCase ], $result); } + public function testGeoIP2CityWithoutRegionIsoCode() + { + // The IP 99.99.99.99 will only return a region name, based on that the region code should be determined + $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]); + $result = $locationProvider->getLocation(['ip' => '99.99.99.99']); + + $this->assertEquals([ + 'continent_name' => 'North America', + 'continent_code' => 'NA', + 'country_code' => 'US', + 'country_name' => 'United States', + 'city_name' => 'Englewood Cliffs', + 'lat' => 40.892, + 'long' => -73.947, + 'postal_code' => null, + 'region_code' => 'NJ', + 'region_name' => 'New Jersey', + ], $result); + } + public function testGeoIP2Country() { $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-Country.mmdb'], 'isp' => []]); diff --git a/plugins/GeoIp2/tests/Unit/GeoIP2AutoUpdaterTest.php b/plugins/GeoIp2/tests/Unit/GeoIP2AutoUpdaterTest.php new file mode 100644 index 0000000000..ce68ee14af --- /dev/null +++ b/plugins/GeoIp2/tests/Unit/GeoIP2AutoUpdaterTest.php @@ -0,0 +1,149 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\GeoIp2\tests\Unit; + +use Piwik\DataTable; +use Piwik\DataTable\Row; +use Piwik\Plugins\GeoIp2\GeoIP2AutoUpdater; + +class public_GeoIP2AutoUpdater extends GeoIP2AutoUpdater +{ + public static function isPaidDbIpUrl($url) + { + return parent::isPaidDbIpUrl($url); + } + + public function fetchPaidDbIpUrl($url) + { + return parent::fetchPaidDbIpUrl($url); + } +} + +/** + * @group GeoIP2AutoUpdater + */ +class GeoIP2AutoUpdaterTest extends \PHPUnit\Framework\TestCase +{ + /** + * @dataProvider getPaidDbTestUrls + */ + public function testIsPaidDbIpUrl($expected, $url) + { + $this->assertEquals($expected, public_GeoIP2AutoUpdater::isPaidDbIpUrl($url)); + } + + public function getPaidDbTestUrls() + { + return [ + [true, 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-country/'], + [true, 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-location/mmdb/'], + [true, 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-location-isp/mmdb/url'], + [false, 'https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb'], + [false, 'https://download.db-ip.com/free/dbip-country-lite-2020-02.mmdb.gz'], + ]; + } + + /** + * @dataProvider getDbTestUrls + */ + public function testIsDbIpUrl($expected, $url) + { + $this->assertEquals($expected, GeoIP2AutoUpdater::isDbIpUrl($url)); + } + + public function getDbTestUrls() + { + return [ + [true, 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-country/'], + [true, 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-location/mmdb/'], + [true, 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-location-isp/mmdb/url'], + [true, 'https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb'], + [true, 'https://download.db-ip.com/free/dbip-country-lite-2020-02.mmdb.gz'], + [false, 'https://dbip.com/free/dbip-country-lite-2020-02.mmdb.gz'], + ]; + } + + public function testFetchPaidUrlForPlainUrl() + { + $url = 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-country/mmdb/url'; + + $mock = $this->getMockBuilder(public_GeoIP2AutoUpdater::class) + ->setMethods(['fetchUrl']) + ->disableOriginalConstructor() + ->getMock(); + + $mock->expects($this->once())->method('fetchUrl')->with($url)->willReturn('https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb'); + + $determinedUrl = $mock->fetchPaidDbIpUrl($url); + + $this->assertEquals('https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb', $determinedUrl); + } + + public function testFetchPaidUrlForMmdbJson() + { + $url = 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-country/mmdb'; + + $mock = $this->getMockBuilder(public_GeoIP2AutoUpdater::class) + ->setMethods(['fetchUrl']) + ->disableOriginalConstructor() + ->getMock(); + + $mock->expects($this->once())->method('fetchUrl')->with($url)->willReturn(' +{ + "url": "https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb", + "name": "dbip-country-2020-02-22.mmdb.gz", + "date": "February 22nd 2020", + "size": 8807222, + "rows": 808624, + "md5sum": "dd8250ca45ad42dd5c3a63670ff46968", + "sha1sum": "062615e15dd1496ac9fddc311231efa2d75f09d6" +}'); + + $determinedUrl = $mock->fetchPaidDbIpUrl($url); + + $this->assertEquals('https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb', $determinedUrl); + } + + public function testFetchPaidUrlForFullJson() + { + $url = 'https://db-ip.com/account/ad446bf4cb9a44e4fff3f215deabc710f12f3/db/ip-to-country'; + + $mock = $this->getMockBuilder(public_GeoIP2AutoUpdater::class) + ->setMethods(['fetchUrl']) + ->disableOriginalConstructor() + ->getMock(); + + $mock->expects($this->once())->method('fetchUrl')->with($url)->willReturn(' +{ + "csv": { + "url": "https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.csv", + "name": "dbip-country-2020-02-22.csv.gz", + "date": "February 22nd 2020", + "size": 35113592, + "rows": 808314, + "md5sum": "22cd9abcc07e6b5c1c0fe89eef4503e2", + "sha1sum": "d3cc6e7ed30cc58abcc77ae73318d63af2687b06", + "version": 3 + }, + "mmdb": { + "url": "https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb", + "name": "dbip-country-2020-02-22.mmdb.gz", + "date": "February 22nd 2020", + "size": 8807222, + "rows": 808314, + "md5sum": "dd8250ca45ad42c55abc63670ff46968", + "sha1sum": "062615e15e01496ac9fddabc1231efa2d75f09d6" + } +}'); + + $determinedUrl = $mock->fetchPaidDbIpUrl($url); + + $this->assertEquals('https://download.db-ip.com/key/ad446bf4cb9a44e4fff3f215deabc710f12f3.mmdb', $determinedUrl); + } +} diff --git a/plugins/Overlay/Controller.php b/plugins/Overlay/Controller.php index 98ff9937d5..d7d42fa48e 100644 --- a/plugins/Overlay/Controller.php +++ b/plugins/Overlay/Controller.php @@ -20,6 +20,7 @@ use Piwik\Plugins\SegmentEditor\SegmentFormatter; use Piwik\Plugins\SitesManager\API as APISitesManager; use Piwik\ProxyHttp; use Piwik\Segment; +use Piwik\Session; use Piwik\Tracker\Action; use Piwik\Tracker\PageUrl; use Piwik\View; @@ -43,6 +44,12 @@ class Controller extends \Piwik\Plugin\Controller { Piwik::checkUserHasViewAccess($this->idSite); + // Overlay needs to send requests w/ the session cookie from within the tracked website, which means + // we can't use SameSite=Lax. So, we regenerate the session ID here (in Session.php there is a hardcoded + // check for Overlay, so will be set to SameSite=None). + // Note: this means the new session ID will have SameSite=None until it regenerates on a non-Overlay page. + Session::regenerateId(); + $template = '@Overlay/index'; if (Config::getInstance()->General['overlay_disable_framed_mode']) { $template = '@Overlay/index_noframe'; |