File indexing completed on 2025-01-19 05:21:06

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Feed_Reader
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Feed_Reader
0024  */
0025 // require_once 'Zend/Feed/Reader.php';
0026 
0027 /**
0028  * @see Zend_Uri
0029  */
0030 // require_once 'Zend/Uri.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Feed_Reader
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Feed_Reader_FeedSet extends ArrayObject
0039 {
0040 
0041     public $rss = null;
0042 
0043     public $rdf = null;
0044 
0045     public $atom = null;
0046 
0047     /**
0048      * Import a DOMNodeList from any document containing a set of links
0049      * for alternate versions of a document, which will normally refer to
0050      * RSS/RDF/Atom feeds for the current document.
0051      *
0052      * All such links are stored internally, however the first instance of
0053      * each RSS, RDF or Atom type has its URI stored as a public property
0054      * as a shortcut where the use case is simply to get a quick feed ref.
0055      *
0056      * Note that feeds are not loaded at this point, but will be lazy
0057      * loaded automatically when each links 'feed' array key is accessed.
0058      *
0059      * @param DOMNodeList $links
0060      * @param string $uri
0061      * @return void
0062      */
0063     public function addLinks(DOMNodeList $links, $uri)
0064     {
0065         foreach ($links as $link) {
0066             if (strtolower($link->getAttribute('rel')) !== 'alternate'
0067                 || !$link->getAttribute('type') || !$link->getAttribute('href')) {
0068                 continue;
0069             }
0070             if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
0071                 $this->rss = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
0072             } elseif(!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
0073                 $this->atom = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
0074             } elseif(!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
0075                 $this->rdf = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
0076             }
0077             $this[] = new self(array(
0078                 'rel' => 'alternate',
0079                 'type' => $link->getAttribute('type'),
0080                 'href' => $this->_absolutiseUri(trim($link->getAttribute('href')), $uri),
0081             ));
0082         }
0083     }
0084 
0085     /**
0086      *  Attempt to turn a relative URI into an absolute URI
0087      */
0088     protected function _absolutiseUri($link, $uri = null)
0089     {
0090         if (!Zend_Uri::check($link)) {
0091             if ($uri !== null) {
0092                 $uri = Zend_Uri::factory($uri);
0093 
0094                 if ($link[0] !== '/') {
0095                     $link = $uri->getPath() . '/' . $link;
0096                 }
0097 
0098                 $link = $uri->getScheme() . '://' . $uri->getHost() . '/' . $this->_canonicalizePath($link);
0099                 if (!Zend_Uri::check($link)) {
0100                     $link = null;
0101                 }
0102             }
0103         }
0104         return $link;
0105     }
0106 
0107     /**
0108      *  Canonicalize relative path
0109      */
0110     protected function _canonicalizePath($path)
0111     {
0112         $parts = array_filter(explode('/', $path));
0113         $absolutes = array();
0114         foreach ($parts as $part) {
0115             if ('.' == $part) {
0116                 continue;
0117             }
0118             if ('..' == $part) {
0119                 array_pop($absolutes);
0120             } else {
0121                 $absolutes[] = $part;
0122             }
0123         }
0124         return implode('/', $absolutes);
0125     }
0126 
0127     /**
0128      * Supports lazy loading of feeds using Zend_Feed_Reader::import() but
0129      * delegates any other operations to the parent class.
0130      *
0131      * @param string $offset
0132      * @return mixed
0133      * @uses Zend_Feed_Reader
0134      */
0135     public function offsetGet($offset)
0136     {
0137         if ($offset == 'feed' && !$this->offsetExists('feed')) {
0138             if (!$this->offsetExists('href')) {
0139                 return null;
0140             }
0141             $feed = Zend_Feed_Reader::import($this->offsetGet('href'));
0142             $this->offsetSet('feed', $feed);
0143             return $feed;
0144         }
0145         return parent::offsetGet($offset);
0146     }
0147 
0148 }