Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mozilla Firefox user agent to import script #2515

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/current/2515-use-firefox-user-agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fixed

- Usage of firefox user agent to import foreign recipes
8 changes: 7 additions & 1 deletion lib/Helper/DownloadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public function __construct(
*
* @param string $url The URL of the file to fetch
* @param array $options Options to pass on for curl. This allows to fine-tune the transfer.
* @param array $headers Additinal headers to be sent to the server
* @throws NoDownloadWasCarriedOutException if the download fails for some reason
*/
public function downloadFile(string $url, array $options = []): void {
public function downloadFile(string $url, array $options = [], array $headers = []): void {
$this->downloaded = false;

$ch = curl_init($url);
Expand All @@ -66,9 +67,14 @@ public function downloadFile(string $url, array $options = []): void {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_WRITEHEADER, $hp);

if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt_array($ch, $options);

$ret = curl_exec($ch);

if ($ret === false) {
$ex = new NoDownloadWasCarriedOutException($this->l->t('Downloading of a file failed returned the following error message: %s', [curl_error($ch)]));
fclose($hp);
Expand Down
23 changes: 21 additions & 2 deletions lib/Service/HtmlDownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,30 @@ private function fetchHtmlPage(string $url): string {
}

$opt = [
CURLOPT_USERAGENT => 'Nextcloud Cookbook App',
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0',
];

$langCode = $this->l->getLocaleCode();
$langCode = str_replace('_', '-', $langCode);

$headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8',
"Accept-Language: $langCode,en;q=0.5",
'DNT: 1',
// 'Alt-Used: www.thefooddictator.com',
'Connection: keep-alive',
'Cookie: nitroCachedPage=1',
'Upgrade-Insecure-Requests: 1',
'Sec-Fetch-Dest: document',
'Sec-Fetch-Mode: navigate',
'Sec-Fetch-Site: none',
'Sec-Fetch-User: ?1',
'Priority: u=0, i',
'TE: trailers'
];

try {
$this->downloadHelper->downloadFile($url, $opt);
$this->downloadHelper->downloadFile($url, $opt, $headers);
} catch (NoDownloadWasCarriedOutException $ex) {
throw new ImportException($this->l->t('Exception while downloading recipe from %s.', [$url]), 0, $ex);
}
Expand Down
Loading