diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-09-22 12:14:23 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-09-22 12:14:23 +0200 |
commit | 19910df42e93ef370048e8d0d2609af71bf46676 (patch) | |
tree | 2a9557b219a6f3d8efb830333900473a93241b19 /articleenhancer/globalarticleenhancer.php | |
parent | fa73d339df403506d58104ee60575972b88bfc09 (diff) |
fix #454, allow global enhancers
Diffstat (limited to 'articleenhancer/globalarticleenhancer.php')
-rw-r--r-- | articleenhancer/globalarticleenhancer.php | 56 |
1 files changed, 56 insertions, 0 deletions
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 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @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 |