2016-09-26 00:58:52 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
2023-06-02 21:22:09 +03:00
|
|
|
* Render template using base.html.php as base
|
2018-11-16 23:48:59 +03:00
|
|
|
*/
|
2022-07-08 15:17:25 +03:00
|
|
|
function render(string $template, array $context = []): string
|
|
|
|
{
|
2022-10-16 18:55:43 +03:00
|
|
|
if ($template === 'base.html.php') {
|
|
|
|
throw new \Exception('Do not render base.html.php into itself');
|
|
|
|
}
|
2023-06-02 21:22:09 +03:00
|
|
|
$context['messages'] = $context['messages'] ?? [];
|
|
|
|
if (Configuration::getConfig('system', 'message')) {
|
|
|
|
$context['messages'][] = [
|
|
|
|
'body' => Configuration::getConfig('system', 'message'),
|
|
|
|
'level' => 'info',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if (Debug::isEnabled()) {
|
|
|
|
$debugModeWhitelist = Configuration::getConfig('system', 'debug_mode_whitelist') ?: [];
|
|
|
|
if ($debugModeWhitelist === []) {
|
|
|
|
$context['messages'][] = [
|
|
|
|
'body' => 'Warning : Debug mode is active from any location, make sure only you can access RSS-Bridge.',
|
|
|
|
'level' => 'error'
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$context['messages'][] = [
|
|
|
|
'body' => 'Warning : Debug mode is active from your IP address, your requests will bypass the cache.',
|
|
|
|
'level' => 'warning'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 15:17:25 +03:00
|
|
|
$context['page'] = render_template($template, $context);
|
|
|
|
return render_template('base.html.php', $context);
|
|
|
|
}
|
|
|
|
|
2022-10-16 13:03:57 +03:00
|
|
|
/**
|
2023-09-23 19:54:14 +03:00
|
|
|
* Render php template with context
|
|
|
|
*
|
2024-01-25 01:06:23 +03:00
|
|
|
* DO NOT PASS USER INPUT IN $template OR $context (keys!)
|
2022-10-16 13:03:57 +03:00
|
|
|
*/
|
2022-07-08 15:17:25 +03:00
|
|
|
function render_template(string $template, array $context = []): string
|
|
|
|
{
|
|
|
|
if (isset($context['template'])) {
|
|
|
|
throw new \Exception("Don't use `template` as a context key");
|
|
|
|
}
|
2022-10-16 13:03:57 +03:00
|
|
|
$templateFilepath = __DIR__ . '/../templates/' . $template;
|
2022-07-08 15:17:25 +03:00
|
|
|
extract($context);
|
|
|
|
ob_start();
|
|
|
|
try {
|
2022-10-16 13:03:57 +03:00
|
|
|
if (is_file($template)) {
|
|
|
|
require $template;
|
|
|
|
} elseif (is_file($templateFilepath)) {
|
|
|
|
require $templateFilepath;
|
|
|
|
} else {
|
|
|
|
throw new \Exception(sprintf('Unable to find template `%s`', $template));
|
|
|
|
}
|
2022-07-08 15:17:25 +03:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
ob_end_clean();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape for html context
|
|
|
|
*/
|
|
|
|
function e(string $s): string
|
|
|
|
{
|
|
|
|
return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly don't escape
|
|
|
|
*/
|
|
|
|
function raw(string $s): string
|
|
|
|
{
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
2022-07-31 04:52:27 +03:00
|
|
|
function truncate(string $s, int $length = 150, $marker = '...'): string
|
|
|
|
{
|
|
|
|
$s = trim($s);
|
|
|
|
if (mb_strlen($s) <= $length) {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
return mb_substr($s, 0, $length) . $marker;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Removes unwanted tags from a given HTML text.
|
|
|
|
*
|
2018-11-18 19:43:34 +03:00
|
|
|
* @param string $html The HTML text to sanitize.
|
|
|
|
* @param array $tags_to_remove A list of tags to remove from the DOM.
|
|
|
|
* @param array $attributes_to_keep A list of attributes to keep on tags (other
|
2018-11-16 23:48:59 +03:00
|
|
|
* attributes are removed).
|
2018-11-18 19:43:34 +03:00
|
|
|
* @param array $text_to_keep A list of tags where the innertext replaces the tag
|
2018-11-16 23:48:59 +03:00
|
|
|
* (i.e. `<p>Hello World!</p>` becomes `Hello World!`).
|
|
|
|
* @return object A simplehtmldom object of the remaining contents.
|
|
|
|
*
|
|
|
|
* @todo Check if this implementation is still necessary, because simplehtmldom
|
|
|
|
* already removes some of the tags (search for `remove_noise` in simple_html_dom.php).
|
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
function sanitize(
|
|
|
|
$html,
|
|
|
|
$tags_to_remove = ['script', 'iframe', 'input', 'form'],
|
|
|
|
$attributes_to_keep = ['title', 'href', 'src'],
|
|
|
|
$text_to_keep = []
|
|
|
|
) {
|
|
|
|
$htmlContent = str_get_html($html);
|
|
|
|
|
|
|
|
foreach ($htmlContent->find('*') as $element) {
|
|
|
|
if (in_array($element->tag, $text_to_keep)) {
|
|
|
|
$element->outertext = $element->plaintext;
|
|
|
|
} elseif (in_array($element->tag, $tags_to_remove)) {
|
|
|
|
$element->outertext = '';
|
|
|
|
} else {
|
|
|
|
foreach ($element->getAllAttributes() as $attributeName => $attribute) {
|
|
|
|
if (!in_array($attributeName, $attributes_to_keep)) {
|
|
|
|
$element->removeAttribute($attributeName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
2016-09-26 00:58:52 +03:00
|
|
|
}
|
|
|
|
|
2023-06-09 00:04:16 +03:00
|
|
|
function break_annoying_html_tags(string $html): string
|
2022-08-06 23:46:28 +03:00
|
|
|
{
|
|
|
|
$html = str_replace('<script', '<‌script', $html); // Disable scripts, but leave them visible.
|
|
|
|
$html = str_replace('<iframe', '<‌iframe', $html);
|
|
|
|
$html = str_replace('<link', '<‌link', $html);
|
|
|
|
// We leave alone object and embed so that videos can play in RSS readers.
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Replace background by image
|
|
|
|
*
|
|
|
|
* Replaces tags with styles of `backgroud-image` by `<img />` tags.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* ```HTML
|
|
|
|
* <html>
|
|
|
|
* <body style="background-image: url('bgimage.jpg');">
|
|
|
|
* <h1>Hello world!</h1>
|
|
|
|
* </body>
|
|
|
|
* </html>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* results in this output:
|
|
|
|
*
|
|
|
|
* ```HTML
|
|
|
|
* <html>
|
|
|
|
* <img style="display:block;" src="bgimage.jpg" />
|
|
|
|
* </html>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param string $htmlContent The HTML content
|
|
|
|
* @return string The HTML content with all ocurrences replaced
|
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
function backgroundToImg($htmlContent)
|
|
|
|
{
|
|
|
|
$regex = '/background-image[ ]{0,}:[ ]{0,}url\([\'"]{0,}(.*?)[\'"]{0,}\)/';
|
|
|
|
$htmlContent = str_get_html($htmlContent);
|
|
|
|
|
|
|
|
foreach ($htmlContent->find('*') as $element) {
|
|
|
|
if (preg_match($regex, $element->style, $matches) > 0) {
|
|
|
|
$element->outertext = '<img style="display:block;" src="' . $matches[1] . '" />';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $htmlContent;
|
2017-03-03 17:13:29 +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
|
|
|
/**
|
|
|
|
* Convert relative links in HTML into absolute links
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
|
|
|
* This function is based on `php-urljoin`.
|
|
|
|
*
|
|
|
|
* @link https://github.com/plaidfluff/php-urljoin php-urljoin
|
|
|
|
*
|
2022-11-15 05:01:27 +03:00
|
|
|
* @param string|object $dom The HTML content. Supports HTML objects or string objects
|
|
|
|
* @param string $url Fully qualified URL to the page containing relative links
|
|
|
|
* @return string|object Content with fixed URLs.
|
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
|
|
|
*/
|
2022-11-15 05:01:27 +03:00
|
|
|
function defaultLinkTo($dom, $url)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-11-15 05:01:27 +03:00
|
|
|
if ($dom === '') {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$string_convert = false;
|
2022-11-15 05:01:27 +03:00
|
|
|
if (is_string($dom)) {
|
2022-07-01 16:10:30 +03:00
|
|
|
$string_convert = true;
|
2022-11-15 05:01:27 +03:00
|
|
|
$dom = str_get_html($dom);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-05-18 14:50:50 +03:00
|
|
|
// Use long method names for compatibility with simple_html_dom and DOMDocument
|
|
|
|
|
2023-05-21 22:06:35 +03:00
|
|
|
// Work around bug in simple_html_dom->getElementsByTagName
|
|
|
|
if ($dom instanceof simple_html_dom) {
|
|
|
|
$findByTag = function ($name) use ($dom) {
|
|
|
|
return $dom->getElementsByTagName($name, null);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
$findByTag = function ($name) use ($dom) {
|
|
|
|
return $dom->getElementsByTagName($name);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($findByTag('img') as $image) {
|
|
|
|
$image->setAttribute('src', urljoin($url, $image->getAttribute('src')));
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-05-21 22:06:35 +03:00
|
|
|
foreach ($findByTag('a') as $anchor) {
|
|
|
|
$anchor->setAttribute('href', urljoin($url, $anchor->getAttribute('href')));
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-05-18 14:50:50 +03:00
|
|
|
// Will never be true for DOMDocument
|
2022-07-01 16:10:30 +03:00
|
|
|
if ($string_convert) {
|
2022-11-15 05:01:27 +03:00
|
|
|
$dom = $dom->outertext;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-11-15 05:01:27 +03:00
|
|
|
return $dom;
|
2016-09-26 00:58:52 +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
|
|
|
|
2022-11-20 14:41:59 +03:00
|
|
|
/**
|
|
|
|
* Convert lazy-loading images and frames (video embeds) into static elements
|
|
|
|
*
|
|
|
|
* This function looks for lazy-loading attributes such as 'data-src' and converts
|
|
|
|
* them back to regular ones such as 'src', making them loadable in RSS readers.
|
|
|
|
* It also converts <picture> elements to plain <img> elements.
|
|
|
|
*
|
|
|
|
* @param string|object $content The HTML content. Supports HTML objects or string objects
|
|
|
|
* @return string|object Content with fixed image/frame URLs (same type as input).
|
|
|
|
*/
|
|
|
|
function convertLazyLoading($dom)
|
|
|
|
{
|
|
|
|
$string_convert = false;
|
|
|
|
if (is_string($dom)) {
|
|
|
|
$string_convert = true;
|
|
|
|
$dom = str_get_html($dom);
|
|
|
|
}
|
|
|
|
|
2023-10-18 20:12:19 +03:00
|
|
|
// Retrieve image URL from srcset attribute
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset
|
|
|
|
// Example: convert "header640.png 640w, header960.png 960w, header1024.png 1024w" to "header1024.png"
|
|
|
|
$srcset_to_src = function ($srcset) {
|
|
|
|
$sources = explode(',', $srcset);
|
2023-10-20 14:31:52 +03:00
|
|
|
$last_entry = trim($sources[array_key_last($sources)]);
|
2023-10-18 20:12:19 +03:00
|
|
|
$url = explode(' ', $last_entry)[0];
|
|
|
|
return $url;
|
|
|
|
};
|
|
|
|
|
2022-11-20 14:41:59 +03:00
|
|
|
// Process standalone images, embeds and picture sources
|
|
|
|
foreach ($dom->find('img, iframe, source') as $img) {
|
|
|
|
if (!empty($img->getAttribute('data-src'))) {
|
|
|
|
$img->src = $img->getAttribute('data-src');
|
|
|
|
} elseif (!empty($img->getAttribute('data-srcset'))) {
|
2023-10-18 20:12:19 +03:00
|
|
|
$img->src = $srcset_to_src($img->getAttribute('data-srcset'));
|
2022-11-20 14:41:59 +03:00
|
|
|
} elseif (!empty($img->getAttribute('data-lazy-src'))) {
|
|
|
|
$img->src = $img->getAttribute('data-lazy-src');
|
2023-10-20 14:31:52 +03:00
|
|
|
} elseif (!empty($img->getAttribute('data-orig-file'))) {
|
|
|
|
$img->src = $img->getAttribute('data-orig-file');
|
2022-11-20 14:41:59 +03:00
|
|
|
} elseif (!empty($img->getAttribute('srcset'))) {
|
2023-10-18 20:12:19 +03:00
|
|
|
$img->src = $srcset_to_src($img->getAttribute('srcset'));
|
2022-11-20 14:41:59 +03:00
|
|
|
} else {
|
|
|
|
continue; // Proceed to next element without removing attributes
|
|
|
|
}
|
2023-10-24 20:57:25 +03:00
|
|
|
|
|
|
|
// Remove data attributes, no longer necessary
|
|
|
|
foreach ($img->getAllAttributes() as $attr => $val) {
|
|
|
|
if (str_starts_with($attr, 'data-')) {
|
|
|
|
$img->removeAttribute($attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove other attributes that may be processed by the client
|
2023-10-20 14:31:52 +03:00
|
|
|
foreach (['loading', 'decoding', 'srcset'] as $attr) {
|
2022-11-20 14:41:59 +03:00
|
|
|
if ($img->hasAttribute($attr)) {
|
|
|
|
$img->removeAttribute($attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert complex HTML5 pictures to plain, standalone images
|
|
|
|
// <img> and <source> tags already have their "src" attribute set at this point,
|
|
|
|
// so we replace the whole <picture> with a standalone <img> from within the <picture>
|
|
|
|
foreach ($dom->find('picture') as $picture) {
|
|
|
|
$img = $picture->find('img, source', 0);
|
|
|
|
if (!empty($img)) {
|
|
|
|
if ($img->tag == 'source') {
|
|
|
|
$img->tag = 'img';
|
|
|
|
}
|
|
|
|
// Adding/removing node would change its position inside the parent element,
|
2023-10-20 14:31:52 +03:00
|
|
|
// So instead we rewrite the node in-place through the outertext attribute
|
2022-11-20 14:41:59 +03:00
|
|
|
$picture->outertext = $img->outertext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the expected return type is object, reload the DOM to make sure
|
|
|
|
// all $picture->outertext rewritten above are converted back to objects
|
|
|
|
$dom = $dom->outertext;
|
|
|
|
if (!$string_convert) {
|
|
|
|
$dom = str_get_html($dom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dom;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Extract the first part of a string matching the specified start and end delimiters
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
|
|
|
* @param string $string Input string, e.g. `<div>Post author: John Doe</div>`
|
|
|
|
* @param string $start Start delimiter, e.g. `author: `
|
|
|
|
* @param string $end End delimiter, e.g. `<`
|
|
|
|
* @return string|bool Extracted string, e.g. `John Doe`, or false if the
|
|
|
|
* delimiters were not found.
|
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
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
function extractFromDelimiters($string, $start, $end)
|
|
|
|
{
|
|
|
|
if (strpos($string, $start) !== false) {
|
|
|
|
$section_retrieved = substr($string, strpos($string, $start) + strlen($start));
|
|
|
|
$section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
|
|
|
|
return $section_retrieved;
|
Fix coding style missed by phpbcf (#2901)
$ composer require --dev friendsofphp/php-cs-fixer
$ echo >.php-cs-fixer.dist.php "<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__);
$rules = [
'@PSR12' => true,
// '@PSR12:risky' => true,
'@PHP74Migration' => true,
// '@PHP74Migration:risky' => true,
// buggy, duplicates existing comment sometimes
'no_break_comment' => false,
'array_syntax' => true,
'lowercase_static_reference' => true,
'visibility_required' => false,
// Too much noise
'binary_operator_spaces' => false,
'heredoc_indentation' => false,
'trailing_comma_in_multiline' => false,
];
$config = new PhpCsFixer\Config();
return $config
->setRules($rules)
// ->setRiskyAllowed(true)
->setFinder($finder);
"
$ vendor/bin/php-cs-fixer --version
PHP CS Fixer 3.8.0 BerSzcz against war! by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.1.7
$ vendor/bin/php-cs-fixer fix
$ rm .php-cs-fixer.cache
$ vendor/bin/php-cs-fixer fix
2022-07-08 14:00:52 +03:00
|
|
|
}
|
|
|
|
return false;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove one or more part(s) of a string using a start and end delmiters
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
|
|
|
* @param string $string Input string, e.g. `foo<script>superscript()</script>bar`
|
2022-05-10 10:37:53 +03:00
|
|
|
* @param string $start Start delimiter, e.g. `<script>`
|
2018-11-16 23:48:59 +03:00
|
|
|
* @param string $end End delimiter, e.g. `</script>`
|
|
|
|
* @return string Cleaned string, e.g. `foobar`
|
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
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
function stripWithDelimiters($string, $start, $end)
|
|
|
|
{
|
|
|
|
while (strpos($string, $start) !== false) {
|
|
|
|
$section_to_remove = substr($string, strpos($string, $start));
|
|
|
|
$section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
|
|
|
|
$string = str_replace($section_to_remove, '', $string);
|
|
|
|
}
|
|
|
|
return $string;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove HTML sections containing one or more sections using the same HTML tag
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
|
|
|
* @param string $string Input string, e.g. `foo<div class="ads"><div>ads</div>ads</div>bar`
|
|
|
|
* @param string $tag_name Name of the HTML tag, e.g. `div`
|
|
|
|
* @param string $tag_start Start of the HTML tag to remove, e.g. `<div class="ads">`
|
|
|
|
* @return string Cleaned String, e.g. `foobar`
|
|
|
|
*
|
2022-11-20 14:41:59 +03:00
|
|
|
* This function works by locating the desired tag start, then finding the appropriate
|
|
|
|
* end by counting opening and ending tags until the amount of open tags reaches zero:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* Amount of open tags:
|
|
|
|
* 1 2 1 0
|
|
|
|
* |---------------||---| |----| |----|
|
|
|
|
* <div class="ads"><div>ads</div>ads</div>bar
|
|
|
|
* | <-------- Section to remove -------> |
|
|
|
|
* ```
|
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
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
function stripRecursiveHTMLSection($string, $tag_name, $tag_start)
|
|
|
|
{
|
|
|
|
$open_tag = '<' . $tag_name;
|
|
|
|
$close_tag = '</' . $tag_name . '>';
|
|
|
|
$close_tag_length = strlen($close_tag);
|
2022-11-20 14:41:59 +03:00
|
|
|
|
|
|
|
// Make sure the provided $tag_start argument matches the provided $tag_name argument
|
2022-07-01 16:10:30 +03:00
|
|
|
if (strpos($tag_start, $open_tag) === 0) {
|
2022-11-20 14:41:59 +03:00
|
|
|
// While tag_start is present, there is at least one remaining section to remove
|
2022-07-01 16:10:30 +03:00
|
|
|
while (strpos($string, $tag_start) !== false) {
|
2022-11-20 14:41:59 +03:00
|
|
|
// In order to locate the end of the section, we attempt each closing tag until we find the right one
|
|
|
|
// We know we found the right one when the amount of "<tag" is the same as amount of "</tag"
|
|
|
|
// When the attempted "</tag" is not the correct one, we increase $search_offset to skip it
|
|
|
|
// and retry unless $max_recursion is reached (prevents infinite loop on malformed HTML)
|
2022-07-01 16:10:30 +03:00
|
|
|
$max_recursion = 100;
|
|
|
|
$section_to_remove = null;
|
|
|
|
$section_start = strpos($string, $tag_start);
|
|
|
|
$search_offset = $section_start;
|
|
|
|
do {
|
|
|
|
$max_recursion--;
|
2022-11-20 14:41:59 +03:00
|
|
|
// Move on to the next occurrence of "</tag"
|
2022-07-01 16:10:30 +03:00
|
|
|
$section_end = strpos($string, $close_tag, $search_offset);
|
|
|
|
$search_offset = $section_end + $close_tag_length;
|
2022-11-20 14:41:59 +03:00
|
|
|
// If the next "</tag" is the correct one, then this is the section we must remove:
|
2022-07-01 16:10:30 +03:00
|
|
|
$section_to_remove = substr($string, $section_start, $section_end - $section_start + $close_tag_length);
|
2022-11-20 14:41:59 +03:00
|
|
|
// Count amount of "<tag" and "</tag" in the section to remove
|
2022-07-01 16:10:30 +03:00
|
|
|
$open_tag_count = substr_count($section_to_remove, $open_tag);
|
|
|
|
$close_tag_count = substr_count($section_to_remove, $close_tag);
|
|
|
|
} while ($open_tag_count > $close_tag_count && $max_recursion > 0);
|
2022-11-20 14:41:59 +03:00
|
|
|
// We exited the loop, let's remove the section
|
2022-07-01 16:10:30 +03:00
|
|
|
$string = str_replace($section_to_remove, '', $string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $string;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-09 21:29:02 +03:00
|
|
|
* Convert Markdown into HTML with Parsedown.
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
2022-05-10 10:37:53 +03:00
|
|
|
* @link https://parsedown.org/ Parsedown
|
2018-11-16 23:48:59 +03:00
|
|
|
*
|
|
|
|
* @param string $string Input string in Markdown format
|
2023-05-11 22:24:12 +03:00
|
|
|
* @param array $config Parsedown options to control output
|
2018-11-16 23:48:59 +03:00
|
|
|
* @return string output string in HTML format
|
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
|
|
|
*/
|
2023-05-11 22:24:12 +03:00
|
|
|
function markdownToHtml($string, $config = [])
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
|
|
|
$Parsedown = new Parsedown();
|
2023-05-11 22:24:12 +03:00
|
|
|
foreach ($config as $option => $value) {
|
|
|
|
if ($option === 'breaksEnabled') {
|
|
|
|
$Parsedown->setBreaksEnabled($value);
|
|
|
|
} elseif ($option === 'markupEscaped') {
|
|
|
|
$Parsedown->setMarkupEscaped($value);
|
|
|
|
} elseif ($option === 'urlsLinked') {
|
|
|
|
$Parsedown->setUrlsLinked($value);
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException("Invalid Parsedown option \"$option\"");
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
return $Parsedown->text($string);
|
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
|
|
|
}
|