X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=inline;f=src%2FWallabag%2FCoreBundle%2FHelper%2FDownloadImages.php;h=1d98fd1a93135dbca275a721558749fda3bdd4c6;hb=3c5aa05311d057c8fed83f085e6fb6ecc874fca4;hp=7a39a2e4a6d9cbf62e329822e9a806a328863356;hpb=2cbee36a0184786644470a84fdd8c720cfcac58e;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php index 7a39a2e4..1d98fd1a 100644 --- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php +++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php @@ -2,6 +2,8 @@ namespace Wallabag\CoreBundle\Helper; +use GuzzleHttp\Psr7\Uri; +use GuzzleHttp\Psr7\UriResolver; use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\Plugin\ErrorPlugin; use Http\Client\Common\PluginClient; @@ -45,10 +47,8 @@ class DownloadImages public static function extractImagesUrlsFromHtml($html) { $crawler = new Crawler($html); - $imagesCrawler = $crawler - ->filterXpath('//img'); - $imagesUrls = $imagesCrawler - ->extract(['src']); + $imagesCrawler = $crawler->filterXpath('//img'); + $imagesUrls = $imagesCrawler->extract(['src']); $imagesSrcsetUrls = self::getSrcsetUrls($imagesCrawler); return array_unique(array_merge($imagesUrls, $imagesSrcsetUrls)); @@ -213,29 +213,30 @@ class DownloadImages /** * Get images urls from the srcset image attribute. * - * @param Crawler $imagesCrawler - * * @return array An array of urls */ private static function getSrcsetUrls(Crawler $imagesCrawler) { $urls = []; - $iterator = $imagesCrawler - ->getIterator(); + $iterator = $imagesCrawler->getIterator(); + while ($iterator->valid()) { $srcsetAttribute = $iterator->current()->getAttribute('srcset'); + if ('' !== $srcsetAttribute) { // Couldn't start with " OR ' OR a white space // Could be one or more white space // Must be one or more digits followed by w OR x $pattern = "/(?:[^\"'\s]+\s*(?:\d+[wx])+)/"; preg_match_all($pattern, $srcsetAttribute, $matches); + $srcset = \call_user_func_array('array_merge', $matches); $srcsetUrls = array_map(function ($src) { return trim(explode(' ', $src, 2)[0]); }, $srcset); $urls = array_merge($srcsetUrls, $urls); } + $iterator->next(); } @@ -292,20 +293,16 @@ class DownloadImages return $url; } - $base = new \SimplePie_IRI($base); + $base = new Uri($base); - // remove '//' in URL path (causes URLs not to resolve properly) - if (isset($base->ipath)) { - $base->ipath = preg_replace('!//+!', '/', $base->ipath); - } + // in case the url has no scheme & host + if ('' === $base->getAuthority() || '' === $base->getScheme()) { + $this->logger->error('DownloadImages: Can not make an absolute link', ['base' => $base, 'url' => $url]); - if ($absolute = \SimplePie_IRI::absolutize($base, $url)) { - return $absolute->get_uri(); + return false; } - $this->logger->error('DownloadImages: Can not make an absolute link', ['base' => $base, 'url' => $url]); - - return false; + return (string) UriResolver::resolve($base, new Uri($url)); } /**