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

Use https for urls in visitor details if host is defined with https in site #17151

Merged
merged 7 commits into from Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 43 additions & 2 deletions core/Tracker/PageUrl.php
Expand Up @@ -9,9 +9,12 @@

namespace Piwik\Tracker;

use Piwik\Cache;
use Piwik\Tracker\Cache as TrackerCache;
use Piwik\Common;
use Piwik\Config;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\UrlHelper;

class PageUrl
Expand Down Expand Up @@ -80,7 +83,7 @@ public static function getQueryParametersToExclude($idSite)
$campaignTrackingParameters[1] // campaign keyword parameters
);

$website = Cache::getCacheWebsiteAttributes($idSite);
$website = TrackerCache::getCacheWebsiteAttributes($idSite);
$excludedParameters = self::getExcludedParametersFromWebsite($website);

$parametersToExclude = array_merge($excludedParameters,
Expand Down Expand Up @@ -127,7 +130,7 @@ protected static function getUrlParameterNamesToExcludeFromUrl()
*/
public static function shouldRemoveURLFragmentFor($idSite)
{
$websiteAttributes = Cache::getCacheWebsiteAttributes($idSite);
$websiteAttributes = TrackerCache::getCacheWebsiteAttributes($idSite);
return empty($websiteAttributes['keep_url_fragment']);
}

Expand Down Expand Up @@ -335,6 +338,44 @@ public static function reconstructNormalizedUrl($url, $prefixId)
return $fullUrl;
}

/**
* Returns if the given host is also configured as https in page urls of given site
*
* @param $idSite
* @param $host
* @return false|mixed
* @throws \Exception
*/
public static function shouldUseHttpsHost($idSite, $host)
{
$cache = Cache::getTransientCache();

$cacheKeySiteUrls = sprintf('siteurls-%s', $idSite);
sgiehl marked this conversation as resolved.
Show resolved Hide resolved
$cacheKeyHttpsForHost = sprintf('shouldusehttps-%s-%s', $idSite, $host);

$siteUrlCache = $cache->fetch($cacheKeySiteUrls);

if (empty($siteUrlCache)) {
$siteUrlCache = APISitesManager::getInstance()->getSiteUrlsFromId($idSite);
$cache->save($cacheKeySiteUrls, $siteUrlCache);
}

if (!$cache->contains($cacheKeyHttpsForHost)) {
$hostSiteCache = false;

foreach ($siteUrlCache as $siteUrl) {
if (strpos(strtolower($siteUrl), strtolower('https://' . $host)) === 0) {
$hostSiteCache = true;
break;
}
}

$cache->save($cacheKeyHttpsForHost, $hostSiteCache);
}

return $cache->fetch($cacheKeyHttpsForHost);
}

/**
* Extract the prefix from a URL.
* Return the prefix ID and the rest.
Expand Down
9 changes: 9 additions & 0 deletions plugins/Actions/DataTable/Filter/Actions.php
Expand Up @@ -15,6 +15,7 @@
use Piwik\DataTable;
use Piwik\Plugins\Actions\ArchivingHelper;
use Piwik\Tracker\Action;
use Piwik\Tracker\PageUrl;

class Actions extends BaseFilter
{
Expand Down Expand Up @@ -66,6 +67,14 @@ public function filter($table)
$label = $row->getColumn('label');
if ($url) {
$row->setMetadata('segmentValue', urlencode($url));

if ($site && strpos($url, 'http://') === 0) {
$host = parse_url($url, PHP_URL_HOST);

if ($host && PageUrl::shouldUseHttpsHost($site->getId(), $host)) {
$row->setMetadata('url', 'https://' . Common::mb_substr($url, 7 /* = strlen('http://') */));
sgiehl marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else if ($folderUrlStart) {
$row->setMetadata('segment', 'pageUrl=^' . urlencode(urlencode($folderUrlStart)));
} else if ($pageTitlePath) {
Expand Down
10 changes: 10 additions & 0 deletions plugins/Actions/VisitorDetails.php
Expand Up @@ -8,6 +8,7 @@
*/
namespace Piwik\Plugins\Actions;

use Piwik\Cache;
use Piwik\Common;
use Piwik\Config;
use Piwik\Date;
Expand All @@ -16,6 +17,7 @@
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\Live\VisitorDetailsAbstract;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Site;
use Piwik\Tracker\Action;
use Piwik\Tracker\PageUrl;
Expand Down Expand Up @@ -160,6 +162,14 @@ public function extendActionDetails(&$action, $nextAction, $visitorDetails)
unset($action['url_prefix']);
}

if (array_key_exists('url', $action) && strpos($action['url'], 'http://') === 0) {
$host = parse_url($action['url'], PHP_URL_HOST);

if ($host && PageUrl::shouldUseHttpsHost($visitorDetails['idSite'], $host)) {
$action['url'] = 'https://' . Common::mb_substr($action['url'], 7 /* = strlen('http://') */);
}
}
sgiehl marked this conversation as resolved.
Show resolved Hide resolved

switch ($action['type']) {
case 'goal':
$action['icon'] = 'plugins/Morpheus/images/goal.png';
Expand Down