diff --git a/.changelog/current/2515-use-firefox-user-agent.md b/.changelog/current/2515-use-firefox-user-agent.md new file mode 100644 index 000000000..7a7fdcb4b --- /dev/null +++ b/.changelog/current/2515-use-firefox-user-agent.md @@ -0,0 +1,3 @@ +# Fixed + +- Usage of firefox user agent to import foreign recipes diff --git a/lib/Helper/DownloadHelper.php b/lib/Helper/DownloadHelper.php index 5a146b809..2e9b99bc8 100644 --- a/lib/Helper/DownloadHelper.php +++ b/lib/Helper/DownloadHelper.php @@ -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); @@ -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); diff --git a/lib/Service/HtmlDownloadService.php b/lib/Service/HtmlDownloadService.php index 443dcacd4..5ce2013ef 100644 --- a/lib/Service/HtmlDownloadService.php +++ b/lib/Service/HtmlDownloadService.php @@ -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); }