2016-09-26 00:22:33 +03:00
|
|
|
<?php
|
2022-03-11 23:18:01 +03:00
|
|
|
|
2022-10-29 09:42:50 +03:00
|
|
|
final class Response
|
|
|
|
{
|
|
|
|
public const STATUS_CODES = [
|
|
|
|
'100' => 'Continue',
|
|
|
|
'101' => 'Switching Protocols',
|
|
|
|
'200' => 'OK',
|
|
|
|
'201' => 'Created',
|
|
|
|
'202' => 'Accepted',
|
|
|
|
'203' => 'Non-Authoritative Information',
|
|
|
|
'204' => 'No Content',
|
|
|
|
'205' => 'Reset Content',
|
|
|
|
'206' => 'Partial Content',
|
|
|
|
'300' => 'Multiple Choices',
|
|
|
|
'301' => 'Moved Permanently',
|
|
|
|
'302' => 'Found',
|
|
|
|
'303' => 'See Other',
|
|
|
|
'304' => 'Not Modified',
|
|
|
|
'305' => 'Use Proxy',
|
|
|
|
'400' => 'Bad Request',
|
|
|
|
'401' => 'Unauthorized',
|
|
|
|
'402' => 'Payment Required',
|
|
|
|
'403' => 'Forbidden',
|
|
|
|
'404' => 'Not Found',
|
|
|
|
'405' => 'Method Not Allowed',
|
|
|
|
'406' => 'Not Acceptable',
|
|
|
|
'407' => 'Proxy Authentication Required',
|
|
|
|
'408' => 'Request Timeout',
|
|
|
|
'409' => 'Conflict',
|
|
|
|
'410' => 'Gone',
|
|
|
|
'411' => 'Length Required',
|
|
|
|
'412' => 'Precondition Failed',
|
|
|
|
'413' => 'Request Entity Too Large',
|
|
|
|
'414' => 'Request-URI Too Long',
|
|
|
|
'415' => 'Unsupported Media Type',
|
|
|
|
'416' => 'Requested Range Not Satisfiable',
|
|
|
|
'417' => 'Expectation Failed',
|
|
|
|
'429' => 'Too Many Requests',
|
|
|
|
'500' => 'Internal Server Error',
|
|
|
|
'501' => 'Not Implemented',
|
|
|
|
'502' => 'Bad Gateway',
|
|
|
|
'503' => 'Service Unavailable',
|
|
|
|
'504' => 'Gateway Timeout',
|
|
|
|
'505' => 'HTTP Version Not Supported'
|
|
|
|
];
|
2022-11-07 20:22:54 +03:00
|
|
|
private string $body;
|
|
|
|
private int $code;
|
|
|
|
private array $headers;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $body = '',
|
|
|
|
int $code = 200,
|
|
|
|
array $headers = []
|
|
|
|
) {
|
|
|
|
$this->body = $body;
|
|
|
|
$this->code = $code;
|
|
|
|
$this->headers = $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBody()
|
|
|
|
{
|
|
|
|
return $this->body;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHeaders()
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function send(): void
|
|
|
|
{
|
|
|
|
http_response_code($this->code);
|
|
|
|
foreach ($this->headers as $name => $value) {
|
|
|
|
header(sprintf('%s: %s', $name, $value));
|
|
|
|
}
|
|
|
|
print $this->body;
|
|
|
|
}
|
2022-10-29 09:42:50 +03:00
|
|
|
}
|
2022-05-11 23:34:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch data from an http url
|
|
|
|
*
|
|
|
|
* @param array $httpHeaders E.g. ['Content-type: text/plain']
|
|
|
|
* @param array $curlOptions Associative array e.g. [CURLOPT_MAXREDIRS => 3]
|
2022-05-23 04:27:23 +03:00
|
|
|
* @param bool $returnFull Whether to return an array:
|
|
|
|
* [
|
|
|
|
* 'code' => int,
|
|
|
|
* 'header' => array,
|
|
|
|
* 'content' => string,
|
|
|
|
* 'status_lines' => array,
|
|
|
|
* ]
|
|
|
|
|
2022-05-11 23:34:18 +03:00
|
|
|
* @return string|array
|
|
|
|
*/
|
2022-05-08 04:58:57 +03:00
|
|
|
function getContents(
|
|
|
|
string $url,
|
|
|
|
array $httpHeaders = [],
|
|
|
|
array $curlOptions = [],
|
2022-05-18 00:46:37 +03:00
|
|
|
bool $returnFull = false
|
2022-05-08 04:58:57 +03:00
|
|
|
) {
|
2023-07-08 18:03:12 +03:00
|
|
|
$cache = RssBridge::getCache();
|
2022-05-08 04:58:57 +03:00
|
|
|
$cache->setScope('server');
|
|
|
|
$cache->setKey([$url]);
|
|
|
|
|
2022-07-31 05:21:56 +03:00
|
|
|
// Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102
|
|
|
|
$defaultHttpHeaders = [
|
|
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
|
|
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
|
|
'Upgrade-Insecure-Requests' => '1',
|
|
|
|
'Sec-Fetch-Dest' => 'document',
|
|
|
|
'Sec-Fetch-Mode' => 'navigate',
|
|
|
|
'Sec-Fetch-Site' => 'none',
|
|
|
|
'Sec-Fetch-User' => '?1',
|
2023-04-21 03:50:48 +03:00
|
|
|
'TE' => 'trailers',
|
2022-07-31 05:21:56 +03:00
|
|
|
];
|
|
|
|
$httpHeadersNormalized = [];
|
|
|
|
foreach ($httpHeaders as $httpHeader) {
|
|
|
|
$parts = explode(':', $httpHeader);
|
|
|
|
$headerName = trim($parts[0]);
|
|
|
|
$headerValue = trim(implode(':', array_slice($parts, 1)));
|
|
|
|
$httpHeadersNormalized[$headerName] = $headerValue;
|
|
|
|
}
|
2022-05-08 04:58:57 +03:00
|
|
|
$config = [
|
2022-12-13 11:53:42 +03:00
|
|
|
'useragent' => Configuration::getConfig('http', 'useragent'),
|
|
|
|
'timeout' => Configuration::getConfig('http', 'timeout'),
|
2022-07-31 05:21:56 +03:00
|
|
|
'headers' => array_merge($defaultHttpHeaders, $httpHeadersNormalized),
|
2022-05-08 04:58:57 +03:00
|
|
|
'curl_options' => $curlOptions,
|
|
|
|
];
|
2022-11-16 19:56:26 +03:00
|
|
|
|
|
|
|
$maxFileSize = Configuration::getConfig('http', 'max_filesize');
|
|
|
|
if ($maxFileSize) {
|
|
|
|
// Multiply with 2^20 (1M) to the value in bytes
|
|
|
|
$config['max_filesize'] = $maxFileSize * 2 ** 20;
|
|
|
|
}
|
|
|
|
|
2022-07-24 20:26:12 +03:00
|
|
|
if (Configuration::getConfig('proxy', 'url') && !defined('NOPROXY')) {
|
|
|
|
$config['proxy'] = Configuration::getConfig('proxy', 'url');
|
2022-03-11 23:18:01 +03:00
|
|
|
}
|
2022-05-08 04:58:57 +03:00
|
|
|
if (!Debug::isEnabled() && $cache->getTime()) {
|
|
|
|
$config['if_not_modified_since'] = $cache->getTime();
|
2022-03-11 23:18:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-08 04:58:57 +03:00
|
|
|
$result = _http_request($url, $config);
|
|
|
|
$response = [
|
2022-05-18 00:46:37 +03:00
|
|
|
'code' => $result['code'],
|
|
|
|
'status_lines' => $result['status_lines'],
|
2022-05-08 04:58:57 +03:00
|
|
|
'header' => $result['headers'],
|
|
|
|
'content' => $result['body'],
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($result['code']) {
|
|
|
|
case 200:
|
|
|
|
case 201:
|
|
|
|
case 202:
|
|
|
|
if (isset($result['headers']['cache-control'])) {
|
|
|
|
$cachecontrol = $result['headers']['cache-control'];
|
|
|
|
$lastValue = array_pop($cachecontrol);
|
|
|
|
$directives = explode(',', $lastValue);
|
|
|
|
$directives = array_map('trim', $directives);
|
|
|
|
if (in_array('no-cache', $directives) || in_array('no-store', $directives)) {
|
2022-05-11 23:34:18 +03:00
|
|
|
// Don't cache as instructed by the server
|
2022-05-08 04:58:57 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cache->saveData($result['body']);
|
|
|
|
break;
|
2022-09-06 16:40:20 +03:00
|
|
|
case 301:
|
|
|
|
case 302:
|
|
|
|
case 303:
|
|
|
|
// todo: cache
|
|
|
|
break;
|
2022-08-06 23:46:28 +03:00
|
|
|
case 304:
|
|
|
|
// Not Modified
|
2022-05-08 04:58:57 +03:00
|
|
|
$response['content'] = $cache->loadData();
|
|
|
|
break;
|
|
|
|
default:
|
2023-03-20 21:11:51 +03:00
|
|
|
$exceptionMessage = sprintf(
|
|
|
|
'%s resulted in %s %s %s',
|
|
|
|
$url,
|
|
|
|
$result['code'],
|
|
|
|
Response::STATUS_CODES[$result['code']] ?? '',
|
|
|
|
// If debug, include a part of the response body in the exception message
|
|
|
|
Debug::isEnabled() ? mb_substr($result['body'], 0, 500) : '',
|
|
|
|
);
|
|
|
|
|
|
|
|
// The following code must be extracted if it grows too much
|
|
|
|
$cloudflareTitles = [
|
|
|
|
'<title>Just a moment...',
|
|
|
|
'<title>Please Wait...',
|
2023-05-11 02:33:38 +03:00
|
|
|
'<title>Attention Required!',
|
|
|
|
'<title>Security | Glassdoor',
|
2023-03-20 21:11:51 +03:00
|
|
|
];
|
|
|
|
foreach ($cloudflareTitles as $cloudflareTitle) {
|
|
|
|
if (str_contains($result['body'], $cloudflareTitle)) {
|
|
|
|
throw new CloudFlareException($exceptionMessage, $result['code']);
|
|
|
|
}
|
2022-10-29 09:42:50 +03:00
|
|
|
}
|
2023-03-20 21:11:51 +03:00
|
|
|
|
2023-07-16 08:18:38 +03:00
|
|
|
throw new HttpException(trim($exceptionMessage), $result['code']);
|
2022-03-11 23:18:01 +03:00
|
|
|
}
|
2022-05-18 00:46:37 +03:00
|
|
|
if ($returnFull === true) {
|
2022-05-08 04:58:57 +03:00
|
|
|
return $response;
|
2022-03-11 23:18:01 +03:00
|
|
|
}
|
2022-05-08 04:58:57 +03:00
|
|
|
return $response['content'];
|
2022-03-11 23:18:01 +03:00
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
2022-05-08 04:58:57 +03:00
|
|
|
* Fetch content from url
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
2022-11-16 19:56:26 +03:00
|
|
|
* @internal Private function used internally
|
2022-05-08 04:58:57 +03:00
|
|
|
* @throws HttpException
|
2018-11-16 23:48:59 +03:00
|
|
|
*/
|
2022-05-08 04:58:57 +03:00
|
|
|
function _http_request(string $url, array $config = []): array
|
|
|
|
{
|
|
|
|
$defaults = [
|
2022-12-13 11:53:42 +03:00
|
|
|
'useragent' => null,
|
|
|
|
'timeout' => 5,
|
2022-05-08 04:58:57 +03:00
|
|
|
'headers' => [],
|
|
|
|
'proxy' => null,
|
|
|
|
'curl_options' => [],
|
|
|
|
'if_not_modified_since' => null,
|
|
|
|
'retries' => 3,
|
2022-11-16 19:56:26 +03:00
|
|
|
'max_filesize' => null,
|
2023-07-09 00:21:55 +03:00
|
|
|
'max_redirections' => 5,
|
2022-05-08 04:58:57 +03:00
|
|
|
];
|
|
|
|
$config = array_merge($defaults, $config);
|
2019-11-01 00:02:38 +03:00
|
|
|
|
2022-04-10 19:54:18 +03:00
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
2023-07-09 00:21:55 +03:00
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, $config['max_redirections']);
|
2022-05-08 04:58:57 +03:00
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
2022-07-31 05:21:56 +03:00
|
|
|
$httpHeaders = [];
|
|
|
|
foreach ($config['headers'] as $name => $value) {
|
|
|
|
$httpHeaders[] = sprintf('%s: %s', $name, $value);
|
|
|
|
}
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
|
2022-12-13 11:53:42 +03:00
|
|
|
if ($config['useragent']) {
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
|
|
|
|
}
|
2022-05-08 04:58:57 +03:00
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $config['timeout']);
|
2022-04-10 19:54:18 +03:00
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, '');
|
|
|
|
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
2022-11-16 19:56:26 +03:00
|
|
|
|
|
|
|
if ($config['max_filesize']) {
|
|
|
|
// This option inspects the Content-Length header
|
|
|
|
curl_setopt($ch, CURLOPT_MAXFILESIZE, $config['max_filesize']);
|
|
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
|
|
|
|
// This progress function will monitor responses who omit the Content-Length header
|
|
|
|
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($ch, $downloadSize, $downloaded, $uploadSize, $uploaded) use ($config) {
|
|
|
|
if ($downloaded > $config['max_filesize']) {
|
|
|
|
// Return a non-zero value to abort the transfer
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-08 04:58:57 +03:00
|
|
|
if ($config['proxy']) {
|
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
|
2022-04-10 19:54:18 +03:00
|
|
|
}
|
2022-05-11 23:34:18 +03:00
|
|
|
if (curl_setopt_array($ch, $config['curl_options']) === false) {
|
|
|
|
throw new \Exception('Tried to set an illegal curl option');
|
2022-04-10 19:54:18 +03:00
|
|
|
}
|
2022-05-11 23:34:18 +03:00
|
|
|
|
2022-05-08 04:58:57 +03:00
|
|
|
if ($config['if_not_modified_since']) {
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEVALUE, $config['if_not_modified_since']);
|
2022-04-10 19:54:18 +03:00
|
|
|
curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
|
|
|
|
}
|
2018-07-21 18:27:38 +03:00
|
|
|
|
2022-05-18 00:46:37 +03:00
|
|
|
$responseStatusLines = [];
|
2022-05-08 04:58:57 +03:00
|
|
|
$responseHeaders = [];
|
2022-05-18 00:46:37 +03:00
|
|
|
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $rawHeader) use (&$responseHeaders, &$responseStatusLines) {
|
2022-05-08 04:58:57 +03:00
|
|
|
$len = strlen($rawHeader);
|
2022-05-18 00:46:37 +03:00
|
|
|
if ($rawHeader === "\r\n") {
|
|
|
|
return $len;
|
|
|
|
}
|
|
|
|
if (preg_match('#^HTTP/(2|1.1|1.0)#', $rawHeader)) {
|
|
|
|
$responseStatusLines[] = $rawHeader;
|
2022-05-08 04:58:57 +03:00
|
|
|
return $len;
|
|
|
|
}
|
|
|
|
$header = explode(':', $rawHeader);
|
|
|
|
if (count($header) === 1) {
|
|
|
|
return $len;
|
|
|
|
}
|
|
|
|
$name = mb_strtolower(trim($header[0]));
|
|
|
|
$value = trim(implode(':', array_slice($header, 1)));
|
|
|
|
if (!isset($responseHeaders[$name])) {
|
|
|
|
$responseHeaders[$name] = [];
|
|
|
|
}
|
|
|
|
$responseHeaders[$name][] = $value;
|
|
|
|
return $len;
|
|
|
|
});
|
|
|
|
|
|
|
|
$attempts = 0;
|
|
|
|
while (true) {
|
|
|
|
$attempts++;
|
|
|
|
$data = curl_exec($ch);
|
|
|
|
if ($data !== false) {
|
|
|
|
// The network call was successful, so break out of the loop
|
2019-11-01 00:02:38 +03:00
|
|
|
break;
|
2022-05-08 04:58:57 +03:00
|
|
|
}
|
|
|
|
if ($attempts > $config['retries']) {
|
|
|
|
// Finally give up
|
2023-07-09 00:21:55 +03:00
|
|
|
$curl_error = curl_error($ch);
|
|
|
|
$curl_errno = curl_errno($ch);
|
2022-09-08 20:07:57 +03:00
|
|
|
throw new HttpException(sprintf(
|
|
|
|
'cURL error %s: %s (%s) for %s',
|
2023-07-09 00:21:55 +03:00
|
|
|
$curl_error,
|
|
|
|
$curl_errno,
|
2022-09-08 20:07:57 +03:00
|
|
|
'https://curl.haxx.se/libcurl/c/libcurl-errors.html',
|
|
|
|
$url
|
|
|
|
));
|
2022-05-08 04:58:57 +03:00
|
|
|
}
|
2018-07-21 18:27:38 +03:00
|
|
|
}
|
2019-11-01 00:02:38 +03:00
|
|
|
|
2022-05-08 04:58:57 +03:00
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
return [
|
|
|
|
'code' => $statusCode,
|
2022-05-18 00:46:37 +03:00
|
|
|
'status_lines' => $responseStatusLines,
|
2022-05-08 04:58:57 +03:00
|
|
|
'headers' => $responseHeaders,
|
|
|
|
'body' => $data,
|
|
|
|
];
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Gets contents from the Internet as simplhtmldom object.
|
|
|
|
*
|
|
|
|
* @param string $url The URL.
|
|
|
|
* @param array $header (optional) A list of cURL header.
|
|
|
|
* For more information follow the links below.
|
|
|
|
* * https://php.net/manual/en/function.curl-setopt.php
|
|
|
|
* * https://curl.haxx.se/libcurl/c/CURLOPT_HTTPHEADER.html
|
|
|
|
* @param array $opts (optional) A list of cURL options as associative array in
|
|
|
|
* the format `$opts[$option] = $value;`, where `$option` is any `CURLOPT_XXX`
|
|
|
|
* option and `$value` the corresponding value.
|
|
|
|
*
|
|
|
|
* For more information see http://php.net/manual/en/function.curl-setopt.php
|
|
|
|
* @param bool $lowercase Force all selectors to lowercase.
|
|
|
|
* @param bool $forceTagsClosed Forcefully close tags in malformed HTML.
|
|
|
|
*
|
|
|
|
* _Remarks_: Forcefully closing tags is great for malformed HTML, but it can
|
|
|
|
* lead to parsing errors.
|
|
|
|
* @param string $target_charset Defines the target charset.
|
|
|
|
* @param bool $stripRN Replace all occurrences of `"\r"` and `"\n"` by `" "`.
|
|
|
|
* @param string $defaultBRText Specifies the replacement text for `<br>` tags
|
|
|
|
* when returning plaintext.
|
|
|
|
* @param string $defaultSpanText Specifies the replacement text for `<span />`
|
|
|
|
* tags when returning plaintext.
|
2020-11-08 10:19:18 +03:00
|
|
|
* @return false|simple_html_dom Contents as simplehtmldom object.
|
2018-11-16 23:48:59 +03:00
|
|
|
*/
|
2017-02-14 19:28:07 +03:00
|
|
|
function getSimpleHTMLDOM(
|
|
|
|
$url,
|
2019-03-20 19:59:16 +03:00
|
|
|
$header = [],
|
|
|
|
$opts = [],
|
|
|
|
$lowercase = true,
|
|
|
|
$forceTagsClosed = true,
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET,
|
|
|
|
$stripRN = true,
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT,
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT
|
|
|
|
) {
|
2022-05-08 04:58:57 +03:00
|
|
|
$content = getContents(
|
|
|
|
$url,
|
|
|
|
$header ?? [],
|
|
|
|
$opts ?? []
|
|
|
|
);
|
2017-02-14 19:28:07 +03:00
|
|
|
return str_get_html(
|
|
|
|
$content,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText
|
|
|
|
);
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-16 23:48:59 +03:00
|
|
|
* Gets contents from the Internet as simplhtmldom object. Contents are cached
|
|
|
|
* and re-used for subsequent calls until the cache duration elapsed.
|
|
|
|
*
|
|
|
|
* _Notice_: Cached contents are forcefully removed after 24 hours (86400 seconds).
|
|
|
|
*
|
|
|
|
* @param string $url The URL.
|
|
|
|
* @param int $duration Cache duration in seconds.
|
|
|
|
* @param array $header (optional) A list of cURL header.
|
|
|
|
* For more information follow the links below.
|
|
|
|
* * https://php.net/manual/en/function.curl-setopt.php
|
|
|
|
* * https://curl.haxx.se/libcurl/c/CURLOPT_HTTPHEADER.html
|
|
|
|
* @param array $opts (optional) A list of cURL options as associative array in
|
|
|
|
* the format `$opts[$option] = $value;`, where `$option` is any `CURLOPT_XXX`
|
|
|
|
* option and `$value` the corresponding value.
|
|
|
|
*
|
|
|
|
* For more information see http://php.net/manual/en/function.curl-setopt.php
|
|
|
|
* @param bool $lowercase Force all selectors to lowercase.
|
|
|
|
* @param bool $forceTagsClosed Forcefully close tags in malformed HTML.
|
|
|
|
*
|
|
|
|
* _Remarks_: Forcefully closing tags is great for malformed HTML, but it can
|
|
|
|
* lead to parsing errors.
|
|
|
|
* @param string $target_charset Defines the target charset.
|
|
|
|
* @param bool $stripRN Replace all occurrences of `"\r"` and `"\n"` by `" "`.
|
|
|
|
* @param string $defaultBRText Specifies the replacement text for `<br>` tags
|
|
|
|
* when returning plaintext.
|
|
|
|
* @param string $defaultSpanText Specifies the replacement text for `<span />`
|
|
|
|
* tags when returning plaintext.
|
2020-11-08 10:19:18 +03:00
|
|
|
* @return false|simple_html_dom Contents as simplehtmldom object.
|
2016-09-26 00:22:33 +03:00
|
|
|
*/
|
2017-02-14 19:28:07 +03:00
|
|
|
function getSimpleHTMLDOMCached(
|
|
|
|
$url,
|
2019-03-20 19:59:16 +03:00
|
|
|
$duration = 86400,
|
|
|
|
$header = [],
|
|
|
|
$opts = [],
|
|
|
|
$lowercase = true,
|
|
|
|
$forceTagsClosed = true,
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET,
|
|
|
|
$stripRN = true,
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT,
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT
|
|
|
|
) {
|
2023-07-08 18:03:12 +03:00
|
|
|
$cache = RssBridge::getCache();
|
2019-04-29 21:12:43 +03:00
|
|
|
$cache->setScope('pages');
|
2023-07-06 19:52:19 +03:00
|
|
|
$cache->setKey([$url]);
|
2016-09-26 00:22:33 +03:00
|
|
|
|
2016-10-08 17:30:01 +03:00
|
|
|
// Determine if cached file is within duration
|
|
|
|
$time = $cache->getTime();
|
|
|
|
if (
|
2023-07-05 18:37:21 +03:00
|
|
|
$time
|
|
|
|
&& time() - $duration < $time
|
2021-02-09 15:40:16 +03:00
|
|
|
&& !Debug::isEnabled()
|
2023-06-02 21:22:09 +03:00
|
|
|
) {
|
2023-07-06 19:52:19 +03:00
|
|
|
// Cache hit
|
2016-10-08 17:30:01 +03:00
|
|
|
$content = $cache->loadData();
|
2023-06-02 21:22:09 +03:00
|
|
|
} else {
|
2022-05-08 05:42:24 +03:00
|
|
|
$content = getContents(
|
|
|
|
$url,
|
|
|
|
$header ?? [],
|
|
|
|
$opts ?? []
|
|
|
|
);
|
2023-07-06 19:52:19 +03:00
|
|
|
if ($content) {
|
2023-07-09 16:24:29 +03:00
|
|
|
$cache->setScope('pages');
|
|
|
|
$cache->setKey([$url]);
|
2016-10-08 17:30:01 +03:00
|
|
|
$cache->saveData($content);
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 19:28:07 +03:00
|
|
|
return str_get_html(
|
|
|
|
$content,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText
|
|
|
|
);
|
2016-09-26 00:22:33 +03:00
|
|
|
}
|