From fe58bcbf46a4bc7a9d95f443fdda370710662c76 Mon Sep 17 00:00:00 2001 From: Padraic Brady Date: Sun, 1 Aug 2010 20:21:09 +0100 Subject: [PATCH] Added Tag URI support when rendering Atom Entries - fixes ZF-10246 --- src/Writer/Renderer/Entry/Atom.php | 40 +++++++++++++++++++++++-- test/Writer/Renderer/Entry/AtomTest.php | 18 +++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Writer/Renderer/Entry/Atom.php b/src/Writer/Renderer/Entry/Atom.php index 47fbc05a..68d6f2f6 100644 --- a/src/Writer/Renderer/Entry/Atom.php +++ b/src/Writer/Renderer/Entry/Atom.php @@ -27,6 +27,7 @@ use Zend\Feed\Writer; use Zend\Date; use Zend\URI; +use Zend\Validator; /** * @uses DOMDocument @@ -36,7 +37,8 @@ * @uses \Zend\Feed\Writer\Renderer\Feed\Atom\Source * @uses \Zend\Feed\Writer\Renderer\RendererAbstract * @uses \Zend\Feed\Writer\Renderer\RendererInterface -* @uses \Zend\Uri\Uri +* @uses \Zend\Uri\Url +* @uses \Zend\Validator * @uses tidy * @category Zend * @package Zend_Feed_Writer @@ -292,7 +294,9 @@ protected function _setId(\DOMDocument $dom, \DOMElement $root) $this->getDataContainer()->getLink()); } if (!URI\URL::validate($this->getDataContainer()->getId()) && - !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $this->getDataContainer()->getId())) { + !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", + $this->getDataContainer()->getId() + ) && !$this->_validateTagUri($this->getDataContainer()->getId())) { throw new Exception('Atom 1.0 IDs must be a valid URI/IRI'); } $id = $dom->createElement('id'); @@ -301,6 +305,38 @@ protected function _setId(\DOMDocument $dom, \DOMElement $root) $id->appendChild($text); } + /** + * Validate a URI using the tag scheme (RFC 4151) + * + * @param string $id + * @return bool + */ + protected function _validateTagUri($id) + { + if (preg_match('/^tag:(?.*),(?\d{4}-?\d{0,2}-?\d{0,2}):(?.*)(.*:)*$/', $id, $matches)) { + $dvalid = false; + $nvalid = false; + $date = $matches['date']; + $d6 = strtotime($date); + if ((strlen($date) == 4) && $date <= date('Y')) { + $dvalid = true; + } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) { + $dvalid = true; + } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) { + $dvalid = true; + } + $validator = new Validator\EmailAddress; + if ($validator->isValid($matches['name'])) { + $nvalid = true; + } else { + $nvalid = $validator->isValid('info@' . $matches['name']); + } + return $dvalid && $nvalid; + + } + return false; + } + /** * Set entry content * diff --git a/test/Writer/Renderer/Entry/AtomTest.php b/test/Writer/Renderer/Entry/AtomTest.php index a647f428..18783c0d 100644 --- a/test/Writer/Renderer/Entry/AtomTest.php +++ b/test/Writer/Renderer/Entry/AtomTest.php @@ -218,6 +218,24 @@ public function testEntryIdHasBeenSet() $entry = $feed->current(); $this->assertEquals('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6', $entry->getId()); } + + public function testEntryIdHasBeenSetUsingSimpleTagUri() + { + $this->_validEntry->setId('tag:example.org,2010:/foo/bar/'); + $renderer = new Renderer\Feed\Atom($this->_validWriter); + $feed = Reader\Reader::importString($renderer->render()->saveXml()); + $entry = $feed->current(); + $this->assertEquals('tag:example.org,2010:/foo/bar/', $entry->getId()); + } + + public function testEntryIdHasBeenSetUsingComplexTagUri() + { + $this->_validEntry->setId('tag:diveintomark.org,2004-05-27:/archives/2004/05/27/howto-atom-linkblog'); + $renderer = new Renderer\Feed\Atom($this->_validWriter); + $feed = Reader\Reader::importString($renderer->render()->saveXml()); + $entry = $feed->current(); + $this->assertEquals('tag:diveintomark.org,2004-05-27:/archives/2004/05/27/howto-atom-linkblog', $entry->getId()); + } public function testFeedIdDefaultIsUsedIfNotSetByHand() {