fetchRemoteFile() public static method

Fetches a file located at $url and saves it to $destinationPath.
public static fetchRemoteFile ( string $url, string $destinationPath = null, integer $tries, integer $timeout = 10 ) : boolean
$url string The URL of the file to download.
$destinationPath string The path to download the file to.
$tries integer (deprecated)
$timeout integer The amount of seconds to wait before aborting the HTTP request.
return boolean `true` on success, throws Exception on failure
Exemplo n.º 1
0
 public function get()
 {
     try {
         $content = Http::fetchRemoteFile($this->url);
         $rss = simplexml_load_string($content);
     } catch (\Exception $e) {
         echo "Error while importing feed: {$e->getMessage()}\n";
         exit;
     }
     $output = '<div style="padding:10px 15px;"><ul class="rss">';
     $i = 0;
     $items = array();
     if (!empty($rss->channel->item)) {
         $items = $rss->channel->item;
     }
     foreach ($items as $post) {
         $title = $post->title;
         $date = @strftime("%B %e, %Y", strtotime($post->pubDate));
         $link = $post->link;
         $output .= '<li><a class="rss-title" title="" target="_blank" href="?module=Proxy&action=redirect&url=' . $link . '">' . $title . '</a>' . '<span class="rss-date">' . $date . '</span>';
         if ($this->showDescription) {
             $output .= '<div class="rss-description">' . $post->description . '</div>';
         }
         if ($this->showContent) {
             $output .= '<div class="rss-content">' . $post->content . '</div>';
         }
         $output .= '</li>';
         if (++$i == $this->count) {
             break;
         }
     }
     $output .= '</ul></div>';
     return $output;
 }
Exemplo n.º 2
0
 /**
  * @group Core
  */
 public function testFetchLatestZip()
 {
     $destinationPath = PIWIK_USER_PATH . '/tmp/latest/latest.zip';
     Http::fetchRemoteFile('http://builds.piwik.org/latest.zip', $destinationPath, 3, 30);
     $this->assertFileExists($destinationPath);
     $this->assertGreaterThan(0, filesize($destinationPath));
 }
Exemplo n.º 3
0
 public function testFetchLatestZip()
 {
     $destinationPath = PIWIK_USER_PATH . '/tmp/latest/latest.zip';
     Http::fetchRemoteFile(Fixture::getRootUrl() . 'tests/PHPUnit/Integration/Http/fixture.zip', $destinationPath, 3, 30);
     $this->assertFileExists($destinationPath);
     $this->assertGreaterThan(0, filesize($destinationPath));
 }
 public function download($pluginOrThemeName, $target)
 {
     $downloadUrl = $this->getDownloadUrl($pluginOrThemeName);
     if (empty($downloadUrl)) {
         return false;
     }
     $success = Http::fetchRemoteFile($downloadUrl, $target, 0, static::HTTP_REQUEST_TIMEOUT);
     return $success;
 }
