web/enmi/Zend/Feed/Writer/Renderer/Entry/Atom.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Feed_Writer
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Atom.php 22758 2010-08-01 19:23:27Z padraic $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Feed_Writer_Renderer_RendererAbstract
       
    24  */
       
    25 require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php';
       
    26 
       
    27 require_once 'Zend/Feed/Writer/Renderer/Feed/Atom/Source.php';
       
    28 
       
    29 /**
       
    30  * @category   Zend
       
    31  * @package    Zend_Feed_Writer
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_Feed_Writer_Renderer_Entry_Atom
       
    36     extends Zend_Feed_Writer_Renderer_RendererAbstract
       
    37     implements Zend_Feed_Writer_Renderer_RendererInterface
       
    38 {
       
    39     /**
       
    40      * Constructor
       
    41      * 
       
    42      * @param  Zend_Feed_Writer_Entry $container 
       
    43      * @return void
       
    44      */
       
    45     public function __construct (Zend_Feed_Writer_Entry $container)
       
    46     {
       
    47         parent::__construct($container);
       
    48     }
       
    49 
       
    50     /**
       
    51      * Render atom entry
       
    52      * 
       
    53      * @return Zend_Feed_Writer_Renderer_Entry_Atom
       
    54      */
       
    55     public function render()
       
    56     {
       
    57         $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
       
    58         $this->_dom->formatOutput = true;
       
    59         $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
       
    60         $this->_dom->appendChild($entry);
       
    61         
       
    62         $this->_setSource($this->_dom, $entry);
       
    63         $this->_setTitle($this->_dom, $entry);
       
    64         $this->_setDescription($this->_dom, $entry);
       
    65         $this->_setDateCreated($this->_dom, $entry);
       
    66         $this->_setDateModified($this->_dom, $entry);
       
    67         $this->_setLink($this->_dom, $entry);
       
    68         $this->_setId($this->_dom, $entry);
       
    69         $this->_setAuthors($this->_dom, $entry);
       
    70         $this->_setEnclosure($this->_dom, $entry);
       
    71         $this->_setContent($this->_dom, $entry);
       
    72         $this->_setCategories($this->_dom, $entry);
       
    73 
       
    74         foreach ($this->_extensions as $ext) {
       
    75             $ext->setType($this->getType());
       
    76             $ext->setRootElement($this->getRootElement());
       
    77             $ext->setDomDocument($this->getDomDocument(), $entry);
       
    78             $ext->render();
       
    79         }
       
    80         
       
    81         return $this;
       
    82     }
       
    83     
       
    84     /**
       
    85      * Set entry title
       
    86      * 
       
    87      * @param  DOMDocument $dom 
       
    88      * @param  DOMElement $root 
       
    89      * @return void
       
    90      */
       
    91     protected function _setTitle(DOMDocument $dom, DOMElement $root)
       
    92     {
       
    93         if(!$this->getDataContainer()->getTitle()) {
       
    94             require_once 'Zend/Feed/Exception.php';
       
    95             $message = 'Atom 1.0 entry elements MUST contain exactly one'
       
    96             . ' atom:title element but a title has not been set';
       
    97             $exception = new Zend_Feed_Exception($message);
       
    98             if (!$this->_ignoreExceptions) {
       
    99                 throw $exception;
       
   100             } else {
       
   101                 $this->_exceptions[] = $exception;
       
   102                 return;
       
   103             }
       
   104         }
       
   105         $title = $dom->createElement('title');
       
   106         $root->appendChild($title);
       
   107         $title->setAttribute('type', 'html');
       
   108         $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
       
   109         $title->appendChild($cdata);
       
   110     }
       
   111     
       
   112     /**
       
   113      * Set entry description
       
   114      * 
       
   115      * @param  DOMDocument $dom 
       
   116      * @param  DOMElement $root 
       
   117      * @return void
       
   118      */
       
   119     protected function _setDescription(DOMDocument $dom, DOMElement $root)
       
   120     {
       
   121         if(!$this->getDataContainer()->getDescription()) {
       
   122             return; // unless src content or base64
       
   123         }
       
   124         $subtitle = $dom->createElement('summary');
       
   125         $root->appendChild($subtitle);
       
   126         $subtitle->setAttribute('type', 'html');
       
   127         $cdata = $dom->createCDATASection(
       
   128             $this->getDataContainer()->getDescription()
       
   129         );
       
   130         $subtitle->appendChild($cdata);
       
   131     }
       
   132     
       
   133     /**
       
   134      * Set date entry was modified
       
   135      * 
       
   136      * @param  DOMDocument $dom 
       
   137      * @param  DOMElement $root 
       
   138      * @return void
       
   139      */
       
   140     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
       
   141     {
       
   142         if(!$this->getDataContainer()->getDateModified()) {
       
   143             require_once 'Zend/Feed/Exception.php';
       
   144             $message = 'Atom 1.0 entry elements MUST contain exactly one'
       
   145             . ' atom:updated element but a modification date has not been set';
       
   146             $exception = new Zend_Feed_Exception($message);
       
   147             if (!$this->_ignoreExceptions) {
       
   148                 throw $exception;
       
   149             } else {
       
   150                 $this->_exceptions[] = $exception;
       
   151                 return;
       
   152             }
       
   153         }
       
   154 
       
   155         $updated = $dom->createElement('updated');
       
   156         $root->appendChild($updated);
       
   157         $text = $dom->createTextNode(
       
   158             $this->getDataContainer()->getDateModified()->get(Zend_Date::ISO_8601)
       
   159         );
       
   160         $updated->appendChild($text);
       
   161     }
       
   162     
       
   163     /**
       
   164      * Set date entry was created
       
   165      * 
       
   166      * @param  DOMDocument $dom 
       
   167      * @param  DOMElement $root 
       
   168      * @return void
       
   169      */
       
   170     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
       
   171     {
       
   172         if (!$this->getDataContainer()->getDateCreated()) {
       
   173             return;
       
   174         }
       
   175         $el = $dom->createElement('published');
       
   176         $root->appendChild($el);
       
   177         $text = $dom->createTextNode(
       
   178             $this->getDataContainer()->getDateCreated()->get(Zend_Date::ISO_8601)
       
   179         );
       
   180         $el->appendChild($text);
       
   181     }
       
   182     
       
   183     /**
       
   184      * Set entry authors 
       
   185      * 
       
   186      * @param  DOMDocument $dom 
       
   187      * @param  DOMElement $root 
       
   188      * @return void
       
   189      */
       
   190     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
       
   191     {
       
   192         $authors = $this->_container->getAuthors();
       
   193         if ((!$authors || empty($authors))) {
       
   194             /**
       
   195              * This will actually trigger an Exception at the feed level if
       
   196              * a feed level author is not set.
       
   197              */
       
   198             return;
       
   199         }
       
   200         foreach ($authors as $data) {
       
   201             $author = $this->_dom->createElement('author');
       
   202             $name = $this->_dom->createElement('name');
       
   203             $author->appendChild($name);
       
   204             $root->appendChild($author);
       
   205             $text = $dom->createTextNode($data['name']);
       
   206             $name->appendChild($text);
       
   207             if (array_key_exists('email', $data)) {
       
   208                 $email = $this->_dom->createElement('email');
       
   209                 $author->appendChild($email);
       
   210                 $text = $dom->createTextNode($data['email']);
       
   211                 $email->appendChild($text);
       
   212             }
       
   213             if (array_key_exists('uri', $data)) {
       
   214                 $uri = $this->_dom->createElement('uri');
       
   215                 $author->appendChild($uri);
       
   216                 $text = $dom->createTextNode($data['uri']);
       
   217                 $uri->appendChild($text);
       
   218             }
       
   219         }
       
   220     }
       
   221     
       
   222     /**
       
   223      * Set entry enclosure
       
   224      * 
       
   225      * @param  DOMDocument $dom 
       
   226      * @param  DOMElement $root 
       
   227      * @return void
       
   228      */
       
   229     protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
       
   230     {
       
   231         $data = $this->_container->getEnclosure();
       
   232         if ((!$data || empty($data))) {
       
   233             return;
       
   234         }
       
   235         $enclosure = $this->_dom->createElement('link');
       
   236         $enclosure->setAttribute('rel', 'enclosure');
       
   237         if (isset($data['type'])) {
       
   238             $enclosure->setAttribute('type', $data['type']);
       
   239         }
       
   240         if (isset($data['length'])) {
       
   241             $enclosure->setAttribute('length', $data['length']);
       
   242         }
       
   243         $enclosure->setAttribute('href', $data['uri']);
       
   244         $root->appendChild($enclosure);
       
   245     }
       
   246     
       
   247     protected function _setLink(DOMDocument $dom, DOMElement $root)
       
   248     {
       
   249         if(!$this->getDataContainer()->getLink()) {
       
   250             return;
       
   251         }
       
   252         $link = $dom->createElement('link');
       
   253         $root->appendChild($link);
       
   254         $link->setAttribute('rel', 'alternate');
       
   255         $link->setAttribute('type', 'text/html');
       
   256         $link->setAttribute('href', $this->getDataContainer()->getLink());
       
   257     }
       
   258     
       
   259     /**
       
   260      * Set entry identifier 
       
   261      * 
       
   262      * @param  DOMDocument $dom 
       
   263      * @param  DOMElement $root 
       
   264      * @return void
       
   265      */
       
   266     protected function _setId(DOMDocument $dom, DOMElement $root)
       
   267     {
       
   268         if(!$this->getDataContainer()->getId()
       
   269         && !$this->getDataContainer()->getLink()) {
       
   270             require_once 'Zend/Feed/Exception.php';
       
   271             $message = 'Atom 1.0 entry elements MUST contain exactly one '
       
   272             . 'atom:id element, or as an alternative, we can use the same '
       
   273             . 'value as atom:link however neither a suitable link nor an '
       
   274             . 'id have been set';
       
   275             $exception = new Zend_Feed_Exception($message);
       
   276             if (!$this->_ignoreExceptions) {
       
   277                 throw $exception;
       
   278             } else {
       
   279                 $this->_exceptions[] = $exception;
       
   280                 return;
       
   281             }
       
   282         }
       
   283 
       
   284         if (!$this->getDataContainer()->getId()) {
       
   285             $this->getDataContainer()->setId(
       
   286                 $this->getDataContainer()->getLink());
       
   287         }
       
   288         if (!Zend_Uri::check($this->getDataContainer()->getId()) &&
       
   289         !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#",
       
   290             $this->getDataContainer()->getId()
       
   291         ) && !$this->_validateTagUri($this->getDataContainer()->getId())) {
       
   292             require_once 'Zend/Feed/Exception.php';
       
   293             throw new Zend_Feed_Exception('Atom 1.0 IDs must be a valid URI/IRI');
       
   294         }
       
   295         $id = $dom->createElement('id');
       
   296         $root->appendChild($id);
       
   297         $text = $dom->createTextNode($this->getDataContainer()->getId());
       
   298         $id->appendChild($text);
       
   299     }
       
   300     
       
   301     /**
       
   302      * Validate a URI using the tag scheme (RFC 4151)
       
   303      *
       
   304      * @param string $id
       
   305      * @return bool
       
   306      */
       
   307     protected function _validateTagUri($id)
       
   308     {
       
   309         if (preg_match('/^tag:(?<name>.*),(?<date>\d{4}-?\d{0,2}-?\d{0,2}):(?<specific>.*)(.*:)*$/', $id, $matches)) {
       
   310             $dvalid = false;
       
   311             $nvalid = false;
       
   312             $date = $matches['date'];
       
   313             $d6 = strtotime($date);
       
   314             if ((strlen($date) == 4) && $date <= date('Y')) {
       
   315                 $dvalid = true;
       
   316             } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
       
   317                 $dvalid = true;
       
   318             } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
       
   319                 $dvalid = true;
       
   320             }
       
   321             $validator = new Zend_Validate_EmailAddress;
       
   322             if ($validator->isValid($matches['name'])) {
       
   323                 $nvalid = true;
       
   324             } else {
       
   325                 $nvalid = $validator->isValid('info@' . $matches['name']);
       
   326             }
       
   327             return $dvalid && $nvalid;
       
   328 
       
   329         }
       
   330         return false;
       
   331     }
       
   332     
       
   333     /**
       
   334      * Set entry content 
       
   335      * 
       
   336      * @param  DOMDocument $dom 
       
   337      * @param  DOMElement $root 
       
   338      * @return void
       
   339      */
       
   340     protected function _setContent(DOMDocument $dom, DOMElement $root)
       
   341     {
       
   342         $content = $this->getDataContainer()->getContent();
       
   343         if (!$content && !$this->getDataContainer()->getLink()) {
       
   344             require_once 'Zend/Feed/Exception.php';
       
   345             $message = 'Atom 1.0 entry elements MUST contain exactly one '
       
   346             . 'atom:content element, or as an alternative, at least one link '
       
   347             . 'with a rel attribute of "alternate" to indicate an alternate '
       
   348             . 'method to consume the content.';
       
   349             $exception = new Zend_Feed_Exception($message);
       
   350             if (!$this->_ignoreExceptions) {
       
   351                 throw $exception;
       
   352             } else {
       
   353                 $this->_exceptions[] = $exception;
       
   354                 return;
       
   355             }
       
   356         }
       
   357         if (!$content) {
       
   358             return;
       
   359         }
       
   360         $element = $dom->createElement('content');
       
   361         $element->setAttribute('type', 'xhtml');
       
   362         $xhtmlElement = $this->_loadXhtml($content);
       
   363         $xhtml = $dom->importNode($xhtmlElement, true);
       
   364         $element->appendChild($xhtml);
       
   365         $root->appendChild($element);
       
   366     }
       
   367     
       
   368     /**
       
   369      * Load a HTML string and attempt to normalise to XML
       
   370      */
       
   371     protected function _loadXhtml($content)
       
   372     {
       
   373         $xhtml = '';
       
   374         if (class_exists('tidy', false)) {
       
   375             $tidy = new tidy;
       
   376             $config = array(
       
   377                 'output-xhtml' => true,
       
   378                 'show-body-only' => true,
       
   379                 'quote-nbsp' => false
       
   380             );
       
   381             $encoding = str_replace('-', '', $this->getEncoding());
       
   382             $tidy->parseString($content, $config, $encoding);
       
   383             $tidy->cleanRepair();
       
   384             $xhtml = (string) $tidy;
       
   385         } else {
       
   386             $xhtml = $content;
       
   387         }
       
   388         $xhtml = preg_replace(array(
       
   389             "/(<[\/]?)([a-zA-Z]+)/"   
       
   390         ), '$1xhtml:$2', $xhtml);
       
   391         $dom = new DOMDocument('1.0', $this->getEncoding());
       
   392         $dom->loadXML('<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
       
   393             . $xhtml . '</xhtml:div>');
       
   394         return $dom->documentElement;
       
   395     }
       
   396     
       
   397     /**
       
   398      * Set entry cateories 
       
   399      * 
       
   400      * @param  DOMDocument $dom 
       
   401      * @param  DOMElement $root 
       
   402      * @return void
       
   403      */
       
   404     protected function _setCategories(DOMDocument $dom, DOMElement $root)
       
   405     {
       
   406         $categories = $this->getDataContainer()->getCategories();
       
   407         if (!$categories) {
       
   408             return;
       
   409         }
       
   410         foreach ($categories as $cat) {
       
   411             $category = $dom->createElement('category');
       
   412             $category->setAttribute('term', $cat['term']);
       
   413             if (isset($cat['label'])) {
       
   414                 $category->setAttribute('label', $cat['label']);
       
   415             } else {
       
   416                 $category->setAttribute('label', $cat['term']);
       
   417             }
       
   418             if (isset($cat['scheme'])) {
       
   419                 $category->setAttribute('scheme', $cat['scheme']);
       
   420             }
       
   421             $root->appendChild($category);
       
   422         }
       
   423     }
       
   424     
       
   425     /**
       
   426      * Append Source element (Atom 1.0 Feed Metadata)
       
   427      *
       
   428      * @param  DOMDocument $dom 
       
   429      * @param  DOMElement $root 
       
   430      * @return void
       
   431      */
       
   432     protected function _setSource(DOMDocument $dom, DOMElement $root)
       
   433     {
       
   434         $source = $this->getDataContainer()->getSource();
       
   435         if (!$source) {
       
   436             return;
       
   437         }
       
   438         $renderer = new Zend_Feed_Writer_Renderer_Feed_Atom_Source($source);
       
   439         $renderer->setType($this->getType());
       
   440         $element = $renderer->render()->getElement();
       
   441         $imported = $dom->importNode($element, true);
       
   442         $root->appendChild($imported); 
       
   443     }
       
   444 }