mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
Use PHP 8.4+ \Dom classes and functions (#9567)
Currently, SimplePie uses legacy DOMDocument parsing, which outputs potentially problematic HTML, not correctly sanitised.
This breaks the assumption that one can use something like `<div><!-- some HTML sanitized from SimplePie --></div>`.
```
--- Expected
+++ Actual
@@ @@
'<ul>
- <li><div></div></li>
+ <li><div>
<li>Item</li>
-</ul>'
+</div></li></ul>'
```
When shown in a compliant Web browser, several tags will be wrongly closed, breaking the layout of the client.
This PR patches SimplePie to use the modern parser and code syntax, while maintaining a best-effort polyfill for legacy PHP versions < 8.4.
Depends on:
SimplePie: Upgrade to PHPStan 2
* https://github.com/FreshRSS/simplepie/pull/97
* https://github.com/simplepie/simplepie/pull/939
* https://github.com/simplepie/simplepie/pull/1019
SimplePie: Use PHP 8.4+ \Dom classes and functions
* https://github.com/FreshRSS/simplepie/pull/98
* https://github.com/simplepie/simplepie/issues/1021
SimplePie: Add unit test for HTML5 sanitizing compliance
* https://github.com/simplepie/simplepie/pull/1022
SimplePie: Remove obsolete xml_is_sane and XMLReader fallback
* https://github.com/simplepie/simplepie/pull/1023
CssXPath: Add support for case insensitivity
* https://github.com/phpgt/CssXPath/pull/256
fix https://github.com/FreshRSS/FreshRSS/issues/5201
fix https://github.com/FreshRSS/FreshRSS/issues/6830
fix https://github.com/FreshRSS/FreshRSS/issues/6831
~5% faster for HTML sanitising.
Remove SimpleXML. Remove XMLReader.
This commit is contained in:
1 parent
5ad167e6f3
commit
ea646d2bde
44 files changed
+1004
-615
No files matched your search
@@ -347,46 +347,59 @@ class FreshRSS_importExport_Controller extends FreshRSS_ActionController {
|
||||
. '</body></opml>' . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|false JSON, or false if the file is invalid XML
|
||||
*/
|
||||
private function ttrssXmlToJson(string $xml): string|false {
|
||||
$table = (array)simplexml_load_string($xml, options: LIBXML_NOBLANKS | LIBXML_NOCDATA);
|
||||
$table['items'] = $table['article'] ?? [];
|
||||
if (!is_array($table['items'])) {
|
||||
$table['items'] = [];
|
||||
try {
|
||||
$dom = @Dom\XMLDocument::createFromString($xml, LIBXML_NOERROR);
|
||||
} catch (Throwable $ex) { // @phpstan-ignore catch.neverThrown (Dom\XMLDocument::createFromString() does throw)
|
||||
Minz_Log::warning('Error during TT-RSS articles import: ' . $ex->getMessage());
|
||||
return false;
|
||||
}
|
||||
unset($table['article']);
|
||||
for ($i = count($table['items']) - 1; $i >= 0; $i--) {
|
||||
$item = (array)($table['items'][$i]);
|
||||
$item = array_filter($item, static fn($v) =>
|
||||
// Filter out empty properties, potentially reported as empty objects
|
||||
(is_string($v) && trim($v) !== '') || !empty($v));
|
||||
$item['updated'] = is_string($item['updated'] ?? null) ? strtotime($item['updated']) : '';
|
||||
$item['published'] = $item['updated'];
|
||||
$item['content'] = ['content' => $item['content'] ?? ''];
|
||||
$item['categories'] = is_string($item['tag_cache'] ?? null) ? [$item['tag_cache']] : [];
|
||||
if (!empty($item['marked'])) {
|
||||
$item['categories'][] = 'user/-/state/com.google/starred';
|
||||
}
|
||||
if (!empty($item['published'])) {
|
||||
$item['categories'][] = 'user/-/state/com.google/broadcast';
|
||||
}
|
||||
if (is_string($item['label_cache'] ?? null)) {
|
||||
$labels_cache = json_decode($item['label_cache'], true);
|
||||
if (is_array($labels_cache)) {
|
||||
foreach ($labels_cache as $label_cache) {
|
||||
if (is_array($label_cache) && !empty($label_cache[1]) && is_string($label_cache[1])) {
|
||||
$item['categories'][] = 'user/-/label/' . trim($label_cache[1]);
|
||||
$domElement = $dom->documentElement;
|
||||
$table = ['items' => []];
|
||||
if ($domElement !== null) {
|
||||
foreach ($domElement->childNodes as $article) {
|
||||
if (!$article instanceof Dom\Element || $article->nodeName !== 'article') {
|
||||
continue;
|
||||
}
|
||||
$item = [];
|
||||
foreach ($article->childNodes as $child) {
|
||||
if ($child instanceof Dom\Element) {
|
||||
$item[$child->nodeName] = $child->textContent;
|
||||
}
|
||||
}
|
||||
$item = array_filter($item, static fn($v) => trim($v) !== '');
|
||||
$item['updated'] = isset($item['updated']) ? strtotime($item['updated']) : '';
|
||||
$item['published'] = $item['updated'];
|
||||
$item['content'] = ['content' => $item['content'] ?? ''];
|
||||
$item['categories'] = isset($item['tag_cache']) ? [$item['tag_cache']] : [];
|
||||
if (!empty($item['marked'])) {
|
||||
$item['categories'][] = 'user/-/state/com.google/starred';
|
||||
}
|
||||
if (!empty($item['published'])) {
|
||||
$item['categories'][] = 'user/-/state/com.google/broadcast';
|
||||
}
|
||||
if (is_string($item['label_cache'] ?? null)) {
|
||||
$labels_cache = json_decode($item['label_cache'], true);
|
||||
if (is_array($labels_cache)) {
|
||||
foreach ($labels_cache as $label_cache) {
|
||||
if (is_array($label_cache) && !empty($label_cache[1]) && is_string($label_cache[1])) {
|
||||
$item['categories'][] = 'user/-/label/' . trim($label_cache[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$item['alternate'] = [['href' => $item['link'] ?? '']];
|
||||
$item['origin'] = [
|
||||
'title' => $item['feed_title'] ?? '',
|
||||
'feedUrl' => $item['feed_url'] ?? '',
|
||||
];
|
||||
$item['id'] = $item['guid'] ?? ($item['feed_url'] ?? $item['published']);
|
||||
$item['guid'] = $item['id'];
|
||||
$table['items'][] = $item;
|
||||
}
|
||||
$item['alternate'] = [['href' => $item['link'] ?? '']];
|
||||
$item['origin'] = [
|
||||
'title' => $item['feed_title'] ?? '',
|
||||
'feedUrl' => $item['feed_url'] ?? '',
|
||||
];
|
||||
$item['id'] = $item['guid'] ?? ($item['feed_url'] ?? $item['published']);
|
||||
$item['guid'] = $item['id'];
|
||||
$table['items'][$i] = $item;
|
||||
}
|
||||
return json_encode($table);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user