| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Tracker; |
|
|
| use Piwik\Cache; |
| use Piwik\CacheId; |
| 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 |
| { |
| |
| |
| |
| |
| public static $urlPrefixMap = array( |
| 'http://www.' => 1, |
| 'http://' => 0, |
| 'https://www.' => 3, |
| 'https://' => 2, |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function excludeQueryParametersFromUrl($originalUrl, $idSite, $additionalParametersToExclude = []) |
| { |
| $originalUrl = self::cleanupUrl($originalUrl); |
|
|
| $parsedUrl = @parse_url($originalUrl); |
| $parsedUrl = self::cleanupHostAndHashTag($parsedUrl, $idSite); |
| $parametersToExclude = array_merge(self::getQueryParametersToExclude($idSite), $additionalParametersToExclude); |
|
|
| if (empty($parsedUrl['query'])) { |
| if (empty($parsedUrl['fragment'])) { |
| return UrlHelper::getParseUrlReverse($parsedUrl); |
| } |
|
|
| |
| $queryParameters = UrlHelper::getArrayFromQueryString($parsedUrl['fragment']); |
| $parsedUrl['fragment'] = UrlHelper::getQueryStringWithExcludedParameters($queryParameters, $parametersToExclude); |
| $url = UrlHelper::getParseUrlReverse($parsedUrl); |
|
|
| return $url; |
| } |
|
|
| $queryParameters = UrlHelper::getArrayFromQueryString($parsedUrl['query']); |
| $parsedUrl['query'] = UrlHelper::getQueryStringWithExcludedParameters($queryParameters, $parametersToExclude); |
| $url = UrlHelper::getParseUrlReverse($parsedUrl); |
|
|
| return $url; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getQueryParametersToExclude($idSite) |
| { |
| $campaignTrackingParameters = Common::getCampaignParameters(); |
|
|
| $campaignTrackingParameters = array_merge( |
| $campaignTrackingParameters[0], // campaign name parameters |
| $campaignTrackingParameters[1] // campaign keyword parameters |
| ); |
|
|
| $website = TrackerCache::getCacheWebsiteAttributes($idSite); |
| $excludedParameters = self::getExcludedParametersFromWebsite($website); |
|
|
| $parametersToExclude = array_merge( |
| ['ignore_referrer', 'ignore_referer'], |
| $excludedParameters, |
| self::getUrlParameterNamesToExcludeFromUrl(), |
| $campaignTrackingParameters |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| Piwik::postEvent('Tracker.PageUrl.getQueryParametersToExclude', array(&$parametersToExclude)); |
|
|
| if (!empty($parametersToExclude)) { |
| Common::printDebug('Excluding parameters "' . implode(',', $parametersToExclude) . '" from URL'); |
| } |
|
|
| $parametersToExclude = array_map('strtolower', $parametersToExclude); |
| return $parametersToExclude; |
| } |
|
|
| |
| |
| |
| |
| |
| protected static function getUrlParameterNamesToExcludeFromUrl() |
| { |
| $paramsToExclude = Config::getInstance()->Tracker['url_query_parameter_to_exclude_from_url']; |
| $paramsToExclude = explode(",", $paramsToExclude); |
| $paramsToExclude = array_map('trim', $paramsToExclude); |
| return $paramsToExclude; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function shouldRemoveURLFragmentFor($idSite) |
| { |
| $websiteAttributes = TrackerCache::getCacheWebsiteAttributes($idSite); |
| return empty($websiteAttributes['keep_url_fragment']); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function processUrlFragment($urlFragment, $idSite = false) |
| { |
| |
| |
| if ( |
| $idSite !== false |
| && PageUrl::shouldRemoveURLFragmentFor($idSite) |
| ) { |
| return ''; |
| } else { |
| |
| if (substr($urlFragment, -1) == '#') { |
| $urlFragment = substr($urlFragment, 0, strlen($urlFragment) - 1); |
| } |
| return $urlFragment; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function cleanupHostAndHashTag($parsedUrl, $idSite = false) |
| { |
| if (empty($parsedUrl)) { |
| return $parsedUrl; |
| } |
|
|
| if (!empty($parsedUrl['host'])) { |
| $parsedUrl['host'] = mb_strtolower($parsedUrl['host']); |
| } |
|
|
| if (!empty($parsedUrl['fragment'])) { |
| $parsedUrl['fragment'] = PageUrl::processUrlFragment($parsedUrl['fragment'], $idSite); |
| } |
|
|
| return $parsedUrl; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public static function convertMatrixUrl($originalUrl) |
| { |
| $posFirstSemiColon = strpos($originalUrl, ";"); |
|
|
| if (false === $posFirstSemiColon) { |
| return $originalUrl; |
| } |
|
|
| $posQuestionMark = strpos($originalUrl, "?"); |
| $replace = (false === $posQuestionMark); |
|
|
| if ($posQuestionMark > $posFirstSemiColon) { |
| $originalUrl = substr_replace($originalUrl, ";", $posQuestionMark, 1); |
| $replace = true; |
| } |
|
|
| if ($replace) { |
| $originalUrl = substr_replace($originalUrl, "?", strpos($originalUrl, ";"), 1); |
| $originalUrl = str_replace(";", "&", $originalUrl); |
| } |
|
|
| return $originalUrl; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function cleanupString($string) |
| { |
| $string = trim($string); |
| $string = str_replace(array("\n", "\r", "\0"), '', $string); |
|
|
| $limit = Config::getInstance()->Tracker['page_maximum_length']; |
| $clean = substr($string, 0, $limit); |
| return $clean; |
| } |
|
|
| protected static function reencodeParameterValue($value, $encoding) |
| { |
| if (is_string($value)) { |
| $decoded = urldecode($value); |
| try { |
| if ( |
| function_exists('mb_check_encoding') |
| && @mb_check_encoding($decoded, $encoding) |
| ) { |
| $value = urlencode(mb_convert_encoding($decoded, 'UTF-8', $encoding)); |
| } |
| } catch (\Error $e) { |
| |
| |
| } |
| } |
|
|
| return $value; |
| } |
|
|
| protected static function reencodeParametersArray($queryParameters, $encoding) |
| { |
| foreach ($queryParameters as &$value) { |
| if (is_array($value)) { |
| $value = self::reencodeParametersArray($value, $encoding); |
| } else { |
| $value = PageUrl::reencodeParameterValue($value, $encoding); |
| } |
| } |
|
|
| return $queryParameters; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function reencodeParameters(&$queryParameters, $encoding = false) |
| { |
| if (function_exists('mb_check_encoding')) { |
| |
| |
| if ( |
| is_string($encoding) && |
| strtolower($encoding) !== 'utf-8' |
| ) { |
| Common::printDebug("Encoding page URL query parameters to $encoding."); |
|
|
| $queryParameters = PageUrl::reencodeParametersArray($queryParameters, $encoding); |
| } |
| } else { |
| Common::printDebug("Page charset supplied in tracking request, but mbstring extension is not available."); |
| } |
|
|
| return $queryParameters; |
| } |
|
|
| public static function cleanupUrl($url) |
| { |
| $url = Common::unsanitizeInputValue($url); |
| $url = PageUrl::cleanupString($url); |
| $url = PageUrl::convertMatrixUrl($url); |
|
|
| return $url; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function reconstructNormalizedUrl($url, $prefixId) |
| { |
| $map = array_flip(self::$urlPrefixMap); |
|
|
| if ($prefixId !== null && isset($map[$prefixId])) { |
| $fullUrl = $map[$prefixId] . $url; |
| } else { |
| $fullUrl = $url; |
| } |
|
|
| |
| $parsedUrl = @parse_url($fullUrl); |
| $parsedUrl = PageUrl::cleanupHostAndHashTag($parsedUrl); |
| $url = UrlHelper::getParseUrlReverse($parsedUrl); |
|
|
| if (!empty($url)) { |
| return $url; |
| } |
|
|
| return $fullUrl; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public static function shouldUseHttpsHost($idSite, $host) |
| { |
| $cache = Cache::getTransientCache(); |
|
|
| $cacheKeySiteUrls = CacheId::siteAware('siteurls', [$idSite]); |
| $cacheKeyHttpsForHost = CacheId::siteAware(sprintf('shouldusehttps-%s', $host), [$idSite]); |
|
|
| $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(mb_strtolower($siteUrl), mb_strtolower('https://' . $host)) === 0) { |
| $hostSiteCache = true; |
| break; |
| } |
| } |
|
|
| $cache->save($cacheKeyHttpsForHost, $hostSiteCache); |
| } |
|
|
| return $cache->fetch($cacheKeyHttpsForHost); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function normalizeUrl($url) |
| { |
| foreach (self::$urlPrefixMap as $prefix => $id) { |
| if (strtolower(substr($url, 0, strlen($prefix))) == $prefix) { |
| return array( |
| 'url' => substr($url, strlen($prefix)), |
| 'prefixId' => $id, |
| ); |
| } |
| } |
|
|
| return array('url' => $url, 'prefixId' => null); |
| } |
|
|
| public static function getUrlIfLookValid($url) |
| { |
| $url = PageUrl::cleanupString($url); |
|
|
| if (!UrlHelper::isLookLikeUrl($url)) { |
| Common::printDebug("WARNING: URL looks invalid and is discarded"); |
|
|
| return false; |
| } |
|
|
| return $url; |
| } |
|
|
| private static function getExcludedParametersFromWebsite($website) |
| { |
| if (isset($website['excluded_parameters'])) { |
| return $website['excluded_parameters']; |
| } |
|
|
| return array(); |
| } |
|
|
| public static function urldecodeValidUtf8($value) |
| { |
| $value = urldecode($value); |
| if ( |
| function_exists('mb_check_encoding') |
| && !@mb_check_encoding($value, 'utf-8') |
| ) { |
| return urlencode($value); |
| } |
| return $value; |
| } |
| } |
|
|