Browse Source

Added patches to fix relative to absolute URIs in findFeedLinks - fixes ZF-8330 (patches courtesy of Jan Sorgalla [jsor]).

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19136 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 16 years ago
parent
commit
ce3c247e78
2 changed files with 50 additions and 1 deletions
  1. 27 1
      library/Zend/Feed/Reader/FeedSet.php
  2. 23 0
      tests/Zend/Feed/ReaderTest.php

+ 27 - 1
library/Zend/Feed/Reader/FeedSet.php

@@ -89,7 +89,13 @@ class Zend_Feed_Reader_FeedSet extends ArrayObject
     {
         if (!Zend_Uri::check($link)) {
             if (!is_null($uri)) {
-                $link = rtrim($uri, '/') . '/' . ltrim($link, '/');
+                $uri = Zend_Uri::factory($uri);
+
+                if ($link[0] !== '/') {
+                    $link = $uri->getPath() . '/' . $link;
+                }
+
+                $link = $uri->getScheme() . '://' . $uri->getHost() . '/' . $this->_canonicalizePath($link);
                 if (!Zend_Uri::check($link)) {
                     $link = null;
                 }
@@ -97,6 +103,26 @@ class Zend_Feed_Reader_FeedSet extends ArrayObject
         }
         return $link;
     }
+    
+    /**
+     *  Canonicalize relative path
+     */
+    protected function _canonicalizePath($path)
+    {
+        $parts = array_filter(explode('/', $path));
+        $absolutes = array();
+        foreach ($parts as $part) {
+            if ('.' == $part) {
+                continue;
+            }
+            if ('..' == $part) {
+                array_pop($absolutes);
+            } else {
+                $absolutes[] = $part;
+            }
+        }
+        return implode('/', $absolutes);
+    }
 
     /**
      * Supports lazy loading of feeds using Zend_Feed_Reader::import() but

+ 23 - 0
tests/Zend/Feed/ReaderTest.php

@@ -269,6 +269,29 @@ class Zend_Feed_ReaderTest extends PHPUnit_Framework_TestCase
         }
         $this->assertEquals('http://meiobit.com/rss.xml', $links->rss);
     }
+    
+    /**
+     * @group ZF-8330
+     */
+    public function testGetsFeedLinksAndNormalisesRelativeUrlsOnUriWithPath()
+    {
+        try {
+            $currClient = Zend_Feed_Reader::getHttpClient();
+
+            $testAdapter = new Zend_Http_Client_Adapter_Test();
+            $testAdapter->setResponse(new Zend_Http_Response(200, array(), '<!DOCTYPE html><html><head><link rel="alternate" type="application/rss+xml" href="../test.rss"><link rel="alternate" type="application/atom+xml" href="/test.atom"></head><body></body></html>'));
+            Zend_Feed_Reader::setHttpClient(new Zend_Http_Client(null, array('adapter' => $testAdapter)));
+
+            $links = Zend_Feed_Reader::findFeedLinks('http://foo/bar');
+
+            Zend_Feed_Reader::setHttpClient($currClient);
+        } catch(Exception $e) {
+            $this->fail($e->getMessage());
+        }
+
+        $this->assertEquals('http://foo/test.rss', $links->rss);
+        $this->assertEquals('http://foo/test.atom', $links->atom);
+    }
 
     public function testAddsPrefixPath()
     {