Calls mb_strtolower if available and falls back to strtolower if not.
public static mb_strtolower ( string $string ) : string | ||
$string | string | |
Résultat | string |
private function getTranslationForComparison($operand, $segmentType) { $operator = $operand[SegmentExpression::INDEX_OPERAND_OPERATOR]; $translation = $operator; if ($operator === SegmentExpression::MATCH_IS_NULL_OR_EMPTY) { return Piwik::translate('SegmentEditor_SegmentOperatorIsNullOrEmpty'); } if ($operator === SegmentExpression::MATCH_IS_NOT_NULL_NOR_EMPTY) { return Piwik::translate('SegmentEditor_SegmentOperatorIsNotNullNorEmpty'); } if ($segmentType === 'dimension' && !empty($this->matchesDimension[$operator])) { $translation = Piwik::translate($this->matchesDimension[$operator]); } if ($segmentType === 'metric' && !empty($this->matchesMetric[$operator])) { $translation = Piwik::translate($this->matchesMetric[$operator]); } return Common::mb_strtolower($translation); }
/** * Constructor. */ public function __construct($idSite = false) { parent::__construct(); $this->jsClass = "SegmentSelectorControl"; $this->cssIdentifier = "segmentEditorPanel"; $this->cssClass = "piwikTopControl borderedControl piwikSelector"; $this->idSite = $idSite ?: Common::getRequestVar('idSite', false, 'int'); $this->selectedSegment = Common::getRequestVar('segment', false, 'string'); $formatter = StaticContainer::get('Piwik\\Plugins\\SegmentEditor\\SegmentFormatter'); $this->segmentDescription = $formatter->getHumanReadable(Request::getRawSegmentFromRequest(), $this->idSite); $this->isAddingSegmentsForAllWebsitesEnabled = SegmentEditor::isAddingSegmentsForAllWebsitesEnabled(); $segments = APIMetadata::getInstance()->getSegmentsMetadata($this->idSite); $visitTitle = Piwik::translate('General_Visit'); $segmentsByCategory = array(); foreach ($segments as $segment) { if ($segment['category'] == $visitTitle && ($segment['type'] == 'metric' && $segment['segment'] != 'visitIp')) { $metricsLabel = Piwik::translate('General_Metrics'); $metricsLabel[0] = Common::mb_strtolower($metricsLabel[0]); $segment['category'] .= ' (' . $metricsLabel . ')'; } $segmentsByCategory[$segment['category']][] = $segment; } $this->createRealTimeSegmentsIsEnabled = Config::getInstance()->General['enable_create_realtime_segments']; $this->segmentsByCategory = $segmentsByCategory; $this->nameOfCurrentSegment = ''; $this->isSegmentNotAppliedBecauseBrowserArchivingIsDisabled = 0; $this->availableSegments = API::getInstance()->getAll($this->idSite); foreach ($this->availableSegments as &$savedSegment) { $savedSegment['name'] = Common::sanitizeInputValue($savedSegment['name']); if (!empty($this->selectedSegment) && $this->selectedSegment == $savedSegment['definition']) { $this->nameOfCurrentSegment = $savedSegment['name']; $this->isSegmentNotAppliedBecauseBrowserArchivingIsDisabled = $this->wouldApplySegment($savedSegment) ? 0 : 1; } } $this->authorizedToCreateSegments = SegmentEditorAPI::getInstance()->isUserCanAddNewSegment($this->idSite); $this->isUserAnonymous = Piwik::isUserIsAnonymous(); $this->segmentTranslations = $this->getTranslations(); $this->segmentProcessedOnRequest = Rules::isBrowserArchivingAvailableForSegments(); $this->hideSegmentDefinitionChangeMessage = UsersManagerAPI::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), 'hideSegmentDefinitionChangeMessage'); }
/** * Extracts a keyword from a raw not encoded URL. * Will only extract keyword if a known search engine has been detected. * Returns the keyword: * - in UTF8: automatically converted from other charsets when applicable * - strtolowered: "QUErY test!" will return "query test!" * - trimmed: extra spaces before and after are removed * * Lists of supported search engines can be found in /core/DataFiles/SearchEngines.php * The function returns false when a keyword couldn't be found. * eg. if the url is "http://www.google.com/partners.html" this will return false, * as the google keyword parameter couldn't be found. * * @see unit tests in /tests/core/Common.test.php * @param string $referrerUrl URL referrer URL, eg. $_SERVER['HTTP_REFERER'] * @return array|bool false if a keyword couldn't be extracted, * or array( * 'name' => 'Google', * 'keywords' => 'my searched keywords') */ public static function extractSearchEngineInformationFromUrl($referrerUrl) { $referrerParsed = @parse_url($referrerUrl); $referrerHost = ''; if (isset($referrerParsed['host'])) { $referrerHost = $referrerParsed['host']; } if (empty($referrerHost)) { return false; } // some search engines (eg. Bing Images) use the same domain // as an existing search engine (eg. Bing), we must also use the url path $referrerPath = ''; if (isset($referrerParsed['path'])) { $referrerPath = $referrerParsed['path']; } // no search query if (!isset($referrerParsed['query'])) { $referrerParsed['query'] = ''; } $query = $referrerParsed['query']; // Google Referrers URLs sometimes have the fragment which contains the keyword if (!empty($referrerParsed['fragment'])) { $query .= '&' . $referrerParsed['fragment']; } $searchEngines = Common::getSearchEngineUrls(); $hostPattern = self::getLossyUrl($referrerHost); /* * Try to get the best matching 'host' in definitions * 1. check if host + path matches an definition * 2. check if host only matches * 3. check if host pattern + path matches * 4. check if host pattern matches * 5. special handling */ if (array_key_exists($referrerHost . $referrerPath, $searchEngines)) { $referrerHost = $referrerHost . $referrerPath; } elseif (array_key_exists($referrerHost, $searchEngines)) { // no need to change host } elseif (array_key_exists($hostPattern . $referrerPath, $searchEngines)) { $referrerHost = $hostPattern . $referrerPath; } elseif (array_key_exists($hostPattern, $searchEngines)) { $referrerHost = $hostPattern; } elseif (!array_key_exists($referrerHost, $searchEngines)) { if (!strncmp($query, 'cx=partner-pub-', 15)) { // Google custom search engine $referrerHost = 'google.com/cse'; } elseif (!strncmp($referrerPath, '/pemonitorhosted/ws/results/', 28)) { // private-label search powered by InfoSpace Metasearch $referrerHost = 'wsdsold.infospace.com'; } elseif (strpos($referrerHost, '.images.search.yahoo.com') != false) { // Yahoo! Images $referrerHost = 'images.search.yahoo.com'; } elseif (strpos($referrerHost, '.search.yahoo.com') != false) { // Yahoo! $referrerHost = 'search.yahoo.com'; } else { return false; } } $searchEngineName = $searchEngines[$referrerHost][0]; $variableNames = null; if (isset($searchEngines[$referrerHost][1])) { $variableNames = $searchEngines[$referrerHost][1]; } if (!$variableNames) { $searchEngineNames = Common::getSearchEngineNames(); $url = $searchEngineNames[$searchEngineName]; $variableNames = $searchEngines[$url][1]; } if (!is_array($variableNames)) { $variableNames = array($variableNames); } $key = null; if ($searchEngineName === 'Google Images' || $searchEngineName === 'Google' && strpos($referrerUrl, '/imgres') !== false) { if (strpos($query, '&prev') !== false) { $query = urldecode(trim(self::getParameterFromQueryString($query, 'prev'))); $query = str_replace('&', '&', strstr($query, '?')); } $searchEngineName = 'Google Images'; } elseif ($searchEngineName === 'Google' && (strpos($query, '&as_') !== false || strpos($query, 'as_') === 0)) { $keys = array(); $key = self::getParameterFromQueryString($query, 'as_q'); if (!empty($key)) { array_push($keys, $key); } $key = self::getParameterFromQueryString($query, 'as_oq'); if (!empty($key)) { array_push($keys, str_replace('+', ' OR ', $key)); } $key = self::getParameterFromQueryString($query, 'as_epq'); if (!empty($key)) { array_push($keys, "\"{$key}\""); } $key = self::getParameterFromQueryString($query, 'as_eq'); if (!empty($key)) { array_push($keys, "-{$key}"); } $key = trim(urldecode(implode(' ', $keys))); } if ($searchEngineName === 'Google') { // top bar menu $tbm = self::getParameterFromQueryString($query, 'tbm'); switch ($tbm) { case 'isch': $searchEngineName = 'Google Images'; break; case 'vid': $searchEngineName = 'Google Video'; break; case 'shop': $searchEngineName = 'Google Shopping'; break; } } if (empty($key)) { foreach ($variableNames as $variableName) { if ($variableName[0] == '/') { // regular expression match if (preg_match($variableName, $referrerUrl, $matches)) { $key = trim(urldecode($matches[1])); break; } } else { // search for keywords now &vname=keyword $key = self::getParameterFromQueryString($query, $variableName); $key = trim(urldecode($key)); // Special cases: empty or no keywords if (empty($key) && ($searchEngineName == 'Google' && (empty($query) && (empty($referrerPath) || $referrerPath == '/') && empty($referrerParsed['fragment'])) || $searchEngineName == 'Yahoo!' && $referrerParsed['host'] == 'r.search.yahoo.com' || strpos($query, sprintf('&%s=', $variableName)) !== false || strpos($query, sprintf('?%s=', $variableName)) !== false || $searchEngineName == 'Ixquick' || $searchEngineName == 'Google Images' || $searchEngineName == 'DuckDuckGo')) { $key = false; } if (!empty($key) || $key === false) { break; } } } } // $key === false is the special case "No keyword provided" which is a Search engine match if ($key === null || $key === '') { return false; } if (!empty($key)) { if (function_exists('iconv') && isset($searchEngines[$referrerHost][3])) { // accepts string, array, or comma-separated list string in preferred order $charsets = $searchEngines[$referrerHost][3]; if (!is_array($charsets)) { $charsets = explode(',', $charsets); } if (!empty($charsets)) { $charset = $charsets[0]; if (count($charsets) > 1 && function_exists('mb_detect_encoding')) { $charset = mb_detect_encoding($key, $charsets); if ($charset === false) { $charset = $charsets[0]; } } $newkey = @iconv($charset, 'UTF-8//IGNORE', $key); if (!empty($newkey)) { $key = $newkey; } } } $key = Common::mb_strtolower($key); } return array('name' => $searchEngineName, 'keywords' => $key); }
protected function hasReferrerColumnChanged(Visitor $visitor, $information, $infoName) { return Common::mb_strtolower($visitor->getVisitorColumn($infoName)) != Common::mb_strtolower($information[$infoName]); }
private function getCanonicalPathFromParsedUrl($urlParsed) { $path = '/'; if (isset($urlParsed['path'])) { $path = Common::mb_strtolower($urlParsed['path']); if (!Common::stringEndsWith($path, '/')) { $path .= '/'; } } return $path; }
/** * Validates the **Host** HTTP header (untrusted user input). Used to prevent Host header * attacks. * * @param string|bool $host Contents of Host: header from the HTTP request. If `false`, gets the * value from the request. * @return bool `true` if valid; `false` otherwise. */ public static function isValidHost($host = false) { // only do trusted host check if it's enabled if (isset(Config::getInstance()->General['enable_trusted_host_check']) && Config::getInstance()->General['enable_trusted_host_check'] == 0) { return true; } if ($host === false) { $host = @$_SERVER['HTTP_HOST']; if (empty($host)) { return true; } } // if host is in hardcoded whitelist, assume it's valid if (in_array($host, self::$alwaysTrustedHosts)) { return true; } $trustedHosts = self::getTrustedHosts(); // if no trusted hosts, just assume it's valid if (empty($trustedHosts)) { self::saveTrustedHostnameInConfig($host); return true; } // Only punctuation we allow is '[', ']', ':', '.' and '-' $hostLength = strlen($host); if ($hostLength !== strcspn($host, '`~!@#$%^&*()_+={}\\|;"\'<>,?/ ')) { return false; } foreach ($trustedHosts as &$trustedHost) { $trustedHost = preg_quote($trustedHost); } $untrustedHost = Common::mb_strtolower($host); $untrustedHost = rtrim($untrustedHost, '.'); $hostRegex = Common::mb_strtolower('/(^|.)' . implode('|', $trustedHosts) . '$/'); $result = preg_match($hostRegex, $untrustedHost); return 0 !== $result; }
private static function toCanonicalHost($host) { $hostLower = Common::mb_strtolower($host); return str_replace('www.', '', $hostLower); }
/** * Extracts a keyword from a raw not encoded URL. * Will only extract keyword if a known search engine has been detected. * Returns the keyword: * - in UTF8: automatically converted from other charsets when applicable * - strtolowered: "QUErY test!" will return "query test!" * - trimmed: extra spaces before and after are removed * * The function returns false when a keyword couldn't be found. * eg. if the url is "http://www.google.com/partners.html" this will return false, * as the google keyword parameter couldn't be found. * * @see unit tests in /tests/core/Common.test.php * @param string $referrerUrl URL referrer URL, eg. $_SERVER['HTTP_REFERER'] * @return array|bool false if a keyword couldn't be extracted, * or array( * 'name' => 'Google', * 'keywords' => 'my searched keywords') */ public function extractInformationFromUrl($referrerUrl) { $referrerParsed = @parse_url($referrerUrl); $referrerHost = ''; if (isset($referrerParsed['host'])) { $referrerHost = $referrerParsed['host']; } if (empty($referrerHost)) { return false; } // some search engines (eg. Bing Images) use the same domain // as an existing search engine (eg. Bing), we must also use the url path $referrerPath = ''; if (isset($referrerParsed['path'])) { $referrerPath = $referrerParsed['path']; } $query = ''; if (isset($referrerParsed['query'])) { $query = $referrerParsed['query']; } // Google Referrers URLs sometimes have the fragment which contains the keyword if (!empty($referrerParsed['fragment'])) { $query .= '&' . $referrerParsed['fragment']; } $referrerHost = $this->getEngineHostFromUrl($referrerHost, $referrerPath, $query); if (empty($referrerHost)) { return false; } $definitions = $this->getDefinitionByHost($referrerHost); $searchEngineName = $definitions['name']; $variableNames = $definitions['params']; $key = null; if ($searchEngineName === 'Google Images' || $searchEngineName === 'Google' && strpos($referrerUrl, '/imgres') !== false) { if (strpos($query, '&prev') !== false) { $query = urldecode(trim(UrlHelper::getParameterFromQueryString($query, 'prev'))); $query = str_replace('&', '&', strstr($query, '?')); } $searchEngineName = 'Google Images'; } elseif ($searchEngineName === 'Google' && (strpos($query, '&as_') !== false || strpos($query, 'as_') === 0)) { $keys = array(); $key = UrlHelper::getParameterFromQueryString($query, 'as_q'); if (!empty($key)) { array_push($keys, $key); } $key = UrlHelper::getParameterFromQueryString($query, 'as_oq'); if (!empty($key)) { array_push($keys, str_replace('+', ' OR ', $key)); } $key = UrlHelper::getParameterFromQueryString($query, 'as_epq'); if (!empty($key)) { array_push($keys, "\"{$key}\""); } $key = UrlHelper::getParameterFromQueryString($query, 'as_eq'); if (!empty($key)) { array_push($keys, "-{$key}"); } $key = trim(urldecode(implode(' ', $keys))); } if ($searchEngineName === 'Google') { // top bar menu $tbm = UrlHelper::getParameterFromQueryString($query, 'tbm'); switch ($tbm) { case 'isch': $searchEngineName = 'Google Images'; break; case 'vid': $searchEngineName = 'Google Video'; break; case 'shop': $searchEngineName = 'Google Shopping'; break; } } if (empty($key)) { foreach ($variableNames as $variableName) { if ($variableName[0] == '/') { // regular expression match if (preg_match($variableName, $referrerUrl, $matches)) { $key = trim(urldecode($matches[1])); break; } } else { // search for keywords now &vname=keyword $key = UrlHelper::getParameterFromQueryString($query, $variableName); $key = trim(urldecode($key)); // Special cases: empty or no keywords if (empty($key) && ($searchEngineName == 'Google' && (empty($query) && (empty($referrerPath) || $referrerPath == '/') && empty($referrerParsed['fragment'])) || $searchEngineName == 'Yahoo!' && $referrerParsed['host'] == 'r.search.yahoo.com' || strpos($query, sprintf('&%s=', $variableName)) !== false || strpos($query, sprintf('?%s=', $variableName)) !== false || $searchEngineName == 'Ixquick' || $searchEngineName == 'Google Images' || $searchEngineName == 'DuckDuckGo')) { $key = false; } if (!empty($key) || $key === false) { break; } } } } // $key === false is the special case "No keyword provided" which is a Search engine match if ($key === null || $key === '') { return false; } if (!empty($key)) { if (!empty($definitions['charsets'])) { $key = $this->convertCharset($key, $definitions['charsets']); } $key = Common::mb_strtolower($key); } return array('name' => $searchEngineName, 'keywords' => $key); }
/** * Checks whether any of the given URLs has the given host. If not, we will also check whether any URL uses a * subdomain of the given host. For instance if host is "example.com" and a URL is "http://www.example.com" we * consider this as valid and return true. The always trusted hosts such as "127.0.0.1" are considered valid as well. * * @param $host * @param $urls * @return bool */ public static function isHostInUrls($host, $urls) { if (empty($host)) { return false; } $host = Common::mb_strtolower($host); if (!empty($urls)) { foreach ($urls as $url) { if (Common::mb_strtolower($url) === $host) { return true; } $siteHost = self::getHostFromUrl($url); if ($siteHost === $host) { return true; } if (Common::stringEndsWith($siteHost, '.' . $host)) { // allow subdomains return true; } } } return in_array($host, self::$alwaysTrustedHosts); }
/** * Will cleanup the hostname (some browser do not strolower the hostname), * and deal ith the hash tag on incoming URLs based on website setting. * * @param $parsedUrl * @param $idSite int|bool The site ID of the current visit. This parameter is * only used by the tracker to see if we should remove * the URL fragment for this site. * @return array */ protected static function cleanupHostAndHashTag($parsedUrl, $idSite = false) { if (empty($parsedUrl)) { return $parsedUrl; } if (!empty($parsedUrl['host'])) { $parsedUrl['host'] = Common::mb_strtolower($parsedUrl['host'], 'UTF-8'); } if (!empty($parsedUrl['fragment'])) { $parsedUrl['fragment'] = PageUrl::processUrlFragment($parsedUrl['fragment'], $idSite); } return $parsedUrl; }
protected function detectSiteSearchFromUrl($website, $parsedUrl) { $doRemoveSearchParametersFromUrl = true; $separator = '&'; $count = $actionName = $categoryName = false; $keywordParameters = isset($website['sitesearch_keyword_parameters']) ? $website['sitesearch_keyword_parameters'] : array(); $queryString = (!empty($parsedUrl['query']) ? $parsedUrl['query'] : '') . (!empty($parsedUrl['fragment']) ? $separator . $parsedUrl['fragment'] : ''); $parametersRaw = UrlHelper::getArrayFromQueryString($queryString); // strtolower the parameter names for smooth site search detection $parameters = array(); foreach ($parametersRaw as $k => $v) { $parameters[Common::mb_strtolower($k)] = $v; } // decode values if they were sent from a client using another charset $pageEncoding = $this->request->getParam('cs'); PageUrl::reencodeParameters($parameters, $pageEncoding); // Detect Site Search keyword foreach ($keywordParameters as $keywordParameterRaw) { $keywordParameter = Common::mb_strtolower($keywordParameterRaw); if (!empty($parameters[$keywordParameter])) { $actionName = $parameters[$keywordParameter]; break; } } if (empty($actionName)) { return false; } $categoryParameters = isset($website['sitesearch_category_parameters']) ? $website['sitesearch_category_parameters'] : array(); foreach ($categoryParameters as $categoryParameterRaw) { $categoryParameter = Common::mb_strtolower($categoryParameterRaw); if (!empty($parameters[$categoryParameter])) { $categoryName = $parameters[$categoryParameter]; break; } } if (isset($parameters['search_count']) && $this->isValidSearchCount($parameters['search_count'])) { $count = $parameters['search_count']; } // Remove search kwd from URL if ($doRemoveSearchParametersFromUrl) { // @see excludeQueryParametersFromUrl() // Excluded the detected parameters from the URL $parametersToExclude = array($categoryParameterRaw, $keywordParameterRaw); if (isset($parsedUrl['query'])) { $parsedUrl['query'] = UrlHelper::getQueryStringWithExcludedParameters(UrlHelper::getArrayFromQueryString($parsedUrl['query']), $parametersToExclude); } if (isset($parsedUrl['fragment'])) { $parsedUrl['fragment'] = UrlHelper::getQueryStringWithExcludedParameters(UrlHelper::getArrayFromQueryString($parsedUrl['fragment']), $parametersToExclude); } } $url = UrlHelper::getParseUrlReverse($parsedUrl); if (is_array($actionName)) { $actionName = reset($actionName); } $actionName = trim(urldecode($actionName)); if (empty($actionName)) { return false; } if (is_array($categoryName)) { $categoryName = reset($categoryName); } $categoryName = trim(urldecode($categoryName)); return array($url, $actionName, $categoryName, $count); }
/** * @param $type * @param $name * @param $keyword */ protected function setCampaignValuesToLowercase($type, &$name, &$keyword) { if ($type === Common::REFERRER_TYPE_CAMPAIGN) { if (!empty($name)) { $name = Common::mb_strtolower($name); } if (!empty($keyword)) { $keyword = Common::mb_strtolower($keyword); } } }
/** * @return bool */ protected function detectReferrerCampaign() { $this->detectReferrerCampaignFromLandingUrl(); $this->detectCampaignKeywordFromReferrerUrl(); if ($this->typeReferrerAnalyzed != Common::REFERRER_TYPE_CAMPAIGN) { return false; } // if we detected a campaign but there is still no keyword set, we set the keyword to the Referrer host if (empty($this->keywordReferrerAnalyzed)) { $this->keywordReferrerAnalyzed = $this->referrerHost; } $this->keywordReferrerAnalyzed = Common::mb_strtolower($this->keywordReferrerAnalyzed); $this->nameReferrerAnalyzed = Common::mb_strtolower($this->nameReferrerAnalyzed); return true; }