aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-12-17 13:56:24 +0100
committerArthurHoaro <arthur@hoa.ro>2020-12-17 13:56:24 +0100
commit88a8e284b2825cbd77c75dbbbf3655a961d9eb09 (patch)
tree77a5d9f5e43283028b1d462bd608e9e180f415dd
parente4b8330e459b598328bf250208386c06ec257b08 (diff)
downloadShaarli-88a8e284b2825cbd77c75dbbbf3655a961d9eb09.tar.gz
Shaarli-88a8e284b2825cbd77c75dbbbf3655a961d9eb09.tar.zst
Shaarli-88a8e284b2825cbd77c75dbbbf3655a961d9eb09.zip
Fix metadata extract regex (2)
Reference: https://stackoverflow.com/questions/8055727/negating-a-backreference-in-regular-expressions Fixes #1656
-rw-r--r--application/bookmark/LinkUtils.php6
-rw-r--r--tests/bookmark/LinkUtilsTest.php10
2 files changed, 14 insertions, 2 deletions
diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php
index d65e97ed..0ab2d213 100644
--- a/application/bookmark/LinkUtils.php
+++ b/application/bookmark/LinkUtils.php
@@ -68,11 +68,13 @@ function html_extract_tag($tag, $html)
68 $properties = implode('|', $propertiesKey); 68 $properties = implode('|', $propertiesKey);
69 // We need a OR here to accept either 'property=og:noquote' or 'property="og:unrelated og:my-tag"' 69 // We need a OR here to accept either 'property=og:noquote' or 'property="og:unrelated og:my-tag"'
70 $orCondition = '["\']?(?:og:)?' . $tag . '["\']?|["\'][^\'"]*?(?:og:)?' . $tag . '[^\'"]*?[\'"]'; 70 $orCondition = '["\']?(?:og:)?' . $tag . '["\']?|["\'][^\'"]*?(?:og:)?' . $tag . '[^\'"]*?[\'"]';
71 // Support quotes in double quoted content, and the other way around
72 $content = 'content=(["\'])((?:(?!\1).)*)\1';
71 // Try to retrieve OpenGraph tag. 73 // Try to retrieve OpenGraph tag.
72 $ogRegex = '#<meta[^>]+(?:' . $properties . ')=(?:' . $orCondition . ')[^>]*content=(["\'])([^\1]*?)\1.*?>#'; 74 $ogRegex = '#<meta[^>]+(?:' . $properties . ')=(?:' . $orCondition . ')[^>]*' . $content . '.*?>#';
73 // If the attributes are not in the order property => content (e.g. Github) 75 // If the attributes are not in the order property => content (e.g. Github)
74 // New regex to keep this readable... more or less. 76 // New regex to keep this readable... more or less.
75 $ogRegexReverse = '#<meta[^>]+content=(["\'])([^\1]*?)\1[^>]+(?:' . $properties . ')=(?:' . $orCondition . ').*?>#'; 77 $ogRegexReverse = '#<meta[^>]+' . $content . '[^>]+(?:' . $properties . ')=(?:' . $orCondition . ').*?>#';
76 78
77 if ( 79 if (
78 preg_match($ogRegex, $html, $matches) > 0 80 preg_match($ogRegex, $html, $matches) > 0
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php
index ddab4e3c..46a7f1fe 100644
--- a/tests/bookmark/LinkUtilsTest.php
+++ b/tests/bookmark/LinkUtilsTest.php
@@ -245,6 +245,16 @@ class LinkUtilsTest extends TestCase
245 $this->assertFalse(html_extract_tag('description', $html)); 245 $this->assertFalse(html_extract_tag('description', $html));
246 } 246 }
247 247
248 public function testHtmlExtractDescriptionFromGoogleRealCase(): void
249 {
250 $html = 'id="gsr"><meta content="Fêtes de fin d\'année" property="twitter:title"><meta '.
251 'content="Bonnes fêtes de fin d\'année ! #GoogleDoodle" property="twitter:description">'.
252 '<meta content="Bonnes fêtes de fin d\'année ! #GoogleDoodle" property="og:description">'.
253 '<meta content="summary_large_image" property="twitter:card"><meta co'
254 ;
255 $this->assertSame('Bonnes fêtes de fin d\'année ! #GoogleDoodle', html_extract_tag('description', $html));
256 }
257
248 /** 258 /**
249 * Test the header callback with valid value 259 * Test the header callback with valid value
250 */ 260 */