1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2025-04-09 19:15:42 +02:00

remove youtube autoplay in picofeed

This commit is contained in:
Bernhard Posselt 2015-04-15 11:23:49 +02:00
parent 53eccb2ff7
commit 99f1aedd70
12 changed files with 85 additions and 111 deletions

View File

@ -158,10 +158,6 @@ class Application extends App {
}
}
$enhancer->registerGlobalEnhancer(
$c->query('OCA\News\ArticleEnhancer\GlobalArticleEnhancer')
);
return $enhancer;
});

File diff suppressed because one or more lines are too long

View File

@ -1,70 +0,0 @@
<?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 DomDocument;
use DOMXpath;
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();
// wrap it inside a div if there is none to prevent invalid wrapping
// inside <p> tags
$body = '<div>' . $item->getBody() . '</div>';
$isOk = @$dom->loadHTML(
$body, 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
if ($isOk) {
$item->setBody(trim($dom->saveHTML()));
}
return $item;
}
}

10
composer.lock generated
View File

@ -57,12 +57,12 @@
"source": {
"type": "git",
"url": "https://github.com/fguillot/picoFeed.git",
"reference": "273c344b35b468b6c8053f635332c3a404f8c7b9"
"reference": "6ac3334f272478257d3e4486164bc4e84046f784"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/fguillot/picoFeed/zipball/273c344b35b468b6c8053f635332c3a404f8c7b9",
"reference": "273c344b35b468b6c8053f635332c3a404f8c7b9",
"url": "https://api.github.com/repos/fguillot/picoFeed/zipball/6ac3334f272478257d3e4486164bc4e84046f784",
"reference": "6ac3334f272478257d3e4486164bc4e84046f784",
"shasum": ""
},
"require": {
@ -87,7 +87,7 @@
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Unlicense"
"MIT"
],
"authors": [
{
@ -97,7 +97,7 @@
],
"description": "Modern library to write or read feeds (RSS/Atom)",
"homepage": "http://fguillot.github.io/picoFeed",
"time": "2015-04-11 12:46:50"
"time": "2015-04-15 00:10:14"
},
{
"name": "pear/net_url2",

View File

@ -119,12 +119,12 @@
"source": {
"type": "git",
"url": "https://github.com/fguillot/picoFeed.git",
"reference": "273c344b35b468b6c8053f635332c3a404f8c7b9"
"reference": "6ac3334f272478257d3e4486164bc4e84046f784"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/fguillot/picoFeed/zipball/273c344b35b468b6c8053f635332c3a404f8c7b9",
"reference": "273c344b35b468b6c8053f635332c3a404f8c7b9",
"url": "https://api.github.com/repos/fguillot/picoFeed/zipball/6ac3334f272478257d3e4486164bc4e84046f784",
"reference": "6ac3334f272478257d3e4486164bc4e84046f784",
"shasum": ""
},
"require": {
@ -138,7 +138,7 @@
"suggest": {
"ext-curl": "PicoFeed will use cURL if present"
},
"time": "2015-04-11 12:46:50",
"time": "2015-04-15 00:10:14",
"bin": [
"picofeed"
],
@ -151,7 +151,7 @@
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Unlicense"
"MIT"
],
"authors": [
{

21
vendor/fguillot/picofeed/LICENSE vendored Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Frederic Guillot
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -24,7 +24,7 @@ Features
- Content grabber: download from the original website the full content
- Enclosure detection
- RTL languages support
- License: Unlicense <http://unlicense.org/>
- License: MIT
Requirements
------------

View File

@ -1,24 +0,0 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

View File

@ -3,7 +3,7 @@
"description": "Modern library to write or read feeds (RSS/Atom)",
"homepage": "http://fguillot.github.io/picoFeed",
"type": "library",
"license": "Unlicense",
"license": "MIT",
"authors": [
{
"name": "Frédéric Guillot",

View File

@ -235,6 +235,7 @@ class Attribute
'filterProtocolUrlAttribute',
'rewriteImageProxyUrl',
'secureIframeSrc',
'removeYouTubeAutoplay'
);
/**
@ -404,6 +405,25 @@ class Attribute
return true;
}
/**
* Removes YouTube autoplay from iframes
*
* @access public
* @param string $tag Tag name
* @param array $attribute Atttributes name
* @param string $value Attribute value
* @return boolean
*/
public function removeYouTubeAutoplay($tag, $attribute, &$value)
{
$regex = '%^(https://(?:www\.)?youtube.com/.*\?.*autoplay=)(1)(.*)%i';
if ($tag === 'iframe' && $attribute === 'src' && preg_match($regex, $value)) {
$value = preg_replace($regex, '${1}0$3', $value);
}
return true;
}
/**
* Rewrite image url to use with a proxy
*

View File

@ -0,0 +1,13 @@
<?php
return array(
'test_url' => 'http://www.vuxml.org/freebsd/a5f160fa-deee-11e4-99f8-080027ef73ec.html',
'body' => array(
'//body'
),
'strip' => array(
'//h1',
'//div[@class="blurb"]',
'//hr',
'//p[@class="copyright"]'
)
);

View File

@ -128,6 +128,24 @@ class AttributeFilterTest extends PHPUnit_Framework_TestCase
$this->assertEquals(array('src' => 'https://www.youtube.com/test'), $filter->filter('iframe', array('src' => '//www.youtube.com/test')));
}
public function testRemoveYouTubeAutoplay()
{
$filter = new Attribute(new Url('http://google.com'));
$urls = array(
'https://www.youtube.com/something/?autoplay=1' => 'https://www.youtube.com/something/?autoplay=0',
'https://www.youtube.com/something/?test=s&autoplay=1&a=2' => 'https://www.youtube.com/something/?test=s&autoplay=0&a=2',
'https://www.youtube.com/something/?test=s' => 'https://www.youtube.com/something/?test=s',
'https://youtube.com/something/?autoplay=1' => 'https://youtube.com/something/?autoplay=0',
'https://youtube.com/something/?test=s&autoplay=1&a=2' => 'https://youtube.com/something/?test=s&autoplay=0&a=2',
'https://youtube.com/something/?test=s' => 'https://youtube.com/something/?test=s',
);
foreach ($urls as $before => $after) {
$filter->removeYouTubeAutoplay('iframe', 'src', $before);
$this->assertEquals($after, $before);
}
}
public function testFilterBlacklistAttribute()
{
$filter = new Attribute(new Url('http://google.com'));