/** * Return true if the content filtering is enabled * * @access public * @return boolean */ public function isFilteringEnabled() { if ($this->config === null) { return $this->enable_filter; } return $this->config->getContentFiltering($this->enable_filter); }
public function testOverrideFilters() { $data = '<iframe src="http://www.kickstarter.com/projects/lefnire/habitrpg-mobile/widget/video.html" height="480" width="640" frameborder="0"></iframe>'; $expected = '<iframe src="https://www.kickstarter.com/projects/lefnire/habitrpg-mobile/widget/video.html" height="480" width="640" frameborder="0"></iframe>'; $f = Filter::html($data, 'http://blabla'); $f->attribute->setIframeWhitelist(array('http://www.kickstarter.com')); $this->assertEquals($expected, $f->execute()); $data = '<iframe src="http://www.youtube.com/bla" height="480" width="640" frameborder="0"></iframe>'; $f = Filter::html($data, 'http://blabla'); $f->attribute->setIframeWhitelist(array('http://www.kickstarter.com')); $this->assertEmpty($f->execute()); $config = new Config(); $config->setFilterWhitelistedTags(array('p' => array('title'))); $f = Filter::html('<p>Test<strong>boo</strong></p>', 'http://blabla'); $f->setConfig($config); $this->assertEquals('<p>Testboo</p>', $f->execute()); }
/** * Get the list of folders that contains rules. * * @return array */ public function getRulesFolders() { $folders = array(__DIR__ . '/../Rules'); if ($this->config !== null && $this->config->getGrabberRulesFolder() !== null) { $folders[] = $this->config->getGrabberRulesFolder(); } return $folders; }
function addProxyToTags($html, $website, $proxy_images, $cloak_referrer) { if ($html === '' || !$cloak_referrer && !$proxy_images || !$cloak_referrer && $proxy_images && !Helper\isSecureConnection()) { // only proxy enabled, but not connected via HTTPS return $html; } $config = new PicoFeedConfig(); $config->setFilterImageProxyUrl('?action=proxy&url=%s'); if (!$cloak_referrer && $proxy_images) { // image proxy mode only: https links do not need to be proxied, since // they do not trigger mixed content warnings. $config->setFilterImageProxyProtocol('http'); } elseif (!$proxy_images && $cloak_referrer && Helper\isSecureConnection()) { // cloaking mode only: if a request from a HTTPS connection to a HTTP // connection is made, the referrer will be omitted by the browser. // Only the referrer for HTTPS to HTTPs requests needs to be cloaked. $config->setFilterImageProxyProtocol('https'); } $filter = Filter::html($html, $website); $filter->setConfig($config); return $filter->execute(); }
/** * Get a parser instance * * @access public * @param string $url Site url * @param string $content Feed content * @param string $encoding HTTP encoding * @return \PicoFeed\Parser\Parser */ public function getParser($url, $content, $encoding) { $format = $this->detectFormat($content); if (empty($format)) { throw new UnsupportedFeedFormatException('Unable to detect feed format'); } $className = '\\PicoFeed\\Parser\\' . $format; $parser = new $className($content, $encoding, $url); $parser->setHashAlgo($this->config->getParserHashAlgo()); $parser->setTimezone($this->config->getTimezone()); $parser->setConfig($this->config); return $parser; }
public function testGetRulesFolders() { // No custom path $loader = new RuleLoader(new Config()); $dirs = $loader->getRulesFolders(); $this->assertNotEmpty($dirs); $this->assertCount(1, $dirs); $this->assertTrue(strpos($dirs[0], '/../Rules') !== false); // Custom path $config = new Config(); $config->setGrabberRulesFolder('/foobar/rules'); $loader = new RuleLoader($config); $dirs = $loader->getRulesFolders(); $this->assertNotEmpty($dirs); $this->assertCount(2, $dirs); $this->assertTrue(strpos($dirs[0], '/../Rules') !== false); $this->assertEquals('/foobar/rules', $dirs[1]); // No custom path with empty config object $loader = new RuleLoader(new Config()); $dirs = $loader->getRulesFolders(); $this->assertNotEmpty($dirs); $this->assertCount(1, $dirs); $this->assertTrue(strpos($dirs[0], '/../Rules') !== false); }
/** * Set config object. * * @param \PicoFeed\Config\Config $config Config instance * * @return \PicoFeed\Filter\Html */ public function setConfig($config) { $this->config = $config; if ($this->config !== null) { $this->attribute->setImageProxyCallback($this->config->getFilterImageProxyCallback()); $this->attribute->setImageProxyUrl($this->config->getFilterImageProxyUrl()); $this->attribute->setImageProxyProtocol($this->config->getFilterImageProxyProtocol()); $this->attribute->setIframeWhitelist($this->config->getFilterIframeWhitelist(array())); $this->attribute->setIntegerAttributes($this->config->getFilterIntegerAttributes(array())); $this->attribute->setAttributeOverrides($this->config->getFilterAttributeOverrides(array())); $this->attribute->setRequiredAttributes($this->config->getFilterRequiredAttributes(array())); $this->attribute->setMediaBlacklist($this->config->getFilterMediaBlacklist(array())); $this->attribute->setMediaAttributes($this->config->getFilterMediaAttributes(array())); $this->attribute->setSchemeWhitelist($this->config->getFilterSchemeWhitelist(array())); $this->attribute->setWhitelistedAttributes($this->config->getFilterWhitelistedTags(array())); $this->tag->setWhitelistedTags(array_keys($this->config->getFilterWhitelistedTags(array()))); } return $this; }
/** * Download the HTML content. * * @return bool */ public function download() { if (!empty($this->url)) { // Clear everything $this->html = ''; $this->content = ''; $this->encoding = ''; try { $client = Client::getInstance(); $client->setConfig($this->config); $client->setTimeout($this->config->getGrabberTimeout()); $client->setUserAgent($this->config->getGrabberUserAgent()); $client->execute($this->url); $this->url = $client->getUrl(); $this->html = $client->getContent(); $this->encoding = $client->getEncoding(); return true; } catch (ClientException $e) { Logger::setMessage(get_called_class() . ': ' . $e->getMessage()); } } return false; }
/** * Set config object. * * @param \PicoFeed\Config\Config $config Config instance * * @return \PicoFeed\Client\Client */ public function setConfig(Config $config) { if ($config !== null) { $this->setTimeout($config->getClientTimeout()); $this->setUserAgent($config->getClientUserAgent()); $this->setMaxRedirections($config->getMaxRedirections()); $this->setMaxBodySize($config->getMaxBodySize()); $this->setProxyHostname($config->getProxyHostname()); $this->setProxyPort($config->getProxyPort()); $this->setProxyUsername($config->getProxyUsername()); $this->setProxyPassword($config->getProxyPassword()); } return $this; }
function get_reader_config() { $config = new ReaderConfig(); $config->setTimezone(get('timezone')); // Client $config->setClientTimeout(HTTP_TIMEOUT); $config->setClientUserAgent(HTTP_USER_AGENT); // Grabber $config->setGrabberRulesFolder(RULES_DIRECTORY); // Proxy $config->setProxyHostname(PROXY_HOSTNAME); $config->setProxyPort(PROXY_PORT); $config->setProxyUsername(PROXY_USERNAME); $config->setProxyPassword(PROXY_PASSWORD); // Filter $config->setFilterIframeWhitelist(get_iframe_whitelist()); if ((bool) get('debug_mode')) { Logger::enable(); } // Parser $config->setParserHashAlgo('crc32b'); return $config; }
public function __construct(array $urlParams = []) { parent::__construct('news', $urlParams); // files $this->registerFileContents('checksums', 'checksum.json'); $this->registerFileContents('info', 'info.xml'); // parameters $this->registerParameter('exploreDir', __DIR__ . '/../explore'); $this->registerParameter('configFile', 'config.ini'); // factories $this->registerFactory(ItemMapper::class, MapperFactory::class); /** * App config parser */ $this->registerService(AppConfig::class, function ($c) { $config = new AppConfig($c->query(INavigationManager::class), $c->query(IURLGenerator::class)); $config->loadConfig($c->query('info')); return $config; }); /** * Core */ $this->registerService('LoggerParameters', function ($c) { return ['app' => $c->query('AppName')]; }); $this->registerService('databaseType', function ($c) { return $c->query(IConfig::class)->getSystemValue('dbtype'); }); $this->registerService('ConfigView', function ($c) { $fs = $c->query(IRootFolder::class); $path = 'news/config'; if ($fs->nodeExists($path)) { return $fs->get($path); } else { return $fs->newFolder($path); } }); $this->registerService(Config::class, function ($c) { $config = new Config($c->query('ConfigView'), $c->query(ILogger::class), $c->query('LoggerParameters')); $config->read($c->query('configFile'), true); return $config; }); $this->registerService(HTMLPurifier::class, function ($c) { $directory = $c->query(IConfig::class)->getSystemValue('datadirectory') . '/news/cache/purifier'; if (!is_dir($directory)) { mkdir($directory, 0770, true); } $config = HTMLPurifier_Config::createDefault(); $config->set('HTML.ForbiddenAttributes', 'class'); $config->set('Cache.SerializerPath', $directory); $config->set('HTML.SafeIframe', true); $config->set('URI.SafeIframeRegexp', '%^https://(?:www\\.)?(' . 'youtube(?:-nocookie)?.com/embed/|' . 'player.vimeo.com/video/)%'); //allow YouTube and Vimeo $def = $config->getHTMLDefinition(true); $def->addAttribute('iframe', 'allowfullscreen', 'Bool'); return new HTMLPurifier($config); }); /** * Fetchers */ $this->registerService(PicoFeedConfig::class, function ($c) { // FIXME: move this into a separate class for testing? $config = $c->query(Config::class); $appConfig = $c->query(AppConfig::class); $proxy = $c->query(ProxyConfigParser::class); $userAgent = 'ownCloud News/' . $appConfig->getConfig('version') . ' (+https://owncloud.org/; 1 subscriber;)'; $pico = new PicoFeedConfig(); $pico->setClientUserAgent($userAgent)->setClientTimeout($config->getFeedFetcherTimeout())->setMaxRedirections($config->getMaxRedirects())->setMaxBodySize($config->getMaxSize())->setParserHashAlgo('md5'); // proxy settings $proxySettings = $proxy->parse(); $host = $proxySettings['host']; $port = $proxySettings['port']; $user = $proxySettings['user']; $password = $proxySettings['password']; if ($host) { $pico->setProxyHostname($host); if ($port) { $pico->setProxyPort($port); } } if ($user) { $pico->setProxyUsername($user)->setProxyPassword($password); } return $pico; }); $this->registerService(Fetcher::class, function ($c) { $fetcher = new Fetcher(); // register fetchers in order, the most generic fetcher should be // the last one $fetcher->registerFetcher($c->query(YoutubeFetcher::class)); $fetcher->registerFetcher($c->query(FeedFetcher::class)); return $fetcher; }); }
public function testsetFilterImageProxyCallback() { $config = new Config(); $config->setFilterImageProxyCallback(function ($image_url) { $key = hash_hmac('sha1', $image_url, 'secret'); return 'https://mypublicproxy/' . $key . '/' . rawurlencode($image_url); }); $f = Filter::html('<p>Image <img src="/image.png" alt="My Image"/></p>', 'http://foo'); $f->setConfig($config); $this->assertEquals('<p>Image <img src="https://mypublicproxy/4924964043f3119b3cf2b07b1922d491bcc20092/' . rawurlencode('http://foo/image.png') . '" alt="My Image"/></p>', $f->execute()); }
public function __construct(array $urlParams = []) { parent::__construct('news', $urlParams); $container = $this->getContainer(); /** * Mappers */ $container->registerService('OCA\\News\\Db\\ItemMapper', function ($c) { return $c->query('OCA\\News\\Db\\MapperFactory')->getItemMapper($c->query('OCP\\IDBConnection')); }); /** * App config parser */ $container->registerService('OCA\\News\\Config\\AppConfig', function ($c) { $config = new AppConfig($c->query('OCP\\INavigationManager'), $c->query('OCP\\IURLGenerator')); $config->loadConfig(__DIR__ . '/info.xml'); return $config; }); /** * Core */ $container->registerService('LoggerParameters', function ($c) { return ['app' => $c->query('AppName')]; }); $container->registerService('DatabaseType', function ($c) { return $c->query('OCP\\IConfig')->getSystemValue('dbtype'); }); /** * Utility */ $container->registerService('ConfigPath', function () { return 'config.ini'; }); $container->registerService('ConfigView', function ($c) { $fs = $c->query('OCP\\Files\\IRootFolder'); $path = 'news/config'; if ($fs->nodeExists($path)) { return $fs->get($path); } else { return $fs->newFolder($path); } }); $container->registerService('OCA\\News\\Config\\Config', function ($c) { $config = new Config($c->query('ConfigView'), $c->query('OCP\\ILogger'), $c->query('LoggerParameters')); $config->read($c->query('ConfigPath'), true); return $config; }); $container->registerService('HTMLPurifier', function ($c) { $directory = $c->query('OCP\\IConfig')->getSystemValue('datadirectory') . '/news/cache/purifier'; if (!is_dir($directory)) { mkdir($directory, 0770, true); } $config = HTMLPurifier_Config::createDefault(); $config->set('HTML.ForbiddenAttributes', 'class'); $config->set('Cache.SerializerPath', $directory); $config->set('HTML.SafeIframe', true); $config->set('URI.SafeIframeRegexp', '%^https://(?:www\\.)?(' . 'youtube(?:-nocookie)?.com/embed/|' . 'player.vimeo.com/video/)%'); //allow YouTube and Vimeo return new HTMLPurifier($config); }); $container->registerService('OCA\\News\\ArticleEnhancer\\Enhancer', function ($c) { $enhancer = new Enhancer(); // register simple enhancers from config json file $xpathEnhancerConfig = file_get_contents(__DIR__ . '/../articleenhancer/xpathenhancers.json'); $xpathEnhancerConfig = json_decode($xpathEnhancerConfig, true); foreach ($xpathEnhancerConfig as $feed => $config) { $articleEnhancer = new XPathArticleEnhancer($c->query('OCA\\News\\Utility\\PicoFeedClientFactory'), $config); $enhancer->registerEnhancer($feed, $articleEnhancer); } $regexEnhancerConfig = file_get_contents(__DIR__ . '/../articleenhancer/regexenhancers.json'); $regexEnhancerConfig = json_decode($regexEnhancerConfig, true); foreach ($regexEnhancerConfig as $feed => $config) { foreach ($config as $matchArticleUrl => $regex) { $articleEnhancer = new RegexArticleEnhancer($matchArticleUrl, $regex); $enhancer->registerEnhancer($feed, $articleEnhancer); } } return $enhancer; }); /** * Fetchers */ $container->registerService('PicoFeed\\Config\\Config', function ($c) { // FIXME: move this into a separate class for testing? $config = $c->query('OCA\\News\\Config\\Config'); $appConfig = $c->query('OCA\\News\\Config\\AppConfig'); $proxy = $c->query('OCA\\News\\Utility\\ProxyConfigParser'); $userAgent = 'ownCloud News/' . $appConfig->getConfig('version') . ' (+https://owncloud.org/; 1 subscriber;)'; $pico = new PicoFeedConfig(); $pico->setClientUserAgent($userAgent)->setClientTimeout($config->getFeedFetcherTimeout())->setMaxRedirections($config->getMaxRedirects())->setMaxBodySize($config->getMaxSize())->setParserHashAlgo('md5'); // proxy settings $proxySettings = $proxy->parse(); $host = $proxySettings['host']; $port = $proxySettings['port']; $user = $proxySettings['user']; $password = $proxySettings['password']; if ($host) { $pico->setProxyHostname($host); if ($port) { $pico->setProxyPort($port); } } if ($user) { $pico->setProxyUsername($user)->setProxyPassword($password); } return $pico; }); $container->registerService('OCA\\News\\Fetcher\\Fetcher', function ($c) { $fetcher = new Fetcher(); // register fetchers in order // the most generic fetcher should be the last one $fetcher->registerFetcher($c->query('OCA\\News\\Fetcher\\YoutubeFetcher')); $fetcher->registerFetcher($c->query('OCA\\News\\Fetcher\\FeedFetcher')); return $fetcher; }); $container->registerService('OCA\\News\\Explore\\RecommendedSites', function () { return new RecommendedSites(__DIR__ . '/../explore'); }); }
/** * Constructor. * * @param \PicoFeed\Config\Config $config Config class instance */ public function __construct(Config $config = null) { $this->config = $config ?: new Config(); Logger::setTimezone($this->config->getTimezone()); }
/** * Check if a tag is on the whitelist. * * @param string $tag Tag name * * @return bool */ public function isAllowedTag($tag) { return in_array($tag, array_merge($this->tag_whitelist, array_keys($this->config->getFilterWhitelistedTags(array())))); }