diff options
Diffstat (limited to 'vendor/fguillot/picofeed/lib/PicoFeed/Filter')
3 files changed, 76 insertions, 33 deletions
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Attribute.php b/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Attribute.php index 23b1103ad..66b3470f8 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Attribute.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Attribute.php @@ -13,6 +13,22 @@ use \PicoFeed\Client\Url; class Attribute { /** + * Image proxy url + * + * @access private + * @var string + */ + private $image_proxy_url = ''; + + /** + * Image proxy callback + * + * @access private + * @var \Closure|null + */ + private $image_proxy_callback = null; + + /** * Tags and attribute whitelist * * @access private @@ -204,10 +220,11 @@ class Attribute 'filterEmptyAttribute', 'filterAllowedAttribute', 'filterIntegerAttribute', - 'filterAbsoluteUrlAttribute', + 'rewriteAbsoluteUrl', 'filterIframeAttribute', 'filterBlacklistResourceAttribute', 'filterProtocolUrlAttribute', + 'rewriteImageProxyUrl', ); /** @@ -349,7 +366,7 @@ class Attribute * @param string $value Atttribute value * @return boolean */ - public function filterAbsoluteUrlAttribute($tag, $attribute, &$value) + public function rewriteAbsoluteUrl($tag, $attribute, &$value) { if ($this->isResource($attribute)) { $value = Url::resolve($value, $this->website); @@ -359,6 +376,30 @@ class Attribute } /** + * Rewrite image url to use with a proxy + * + * @access public + * @param string $tag Tag name + * @param string $attribute Atttribute name + * @param string $value Atttribute value + * @return boolean + */ + public function rewriteImageProxyUrl($tag, $attribute, &$value) + { + if ($tag === 'img' && $attribute === 'src') { + + if ($this->image_proxy_url) { + $value = sprintf($this->image_proxy_url, urlencode($value)); + } + else if (is_callable($this->image_proxy_callback)) { + $value = call_user_func($this->image_proxy_callback, $value); + } + } + + return true; + } + + /** * Return true if the scheme is authorized * * @access public @@ -484,7 +525,7 @@ class Attribute } /** - * Set whitelisted tags adn attributes for each tag + * Set whitelisted tags and attributes for each tag * * @access public * @param array $values List of tags: ['video' => ['src', 'cover'], 'img' => ['src']] @@ -586,4 +627,32 @@ class Attribute $this->iframe_whitelist = $values ?: $this->iframe_whitelist; return $this; } + + /** + * Set image proxy URL + * + * The original image url will be urlencoded + * + * @access public + * @param string $url Proxy URL + * @return \PicoFeed\Filter\Filter + */ + public function setImageProxyUrl($url) + { + $this->image_proxy_url = $url ?: $this->image_proxy_url; + return $this; + } + + /** + * Set image proxy callback + * + * @access public + * @param \Closure $callback + * @return \PicoFeed\Filter\Filter + */ + public function setImageProxyCallback($callback) + { + $this->image_proxy_callback = $callback ?: $this->image_proxy_callback; + return $this; + } } diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Filter.php b/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Filter.php index 0490e2f49..82289444b 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Filter.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Filter.php @@ -2,6 +2,8 @@ namespace PicoFeed\Filter; +use PicoFeed\Parser\XmlParser; + /** * Filter class * @@ -137,34 +139,4 @@ class Filter return $data; } - - /** - * Get the first XML tag - * - * @static - * @access public - * @param string $data Feed content - * @return string - */ - public static function getFirstTag($data) - { - // Strip HTML comments (max of 5,000 characters long to prevent crashing) - $data = preg_replace('/<!--(.{0,5000}?)-->/Uis', '', $data); - - /* Strip Doctype: - * Doctype needs to be within the first 100 characters. (Ideally the first!) - * If it's not found by then, we need to stop looking to prevent PREG - * from reaching max backtrack depth and crashing. - */ - $data = preg_replace('/^.{0,100}<!DOCTYPE([^>]*)>/Uis', '', $data); - - // Strip <?xml version.... - $data = self::stripXmlTag($data); - - // Find the first tag - $open_tag = strpos($data, '<'); - $close_tag = strpos($data, '>'); - - return substr($data, $open_tag, $close_tag); - } } diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Html.php b/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Html.php index d14d80907..f7816f1d2 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Html.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Filter/Html.php @@ -96,6 +96,8 @@ class Html $this->config = $config; if ($this->config !== null) { + $this->attribute->setImageProxyCallback($this->config->getFilterImageProxyCallback()); + $this->attribute->setImageProxyUrl($this->config->getFilterImageProxyUrl()); $this->attribute->setIframeWhitelist($this->config->getFilterIframeWhitelist(array())); $this->attribute->setIntegerAttributes($this->config->getFilterIntegerAttributes(array())); $this->attribute->setAttributeOverrides($this->config->getFilterAttributeOverrides(array())); |