We don't need a precise test here because the value comes from the website
tracked source code and the URLs may look very strange.
public static isLookLikeUrl ( string $url ) : boolean | ||
$url | string | |
return | boolean |
/** * Returns an array containing the following information: * - referer_type * - direct -- absence of referrer URL OR referrer URL has the same host * - site -- based on the referrer URL * - search_engine -- based on the referrer URL * - campaign -- based on campaign URL parameter * * - referer_name * - () * - piwik.net -- site host name * - google.fr -- search engine host name * - adwords-search -- campaign name * * - referer_keyword * - () * - () * - my keyword * - my paid keyword * - () * - () * * - referer_url : the same for all the referrer types * * @param string $referrerUrl must be URL Encoded * @param string $currentUrl * @param int $idSite * @return array */ public function getReferrerInformation($referrerUrl, $currentUrl, $idSite) { $this->idsite = $idSite; // default values for the referer_* fields $referrerUrl = Common::unsanitizeInputValue($referrerUrl); if (!empty($referrerUrl) && !UrlHelper::isLookLikeUrl($referrerUrl)) { $referrerUrl = ''; } $currentUrl = PageUrl::cleanupUrl($currentUrl); $this->referrerUrl = $referrerUrl; $this->referrerUrlParse = @parse_url($this->referrerUrl); $this->currentUrlParse = @parse_url($currentUrl); $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_DIRECT_ENTRY; $this->nameReferrerAnalyzed = ''; $this->keywordReferrerAnalyzed = ''; $this->referrerHost = ''; if (isset($this->referrerUrlParse['host'])) { $this->referrerHost = $this->referrerUrlParse['host']; } $referrerDetected = $this->detectReferrerCampaign(); if (!$referrerDetected) { if ($this->detectReferrerDirectEntry() || $this->detectReferrerSearchEngine()) { $referrerDetected = true; } } if (!empty($this->referrerHost) && !$referrerDetected) { $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_WEBSITE; $this->nameReferrerAnalyzed = Common::mb_strtolower($this->referrerHost); } $referrerInformation = array('referer_type' => $this->typeReferrerAnalyzed, 'referer_name' => $this->nameReferrerAnalyzed, 'referer_keyword' => $this->keywordReferrerAnalyzed, 'referer_url' => $this->referrerUrl); return $referrerInformation; }
/** * Check that the array of URLs are valid URLs * * @param array $urls * @throws Exception if any of the urls is not valid */ public function checkUrls($urls) { $urls = $this->cleanParameterUrls($urls); foreach ($urls as $url) { if (!UrlHelper::isLookLikeUrl($url)) { throw new Exception(sprintf(Piwik::translate('SitesManager_ExceptionInvalidUrl'), $url)); } } }
public function render() { $idSite = Common::getRequestVar('idSite'); $site = new Site($idSite); $url = urldecode(Common::getRequestVar('url', '', 'string')); if (!empty($url) && strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) { $url = 'http://' . $url; } if (empty($url) || !UrlHelper::isLookLikeUrl($url)) { $url = $site->getMainUrl(); } $dataTable = API::getInstance()->getRank($url); /** @var \Piwik\DataTable\Renderer\Php $renderer */ $renderer = Renderer::factory('php'); $renderer->setSerialize(false); return $this->renderTemplate('getRank', array('urlToRank' => Url::getHostFromUrl($url), 'ranks' => $renderer->render($dataTable))); }
/** * Main Plugin Index * * @return mixed * @throws \Exception */ public function index() { Piwik::checkUserHasSomeAdminAccess(); if (isset($_SERVER['REQUEST_METHOD']) && 'POST' == $_SERVER['REQUEST_METHOD']) { // Cannot use Common::getRequestVar, because the function remove whitespaces and newline breaks $postedSiteData = isset($_POST['site']) ? $_POST['site'] : null; if (is_array($postedSiteData) && count($postedSiteData) > 0) { foreach ($postedSiteData as $id => $site) { if (!isset($site['css'], $site['file'])) { continue; } // Check URL if (!UrlHelper::isLookLikeUrl($site['file'])) { $site['file'] = null; } API::getInstance()->saveSite($id, $site['css'], $site['file']); } // Redirect to, clear POST vars $this->redirectToIndex('CustomOptOut', 'index'); return; } } $view = new View('@CustomOptOut/index.twig'); Site::clearCache(); if (Piwik::hasUserSuperUserAccess()) { $sitesRaw = APISiteManager::getInstance()->getAllSites(); } else { $sitesRaw = APISiteManager::getInstance()->getSitesWithAdminAccess(); } // Gets sites after Site.setSite hook was called $sites = array_values(Site::getSites()); if (count($sites) != count($sitesRaw)) { throw new \Exception("One or more website are missing or invalid."); } foreach ($sites as &$site) { $site['alias_urls'] = APISiteManager::getInstance()->getSiteUrlsFromId($site['idsite']); } $view->adminSites = $sites; $view->adminSitesCount = count($sites); $view->language = LanguagesManager::getLanguageCodeForCurrentUser(); $view->isEditorEnabled = API::getInstance()->isCssEditorEnabled(); $view->editorTheme = API::getInstance()->getEditorTheme(); $view->showOldLinks = false; $this->setBasicVariablesView($view); return $view->render(); }
public function getReferrerUrl() { if ($this->getReferrerType() == 'search') { if ($this->details['referer_keyword'] == API::LABEL_KEYWORD_NOT_DEFINED) { return 'http://piwik.org/faq/general/#faq_144'; } elseif ($this->getReferrerName() == 'Google' && strpos($this->details['referer_url'], '/url')) { $refUrl = @parse_url($this->details['referer_url']); if (isset($refUrl['host'])) { $url = SearchEngine::getInstance()->getBackLinkFromUrlAndKeyword('http://google.com', $this->getKeyword()); $url = str_replace('google.com', $refUrl['host'], $url); return $url; } } } if (UrlHelper::isLookLikeUrl($this->details['referer_url'])) { return $this->details['referer_url']; } return null; }
/** * Returns an array containing the following information: * - referer_type * - direct -- absence of referrer URL OR referrer URL has the same host * - site -- based on the referrer URL * - search_engine -- based on the referrer URL * - campaign -- based on campaign URL parameter * * - referer_name * - () * - piwik.net -- site host name * - google.fr -- search engine host name * - adwords-search -- campaign name * * - referer_keyword * - () * - () * - my keyword * - my paid keyword * - () * - () * * - referer_url : the same for all the referrer types * * @param string $referrerUrl must be URL Encoded * @param string $currentUrl * @param int $idSite * @return array */ protected function getReferrerInformation($referrerUrl, $currentUrl, $idSite, Request $request) { $cacheKey = $referrerUrl . $currentUrl . $idSite; if (isset(self::$cachedReferrer[$cacheKey])) { return self::$cachedReferrer[$cacheKey]; } $this->idsite = $idSite; // default values for the referer_* fields $referrerUrl = Common::unsanitizeInputValue($referrerUrl); if (!empty($referrerUrl) && !UrlHelper::isLookLikeUrl($referrerUrl)) { $referrerUrl = ''; } $currentUrl = PageUrl::cleanupUrl($currentUrl); $this->referrerUrl = $referrerUrl; $this->referrerUrlParse = @parse_url($this->referrerUrl); $this->currentUrlParse = @parse_url($currentUrl); $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_DIRECT_ENTRY; $this->nameReferrerAnalyzed = ''; $this->keywordReferrerAnalyzed = ''; $this->referrerHost = ''; if (isset($this->referrerUrlParse['host'])) { $this->referrerHost = $this->referrerUrlParse['host']; } $referrerDetected = $this->detectReferrerCampaign($request); if (!$referrerDetected) { if ($this->detectReferrerDirectEntry() || $this->detectReferrerSearchEngine()) { $referrerDetected = true; } } if (!$referrerDetected && !empty($this->referrerHost)) { $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_WEBSITE; $this->nameReferrerAnalyzed = Common::mb_strtolower($this->referrerHost); $urlsByHost = $this->getCachedUrlsByHostAndIdSite(); $directEntry = new SiteUrls(); $path = $directEntry->getPathMatchingUrl($this->referrerUrlParse, $urlsByHost); if (!empty($path) && $path !== '/') { $this->nameReferrerAnalyzed .= rtrim($path, '/'); } } $referrerInformation = array('referer_type' => $this->typeReferrerAnalyzed, 'referer_name' => $this->nameReferrerAnalyzed, 'referer_keyword' => $this->keywordReferrerAnalyzed, 'referer_url' => $this->referrerUrl); self::$cachedReferrer[$cacheKey] = $referrerInformation; return $referrerInformation; }
function getRank() { $idSite = Common::getRequestVar('idSite'); $site = new Site($idSite); $url = urldecode(Common::getRequestVar('url', '', 'string')); if (!empty($url) && strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) { $url = 'http://' . $url; } if (empty($url) || !UrlHelper::isLookLikeUrl($url)) { $url = $site->getMainUrl(); } $dataTable = API::getInstance()->getRank($url); $view = new View('@SEO/getRank'); $view->urlToRank = RankChecker::extractDomainFromUrl($url); /** @var \Piwik\DataTable\Renderer\Php $renderer */ $renderer = Renderer::factory('php'); $renderer->setSerialize(false); $view->ranks = $renderer->render($dataTable); return $view->render(); }
/** * Output redirection page instead of linking directly to avoid * exposing the referrer on the Piwik demo. * * @internal param string $url (via $_GET) */ public function redirect() { $url = Common::getRequestVar('url', '', 'string', $_GET); if (!UrlHelper::isLookLikeUrl($url)) { die('Please check the &url= parameter: it should to be a valid URL'); } // validate referrer $referrer = Url::getReferrer(); if (empty($referrer) || !Url::isLocalUrl($referrer)) { die('Invalid Referrer detected - This means that your web browser is not sending the "Referrer URL" which is required to proceed with the redirect. Verify your browser settings and add-ons, to check why your browser is not sending this referrer. <br/><br/>You can access the page at: ' . $url); } // mask visits to *.piwik.org if (!self::isPiwikUrl($url)) { Piwik::checkUserHasSomeViewAccess(); } Common::sendHeader('Content-Type: text/html; charset=utf-8'); echo '<html><head><meta http-equiv="refresh" content="0;url=' . $url . '" /></head></html>'; exit; }
public static function getHostFromUrl($url) { if (!UrlHelper::isLookLikeUrl($url)) { $url = "http://" . $url; } return parse_url($url, PHP_URL_HOST); }
/** * Tests if the URL is a valid URL * * @param string $url * @return bool */ private function isValidUrl($url) { return UrlHelper::isLookLikeUrl($url); }
public static function getTrustedHostsFromConfig() { $trustedHosts = @Config::getInstance()->General['trusted_hosts']; if (!is_array($trustedHosts)) { return array(); } foreach ($trustedHosts as &$trustedHost) { // Case user wrote in the config, http://example.com/test instead of example.com if (UrlHelper::isLookLikeUrl($trustedHost)) { $trustedHost = parse_url($trustedHost, PHP_URL_HOST); } } return $trustedHosts; }
public static function getTrustedHostsFromConfig() { $hosts = self::getHostsFromConfig('General', 'trusted_hosts'); // Case user wrote in the config, http://example.com/test instead of example.com foreach ($hosts as &$host) { if (UrlHelper::isLookLikeUrl($host)) { $host = parse_url($host, PHP_URL_HOST); } } return $hosts; }
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 function initPiwikHost($piwikUrl = false) { // If core:archive command run as a web cron, we use the current hostname+path if (empty($piwikUrl)) { if (!empty(self::$url)) { $piwikUrl = self::$url; } else { // example.org/piwik/ $piwikUrl = SettingsPiwik::getPiwikUrl(); } } if (!$piwikUrl) { $this->logFatalErrorUrlExpected(); } if (!\Piwik\UrlHelper::isLookLikeUrl($piwikUrl)) { // try adding http:// in case it's missing $piwikUrl = "http://" . $piwikUrl; } if (!\Piwik\UrlHelper::isLookLikeUrl($piwikUrl)) { $this->logFatalErrorUrlExpected(); } // ensure there is a trailing slash if ($piwikUrl[strlen($piwikUrl) - 1] != '/' && !Common::stringEndsWith($piwikUrl, 'index.php')) { $piwikUrl .= '/'; } $this->initConfigObject($piwikUrl); if (Config::getInstance()->General['force_ssl'] == 1) { $piwikUrl = str_replace('http://', 'https://', $piwikUrl); } if (!Common::stringEndsWith($piwikUrl, 'index.php')) { $piwikUrl .= 'index.php'; } $this->piwikUrl = $piwikUrl; }
/** * Redirects the user to the specified URL. * * @param string $url * @api */ public static function redirectToUrl($url) { if (UrlHelper::isLookLikeUrl($url) || strpos($url, 'index.php') === 0) { @header("Location: {$url}"); } else { echo "Invalid URL to redirect to."; } exit; }
private function createTrackToCustomSiteUrlSetting() { return $this->makeSetting('customSiteUrl', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) { $field->title = 'Piwik Url'; $field->uiControl = FieldConfig::UI_CONTROL_TEXT; $field->uiControlAttributes = array('placeHolder' => 'eg. http://example.com/piwik'); $field->introduction = 'Send anonymize usage data to a custom Piwik'; $field->description = ''; $field->validate = function ($value, $setting) { if (empty($value)) { return; } if (!UrlHelper::isLookLikeUrl($value)) { throw new Exception("URL '{$value}' seems to be not a valid URL"); } // TODO should we check if URL exists and is valid?!? might not work if instance is not connected to internet }; $field->transform = function ($value) { if (empty($value)) { return ''; } if (!Common::stringEndsWith($value, '/piwik.php')) { if (!Common::stringEndsWith($value, '/')) { $value .= '/'; } $value .= 'piwik.php'; } return $value; }; }); }
/** * @dataProvider getUrls * @group Core */ public function testIsUrl($url, $isValid) { $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url), "{$url} failed test"); }
private function initPiwikHost() { // If archive.php run as a web cron, we use the current hostname+path if (!Common::isPhpCliMode()) { // example.org/piwik/misc/cron/ $piwikUrl = Common::sanitizeInputValue(Url::getCurrentUrlWithoutFileName()); // example.org/piwik/ $piwikUrl = $piwikUrl . "../../"; } else { // If archive.php run as CLI/shell we require the piwik url to be set $piwikUrl = $this->isParameterSet("url", true); if (!$piwikUrl) { $this->logFatalErrorUrlExpected(); } if (!\Piwik\UrlHelper::isLookLikeUrl($piwikUrl)) { // try adding http:// in case it's missing $piwikUrl = "http://" . $piwikUrl; } if (!\Piwik\UrlHelper::isLookLikeUrl($piwikUrl)) { $this->logFatalErrorUrlExpected(); } // ensure there is a trailing slash if ($piwikUrl[strlen($piwikUrl) - 1] != '/') { $piwikUrl .= '/'; } } $this->initConfigObject($piwikUrl); if (Config::getInstance()->General['force_ssl'] == 1) { $piwikUrl = str_replace('http://', 'https://', $piwikUrl); } $this->piwikUrl = $piwikUrl . "index.php"; }
private function createTrackToCustomSiteUrlSetting() { $this->customPiwikSiteUrl = new SystemSetting('customSiteUrl', 'Piwik Url'); $this->customPiwikSiteUrl->readableByCurrentUser = true; $this->customPiwikSiteUrl->uiControlType = static::CONTROL_TEXT; $this->customPiwikSiteUrl->uiControlAttributes = array('placeHolder' => 'eg. http://example.com/piwik'); $this->customPiwikSiteUrl->introduction = 'Send anonymize usage data to a custom Piwik'; $this->customPiwikSiteUrl->description = ''; $this->customPiwikSiteUrl->defaultValue = ''; $this->customPiwikSiteUrl->validate = function ($value, $setting) { if (empty($value)) { return; } if (!UrlHelper::isLookLikeUrl($value)) { throw new Exception("URL '{$value}' seems to be not a valid URL"); } // TODO should we check if URL exists and is valid?!? might not work if instance is not connected to internet }; $this->customPiwikSiteUrl->transform = function ($value) { if (empty($value)) { return ''; } if (!Common::stringEndsWith($value, '/piwik.php')) { if (!Common::stringEndsWith($value, '/')) { $value .= '/'; } $value .= 'piwik.php'; } return $value; }; $this->addSetting($this->customPiwikSiteUrl); }