From 19910df42e93ef370048e8d0d2609af71bf46676 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 22 Sep 2014 12:14:23 +0200 Subject: fix #454, allow global enhancers --- articleenhancer/globalarticleenhancer.php | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 articleenhancer/globalarticleenhancer.php (limited to 'articleenhancer/globalarticleenhancer.php') diff --git a/articleenhancer/globalarticleenhancer.php b/articleenhancer/globalarticleenhancer.php new file mode 100644 index 000000000..cfefb882b --- /dev/null +++ b/articleenhancer/globalarticleenhancer.php @@ -0,0 +1,56 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\ArticleEnhancer; + +use \OCA\News\Db\Item; + + +class GlobalArticleEnhancer implements ArticleEnhancer { + + + /** + * This method is run after all enhancers and for every item + */ + public function enhance(Item $item) { + $dom = new \DOMDocument(); + @$dom->loadHTML($item->getBody(), LIBXML_HTML_NOIMPLIED | + LIBXML_HTML_NODEFDTD); + $xpath = new \DOMXpath($dom); + + // remove youtube autoplay + // NOTE: PHP supports only XPath 1.0 so no matches() function :( + $youtubeIframes = "//iframe[contains(@src, 'youtube.com')]"; + + $elements = $xpath->query($youtubeIframes); + foreach ($elements as $element) { + + // src needs to be matched against regex to prevent false positives + // and because theres no XPath matches function available + $src = $element->getAttribute('src'); + $regex = '%^(http://|https://|//)(www\.)?youtube.com/.*autoplay=1.*%i'; + + if (preg_match($regex, $src)) { + $replaced = str_replace('autoplay=1', 'autoplay=0', $src); + $element->setAttribute('src', $replaced); + } + } + + // save all changes back to the item + $item->setBody(trim($dom->saveHTML())); + + return $item; + } + + +} \ No newline at end of file -- cgit v1.2.3