Ejemplo n.º 1
0
/**
 * Returns path component from a URL
 *
 * @param string $url
 * @return string path
 */
function getPathFromUrl($url)
{
    $path = UrlHelper::getPathAndQueryFromUrl($url);
    if (empty($path)) {
        return 'index';
    }
    return $path;
}
Ejemplo n.º 2
0
Archivo: Urls.php Proyecto: piwik/piwik
 /**
  * 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));
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * @param InputInterface $input
  */
 protected function initHostAndQueryString(InputInterface $input)
 {
     $_GET = array();
     $hostname = $input->getOption('piwik-domain');
     Url::setHost($hostname);
     $query = $input->getArgument('url-query');
     $query = UrlHelper::getArrayFromQueryString($query);
     foreach ($query as $name => $value) {
         $_GET[$name] = $value;
     }
 }
Ejemplo n.º 4
0
 public function getKeywordPosition()
 {
     if ($this->getReferrerType() == 'search' && strpos($this->getReferrerName(), 'Google') !== false) {
         $url = @parse_url($this->details['referer_url']);
         if (empty($url['query'])) {
             return null;
         }
         $position = UrlHelper::getParameterFromQueryString($url['query'], 'cd');
         if (!empty($position)) {
             return $position;
         }
     }
     return null;
 }
Ejemplo n.º 5
0
 /**
  * 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();
 }
Ejemplo n.º 6
0
 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)));
 }
Ejemplo n.º 7
0
 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();
 }
Ejemplo n.º 8
0
    /**
     * 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;
    }
Ejemplo n.º 9
0
 protected function getEngineHostFromUrl($host, $path, $query)
 {
     $searchEngines = $this->getDefinitions();
     $hostPattern = UrlHelper::getLossyUrl($host);
     /*
      * 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($host . $path, $searchEngines)) {
         $host = $host . $path;
     } elseif (array_key_exists($host, $searchEngines)) {
         // no need to change host
     } elseif (array_key_exists($hostPattern . $path, $searchEngines)) {
         $host = $hostPattern . $path;
     } elseif (array_key_exists($hostPattern, $searchEngines)) {
         $host = $hostPattern;
     } elseif (!array_key_exists($host, $searchEngines)) {
         if (!strncmp($query, 'cx=partner-pub-', 15)) {
             // Google custom search engine
             $host = 'google.com/cse';
         } elseif (!strncmp($path, '/pemonitorhosted/ws/results/', 28)) {
             // private-label search powered by InfoSpace Metasearch
             $host = 'wsdsold.infospace.com';
         } elseif (strpos($host, '.images.search.yahoo.com') != false) {
             // Yahoo! Images
             $host = 'images.search.yahoo.com';
         } elseif (strpos($host, '.search.yahoo.com') != false) {
             // Yahoo!
             $host = 'search.yahoo.com';
         } else {
             return false;
         }
     }
     return $host;
 }
Ejemplo n.º 10
0
 /**
  * Returns the original request parameters in the current query string as an array mapping
  * query parameter names with values. The result of this function will not be affected
  * by any modifications to `$_GET` and will not include parameters in `$_POST`.
  *
  * @return array
  */
 public static function getRequestParametersGET()
 {
     if (empty($_SERVER['QUERY_STRING'])) {
         return array();
     }
     $GET = UrlHelper::getArrayFromQueryString($_SERVER['QUERY_STRING']);
     return $GET;
 }
Ejemplo n.º 11
0
 protected function initPiwikHost(InputInterface $input)
 {
     $piwikHostname = $input->getParameterOption('--piwik-domain');
     if (empty($piwikHostname)) {
         $piwikHostname = $input->getParameterOption('--url');
     }
     $piwikHostname = UrlHelper::getHostFromUrl($piwikHostname);
     Url::setHost($piwikHostname);
 }
Ejemplo n.º 12
0
 /**
  * Extracts the utm query parameters into array
  * @return array 
  */
 private function extractUtmDetailsFromUrl()
 {
     $query = $this->currentUrlParse['query'];
     $params = UrlHelper::getArrayFromQueryString($query);
     return array('utm_campaign' => isset($params['utm_campaign']) ? $params['utm_campaign'] : '', 'utm_term' => isset($params['utm_term']) ? $params['utm_term'] : '', 'utm_medium' => isset($params['utm_medium']) ? $params['utm_medium'] : '', 'utm_content' => isset($params['utm_content']) ? $params['utm_content'] : '', 'utm_source' => isset($params['utm_source']) ? $params['utm_source'] : '');
 }
