-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use updated user agent when fetching feeds and favicons
Signed-off-by: Ben Vidulich <ben@vidulich.nz>
- Loading branch information
Showing
6 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,12 @@ class FetcherConfig | |
*/ | ||
protected $redirects; | ||
|
||
/** | ||
* Version number for the news application. | ||
* @var string | ||
*/ | ||
private $version; | ||
|
||
/** | ||
* User agent for the client. | ||
* @var string | ||
|
@@ -75,6 +81,11 @@ public function __construct(IConfig $config) | |
'maxRedirects', | ||
Application::DEFAULT_SETTINGS['maxRedirects'] | ||
); | ||
$this->version = $config->getAppValue( | ||
Check failure on line 84 in lib/Config/FetcherConfig.php GitHub Actions / phpstan: Nextcloud pre-release with 8.2
|
||
Application::NAME, | ||
'installed_version', | ||
'1.0' | ||
); | ||
|
||
$proxy = $config->getSystemValue('proxy', null); | ||
if (is_null($proxy)) { | ||
|
@@ -127,7 +138,7 @@ public function getClient(): ClientInterface | |
$config = [ | ||
'timeout' => $this->client_timeout, | ||
'headers' => [ | ||
'User-Agent' => static::DEFAULT_USER_AGENT, | ||
'User-Agent' => $this->getUserAgent(), | ||
'Accept' => static::DEFAULT_ACCEPT, | ||
'Accept-Encoding' => $this->checkEncoding() | ||
], | ||
|
@@ -143,4 +154,18 @@ public function getClient(): ClientInterface | |
$client = new Client($config); | ||
return new FeedIoClient($client); | ||
} | ||
|
||
/** | ||
* Gets a user agent name for the client | ||
* | ||
* @return string | ||
*/ | ||
public function getUserAgent(): string | ||
{ | ||
if (is_null($this->version)) { | ||
return self::DEFAULT_USER_AGENT; | ||
} | ||
|
||
return 'NextCloud-News/' . $this->version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <ben@vidulich.nz> | ||
* @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(), | ||
] | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters