1<?php
2
3/**
4 * RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.
5 *
6 * @see     http://my.netscape.com/publish/formats/rss-spec-0.91.html
7 * @since   1.3
8 * @author  Kai Blankenhorn <kaib@bitfolge.de>
9 */
10class RSSCreator091 extends FeedCreator
11{
12
13    /** @var string Stores this RSS feed's version number. */
14    protected $RSSVersion;
15
16    /**
17     * RSSCreator091 constructor.
18     */
19    function __construct()
20    {
21        $this->_setRSSVersion("0.91");
22        $this->contentType = "application/rss+xml";
23    }
24
25    /**
26     * Sets this RSS feed's version number.
27     *
28     * @param string $version
29     */
30    protected function _setRSSVersion($version)
31    {
32        $this->RSSVersion = $version;
33    }
34
35    /** @inheritdoc */
36    public function createFeed()
37    {
38        $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
39        $feed .= $this->_createGeneratorComment();
40        $feed .= $this->_createStylesheetReferences();
41        $feed .= "<rss version=\"".$this->RSSVersion."\"";
42
43        if (count($this->items) > 0
44            && !empty($this->items[0]->lat)
45        ) {
46            $feed .= "    xmlns:georss=\"http://www.georss.org/georss/\"\n";
47        }
48        if (count($this->items) > 0
49            && isset($this->items[0]->additionalElements['xcal:dtstart'])
50        ) {
51            $feed .= "    xmlns:xcal=\"urn:ietf:params:xml:ns:xcal\"\n";
52        }
53        $feed .= ">\n";
54        if ($this->format == 'BASE') {
55            $feed .= "    <channel xmlns:g=\"http://base.google.com/ns/1.0\">\n";
56        } else {
57            $feed .= "    <channel>\n";
58        }
59        $feed .= "        <title>".FeedCreator::iTrunc(htmlspecialchars((string) $this->title), 100)."</title>\n";
60        $this->descriptionTruncSize = 500;
61        $feed .= "        <description>".$this->getDescription()."</description>\n";
62        $feed .= "        <link>".$this->link."</link>\n";
63        $now = new FeedDate();
64        $feed .= "        <lastBuildDate>".htmlspecialchars(
65                $this->lastBuildDate ?: $now->rfc822()
66            )."</lastBuildDate>\n";
67        $feed .= "        <generator>".FEEDCREATOR_VERSION."</generator>\n";
68
69        if ($this->image != null) {
70            $feed .= "        <image>\n";
71            $feed .= "            <url>".$this->image->url."</url>\n";
72            $feed .= "            <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title), 100)."</title>\n";
73            $feed .= "            <link>".$this->image->link."</link>\n";
74            if ($this->image->width != "") {
75                $feed .= "            <width>".$this->image->width."</width>\n";
76            }
77            if ($this->image->height != "") {
78                $feed .= "            <height>".$this->image->height."</height>\n";
79            }
80            if ($this->image->description != "") {
81                $feed .= "            <description>".htmlspecialchars($this->image->description)."</description>\n";
82            }
83            $feed .= "        </image>\n";
84        }
85        if ($this->language != "") {
86            $feed .= "        <language>".$this->language."</language>\n";
87        }
88        if ($this->copyright != "") {
89            $feed .= "        <copyright>".FeedCreator::iTrunc(
90                    htmlspecialchars($this->copyright),
91                    100
92                )."</copyright>\n";
93        }
94        if ($this->editor != "") {
95            $feed .= "        <managingEditor>".FeedCreator::iTrunc(
96                    htmlspecialchars($this->editor),
97                    100
98                )."</managingEditor>\n";
99        }
100        if ($this->webmaster != "") {
101            $feed .= "        <webMaster>".FeedCreator::iTrunc(
102                    htmlspecialchars($this->webmaster),
103                    100
104                )."</webMaster>\n";
105        }
106        if ($this->pubDate != "") {
107            $pubDate = new FeedDate($this->pubDate);
108            $feed .= "        <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";
109        }
110        if ($this->category != "") {
111            $feed .= "        <category>".htmlspecialchars($this->category)."</category>\n";
112        }
113        if ($this->docs != "") {
114            $feed .= "        <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs), 500)."</docs>\n";
115        }
116        if ($this->ttl != "") {
117            $feed .= "        <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";
118        }
119        if ($this->rating != "") {
120            $feed .= "        <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating), 500)."</rating>\n";
121        }
122        if ($this->skipHours != "") {
123            $feed .= "        <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";
124        }
125        if ($this->skipDays != "") {
126            $feed .= "        <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";
127        }
128        $feed .= $this->_createAdditionalElements($this->additionalElements, "    ");
129
130        for ($i = 0; $i < count($this->items); $i++) {
131            $feed .= "        <item>\n";
132            $feed .= "            <title>".FeedCreator::iTrunc(
133                    htmlspecialchars(strip_tags((string) $this->items[$i]->title)),
134                    100
135                )."</title>\n";
136            $feed .= "            <link>".htmlspecialchars((string) $this->items[$i]->link)."</link>\n";
137            $feed .= "            <description>".$this->items[$i]->getDescription()."</description>\n";
138
139            $creator = $this->getAuthor($this->items[$i]->author, $this->items[$i]->authorEmail);
140            if ($creator) {
141                $feed .= "            <author>".htmlspecialchars($creator)."</author>\n";
142            }
143
144            /*
145             // on hold
146            if ($this->items[$i]->source!="") {
147            $feed.= "            <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";
148            }
149            */
150            if ($this->items[$i]->lat != "") {
151                $feed .= "            <georss:point>".$this->items[$i]->lat." ".$this->items[$i]->long."</georss:point>\n";
152            }
153            if (is_array($this->items[$i]->category)) {
154                foreach ($this->items[$i]->category as $cat) {
155                    $feed .= "        <category>".htmlspecialchars($cat)."</category>\n";
156                }
157            } else {
158                if ($this->items[$i]->category != "") {
159                    $feed .= "        <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";
160                }
161            }
162            if ($this->items[$i]->comments != "") {
163                $feed .= "            <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";
164            }
165            if ($this->items[$i]->date != "") {
166                $itemDate = new FeedDate($this->items[$i]->date);
167                $feed .= "            <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";
168            }
169            if ($this->items[$i]->guid != "") {
170                $feed .= "            <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";
171            }
172            if ($this->items[$i]->thumb != "") {
173                $feed .= "            <g:image_link>".htmlspecialchars($this->items[$i]->thumb)."</g:image_link>\n";
174            }
175            $feed .= $this->_createAdditionalElements($this->items[$i]->additionalElements, "            ");
176            $feed .= "        </item>\n";
177        }
178        $feed .= "    </channel>\n";
179        $feed .= "</rss>\n";
180
181        return $feed;
182    }
183
184    /**
185     * Compose the RSS-0.91 and RSS-2.0 author field.
186     *
187     * @author Joe Lapp <joe.lapp@pobox.com>
188     */
189    function getAuthor($author, $email)
190    {
191        if ($author && $email) {
192            return $email.' ('.$author.')';
193        }
194
195        return $email;
196    }
197}
198