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