db backup prior to drupal security update
[yaffs-website] / vendor / zendframework / zend-feed / src / Reader / FeedSet.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
5  * @link      http://github.com/zendframework/zf2 for the canonical source repository
6  * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7  * @license   http://framework.zend.com/license/new-bsd New BSD License
8  */
9
10 namespace Zend\Feed\Reader;
11
12 use ArrayObject;
13 use DOMNodeList;
14 use Zend\Feed\Uri;
15
16 class FeedSet extends ArrayObject
17 {
18     public $rss = null;
19
20     public $rdf = null;
21
22     public $atom = null;
23
24     /**
25      * Import a DOMNodeList from any document containing a set of links
26      * for alternate versions of a document, which will normally refer to
27      * RSS/RDF/Atom feeds for the current document.
28      *
29      * All such links are stored internally, however the first instance of
30      * each RSS, RDF or Atom type has its URI stored as a public property
31      * as a shortcut where the use case is simply to get a quick feed ref.
32      *
33      * Note that feeds are not loaded at this point, but will be lazy
34      * loaded automatically when each links 'feed' array key is accessed.
35      *
36      * @param DOMNodeList $links
37      * @param string $uri
38      * @return void
39      */
40     public function addLinks(DOMNodeList $links, $uri)
41     {
42         foreach ($links as $link) {
43             if (strtolower($link->getAttribute('rel')) !== 'alternate'
44                 || ! $link->getAttribute('type') || ! $link->getAttribute('href')) {
45                 continue;
46             }
47             if (! isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
48                 $this->rss = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
49             } elseif (! isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
50                 $this->atom = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
51             } elseif (! isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
52                 $this->rdf = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
53             }
54             $this[] = new static([
55                 'rel' => 'alternate',
56                 'type' => $link->getAttribute('type'),
57                 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri),
58             ]);
59         }
60     }
61
62     /**
63      *  Attempt to turn a relative URI into an absolute URI
64      *
65      *  @param string $link
66      *  @param string $uri OPTIONAL
67      *  @return string|null absolutised link or null if invalid
68      */
69     protected function absolutiseUri($link, $uri = null)
70     {
71         $linkUri = Uri::factory($link);
72         if ($linkUri->isAbsolute()) {
73             // invalid absolute link can not be recovered
74             return $linkUri->isValid() ? $link : null;
75         }
76
77         $scheme = 'http';
78         if ($uri !== null) {
79             $uri = Uri::factory($uri);
80             $scheme = $uri->getScheme() ?: $scheme;
81         }
82
83         if ($linkUri->getHost()) {
84             $link = $this->resolveSchemeRelativeUri($link, $scheme);
85         } elseif ($uri !== null) {
86             $link = $this->resolveRelativeUri($link, $scheme, $uri->getHost(), $uri->getPath());
87         }
88
89         if (! Uri::factory($link)->isValid()) {
90             return null;
91         }
92
93         return $link;
94     }
95
96     /**
97      * Resolves scheme relative link to absolute
98      *
99      * @param string $link
100      * @param string $scheme
101      * @return string
102      */
103     private function resolveSchemeRelativeUri($link, $scheme)
104     {
105         $link = ltrim($link, '/');
106         return sprintf('%s://%s', $scheme, $link);
107     }
108
109     /**
110      *  Resolves relative link to absolute
111      *
112      *  @param string $link
113      *  @param string $scheme
114      *  @param string $host
115      *  @param string $uriPath
116      *  @return string
117      */
118     private function resolveRelativeUri($link, $scheme, $host, $uriPath)
119     {
120         if ($link[0] !== '/') {
121             $link = $uriPath . '/' . $link;
122         }
123         return sprintf(
124             '%s://%s/%s',
125             $scheme,
126             $host,
127             $this->canonicalizePath($link)
128         );
129     }
130
131     /**
132      *  Canonicalize relative path
133      */
134     protected function canonicalizePath($path)
135     {
136         $parts = array_filter(explode('/', $path));
137         $absolutes = [];
138         foreach ($parts as $part) {
139             if ('.' == $part) {
140                 continue;
141             }
142             if ('..' == $part) {
143                 array_pop($absolutes);
144             } else {
145                 $absolutes[] = $part;
146             }
147         }
148         return implode('/', $absolutes);
149     }
150
151     /**
152      * Supports lazy loading of feeds using Reader::import() but
153      * delegates any other operations to the parent class.
154      *
155      * @param string $offset
156      * @return mixed
157      */
158     public function offsetGet($offset)
159     {
160         if ($offset == 'feed' && ! $this->offsetExists('feed')) {
161             if (! $this->offsetExists('href')) {
162                 return;
163             }
164             $feed = Reader::import($this->offsetGet('href'));
165             $this->offsetSet('feed', $feed);
166             return $feed;
167         }
168         return parent::offsetGet($offset);
169     }
170 }