Skip to content

Commit

Permalink
Merge pull request #2515 from nextcloud/fix/use-firefox-headers-for-i…
Browse files Browse the repository at this point in the history
…mport

Add Mozilla Firefox user agent to import script
  • Loading branch information
christianlupus committed Sep 20, 2024
2 parents e89d3a1 + ad9ae3d commit 60b0710
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
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

0 comments on commit 60b0710

Please sign in to comment.