2016-09-26 00:22:33 +03:00
|
|
|
<?php
|
2022-03-11 23:18:01 +03:00
|
|
|
|
2022-05-08 04:58:57 +03:00
|
|
|
final class HttpException extends \Exception {}
|
|
|
|
|
|
|
|
function getContents(
|
|
|
|
string $url,
|
|
|
|
array $httpHeaders = [],
|
|
|
|
array $curlOptions = [],
|
|
|
|
bool $returnHeader = false
|
|
|
|
) {
|
|
|
|
$cacheFactory = new CacheFactory();
|
|
|
|
$cacheFactory->setWorkingDir(PATH_LIB_CACHES);
|
|
|
|
$cache = $cacheFactory->create(Configuration::getConfig('cache', 'type'));
|
|
|
|
$cache->setScope('server');
|
|
|
|
$cache->purgeCache(86400); // 24 hours (forced)
|
|
|
|
$cache->setKey([$url]);
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'headers' => $httpHeaders,
|
|
|
|
'curl_options' => $curlOptions,
|
|
|
|
];
|
|
|
|
if (defined('PROXY_URL') && !defined('NOPROXY')) {
|
|
|
|
$config['proxy'] = 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 = [
|
|
|
|
'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)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cache->saveData($result['body']);
|
|
|
|
break;
|
|
|
|
case 304: // Not Modified
|
|
|
|
$response['content'] = $cache->loadData();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new HttpException('', $result['code']);
|
2022-03-11 23:18:01 +03:00
|
|
|
}
|
2022-05-08 04:58:57 +03:00
|
|
|
if ($returnHeader === true) {
|
|
|
|
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
|
|
|
* Private function used internally
|
2018-11-19 19:53:08 +03:00
|
|
|
*
|
2022-05-08 04:58:57 +03:00
|
|
|
* Fetch content from url
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
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 = [
|
|
|
|
'useragent' => Configuration::getConfig('http', 'useragent'),
|
|
|
|
'timeout' => Configuration::getConfig('http', 'timeout'),
|
|
|
|
'headers' => [],
|
|
|
|
'proxy' => null,
|
|
|
|
'curl_options' => [],
|
|
|
|
'if_not_modified_since' => null,
|
|
|
|
'retries' => 3,
|
|
|
|
];
|
|
|
|
$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);
|
2022-05-08 04:58:57 +03:00
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $config['headers']);
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
|
|
|
|
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-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-08 04:58:57 +03:00
|
|
|
foreach($config['curl_options'] as $key => $value) {
|
|
|
|
curl_setopt($ch, $key, $value);
|
2022-04-10 19:54: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-08 04:58:57 +03:00
|
|
|
$responseHeaders = [];
|
|
|
|
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $rawHeader) use (&$responseHeaders) {
|
|
|
|
$len = strlen($rawHeader);
|
|
|
|
if (preg_match('#^HTTP/(2|1.1|1.0)#', $rawHeader) || $rawHeader === "\r\n") {
|
|
|
|
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
|
|
|
|
throw new HttpException(sprintf('%s (%s)', curl_error($ch), curl_errno($ch)));
|
|
|
|
}
|
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,
|
|
|
|
'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 = array(),
|
|
|
|
$opts = array(),
|
|
|
|
$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 = array(),
|
|
|
|
$opts = array(),
|
|
|
|
$lowercase = true,
|
|
|
|
$forceTagsClosed = true,
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET,
|
|
|
|
$stripRN = true,
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT,
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT){
|
|
|
|
|
2018-11-10 22:03:03 +03:00
|
|
|
Debug::log('Caching url ' . $url . ', duration ' . $duration);
|
2016-09-26 00:22:33 +03:00
|
|
|
|
2016-10-08 17:30:01 +03:00
|
|
|
// Initialize cache
|
2019-06-18 20:04:19 +03:00
|
|
|
$cacheFac = new CacheFactory();
|
|
|
|
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
|
|
|
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
2019-04-29 21:12:43 +03:00
|
|
|
$cache->setScope('pages');
|
2016-10-08 17:30:01 +03:00
|
|
|
$cache->purgeCache(86400); // 24 hours (forced)
|
2016-09-26 00:22:33 +03:00
|
|
|
|
2019-11-01 20:06:38 +03:00
|
|
|
$params = array($url);
|
2019-04-29 21:12:43 +03:00
|
|
|
$cache->setKey($params);
|
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($time !== false
|
|
|
|
&& (time() - $duration < $time)
|
2021-02-09 15:40:16 +03:00
|
|
|
&& !Debug::isEnabled()) { // Contents within duration
|
2016-10-08 17:30:01 +03:00
|
|
|
$content = $cache->loadData();
|
|
|
|
} else { // Content not within duration
|
[contents] Replace file_get_contents by cURL
cURL is a powerful library specifically designed to connect to many
different types of servers with different types of protocols. For
more detailed information refer to the PHP cURL manual:
- http://php.net/manual/en/book.curl.php
Due to this change some parameters for the getContents function were
necessary (also applies to getSimpleHTMLDOM and getSimpleHTMLDOMCached):
> $use_include_path removed
This parameter has never been used and doesn't even make sense in
this context; If set to true file_get_contents would also search
for files in the include_path (specified in php.ini).
> $context replaced by $header and $opts
The $context parameter allowed for customization of the request in
order to change how file_get_contents would acquire the data (i.e.
using POST instead of GET, sending custom header, etc...)
cURL also provides facilities to specify custom headers and change
how it communicates to severs. cURL, however, is much more advanced.
- $header is an optional parameter (empty by default). It receives
an array of strings to send in the HTTP request header.
See 'CURLOPT_HTTPHEADER':
"An array of HTTP header fields to set, in the format
array('Content-type: text/plain', 'Content-length: 100')"
- php.net/manual/en/function.curl-setopt.php
- $opts is an optional parameter (empty by default). It receives
an array of options, where each option is a key-value-pair of
a cURL option (CURLOPT_*) and it's associated parameter. This
parameter accepts any of the CURLOPT_* settings.
Example (sending POST instead of GET):
$opts = array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => '&action=none'
);
$html = getContents($url, array(), $opts);
Refer to the cURL setopt manual for more information:
- php.net/manual/en/function.curl-setopt.php
> $offset and $maxlen removed
These options were supported by file_get_contents, but there doesn't
seem to be an equivalent in cURL. Since no caller uses them they are
safe to remove.
Compressed data / Encoding
By using cURL instead of file_get_contents RSS-Bridge no longer has
to handle compressed data manually.
See 'CURLOPT_ENCODING':
"[...] Supported encodings are "identity", "deflate", and "gzip".
If an empty string, "", is set, a header containing all supported
encoding types is sent."
- http://php.net/manual/en/function.curl-setopt.php
Notice: By default all encoding types are accepted (""). This can
be changed by setting a custom option via $opts.
Example:
$opts = array(CURLOPT_ENCODING => 'gzip');
$html = getContents($url, array(), $opts);
Proxy
The proxy implementation should still work, but there doesn't seem
to be an equivalent for 'request_fulluri = true'. To my understanding
this isn't an issue because cURL knows how to handle proxy communication.
2018-03-25 14:12:13 +03:00
|
|
|
$content = getContents($url, $header, $opts);
|
2017-07-29 20:28:00 +03:00
|
|
|
if($content !== false) {
|
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
|
|
|
}
|
2018-07-21 18:27:38 +03:00
|
|
|
|
Catching up | [Main] Debug mode, parse utils, MIME | [Bridges] Add/Improve 20 bridges (#802)
* Debug mode improvements
- Improve debug warning message
- Restore error reporting in debug mode
- Fix 'notice' messages for unset fields
* Add parsing utility functions
html.php
- extractFromDelimiters
- stripWithDelimiters
- stripRecursiveHTMLSection
- markdownToHtml (partial)
bridges
- remove now-duplicate functions
- call functions from html.php instead
* [Anidex] New bridge
Anime torrent tracker
* [Anime-Ultime] Restore thumbnail
* [CNET] Recreate bridge
Full rewrite as the previous one was broken
* [Dilbert] Minor URI fix
Use new self::URI property
* [EstCeQuonMetEnProd] Fix content extraction
Bridge was broken
* [Facebook] Fix "SpSonsSoriSsés" label
... which was taking space in item title
* [Futura-Sciences] Use HTTPS, More cleanup
Use HTTPS as FS now offer HTTPS
Clean additional useless HTML elements
* [GBATemp] Multiple fixes
- Fix categories: missing "break" statements
- Restore thumbnail as enclosure
- Fix date extraction
- Fix user blog post extraction
- Use getSimpleHTMLDOMCached
* [JapanExpo] Fix bridge, HTTPS, thumbnails
- Fix getSimpleHTMLDOMCached call
- Upgrade to HTTPS as JE now offers HTTPS
- Restore thumbnails as enclosures
* [LeMondeInformatique] Fix bridge, HTTPS
- Upgrade to HTTPS as LMI now offers HTTPS
- Restore thumbnails using small images
- Fix content extraction
- Fix text encoding issue
* [Nextgov] Fix content extraction
- Restore thumbnail and use small image
- Field extraction fixes
* [NextInpact] Add categories and filtering by type
- Offer all RSS feeds
- Allow filtering by article type
- Implement extraction for brief articles
- Remove article limit, many brief articles are publied all at once
* [NyaaTorrents] New bridge
Anime torrent tracker
* [Releases3DS] Cache content, restore thumbnail
- Use getSimpleHTMLDOMCached
- Restore thumbnail as enclosure
* [TheHackerNews] Fix bridge
- Fix content extraction including article body
- Restore thumbnail as enclosure
* [WeLiveSecurity] HTTPS, Fix content extraction
- Upgrade to HTTPS as WLS now offers HTTPS
- Fix content extraction including article body
* [WordPress] Reduce timeout, more content selectors
- Reduce timeout to use default one (1h)
- Add new content selector (articleBody)
- Find thumbnail and set as enclosure
- Fix <script> cleanup
* [YGGTorrent] Increase limit, use cache
- Increase item limit as uploads are very frequent
- Use getSimpleHTMLDOMCached
* [ZDNet] Rewrite with FeedExpander
- Upgrade to HTTPS as ZD now offers HTTPS
- Use FeedExpander for secondary fields
- Fix content extraction for article body
* [Main] Handle MIME type for enclosures
Many feed readers will ignore enclosures (e.g. thumbnails) with no MIME type. This commit adds automatic MIME type detection based on file extension (which may be inaccurate but is the only way without fetching the content).
One can force enclosure type using #.ext anchor (hacky, needs improving)
* [FeedExpander] Improve field extraction
- Add support for passing enclosures
- Improve author and uri extraction
- Fix 'notice' PHP error messages
* [Pull] Coding style fixes for #802
* [Pull] Implementing changes for #802
- Fix coding style issues with str append
- Remove useless CACHE_TIMEOUT
- Use count() instead of $limit
- Use defaultLinkTo() + handle strings
- Use http_build_query()
- Fix missing </em>
- Remove error_reporting(0)
- warning CSS (@LogMANOriginal)
- Fix typo in FeedExpander comment
* [Main] More documentation for markdownToHtml
See #802 for more details
2018-09-09 22:20:13 +03:00
|
|
|
/**
|
2018-11-16 23:48:59 +03:00
|
|
|
* Determines the MIME type from a URL/Path file extension.
|
|
|
|
*
|
|
|
|
* _Remarks_:
|
|
|
|
*
|
|
|
|
* * The built-in functions `mime_content_type` and `fileinfo` require fetching
|
|
|
|
* remote contents.
|
|
|
|
* * A caller can hint for a MIME type by appending `#.ext` to the URL (i.e. `#.image`).
|
|
|
|
*
|
Catching up | [Main] Debug mode, parse utils, MIME | [Bridges] Add/Improve 20 bridges (#802)
* Debug mode improvements
- Improve debug warning message
- Restore error reporting in debug mode
- Fix 'notice' messages for unset fields
* Add parsing utility functions
html.php
- extractFromDelimiters
- stripWithDelimiters
- stripRecursiveHTMLSection
- markdownToHtml (partial)
bridges
- remove now-duplicate functions
- call functions from html.php instead
* [Anidex] New bridge
Anime torrent tracker
* [Anime-Ultime] Restore thumbnail
* [CNET] Recreate bridge
Full rewrite as the previous one was broken
* [Dilbert] Minor URI fix
Use new self::URI property
* [EstCeQuonMetEnProd] Fix content extraction
Bridge was broken
* [Facebook] Fix "SpSonsSoriSsés" label
... which was taking space in item title
* [Futura-Sciences] Use HTTPS, More cleanup
Use HTTPS as FS now offer HTTPS
Clean additional useless HTML elements
* [GBATemp] Multiple fixes
- Fix categories: missing "break" statements
- Restore thumbnail as enclosure
- Fix date extraction
- Fix user blog post extraction
- Use getSimpleHTMLDOMCached
* [JapanExpo] Fix bridge, HTTPS, thumbnails
- Fix getSimpleHTMLDOMCached call
- Upgrade to HTTPS as JE now offers HTTPS
- Restore thumbnails as enclosures
* [LeMondeInformatique] Fix bridge, HTTPS
- Upgrade to HTTPS as LMI now offers HTTPS
- Restore thumbnails using small images
- Fix content extraction
- Fix text encoding issue
* [Nextgov] Fix content extraction
- Restore thumbnail and use small image
- Field extraction fixes
* [NextInpact] Add categories and filtering by type
- Offer all RSS feeds
- Allow filtering by article type
- Implement extraction for brief articles
- Remove article limit, many brief articles are publied all at once
* [NyaaTorrents] New bridge
Anime torrent tracker
* [Releases3DS] Cache content, restore thumbnail
- Use getSimpleHTMLDOMCached
- Restore thumbnail as enclosure
* [TheHackerNews] Fix bridge
- Fix content extraction including article body
- Restore thumbnail as enclosure
* [WeLiveSecurity] HTTPS, Fix content extraction
- Upgrade to HTTPS as WLS now offers HTTPS
- Fix content extraction including article body
* [WordPress] Reduce timeout, more content selectors
- Reduce timeout to use default one (1h)
- Add new content selector (articleBody)
- Find thumbnail and set as enclosure
- Fix <script> cleanup
* [YGGTorrent] Increase limit, use cache
- Increase item limit as uploads are very frequent
- Use getSimpleHTMLDOMCached
* [ZDNet] Rewrite with FeedExpander
- Upgrade to HTTPS as ZD now offers HTTPS
- Use FeedExpander for secondary fields
- Fix content extraction for article body
* [Main] Handle MIME type for enclosures
Many feed readers will ignore enclosures (e.g. thumbnails) with no MIME type. This commit adds automatic MIME type detection based on file extension (which may be inaccurate but is the only way without fetching the content).
One can force enclosure type using #.ext anchor (hacky, needs improving)
* [FeedExpander] Improve field extraction
- Add support for passing enclosures
- Improve author and uri extraction
- Fix 'notice' PHP error messages
* [Pull] Coding style fixes for #802
* [Pull] Implementing changes for #802
- Fix coding style issues with str append
- Remove useless CACHE_TIMEOUT
- Use count() instead of $limit
- Use defaultLinkTo() + handle strings
- Use http_build_query()
- Fix missing </em>
- Remove error_reporting(0)
- warning CSS (@LogMANOriginal)
- Fix typo in FeedExpander comment
* [Main] More documentation for markdownToHtml
See #802 for more details
2018-09-09 22:20:13 +03:00
|
|
|
* Based on https://stackoverflow.com/a/1147952
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
|
|
|
* @param string $url The URL or path to the file.
|
|
|
|
* @return string The MIME type of the file.
|
Catching up | [Main] Debug mode, parse utils, MIME | [Bridges] Add/Improve 20 bridges (#802)
* Debug mode improvements
- Improve debug warning message
- Restore error reporting in debug mode
- Fix 'notice' messages for unset fields
* Add parsing utility functions
html.php
- extractFromDelimiters
- stripWithDelimiters
- stripRecursiveHTMLSection
- markdownToHtml (partial)
bridges
- remove now-duplicate functions
- call functions from html.php instead
* [Anidex] New bridge
Anime torrent tracker
* [Anime-Ultime] Restore thumbnail
* [CNET] Recreate bridge
Full rewrite as the previous one was broken
* [Dilbert] Minor URI fix
Use new self::URI property
* [EstCeQuonMetEnProd] Fix content extraction
Bridge was broken
* [Facebook] Fix "SpSonsSoriSsés" label
... which was taking space in item title
* [Futura-Sciences] Use HTTPS, More cleanup
Use HTTPS as FS now offer HTTPS
Clean additional useless HTML elements
* [GBATemp] Multiple fixes
- Fix categories: missing "break" statements
- Restore thumbnail as enclosure
- Fix date extraction
- Fix user blog post extraction
- Use getSimpleHTMLDOMCached
* [JapanExpo] Fix bridge, HTTPS, thumbnails
- Fix getSimpleHTMLDOMCached call
- Upgrade to HTTPS as JE now offers HTTPS
- Restore thumbnails as enclosures
* [LeMondeInformatique] Fix bridge, HTTPS
- Upgrade to HTTPS as LMI now offers HTTPS
- Restore thumbnails using small images
- Fix content extraction
- Fix text encoding issue
* [Nextgov] Fix content extraction
- Restore thumbnail and use small image
- Field extraction fixes
* [NextInpact] Add categories and filtering by type
- Offer all RSS feeds
- Allow filtering by article type
- Implement extraction for brief articles
- Remove article limit, many brief articles are publied all at once
* [NyaaTorrents] New bridge
Anime torrent tracker
* [Releases3DS] Cache content, restore thumbnail
- Use getSimpleHTMLDOMCached
- Restore thumbnail as enclosure
* [TheHackerNews] Fix bridge
- Fix content extraction including article body
- Restore thumbnail as enclosure
* [WeLiveSecurity] HTTPS, Fix content extraction
- Upgrade to HTTPS as WLS now offers HTTPS
- Fix content extraction including article body
* [WordPress] Reduce timeout, more content selectors
- Reduce timeout to use default one (1h)
- Add new content selector (articleBody)
- Find thumbnail and set as enclosure
- Fix <script> cleanup
* [YGGTorrent] Increase limit, use cache
- Increase item limit as uploads are very frequent
- Use getSimpleHTMLDOMCached
* [ZDNet] Rewrite with FeedExpander
- Upgrade to HTTPS as ZD now offers HTTPS
- Use FeedExpander for secondary fields
- Fix content extraction for article body
* [Main] Handle MIME type for enclosures
Many feed readers will ignore enclosures (e.g. thumbnails) with no MIME type. This commit adds automatic MIME type detection based on file extension (which may be inaccurate but is the only way without fetching the content).
One can force enclosure type using #.ext anchor (hacky, needs improving)
* [FeedExpander] Improve field extraction
- Add support for passing enclosures
- Improve author and uri extraction
- Fix 'notice' PHP error messages
* [Pull] Coding style fixes for #802
* [Pull] Implementing changes for #802
- Fix coding style issues with str append
- Remove useless CACHE_TIMEOUT
- Use count() instead of $limit
- Use defaultLinkTo() + handle strings
- Use http_build_query()
- Fix missing </em>
- Remove error_reporting(0)
- warning CSS (@LogMANOriginal)
- Fix typo in FeedExpander comment
* [Main] More documentation for markdownToHtml
See #802 for more details
2018-09-09 22:20:13 +03:00
|
|
|
*/
|
|
|
|
function getMimeType($url) {
|
|
|
|
static $mime = null;
|
|
|
|
|
|
|
|
if (is_null($mime)) {
|
2018-09-15 15:46:11 +03:00
|
|
|
// Default values, overriden by /etc/mime.types when present
|
Catching up | [Main] Debug mode, parse utils, MIME | [Bridges] Add/Improve 20 bridges (#802)
* Debug mode improvements
- Improve debug warning message
- Restore error reporting in debug mode
- Fix 'notice' messages for unset fields
* Add parsing utility functions
html.php
- extractFromDelimiters
- stripWithDelimiters
- stripRecursiveHTMLSection
- markdownToHtml (partial)
bridges
- remove now-duplicate functions
- call functions from html.php instead
* [Anidex] New bridge
Anime torrent tracker
* [Anime-Ultime] Restore thumbnail
* [CNET] Recreate bridge
Full rewrite as the previous one was broken
* [Dilbert] Minor URI fix
Use new self::URI property
* [EstCeQuonMetEnProd] Fix content extraction
Bridge was broken
* [Facebook] Fix "SpSonsSoriSsés" label
... which was taking space in item title
* [Futura-Sciences] Use HTTPS, More cleanup
Use HTTPS as FS now offer HTTPS
Clean additional useless HTML elements
* [GBATemp] Multiple fixes
- Fix categories: missing "break" statements
- Restore thumbnail as enclosure
- Fix date extraction
- Fix user blog post extraction
- Use getSimpleHTMLDOMCached
* [JapanExpo] Fix bridge, HTTPS, thumbnails
- Fix getSimpleHTMLDOMCached call
- Upgrade to HTTPS as JE now offers HTTPS
- Restore thumbnails as enclosures
* [LeMondeInformatique] Fix bridge, HTTPS
- Upgrade to HTTPS as LMI now offers HTTPS
- Restore thumbnails using small images
- Fix content extraction
- Fix text encoding issue
* [Nextgov] Fix content extraction
- Restore thumbnail and use small image
- Field extraction fixes
* [NextInpact] Add categories and filtering by type
- Offer all RSS feeds
- Allow filtering by article type
- Implement extraction for brief articles
- Remove article limit, many brief articles are publied all at once
* [NyaaTorrents] New bridge
Anime torrent tracker
* [Releases3DS] Cache content, restore thumbnail
- Use getSimpleHTMLDOMCached
- Restore thumbnail as enclosure
* [TheHackerNews] Fix bridge
- Fix content extraction including article body
- Restore thumbnail as enclosure
* [WeLiveSecurity] HTTPS, Fix content extraction
- Upgrade to HTTPS as WLS now offers HTTPS
- Fix content extraction including article body
* [WordPress] Reduce timeout, more content selectors
- Reduce timeout to use default one (1h)
- Add new content selector (articleBody)
- Find thumbnail and set as enclosure
- Fix <script> cleanup
* [YGGTorrent] Increase limit, use cache
- Increase item limit as uploads are very frequent
- Use getSimpleHTMLDOMCached
* [ZDNet] Rewrite with FeedExpander
- Upgrade to HTTPS as ZD now offers HTTPS
- Use FeedExpander for secondary fields
- Fix content extraction for article body
* [Main] Handle MIME type for enclosures
Many feed readers will ignore enclosures (e.g. thumbnails) with no MIME type. This commit adds automatic MIME type detection based on file extension (which may be inaccurate but is the only way without fetching the content).
One can force enclosure type using #.ext anchor (hacky, needs improving)
* [FeedExpander] Improve field extraction
- Add support for passing enclosures
- Improve author and uri extraction
- Fix 'notice' PHP error messages
* [Pull] Coding style fixes for #802
* [Pull] Implementing changes for #802
- Fix coding style issues with str append
- Remove useless CACHE_TIMEOUT
- Use count() instead of $limit
- Use defaultLinkTo() + handle strings
- Use http_build_query()
- Fix missing </em>
- Remove error_reporting(0)
- warning CSS (@LogMANOriginal)
- Fix typo in FeedExpander comment
* [Main] More documentation for markdownToHtml
See #802 for more details
2018-09-09 22:20:13 +03:00
|
|
|
$mime = array(
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
'png' => 'image/png',
|
|
|
|
'image' => 'image/*'
|
|
|
|
);
|
2018-09-15 15:46:11 +03:00
|
|
|
// '@' is used to mute open_basedir warning, see issue #818
|
|
|
|
if (@is_readable('/etc/mime.types')) {
|
Catching up | [Main] Debug mode, parse utils, MIME | [Bridges] Add/Improve 20 bridges (#802)
* Debug mode improvements
- Improve debug warning message
- Restore error reporting in debug mode
- Fix 'notice' messages for unset fields
* Add parsing utility functions
html.php
- extractFromDelimiters
- stripWithDelimiters
- stripRecursiveHTMLSection
- markdownToHtml (partial)
bridges
- remove now-duplicate functions
- call functions from html.php instead
* [Anidex] New bridge
Anime torrent tracker
* [Anime-Ultime] Restore thumbnail
* [CNET] Recreate bridge
Full rewrite as the previous one was broken
* [Dilbert] Minor URI fix
Use new self::URI property
* [EstCeQuonMetEnProd] Fix content extraction
Bridge was broken
* [Facebook] Fix "SpSonsSoriSsés" label
... which was taking space in item title
* [Futura-Sciences] Use HTTPS, More cleanup
Use HTTPS as FS now offer HTTPS
Clean additional useless HTML elements
* [GBATemp] Multiple fixes
- Fix categories: missing "break" statements
- Restore thumbnail as enclosure
- Fix date extraction
- Fix user blog post extraction
- Use getSimpleHTMLDOMCached
* [JapanExpo] Fix bridge, HTTPS, thumbnails
- Fix getSimpleHTMLDOMCached call
- Upgrade to HTTPS as JE now offers HTTPS
- Restore thumbnails as enclosures
* [LeMondeInformatique] Fix bridge, HTTPS
- Upgrade to HTTPS as LMI now offers HTTPS
- Restore thumbnails using small images
- Fix content extraction
- Fix text encoding issue
* [Nextgov] Fix content extraction
- Restore thumbnail and use small image
- Field extraction fixes
* [NextInpact] Add categories and filtering by type
- Offer all RSS feeds
- Allow filtering by article type
- Implement extraction for brief articles
- Remove article limit, many brief articles are publied all at once
* [NyaaTorrents] New bridge
Anime torrent tracker
* [Releases3DS] Cache content, restore thumbnail
- Use getSimpleHTMLDOMCached
- Restore thumbnail as enclosure
* [TheHackerNews] Fix bridge
- Fix content extraction including article body
- Restore thumbnail as enclosure
* [WeLiveSecurity] HTTPS, Fix content extraction
- Upgrade to HTTPS as WLS now offers HTTPS
- Fix content extraction including article body
* [WordPress] Reduce timeout, more content selectors
- Reduce timeout to use default one (1h)
- Add new content selector (articleBody)
- Find thumbnail and set as enclosure
- Fix <script> cleanup
* [YGGTorrent] Increase limit, use cache
- Increase item limit as uploads are very frequent
- Use getSimpleHTMLDOMCached
* [ZDNet] Rewrite with FeedExpander
- Upgrade to HTTPS as ZD now offers HTTPS
- Use FeedExpander for secondary fields
- Fix content extraction for article body
* [Main] Handle MIME type for enclosures
Many feed readers will ignore enclosures (e.g. thumbnails) with no MIME type. This commit adds automatic MIME type detection based on file extension (which may be inaccurate but is the only way without fetching the content).
One can force enclosure type using #.ext anchor (hacky, needs improving)
* [FeedExpander] Improve field extraction
- Add support for passing enclosures
- Improve author and uri extraction
- Fix 'notice' PHP error messages
* [Pull] Coding style fixes for #802
* [Pull] Implementing changes for #802
- Fix coding style issues with str append
- Remove useless CACHE_TIMEOUT
- Use count() instead of $limit
- Use defaultLinkTo() + handle strings
- Use http_build_query()
- Fix missing </em>
- Remove error_reporting(0)
- warning CSS (@LogMANOriginal)
- Fix typo in FeedExpander comment
* [Main] More documentation for markdownToHtml
See #802 for more details
2018-09-09 22:20:13 +03:00
|
|
|
$file = fopen('/etc/mime.types', 'r');
|
|
|
|
while(($line = fgets($file)) !== false) {
|
|
|
|
$line = trim(preg_replace('/#.*/', '', $line));
|
|
|
|
if(!$line)
|
|
|
|
continue;
|
|
|
|
$parts = preg_split('/\s+/', $line);
|
|
|
|
if(count($parts) == 1)
|
|
|
|
continue;
|
|
|
|
$type = array_shift($parts);
|
|
|
|
foreach($parts as $part)
|
|
|
|
$mime[$part] = $type;
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($url, '?') !== false) {
|
|
|
|
$url_temp = substr($url, 0, strpos($url, '?'));
|
|
|
|
if (strpos($url, '#') !== false) {
|
|
|
|
$anchor = substr($url, strpos($url, '#'));
|
|
|
|
$url_temp .= $anchor;
|
|
|
|
}
|
|
|
|
$url = $url_temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
|
|
|
|
if (!empty($mime[$ext])) {
|
|
|
|
return $mime[$ext];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'application/octet-stream';
|
|
|
|
}
|