Ejemplo n.º 13
0
 public static function getHostFromUrl($url)
 {
     if (!UrlHelper::isLookLikeUrl($url)) {
         $url = "http://" . $url;
     }
     return parse_url($url, PHP_URL_HOST);
 }
Ejemplo n.º 14
0
 /**
  * Tests if the URL is a valid URL
  *
  * @param string $url
  * @return bool
  */
 private function isValidUrl($url)
 {
     return UrlHelper::isLookLikeUrl($url);
 }
Ejemplo n.º 15
0
 private function executeAsyncCli($url, Output $output, $cmdId)
 {
     $this->processes[] = new Process($cmdId);
     $url = $this->appendTestmodeParamToUrlIfNeeded($url);
     $query = UrlHelper::getQueryFromUrl($url, array('pid' => $cmdId));
     $hostname = UrlHelper::getHostFromUrl($url);
     $command = $this->buildCommand($hostname, $query, $output->getPathToFile());
     Log::debug($command);
     shell_exec($command);
 }
Ejemplo n.º 16
0
 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;
 }
Ejemplo n.º 17
0
 /**
  * Returns Proxy to use for connecting via HTTP to given URL
  *
  * @param string $url
  * @return array
  */
 private static function getProxyConfiguration($url)
 {
     $hostname = UrlHelper::getHostFromUrl($url);
     $localHostnames = Url::getLocalHostnames();
     if (in_array($hostname, $localHostnames)) {
         return array(null, null, null, null);
     }
     // proxy configuration
     $proxyHost = Config::getInstance()->proxy['host'];
     $proxyPort = Config::getInstance()->proxy['port'];
     $proxyUser = Config::getInstance()->proxy['username'];
     $proxyPassword = Config::getInstance()->proxy['password'];
     return array($proxyHost, $proxyPort, $proxyUser, $proxyPassword);
 }
