Skip to content

Commit

Permalink
set user agent when retrieving favicons
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Vidulich <[email protected]>
  • Loading branch information
zl4bv committed Sep 29, 2024
1 parent 41da35c commit afc7311
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\App;

use OCA\News\Fetcher\FaviconDataAccess;
use OCA\News\Fetcher\FeedFetcher;
use OCA\News\Fetcher\Fetcher;
use OCP\User\Events\BeforeUserDeletedEvent;
Expand Down Expand Up @@ -135,9 +136,15 @@ public function register(IRegistrationContext $context): void
return new Explorer($config->getClient(), $c->get(LoggerInterface::class));
});

$context->registerService(FaviconDataAccess::class, function (ContainerInterface $c): FaviconDataAccess {
$config = $c->get(FetcherConfig::class);
return new FaviconDataAccess($config);
});

$context->registerService(Favicon::class, function (ContainerInterface $c): Favicon {
$favicon = new Favicon();
$favicon->cache(['dir' => $c->get(Cache::class)->getCache("feedFavicon")]);
$favicon->setDataAccess($c->get(FaviconDataAccess::class));
return $favicon;
});
}
Expand Down
61 changes: 61 additions & 0 deletions lib/Fetcher/FaviconDataAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Nextcloud - News
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Ben Vidulich <[email protected]>
* @copyright 2024 Ben Vidulich
*/

namespace OCA\News\Fetcher;

use Favicon\DataAccess;

use OCA\News\Config\FetcherConfig;

/**
* Modified version of DataAccess with a configurable user agent header.
*/
class FaviconDataAccess extends DataAccess
{
/**
* @var FetcherConfig
*/
private $fetcherConfig;

public function __construct(
FetcherConfig $fetcherConfig,
) {
$this->fetcherConfig = $fetcherConfig;
}

public function retrieveUrl($url)
{
$this->setContext();
return @file_get_contents($url);
}

public function retrieveHeader($url)
{
$this->setContext();
$headers = @get_headers($url, 1);
return is_array($headers) ? array_change_key_case($headers) : [];
}

private function setContext()
{
stream_context_set_default(
[
'http' => [
'method' => 'GET',
'follow_location' => 0,
'max_redirects' => 1,
'timeout' => 10,
'header' => 'User-Agent: ' . $this->fetcherConfig->getUserAgent(),
]
]
);
}
}
2 changes: 1 addition & 1 deletion lib/Fetcher/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected function getFavicon(FeedInterface $feed, string $url): ?string
$favicon = trim($feed_logo);
}

ini_set('user_agent', FetcherConfig::DEFAULT_USER_AGENT);
ini_set('user_agent', $this->fetcherConfig->getUserAgent());

$base_url = new Net_URL2($url);
$base_url->setPath("");
Expand Down

0 comments on commit afc7311

Please sign in to comment.