Exemplo n.º 5
0
 private function trackVisits()
 {
     if (!$this->trackInvalidRequests) {
         return;
     }
     $dateTime = $this->dateTime;
     $idSite = $this->idSite;
     API::getInstance()->setSiteSpecificUserAgentExcludeEnabled(true);
     API::getInstance()->setGlobalExcludedUserAgents('globalexcludeduseragent');
     // Trigger empty request
     $trackerUrl = self::getTrackerUrl();
     $response = Http::fetchRemoteFile($trackerUrl);
     self::assertTrue(strpos($response, 'is a free open source web') !== false, 'Piwik empty request response not correct: ' . $response);
     $t = self::getTracker($idSite, $dateTime, $defaultInit = true);
     // test GoogleBot UA visitor
     $t->setUserAgent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
     self::checkResponse($t->doTrackPageView('bot visit, please do not record'));
     // Test IP Exclusion works with or without IP exclusion
     foreach (array(false, true) as $enable) {
         $excludedIp = '154.1.12.34';
         API::getInstance()->updateSite($idSite, 'new site name', $url = array('http://site.com'), $ecommerce = 0, $ss = 1, $ss_kwd = '', $ss_cat = '', $excludedIp . ',1.2.3.4', $excludedQueryParameters = null, $timezone = null, $currency = null, $group = null, $startDate = null, $excludedUserAgents = 'excludeduseragentstring');
         // Enable IP Anonymization
         $t->DEBUG_APPEND_URL = '&forceIpAnonymization=' . (int) $enable;
         // test with excluded User Agent
         $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729) (excludeduseragentstring)');
         $t->setIp('211.1.2.3');
         self::checkResponse($t->doTrackPageView('visit from excluded User Agent'));
         $t->setUserAgent('Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0 Google (+https://developers.google.com/+/web/snippet/)');
         self::checkResponse($t->doTrackPageView('visit from excluded User Agent'));
         // test w/ global excluded User Agent
         $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729) (globalexcludeduseragent)');
         $t->setIp('211.1.2.3');
         self::checkResponse($t->doTrackPageView('visit from global excluded User Agent'));
         // test with excluded IP
         $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)');
         // restore normal user agent
         $t->setIp($excludedIp);
         self::checkResponse($t->doTrackPageView('visit from IP excluded'));
         // test with global list of excluded IPs
         $excludedIpBis = '145.5.3.4';
         API::getInstance()->setGlobalExcludedIps($excludedIpBis);
         $t->setIp($excludedIpBis);
         self::checkResponse($t->doTrackPageView('visit from IP globally excluded'));
     }
     try {
         @$t->setAttributionInfo(array());
         self::fail();
     } catch (Exception $e) {
     }
     try {
         $t->setAttributionInfo(json_encode('test'));
         self::fail();
     } catch (Exception $e) {
     }
     $t->setAttributionInfo(json_encode(array()));
 }
Exemplo n.º 6
0
 private function oneClick_Download()
 {
     $pathPiwikZip = PIWIK_USER_PATH . self::PATH_TO_EXTRACT_LATEST_VERSION . 'latest.zip';
     $this->pathPiwikZip = SettingsPiwik::rewriteTmpPathWithInstanceId($pathPiwikZip);
     Filechecks::dieIfDirectoriesNotWritable(array(self::PATH_TO_EXTRACT_LATEST_VERSION));
     // we catch exceptions in the caller (i.e., oneClickUpdate)
     $url = self::getLatestZipUrl($this->newVersion) . '?cb=' . $this->newVersion;
     Http::fetchRemoteFile($url, $this->pathPiwikZip);
 }
Exemplo n.º 7
0
 private function downloadArchive($version, $url)
 {
     $path = $this->tmpPath . self::PATH_TO_EXTRACT_LATEST_VERSION;
     $archiveFile = $path . 'latest.zip';
     Filechecks::dieIfDirectoriesNotWritable(array($path));
     $url .= '?cb=' . $version;
     try {
         Http::fetchRemoteFile($url, $archiveFile, 0, self::DOWNLOAD_TIMEOUT);
     } catch (Exception $e) {
         // We throw a specific exception allowing to offer HTTP download if HTTPS failed
         throw new ArchiveDownloadException($e);
     }
     return $archiveFile;
 }
