-
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.
set user agent when retrieving favicons
Signed-off-by: Ben Vidulich <[email protected]>
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
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
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(), | ||
] | ||
] | ||
); | ||
} | ||
} |
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