[refactoring] replace direct use of curl with getContents (#3723)

+ some fixed warnings
This commit is contained in:
User123698745 2023-10-01 20:46:51 +02:00 committed by GitHub
parent 41df17bc46
commit 69da0dd583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 52 deletions

View File

@ -1909,6 +1909,7 @@ class DealabsBridge extends PepperBridgeAbstract
'context-group' => 'Deals par groupe',
'context-talk' => 'Surveillance Discussion',
'uri-group' => 'groupe/',
'uri-deal' => 'bons-plans/',
'request-error' => 'Impossible de joindre Dealabs',
'thread-error' => 'Impossible de déterminer l\'ID de la discussion. Vérifiez l\'URL que vous avez entré',
'no-results' => 'Il n'y a rien à afficher pour le moment :(',

View File

@ -21,34 +21,18 @@ class FDroidBridge extends BridgeAbstract
public function getIcon()
{
return self::URI . 'assets/favicon.ico?v=8j6PKzW9Mk';
return self::URI . 'assets/favicon.ico';
}
private function getTimestamp($url)
{
$curlOptions = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_CONNECTTIMEOUT => 19,
CURLOPT_TIMEOUT => 19,
CURLOPT_CUSTOMREQUEST => 'HEAD',
CURLOPT_NOBODY => true,
];
$ch = curl_init($url);
curl_setopt_array($ch, $curlOptions);
$curlHeaders = curl_exec($ch);
$curlError = curl_error($ch);
curl_close($ch);
if (!empty($curlError)) {
return false;
}
$curlHeaders = explode("\n", $curlHeaders);
$timestamp = false;
foreach ($curlHeaders as $header) {
if (strpos($header, 'Last-Modified') !== false) {
$timestamp = str_replace('Last-Modified: ', '', $header);
$timestamp = strtotime($timestamp);
}
}
$reponse = getContents($url, [], $curlOptions, true);
$lastModified = $reponse['headers']['last-modified'][0] ?? null;
$timestamp = strtotime($lastModified ?? 'today');
return $timestamp;
}

View File

@ -3273,6 +3273,7 @@ class HotUKDealsBridge extends PepperBridgeAbstract
'context-group' => 'Deals per group',
'context-talk' => 'Discussion Monitoring',
'uri-group' => 'tag/',
'uri-deal' => 'deals/',
'request-error' => 'Could not request HotUKDeals',
'thread-error' => 'Unable to determine the thread ID. Check the URL you entered',
'no-results' => 'Ooops, looks like we could',

View File

@ -2020,6 +2020,7 @@ class MydealsBridge extends PepperBridgeAbstract
'context-group' => 'Deals pro Gruppen',
'context-talk' => 'Überwachung Diskussion',
'uri-group' => 'gruppe/',
'uri-deal' => 'deals/',
'request-error' => 'Could not request mydeals',
'thread-error' => 'Die ID der Diskussion kann nicht ermittelt werden. Überprüfen Sie die eingegebene URL',
'no-results' => 'Ups, wir konnten keine Deals zu',

View File

@ -165,7 +165,7 @@ class PepperBridgeAbstract extends BridgeAbstract
$url = $this->i8n('bridge-uri') . 'graphql';
// Get Cookies header to do the query
$cookies = $this->getCookies($url);
$cookiesHeaderValue = $this->getCookiesHeaderValue($url);
// GraphQL String
// This was extracted from https://www.dealabs.com/assets/js/modern/common_211b99.js
@ -209,7 +209,7 @@ HEREDOC;
'X-Pepper-Txn: threads.show',
'X-Request-Type: application/vnd.pepper.v1+json',
'X-Requested-With: XMLHttpRequest',
$cookies,
"Cookie: $cookiesHeaderValue",
];
// CURL Options
$opts = [
@ -245,26 +245,12 @@ HEREDOC;
* Extract the cookies obtained from the URL
* @return array the array containing the cookies set by the URL
*/
private function getCookies($url)
private function getCookiesHeaderValue($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
// get cookie
// multi-cookie variant contributed by @Combuster in comments
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = [];
foreach ($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
$header = 'Cookie: ';
foreach ($cookies as $name => $content) {
$header .= $name . '=' . $content . '; ';
}
return $header;
$response = getContents($url, [], [], true);
$setCookieHeaders = $response['headers']['set-cookie'] ?? [];
$cookies = array_map(fn($c): string => explode(';', $c)[0], $setCookieHeaders);
return implode('; ', $cookies);
}
/**
@ -330,7 +316,7 @@ HEREDOC;
private function getTalkTitle()
{
$html = getSimpleHTMLDOMCached($this->getInput('url'));
$title = $html->find('h1[class=thread-title]', 0)->plaintext;
$title = $html->find('.thread-title', 0)->plaintext;
return $title;
}
@ -356,13 +342,8 @@ HEREDOC;
*/
private function getDealURI($deal)
{
$uriA = $deal->find('div[class*=threadGrid-title]', 0)->find('a[class*=thread-link]', 0);
if ($uriA === null) {
$uri = '';
} else {
$uri = $uriA->href;
}
$dealId = $deal->attr['id'];
$uri = $this->i8n('bridge-uri') . $this->i8n('uri-deal') . str_replace('_', '-', $dealId);
return $uri;
}

View File

@ -52,7 +52,11 @@ function getContents(
$config['proxy'] = Configuration::getConfig('proxy', 'url');
}
$cacheKey = 'server_' . $url;
$requestBodyHash = null;
if (isset($curlOptions[CURLOPT_POSTFIELDS])) {
$requestBodyHash = md5(json_encode($curlOptions[CURLOPT_POSTFIELDS]));
}
$cacheKey = implode('_', ['server', $url, $requestBodyHash]);
/** @var Response $cachedResponse */
$cachedResponse = $cache->get($cacheKey);