Ejemplo n.º 18
0
 /**
  * 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;
 }
Ejemplo n.º 19
0
 protected function _testApiUrl($testName, $apiId, $requestUrl, $compareAgainst)
 {
     $isTestLogImportReverseChronological = strpos($testName, 'ImportedInRandomOrderTest') === false;
     $isLiveMustDeleteDates = (strpos($requestUrl, 'Live.getLastVisits') !== false || strpos($requestUrl, 'Live.getVisitorProfile') !== false) && $isTestLogImportReverseChronological;
     $request = new Request($requestUrl);
     $dateTime = Common::getRequestVar('date', '', 'string', UrlHelper::getArrayFromQueryString($requestUrl));
     list($processedFilePath, $expectedFilePath) = $this->getProcessedAndExpectedPaths($testName, $apiId, $format = null, $compareAgainst);
     // Cast as string is important. For example when calling
     // with format=original, objects or php arrays can be returned.
     // we also hide errors to prevent the 'headers already sent' in the ResponseBuilder (which sends Excel headers multiple times eg.)
     $response = (string) $request->process();
     if ($isLiveMustDeleteDates) {
         $response = $this->removeAllLiveDatesFromXml($response);
     }
     $response = $this->normalizePdfContent($response);
     $expected = $this->loadExpectedFile($expectedFilePath);
     $expectedContent = $expected;
     $expected = $this->normalizePdfContent($expected);
     if (empty($expected)) {
         if (empty($compareAgainst)) {
             file_put_contents($processedFilePath, $response);
         }
         print "The expected file is not found at '{$expectedFilePath}'. The Processed response was:";
         print "\n----------------------------\n\n";
         var_dump($response);
         print "\n----------------------------\n";
         return;
     }
     $expected = $this->removeXmlElement($expected, 'idsubdatatable', $testNotSmallAfter = false);
     $response = $this->removeXmlElement($response, 'idsubdatatable', $testNotSmallAfter = false);
     if ($isLiveMustDeleteDates) {
         $expected = $this->removeAllLiveDatesFromXml($expected);
     } elseif (strpos($dateTime, 'last') !== false || strpos($dateTime, 'today') !== false || strpos($dateTime, 'now') !== false) {
         if (strpos($requestUrl, 'API.getProcessedReport') !== false) {
             $expected = $this->removePrettyDateFromXml($expected);
             $response = $this->removePrettyDateFromXml($response);
         }
         $expected = $this->removeXmlElement($expected, 'visitServerHour');
         $response = $this->removeXmlElement($response, 'visitServerHour');
         if (strpos($requestUrl, 'date=') !== false) {
             $regex = "/date=[-0-9,%Ca-z]+/";
             // need to remove %2C which is encoded ,
             $expected = preg_replace($regex, 'date=', $expected);
             $response = preg_replace($regex, 'date=', $response);
         }
     }
     // if idSubtable is in request URL, make sure idSubtable values are not in any urls
     if (strpos($requestUrl, 'idSubtable=') !== false) {
         $regex = "/idSubtable=[0-9]+/";
         $expected = preg_replace($regex, 'idSubtable=', $expected);
         $response = preg_replace($regex, 'idSubtable=', $response);
     }
     // Do not test for TRUNCATE(SUM()) returning .00 on mysqli since this is not working
     // http://bugs.php.net/bug.php?id=54508
     $expected = str_replace('.000000</l', '</l', $expected);
     //lat/long
     $response = str_replace('.000000</l', '</l', $response);
     //lat/long
     $expected = str_replace('.00</revenue>', '</revenue>', $expected);
     $response = str_replace('.00</revenue>', '</revenue>', $response);
     $response = str_replace('.1</revenue>', '</revenue>', $response);
     $expected = str_replace('.1</revenue>', '</revenue>', $expected);
     $expected = str_replace('.11</revenue>', '</revenue>', $expected);
     $response = str_replace('.11</revenue>', '</revenue>', $response);
     if (empty($compareAgainst)) {
         file_put_contents($processedFilePath, $response);
     }
     try {
         if (strpos($requestUrl, 'format=xml') !== false) {
             $this->assertXmlStringEqualsXmlString($expected, $response, "Differences with expected in: {$processedFilePath}");
         } else {
             $this->assertEquals(strlen($expected), strlen($response), "Differences with expected in: {$processedFilePath}");
             $this->assertEquals($expected, $response, "Differences with expected in: {$processedFilePath}");
         }
         if (trim($response) == trim($expected) && empty($compareAgainst)) {
             if (trim($expectedContent) != trim($expected)) {
                 file_put_contents($expectedFilePath, $expected);
             }
         }
     } catch (Exception $ex) {
         $this->comparisonFailures[] = $ex;
     }
 }
Ejemplo n.º 20
0
 public function popout()
 {
     header("Access-Control-Allow-Origin: *");
     $params = UrlHelper::getArrayFromQueryString($_SERVER['QUERY_STRING']);
     $request = new Tracker\Request($params);
     // the IP is needed by isExcluded() and GoalManager->recordGoals()
     $ip = $request->getIp();
     $visitorInfo['location_ip'] = $ip;
     /**
      * Triggered after visits are tested for exclusion so plugins can modify the IP address
      * persisted with a visit.
      *
      * This event is primarily used by the **PrivacyManager** plugin to anonymize IP addresses.
      *
      * @param string &$ip The visitor's IP address.
      */
     Piwik::postEvent('Tracker.setVisitorIp', array(&$visitorInfo['location_ip']));
     /***
      * Visitor recognition
      */
     $settings = new Tracker\Settings($request, $visitorInfo['location_ip']);
     $visitor = new Visitor($request, $settings->getConfigId(), $visitorInfo);
     $visitor->recognize();
     $visitorInfo = $visitor->getVisitorInfo();
     if (!isset($visitorInfo['location_browser_lang'])) {
         return "Who are you ?";
     }
     $idSite = Common::getRequestVar('idsite', null, 'int');
     $conversation = new ChatConversation($idSite, bin2hex($visitorInfo['idvisitor']));
     /***
      * Segment recognition
      */
     foreach (ChatAutomaticMessage::getAll($idSite) as $autoMsg) {
         $segment = ChatSegment::get($autoMsg['segmentID']);
         $fetchSegment = new Segment($segment['definition'], array($idSite));
         $query = $fetchSegment->getSelectQuery("idvisitor", "log_visit", "log_visit.idvisitor = ?", array($visitorInfo['idvisitor']));
         $rows = Db::fetchAll($query['sql'], $query['bind']);
         if (count($rows) == 0) {
             continue;
         }
         if ($autoMsg['segmentID'] != $segment['idsegment']) {
             continue;
         }
         $getAlreadyReceivedMsg = $conversation->getAutomaticMessageReceivedById($autoMsg['id']);
         if (count($getAlreadyReceivedMsg) > 0) {
             // If the AutoMsg is a "one shot"
             if ($autoMsg['frequency'] == 0) {
                 continue;
             }
             if ($autoMsg['frequency'] != 0) {
                 // Now, we gonna try to define when the last AutoMsg received has been sent
                 list($freqTime, $freqScale) = explode('|', $autoMsg['frequency']);
                 if ($freqScale == "w") {
                     $dayMultiplier = 7;
                 } elseif ($freqScale == "m") {
                     $dayMultiplier = 30;
                 } else {
                     $dayMultiplier = 1;
                 }
                 $secToWait = 3600 * 24 * $freqTime * $dayMultiplier;
                 // Is it older than the time range needed to wait ?
                 if ($getAlreadyReceivedMsg[0]['microtime'] + $secToWait > microtime(true)) {
                     continue;
                 }
             }
         }
         $conversation->sendMessage($autoMsg['message'], $autoMsg['transmitter'], $autoMsg['id']);
     }
     $view = new View('@Chat/popout.twig');
     $view->idvisitor = bin2hex($visitorInfo['idvisitor']);
     $view->idsite = $idSite;
     $view->timeLimit = time() - 2 * 60 * 60;
     $view->isStaffOnline = ChatPiwikUser::isStaffOnline();
     $view->siteUrl = ChatSite::getMainUrl($idSite);
     $view->lang = $visitorInfo['location_browser_lang'];
     return $view->render();
 }
