Configurable list of domains to ignore redirects

This commit is contained in:
Michael 2020-09-16 04:56:37 +00:00
parent 26831371f7
commit 55e169db49
4 changed files with 52 additions and 2 deletions

View file

@ -192,7 +192,7 @@ class HTTPRequest implements IHTTPRequest
$curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch)); $curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
if ($curlResponse->isRedirectUrl()) { if (!Network::isRedirectBlocked($url) && $curlResponse->isRedirectUrl()) {
$redirects++; $redirects++;
$this->logger->notice('Curl redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]); $this->logger->notice('Curl redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]);
@curl_close($ch); @curl_close($ch);
@ -280,7 +280,7 @@ class HTTPRequest implements IHTTPRequest
$curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch)); $curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
if ($curlResponse->isRedirectUrl()) { if (!Network::isRedirectBlocked($url) && $curlResponse->isRedirectUrl()) {
$redirects++; $redirects++;
$this->logger->info('Post redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]); $this->logger->info('Post redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]);
curl_close($ch); curl_close($ch);
@ -321,6 +321,11 @@ class HTTPRequest implements IHTTPRequest
return $url; return $url;
} }
if (Network::isRedirectBlocked($url)) {
$this->logger->info('Domain should not be redirected.', ['url' => $url]);
return $url;
}
$url = Network::stripTrackingQueryParams($url); $url = Network::stripTrackingQueryParams($url);
if ($depth > 10) { if ($depth > 10) {
@ -470,4 +475,14 @@ class HTTPRequest implements IHTTPRequest
DB_UPDATE_VERSION . '; ' . DB_UPDATE_VERSION . '; ' .
$this->baseUrl; $this->baseUrl;
} }
private function redirectBlocked(string $url = null)
{
$hosts = $this->config->get('system', 'no_redirect_hosts');
if (empty($hosts)) {
return false;
}
$hostlist = explode(',', $hosts);
}
} }

View file

@ -351,6 +351,8 @@ class Feed
$orig_plink = $item["plink"]; $orig_plink = $item["plink"];
$item["plink"] = DI::httpRequest()->finalUrl($item["plink"]);
$item["parent-uri"] = $item["uri"]; $item["parent-uri"] = $item["uri"];
$item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry); $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);

View file

@ -177,6 +177,35 @@ class Network
return false; return false;
} }
/**
* Checks if the provided url is on the list of domains where redirects are blocked.
* Returns true if it is or malformed URL, false if not.
*
* @param string $url The url to check the domain from
*
* @return boolean
*/
public static function isRedirectBlocked(string $url)
{
$host = @parse_url($url, PHP_URL_HOST);
if (!$host) {
return false;
}
$no_redirect_list = DI::config()->get('system', 'no_redirect_list', []);
if (!$no_redirect_list) {
return false;
}
foreach ($no_redirect_list as $no_redirect) {
if (fnmatch(strtolower($no_redirect), strtolower($host))) {
return true;
}
}
return false;
}
/** /**
* Check if email address is allowed to register here. * Check if email address is allowed to register here.
* *

View file

@ -362,6 +362,10 @@ return [
// Don't use OEmbed to fetch more information about a link. // Don't use OEmbed to fetch more information about a link.
'no_oembed' => false, 'no_oembed' => false,
// no_redirect_list (Array)
// List of domains where HTTP redirects should be ignored.
'no_redirect_list' => [],
// no_smilies (Boolean) // no_smilies (Boolean)
// Don't show smilies. // Don't show smilies.
'no_smilies' => false, 'no_smilies' => false,