Exemplo n.º 8
0
 protected function fetchUnitData(OutputInterface $output, $langCode, $requestLangCode, &$translations)
 {
     $unitsUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-units-full/master/main/%s/units.json';
     try {
         $unitsData = Http::fetchRemoteFile(sprintf($unitsUrl, $requestLangCode));
         $unitsData = json_decode($unitsData, true);
         $unitsData = $unitsData['main'][$requestLangCode]['units'];
         $translations['Intl']['NSeconds'] = $this->replacePlaceHolder($unitsData['long']['duration-second']['unitPattern-count-other']);
         $translations['Intl']['NSecondsShort'] = $this->replacePlaceHolder($unitsData['narrow']['duration-second']['unitPattern-count-other']);
         $translations['Intl']['Seconds'] = $unitsData['long']['duration-second']['displayName'];
         $translations['Intl']['NMinutes'] = $this->replacePlaceHolder($unitsData['long']['duration-minute']['unitPattern-count-other']);
         if (isset($unitsData['long']['duration-minute']['unitPattern-count-one'])) {
             $translations['Intl']['OneMinute'] = $this->replacePlaceHolder($unitsData['long']['duration-minute']['unitPattern-count-one'], '1');
         } else {
             $translations['Intl']['OneMinute'] = $this->replacePlaceHolder($unitsData['long']['duration-minute']['unitPattern-count-other'], '1');
         }
         if (isset($unitsData['short']['duration-minute']['unitPattern-count-one'])) {
             $translations['Intl']['OneMinuteShort'] = $this->replacePlaceHolder($unitsData['short']['duration-minute']['unitPattern-count-one'], '1');
         } else {
             $translations['Intl']['OneMinuteShort'] = $this->replacePlaceHolder($unitsData['short']['duration-minute']['unitPattern-count-other'], '1');
         }
         $translations['Intl']['NMinutesShort'] = $this->replacePlaceHolder($unitsData['short']['duration-minute']['unitPattern-count-other']);
         $translations['Intl']['Minutes'] = $unitsData['long']['duration-minute']['displayName'];
         $translations['Intl']['Hours'] = $unitsData['long']['duration-hour']['displayName'];
         $translations['Intl']['NHoursShort'] = $this->replacePlaceHolder($unitsData['narrow']['duration-hour']['unitPattern-count-other']);
         $translations['Intl']['NDays'] = $this->replacePlaceHolder($unitsData['long']['duration-day']['unitPattern-count-other']);
         if (isset($unitsData['short']['duration-day']['unitPattern-count-one'])) {
             $translations['Intl']['OneDay'] = $this->replacePlaceHolder($unitsData['long']['duration-day']['unitPattern-count-one'], '1');
         } else {
             $translations['Intl']['OneDay'] = $this->replacePlaceHolder($unitsData['long']['duration-day']['unitPattern-count-other'], '1');
         }
         $translations['Intl']['PeriodWeeks'] = $unitsData['long']['duration-week']['displayName'];
         $translations['Intl']['PeriodYears'] = $unitsData['long']['duration-year']['displayName'];
         $translations['Intl']['PeriodDays'] = $unitsData['long']['duration-day']['displayName'];
         $translations['Intl']['PeriodMonths'] = $unitsData['long']['duration-month']['displayName'];
         $output->writeln('Saved unit data for ' . $langCode);
     } catch (\Exception $e) {
         $output->writeln('Unable to import unit data for ' . $langCode);
     }
 }
Exemplo n.º 9
0
 public function test_piwikJs_minified_isUpToDate()
 {
     Http::fetchRemoteFile('https://github.com/downloads/yui/yuicompressor/yuicompressor-2.4.7.zip', PIWIK_DOCUMENT_ROOT . '/tmp/yuicompressor.zip');
     shell_exec('unzip -n ' . PIWIK_DOCUMENT_ROOT . '/tmp/yuicompressor.zip');
     shell_exec("sed '/<DEBUG>/,/<\\/DEBUG>/d' < " . PIWIK_DOCUMENT_ROOT . "/js/piwik.js | sed 's/eval/replacedEvilString/' | java -jar yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar --type js --line-break 1000 | sed 's/replacedEvilString/eval/' | sed 's/^[/][*]/\\/*!/' > " . PIWIK_DOCUMENT_ROOT . "/piwik-minified.js");
     $this->assertFileEquals(PIWIK_DOCUMENT_ROOT . '/piwik-minified.js', PIWIK_DOCUMENT_ROOT . '/piwik.js', 'minified /piwik.js is out of date, please re-generate the minified files using instructions in /js/README');
     $this->assertFileEquals(PIWIK_DOCUMENT_ROOT . '/piwik-minified.js', PIWIK_DOCUMENT_ROOT . '/js/piwik.min.js', 'minified /js/piwik.min.js is out of date, please re-generate the minified files using instructions in /js/README');
 }