Ejemplo n.º 21
0
 /**
  * Sends an HTTP request using the specified transport method.
  *
  * @param string $method
  * @param string $aUrl
  * @param int $timeout
  * @param string $userAgent
  * @param string $destinationPath
  * @param resource $file
  * @param int $followDepth
  * @param bool|string $acceptLanguage Accept-language header
  * @param bool $acceptInvalidSslCertificate Only used with $method == 'curl'. If set to true (NOT recommended!) the SSL certificate will not be checked
  * @param array|bool $byteRange For Range: header. Should be two element array of bytes, eg, array(0, 1024)
  *                                                  Doesn't work w/ fopen method.
  * @param bool $getExtendedInfo True to return status code, headers & response, false if just response.
  * @param string $httpMethod The HTTP method to use. Defaults to `'GET'`.
  *
  * @throws Exception
  * @return bool  true (or string/array) on success; false on HTTP response error code (1xx or 4xx)
  */
 public static function sendHttpRequestBy($method = 'socket', $aUrl, $timeout, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false, $byteRange = false, $getExtendedInfo = false, $httpMethod = 'GET')
 {
     if ($followDepth > 5) {
         throw new Exception('Too many redirects (' . $followDepth . ')');
     }
     $contentLength = 0;
     $fileLength = 0;
     // Piwik services behave like a proxy, so we should act like one.
     $xff = 'X-Forwarded-For: ' . (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] . ',' : '') . IP::getIpFromHeader();
     if (empty($userAgent)) {
         $userAgent = self::getUserAgent();
     }
     $via = 'Via: ' . (isset($_SERVER['HTTP_VIA']) && !empty($_SERVER['HTTP_VIA']) ? $_SERVER['HTTP_VIA'] . ', ' : '') . Version::VERSION . ' ' . ($userAgent ? " ({$userAgent})" : '');
     // range header
     $rangeHeader = '';
     if (!empty($byteRange)) {
         $rangeHeader = 'Range: bytes=' . $byteRange[0] . '-' . $byteRange[1] . "\r\n";
     }
     // proxy configuration
     $proxyHost = Config::getInstance()->proxy['host'];
     $proxyPort = Config::getInstance()->proxy['port'];
     $proxyUser = Config::getInstance()->proxy['username'];
     $proxyPassword = Config::getInstance()->proxy['password'];
     $aUrl = trim($aUrl);
     // other result data
     $status = null;
     $headers = array();
     if ($method == 'socket') {
         if (!self::isSocketEnabled()) {
             // can be triggered in tests
             throw new Exception("HTTP socket support is not enabled (php function fsockopen is not available) ");
         }
         // initialization
         $url = @parse_url($aUrl);
         if ($url === false || !isset($url['scheme'])) {
             throw new Exception('Malformed URL: ' . $aUrl);
         }
         if ($url['scheme'] != 'http') {
             throw new Exception('Invalid protocol/scheme: ' . $url['scheme']);
         }
         $host = $url['host'];
         $port = isset($url['port)']) ? $url['port'] : 80;
         $path = isset($url['path']) ? $url['path'] : '/';
         if (isset($url['query'])) {
             $path .= '?' . $url['query'];
         }
         $errno = null;
         $errstr = null;
         if (!empty($proxyHost) && !empty($proxyPort) || !empty($byteRange)) {
             $httpVer = '1.1';
         } else {
             $httpVer = '1.0';
         }
         $proxyAuth = null;
         if (!empty($proxyHost) && !empty($proxyPort)) {
             $connectHost = $proxyHost;
             $connectPort = $proxyPort;
             if (!empty($proxyUser) && !empty($proxyPassword)) {
                 $proxyAuth = 'Proxy-Authorization: Basic ' . base64_encode("{$proxyUser}:{$proxyPassword}") . "\r\n";
             }
             $requestHeader = "{$httpMethod} {$aUrl} HTTP/{$httpVer}\r\n";
         } else {
             $connectHost = $host;
             $connectPort = $port;
             $requestHeader = "{$httpMethod} {$path} HTTP/{$httpVer}\r\n";
         }
         // connection attempt
         if (($fsock = @fsockopen($connectHost, $connectPort, $errno, $errstr, $timeout)) === false || !is_resource($fsock)) {
             if (is_resource($file)) {
                 @fclose($file);
             }
             throw new Exception("Error while connecting to: {$host}. Please try again later. {$errstr}");
         }
         // send HTTP request header
         $requestHeader .= "Host: {$host}" . ($port != 80 ? ':' . $port : '') . "\r\n" . ($proxyAuth ? $proxyAuth : '') . 'User-Agent: ' . $userAgent . "\r\n" . ($acceptLanguage ? $acceptLanguage . "\r\n" : '') . $xff . "\r\n" . $via . "\r\n" . $rangeHeader . "Connection: close\r\n" . "\r\n";
         fwrite($fsock, $requestHeader);
         $streamMetaData = array('timed_out' => false);
         @stream_set_blocking($fsock, true);
         if (function_exists('stream_set_timeout')) {
             @stream_set_timeout($fsock, $timeout);
         } elseif (function_exists('socket_set_timeout')) {
             @socket_set_timeout($fsock, $timeout);
         }
         // process header
         $status = null;
         while (!feof($fsock)) {
             $line = fgets($fsock, 4096);
             $streamMetaData = @stream_get_meta_data($fsock);
             if ($streamMetaData['timed_out']) {
                 if (is_resource($file)) {
                     @fclose($file);
                 }
                 @fclose($fsock);
                 throw new Exception('Timed out waiting for server response');
             }
             // a blank line marks the end of the server response header
             if (rtrim($line, "\r\n") == '') {
                 break;
             }
             // parse first line of server response header
             if (!$status) {
                 // expect first line to be HTTP response status line, e.g., HTTP/1.1 200 OK
                 if (!preg_match('~^HTTP/(\\d\\.\\d)\\s+(\\d+)(\\s*.*)?~', $line, $m)) {
                     if (is_resource($file)) {
                         @fclose($file);
                     }
                     @fclose($fsock);
                     throw new Exception('Expected server response code.  Got ' . rtrim($line, "\r\n"));
                 }
                 $status = (int) $m[2];
                 // Informational 1xx or Client Error 4xx
                 if ($status < 200 || $status >= 400) {
                     if (is_resource($file)) {
                         @fclose($file);
                     }
                     @fclose($fsock);
                     if (!$getExtendedInfo) {
                         return false;
                     } else {
                         return array('status' => $status);
                     }
                 }
                 continue;
             }
             // handle redirect
             if (preg_match('/^Location:\\s*(.+)/', rtrim($line, "\r\n"), $m)) {
                 if (is_resource($file)) {
                     @fclose($file);
                 }
                 @fclose($fsock);
                 // Successful 2xx vs Redirect 3xx
                 if ($status < 300) {
                     throw new Exception('Unexpected redirect to Location: ' . rtrim($line) . ' for status code ' . $status);
                 }
                 return self::sendHttpRequestBy($method, trim($m[1]), $timeout, $userAgent, $destinationPath, $file, $followDepth + 1, $acceptLanguage, $acceptInvalidSslCertificate = false, $byteRange, $getExtendedInfo, $httpMethod);
             }
             // save expected content length for later verification
             if (preg_match('/^Content-Length:\\s*(\\d+)/', $line, $m)) {
                 $contentLength = (int) $m[1];
             }
             self::parseHeaderLine($headers, $line);
         }
         if (feof($fsock) && $httpMethod != 'HEAD') {
             throw new Exception('Unexpected end of transmission');
         }
         // process content/body
         $response = '';
         while (!feof($fsock)) {
             $line = fread($fsock, 8192);
             $streamMetaData = @stream_get_meta_data($fsock);
             if ($streamMetaData['timed_out']) {
                 if (is_resource($file)) {
                     @fclose($file);
                 }
                 @fclose($fsock);
                 throw new Exception('Timed out waiting for server response');
             }
             $fileLength += strlen($line);
             if (is_resource($file)) {
                 // save to file
                 fwrite($file, $line);
             } else {
                 // concatenate to response string
                 $response .= $line;
             }
         }
         // determine success or failure
         @fclose(@$fsock);
     } else {
         if ($method == 'fopen') {
             $response = false;
             // we make sure the request takes less than a few seconds to fail
             // we create a stream_context (works in php >= 5.2.1)
             // we also set the socket_timeout (for php < 5.2.1)
             $default_socket_timeout = @ini_get('default_socket_timeout');
             @ini_set('default_socket_timeout', $timeout);
             $ctx = null;
             if (function_exists('stream_context_create')) {
                 $stream_options = array('http' => array('header' => 'User-Agent: ' . $userAgent . "\r\n" . ($acceptLanguage ? $acceptLanguage . "\r\n" : '') . $xff . "\r\n" . $via . "\r\n" . $rangeHeader, 'max_redirects' => 5, 'timeout' => $timeout));
                 if (!empty($proxyHost) && !empty($proxyPort)) {
                     $stream_options['http']['proxy'] = 'tcp://' . $proxyHost . ':' . $proxyPort;
                     $stream_options['http']['request_fulluri'] = true;
                     // required by squid proxy
                     if (!empty($proxyUser) && !empty($proxyPassword)) {
                         $stream_options['http']['header'] .= 'Proxy-Authorization: Basic ' . base64_encode("{$proxyUser}:{$proxyPassword}") . "\r\n";
                     }
                 }
                 $ctx = stream_context_create($stream_options);
             }
             // save to file
             if (is_resource($file)) {
                 $handle = fopen($aUrl, 'rb', false, $ctx);
                 while (!feof($handle)) {
                     $response = fread($handle, 8192);
                     $fileLength += strlen($response);
                     fwrite($file, $response);
                 }
                 fclose($handle);
             } else {
                 $response = file_get_contents($aUrl, 0, $ctx);
                 $fileLength = strlen($response);
             }
             // restore the socket_timeout value
             if (!empty($default_socket_timeout)) {
                 @ini_set('default_socket_timeout', $default_socket_timeout);
             }
         } else {
             if ($method == 'curl') {
                 if (!self::isCurlEnabled()) {
                     // can be triggered in tests
                     throw new Exception("CURL is not enabled in php.ini, but is being used.");
                 }
                 $ch = @curl_init();
                 if (!empty($proxyHost) && !empty($proxyPort)) {
                     @curl_setopt($ch, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
                     if (!empty($proxyUser) && !empty($proxyPassword)) {
                         // PROXYAUTH defaults to BASIC
                         @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPassword);
                     }
                 }
                 $curl_options = array(CURLOPT_BINARYTRANSFER => is_resource($file), CURLOPT_URL => $aUrl, CURLOPT_USERAGENT => $userAgent, CURLOPT_HTTPHEADER => array($xff, $via, $rangeHeader, $acceptLanguage), CURLOPT_HEADER => is_resource($file) ? false : true, CURLOPT_CONNECTTIMEOUT => $timeout);
                 // Case core:archive command is triggering archiving on https:// and the certificate is not valid
                 if ($acceptInvalidSslCertificate) {
                     $curl_options += array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false);
                 }
                 @curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
                 if ($httpMethod == 'HEAD') {
                     @curl_setopt($ch, CURLOPT_NOBODY, true);
                 }
                 @curl_setopt_array($ch, $curl_options);
                 self::configCurlCertificate($ch);
                 /*
                  * as of php 5.2.0, CURLOPT_FOLLOWLOCATION can't be set if
                  * in safe_mode or open_basedir is set
                  */
                 if ((string) ini_get('safe_mode') == '' && ini_get('open_basedir') == '') {
                     $curl_options = array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5);
                     @curl_setopt_array($ch, $curl_options);
                 }
                 if (is_resource($file)) {
                     // write output directly to file
                     @curl_setopt($ch, CURLOPT_FILE, $file);
                 } else {
                     // internal to ext/curl
                     @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 }
                 ob_start();
                 $response = @curl_exec($ch);
                 ob_end_clean();
                 if ($response === true) {
                     $response = '';
                 } else {
                     if ($response === false) {
                         $errstr = curl_error($ch);
                         if ($errstr != '') {
                             throw new Exception('curl_exec: ' . $errstr . '. Hostname requested was: ' . UrlHelper::getHostFromUrl($aUrl));
                         }
                         $response = '';
                     } else {
                         $header = '';
                         // redirects are included in the output html, so we look for the last line that starts w/ HTTP/...
                         // to split the response
                         while (substr($response, 0, 5) == "HTTP/") {
                             list($header, $response) = explode("\r\n\r\n", $response, 2);
                         }
                         foreach (explode("\r\n", $header) as $line) {
                             self::parseHeaderLine($headers, $line);
                         }
                     }
                 }
                 $contentLength = @curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
                 $fileLength = is_resource($file) ? @curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) : strlen($response);
                 $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
                 @curl_close($ch);
                 unset($ch);
             } else {
                 throw new Exception('Invalid request method: ' . $method);
             }
         }
     }
     if (is_resource($file)) {
         fflush($file);
         @fclose($file);
         $fileSize = filesize($destinationPath);
         if ($contentLength > 0 && $fileLength != $contentLength || $fileSize != $fileLength) {
             throw new Exception('File size error: ' . $destinationPath . '; expected ' . $contentLength . ' bytes; received ' . $fileLength . ' bytes; saved ' . $fileSize . ' bytes to file');
         }
         return true;
     }
     if (!$getExtendedInfo) {
         return trim($response);
     } else {
         return array('status' => $status, 'headers' => $headers, 'data' => $response);
     }
 }
