1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Document
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 /**
 13  * DocumentFeed class, provides an easy interface to parse and display any feed document
 14  *
 15  * @since  11.1
 16  */
 17 class JDocumentFeed extends JDocument
 18 {
 19     /**
 20      * Syndication URL feed element
 21      *
 22      * optional
 23      *
 24      * @var    string
 25      * @since  11.1
 26      */
 27     public $syndicationURL = '';
 28 
 29     /**
 30      * Image feed element
 31      *
 32      * optional
 33      *
 34      * @var    JFeedImage
 35      * @since  11.1
 36      */
 37     public $image = null;
 38 
 39     /**
 40      * Copyright feed element
 41      *
 42      * optional
 43      *
 44      * @var    string
 45      * @since  11.1
 46      */
 47     public $copyright = '';
 48 
 49     /**
 50      * Published date feed element
 51      *
 52      * optional
 53      *
 54      * @var    string
 55      * @since  11.1
 56      */
 57     public $pubDate = '';
 58 
 59     /**
 60      * Lastbuild date feed element
 61      *
 62      * optional
 63      *
 64      * @var    string
 65      * @since  11.1
 66      */
 67     public $lastBuildDate = '';
 68 
 69     /**
 70      * Editor feed element
 71      *
 72      * optional
 73      *
 74      * @var    string
 75      * @since  11.1
 76      */
 77     public $editor = '';
 78 
 79     /**
 80      * Docs feed element
 81      *
 82      * @var    string
 83      * @since  11.1
 84      */
 85     public $docs = '';
 86 
 87     /**
 88      * Editor email feed element
 89      *
 90      * optional
 91      *
 92      * @var    string
 93      * @since  11.1
 94      */
 95     public $editorEmail = '';
 96 
 97     /**
 98      * Webmaster email feed element
 99      *
100      * optional
101      *
102      * @var    string
103      * @since  11.1
104      */
105     public $webmaster = '';
106 
107     /**
108      * Category feed element
109      *
110      * optional
111      *
112      * @var    string
113      * @since  11.1
114      */
115     public $category = '';
116 
117     /**
118      * TTL feed attribute
119      *
120      * optional
121      *
122      * @var    string
123      * @since  11.1
124      */
125     public $ttl = '';
126 
127     /**
128      * Rating feed element
129      *
130      * optional
131      *
132      * @var    string
133      * @since  11.1
134      */
135     public $rating = '';
136 
137     /**
138      * Skiphours feed element
139      *
140      * optional
141      *
142      * @var    string
143      * @since  11.1
144      */
145     public $skipHours = '';
146 
147     /**
148      * Skipdays feed element
149      *
150      * optional
151      *
152      * @var    string
153      * @since  11.1
154      */
155     public $skipDays = '';
156 
157     /**
158      * The feed items collection
159      *
160      * @var    JFeedItem[]
161      * @since  11.1
162      */
163     public $items = array();
164 
165     /**
166      * Class constructor
167      *
168      * @param   array  $options  Associative array of options
169      *
170      * @since  11.1
171      */
172     public function __construct($options = array())
173     {
174         parent::__construct($options);
175 
176         // Set document type
177         $this->_type = 'feed';
178     }
179 
180     /**
181      * Render the document
182      *
183      * @param   boolean  $cache   If true, cache the output
184      * @param   array    $params  Associative array of attributes
185      *
186      * @return  The rendered data
187      *
188      * @since   11.1
189      * @throws  Exception
190      * @todo    Make this cacheable
191      */
192     public function render($cache = false, $params = array())
193     {
194         // Get the feed type
195         $type = JFactory::getApplication()->input->get('type', 'rss');
196 
197         // Instantiate feed renderer and set the mime encoding
198         $renderer = $this->loadRenderer(($type) ? $type : 'rss');
199 
200         if (!($renderer instanceof JDocumentRenderer))
201         {
202             throw new Exception(JText::_('JGLOBAL_RESOURCE_NOT_FOUND'), 404);
203         }
204 
205         $this->setMimeEncoding($renderer->getContentType());
206 
207         // Output
208         // Generate prolog
209         $data = "<?xml version=\"1.0\" encoding=\"" . $this->_charset . "\"?>\n";
210         $data .= "<!-- generator=\"" . $this->getGenerator() . "\" -->\n";
211 
212         // Generate stylesheet links
213         foreach ($this->_styleSheets as $src => $attr)
214         {
215             $data .= "<?xml-stylesheet href=\"$src\" type=\"" . $attr['type'] . "\"?>\n";
216         }
217 
218         // Render the feed
219         $data .= $renderer->render();
220 
221         parent::render();
222 
223         return $data;
224     }
225 
226     /**
227      * Adds an JFeedItem to the feed.
228      *
229      * @param   JFeedItem  $item  The feeditem to add to the feed.
230      *
231      * @return  JDocumentFeed  instance of $this to allow chaining
232      *
233      * @since   11.1
234      */
235     public function addItem(JFeedItem $item)
236     {
237         $item->source = $this->link;
238         $this->items[] = $item;
239 
240         return $this;
241     }
242 }
243 
244 /**
245  * JFeedItem is an internal class that stores feed item information
246  *
247  * @since  11.1
248  */
249 class JFeedItem
250 {
251     /**
252      * Title item element
253      *
254      * required
255      *
256      * @var    string
257      * @since  11.1
258      */
259     public $title;
260 
261     /**
262      * Link item element
263      *
264      * required
265      *
266      * @var    string
267      * @since  11.1
268      */
269     public $link;
270 
271     /**
272      * Description item element
273      *
274      * required
275      *
276      * @var    string
277      * @since  11.1
278      */
279     public $description;
280 
281     /**
282      * Author item element
283      *
284      * optional
285      *
286      * @var    string
287      * @since  11.1
288      */
289     public $author;
290 
291     /**
292      * Author email element
293      *
294      * optional
295      *
296      * @var    string
297      * @since  11.1
298      */
299     public $authorEmail;
300 
301     /**
302      * Category element
303      *
304      * optional
305      *
306      * @var    array or string
307      * @since  11.1
308      */
309     public $category;
310 
311     /**
312      * Comments element
313      *
314      * optional
315      *
316      * @var    string
317      * @since  11.1
318      */
319     public $comments;
320 
321     /**
322      * Enclosure element
323      *
324      * @var    JFeedEnclosure
325      * @since  11.1
326      */
327     public $enclosure = null;
328 
329     /**
330      * Guid element
331      *
332      * optional
333      *
334      * @var    string
335      * @since  11.1
336      */
337     public $guid;
338 
339     /**
340      * Published date
341      *
342      * optional
343      *
344      * May be in one of the following formats:
345      *
346      * RFC 822:
347      * "Mon, 20 Jan 03 18:05:41 +0400"
348      * "20 Jan 03 18:05:41 +0000"
349      *
350      * ISO 8601:
351      * "2003-01-20T18:05:41+04:00"
352      *
353      * Unix:
354      * 1043082341
355      *
356      * @var    string
357      * @since  11.1
358      */
359     public $date;
360 
361     /**
362      * Source element
363      *
364      * optional
365      *
366      * @var    string
367      * @since  11.1
368      */
369     public $source;
370 
371     /**
372      * Set the JFeedEnclosure for this item
373      *
374      * @param   JFeedEnclosure  $enclosure  The JFeedEnclosure to add to the feed.
375      *
376      * @return  JFeedItem instance of $this to allow chaining
377      *
378      * @since   11.1
379      */
380     public function setEnclosure(JFeedEnclosure $enclosure)
381     {
382         $this->enclosure = $enclosure;
383 
384         return $this;
385     }
386 }
387 
388 /**
389  * JFeedEnclosure is an internal class that stores feed enclosure information
390  *
391  * @since  11.1
392  */
393 class JFeedEnclosure
394 {
395     /**
396      * URL enclosure element
397      *
398      * required
399      *
400      * @var    string
401      * @since  11.1
402      */
403     public $url = '';
404 
405     /**
406      * Length enclosure element
407      *
408      * required
409      *
410      * @var    string
411      * @since  11.1
412      */
413     public $length = '';
414 
415     /**
416      * Type enclosure element
417      *
418      * required
419      *
420      * @var    string
421      * @since  11.1
422      */
423     public $type = '';
424 }
425 
426 /**
427  * JFeedImage is an internal class that stores feed image information
428  *
429  * @since  11.1
430  */
431 class JFeedImage
432 {
433     /**
434      * Title image attribute
435      *
436      * required
437      *
438      * @var    string
439      * @since  11.1
440      */
441     public $title = '';
442 
443     /**
444      * URL image attribute
445      *
446      * required
447      *
448      * @var    string
449      * @since  11.1
450      */
451     public $url = '';
452 
453     /**
454      * Link image attribute
455      *
456      * required
457      *
458      * @var    string
459      * @since  11.1
460      */
461     public $link = '';
462 
463     /**
464      * Width image attribute
465      *
466      * optional
467      *
468      * @var    string
469      * @since  11.1
470      */
471     public $width;
472 
473     /**
474      * Title feed attribute
475      *
476      * optional
477      *
478      * @var    string
479      * @since  11.1
480      */
481     public $height;
482 
483     /**
484      * Title feed attribute
485      *
486      * optional
487      *
488      * @var    string
489      * @since  11.1
490      */
491     public $description;
492 }
493