Ejemplo n.º 22
0
 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";
 }
Ejemplo n.º 23
0
 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;
 }
<?php 
$_GET['idSite'] = $idSite;
define('PIWIK_INCLUDE_PATH', '../..');
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
FrontController::getInstance()->init();
$widgets = WidgetsList::get();
foreach ($widgets as $category => $widgetsInCategory) {
    echo '<h2>' . $category . '</h2>';
    foreach ($widgetsInCategory as $widget) {
        echo '<h3>' . $widget['name'] . '</h3>';
        $widgetUrl = UrlHelper::getArrayFromQueryString($url);
        $widgetUrl['moduleToWidgetize'] = $widget['parameters']['module'];
        $widgetUrl['actionToWidgetize'] = $widget['parameters']['action'];
        $parameters = $widget['parameters'];
        unset($parameters['module']);
        unset($parameters['action']);
        foreach ($parameters as $name => $value) {
            if (is_array($value)) {
                $value = current($value);
            }
            $widgetUrl[$name] = $value;
        }
        $widgetUrl = Url::getQueryStringFromParameters($widgetUrl);
        echo '<div id="widgetIframe"><iframe width="500" height="350"
			src="' . $widgetUrl . '" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe></div>';
    }
Ejemplo n.º 25
0
 /**
  * @return string
  */
 protected function getParameterValueFromReferrerUrl($adsenseReferrerParameter)
 {
     $value = trim(urldecode(UrlHelper::getParameterFromQueryString($this->referrerUrlParse['query'], $adsenseReferrerParameter)));
     return $value;
 }
Ejemplo n.º 26
0
 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;
 }
Ejemplo n.º 27
0
 /**
  * Given a list of default parameters to set, returns the URLs of APIs to call
  * If any API was specified in $this->apiNotToCall we ensure only these are tested.
  * If any API is set as excluded (see list below) then it will be ignored.
  *
  * @param array $parametersToSet Parameters to set in api call
  * @param array $formats         Array of 'format' to fetch from API
  * @param array $periods         Array of 'period' to query API
  * @param bool  $supertableApi
  * @param bool  $setDateLastN    If set to true, the 'date' parameter will be rewritten to query instead a range of dates, rather than one period only.
  * @param bool|string $language        2 letter language code, defaults to default piwik language
  * @param bool|string $fileExtension
  *
  * @throws Exception
  *
  * @return array of API URLs query strings
  */
 protected function generateApiUrlPermutations($parametersToSet)
 {
     $formats = array($this->testConfig->format);
     $originalDate = $parametersToSet['date'];
     $requestUrls = array();
     $apiMetadata = new DocumentationGenerator();
     // Get the URLs to query against the API for all functions starting with get*
     foreach ($this->getAllApiMethods() as $apiMethodInfo) {
         list($class, $moduleName, $methodName) = $apiMethodInfo;
         $apiId = $moduleName . '.' . $methodName;
         foreach ($this->testConfig->periods as $period) {
             $parametersToSet['period'] = $period;
             // If date must be a date range, we process this date range by adding 6 periods to it
             if ($this->testConfig->setDateLastN) {
                 if (!isset($parametersToSet['dateRewriteBackup'])) {
                     $parametersToSet['dateRewriteBackup'] = $parametersToSet['date'];
                 }
                 $lastCount = $this->testConfig->setDateLastN;
                 $secondDate = date('Y-m-d', strtotime("+{$lastCount} " . $period . "s", strtotime($originalDate)));
                 $parametersToSet['date'] = $originalDate . ',' . $secondDate;
             }
             // Set response language
             if ($this->testConfig->language !== false) {
                 $parametersToSet['language'] = $this->testConfig->language;
             }
             // set idSubtable if subtable API is set
             if ($this->testConfig->supertableApi !== false) {
                 $request = new Request(array('module' => 'API', 'method' => $this->testConfig->supertableApi, 'idSite' => $parametersToSet['idSite'], 'period' => $parametersToSet['period'], 'date' => $parametersToSet['date'], 'format' => 'php', 'serialize' => 0));
                 $content = $request->process();
                 SystemTestCase::assertApiResponseHasNoError($content);
                 // find first row w/ subtable
                 foreach ($content as $row) {
                     if (isset($row['idsubdatatable'])) {
                         $parametersToSet['idSubtable'] = $row['idsubdatatable'];
                         break;
                     }
                 }
                 // if no subtable found, throw
                 if (!isset($parametersToSet['idSubtable'])) {
                     throw new Exception("Cannot find subtable to load for {$apiId} in {$this->testConfig->supertableApi}.");
                 }
             }
             // Generate for each specified format
             foreach ($formats as $format) {
                 $parametersToSet['format'] = $format;
                 $parametersToSet['hideIdSubDatable'] = 1;
                 $parametersToSet['serialize'] = 1;
                 $exampleUrl = $apiMetadata->getExampleUrl($class, $methodName, $parametersToSet);
                 if ($exampleUrl === false) {
                     continue;
                 }
                 // Remove the first ? in the query string
                 $exampleUrl = substr($exampleUrl, 1);
                 $apiRequestId = $apiId;
                 if (strpos($exampleUrl, 'period=') !== false) {
                     $apiRequestId .= '_' . $period;
                 }
                 $apiRequestId .= '.' . $format;
                 if ($this->testConfig->fileExtension) {
                     $apiRequestId .= '.' . $this->testConfig->fileExtension;
                 }
                 $requestUrls[$apiRequestId] = UrlHelper::getArrayFromQueryString($exampleUrl);
             }
         }
     }
     return $requestUrls;
 }
Ejemplo n.º 28
0
 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;
 }
Ejemplo n.º 29
0
 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);
 }
 /**
  * @group Core
  */
 public function test_getQueryFromUrl_ShouldAddAdditionalParams_IfGiven()
 {
     $this->assertEquals('foo=bar&foo2=bar2&test[]=1&add=foo', UrlHelper::getQueryFromUrl('http://example.com/?foo=bar&foo2=bar2&test[]=1', array('add' => 'foo')));
     $this->assertEquals('add=foo', UrlHelper::getQueryFromUrl('/', array('add' => 'foo')));
     $this->assertEquals('add[]=foo&add[]=test', UrlHelper::getQueryFromUrl('/', array('add' => array('foo', 'test'))));
 }