5 # An Object to Support RSS 0.92 (Rich Site Summary) Output
7 # Copyright 2002 Axis Data
8 # This code is free software that can be used or redistributed under the
9 # terms of Version 2 of the GNU General Public License, as published by the
10 # Free Software Foundation (http://www.fsf.org).
12 # Author: Edward Almasy (almasy@axisdata.com)
14 # Part of the AxisPHP library v1.2.5
15 # For more information see http://www.axisdata.com/AxisPHP/
21 # ---- PUBLIC INTERFACE --------------------------------------------------
25 $this->ChannelCount = -1;
27 # default encoding is UTF-8
28 $this->Encoding =
"UTF-8";
31 # required channel values
32 function AddChannel($Title, $Link, $Description, $RssLink)
34 $this->ChannelCount++;
41 function SetImage($Url, $Height = NULL, $Width = NULL, $Description = NULL)
49 # optional channel values
68 function SetCloud($Domain, $Port, $Path, $Procedure, $Protocol)
74 function AddItem($Title = NULL, $Link = NULL, $Description = NULL, $Date = NULL)
99 # write out and RSS page
102 # print opening elements
103 header(
"Content-type: application/rss+xml; charset=".$this->Encoding, TRUE);
104 FTOut(
"<?xml version='1.0' encoding='".$this->Encoding.
"' ?>");
105 FTOut(
"<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>", 0);
108 for ($this->ChannelIndex = 0; $this->ChannelIndex <=
$this->ChannelCount; $this->ChannelIndex++)
110 # open channel element
113 # print required channel elements
114 $this->PrintChannelElement(
"Title",
"title");
115 $this->PrintChannelElement(
"Link",
"link");
116 $this->PrintChannelElement(
"Description",
"description");
119 .$this->Channels[$this->ChannelCount][
"RssLink"]
120 .
"' rel='self' type='application/rss+xml' />");
122 # print image element if set (url, title, link required)
123 # title and link should be the same as those for the channel
124 if ($this->IsChannelElementSet(
"ImageUrl"))
127 $this->PrintChannelElement(
"ImageUrl",
"url");
128 $this->PrintChannelElement(
"Title",
"title");
129 $this->PrintChannelElement(
"Link",
"link");
130 $this->PrintChannelElement(
"ImageWidth",
"width");
131 $this->PrintChannelElement(
"ImageHeight",
"height");
132 $this->PrintChannelElement(
"ImageDescription",
"description");
136 # print optional channel elements
137 $this->PrintChannelElement(
"Language",
"language");
138 $this->PrintChannelElement(
"Copyright",
"copyright");
139 $this->PrintChannelElement(
"ManagingEditor",
"managingEditor");
140 $this->PrintChannelElement(
"Webmaster",
"webMaster");
141 $this->PrintChannelElement(
"PicsRating",
"rating");
142 $this->PrintChannelElement(
"PublicationDate",
"pubDate");
143 $this->PrintChannelElement(
"LastChangeDate",
"lastBuildDate");
144 # ??? STILL TO DO: SkipDays, SkipHours, Cloud
145 FTOut(
"<docs>http://www.rssboard.org/rss-2-0-1</docs>");
147 # for each item in this channel
148 for ($this->ItemIndex = 0; $this->ItemIndex <= $this->ItemCounts[
$this->ChannelCount]; $this->ItemIndex++)
153 # print item elements
154 $this->PrintItemElement(
"Title",
"title");
155 $this->PrintItemElement(
"Link",
"link");
156 $this->PrintItemElement(
"Link",
"guid");
157 $this->PrintItemElement(
"Description",
"description");
158 $this->PrintItemElement(
"Date",
"pubDate");
159 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex][
"Category"])
160 && ($this->Items[$this->ChannelIndex][$this->ItemIndex][
"Category"] != NULL))
162 FTOut(
"<category url='".$this->Items[$this->ChannelIndex][$this->ItemIndex][
"CategoryUrl"].
"'>");
163 FTOut($this->Items[$this->ChannelIndex][$this->ItemIndex][
"Category"]);
164 FTOut(
"</category>");
166 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex][
"EnclosureUrl"])
167 && ($this->Items[$this->ChannelIndex][$this->ItemIndex][
"EnclosureUrl"] != NULL))
170 .
"url='".$this->Items[$this->ChannelIndex][$this->ItemIndex][
"EnclosureUrl"].
"' "
171 .
"length='".$this->Items[$this->ChannelIndex][$this->ItemIndex][
"EnclosureLength"].
"' "
172 .
"type='".$this->Items[$this->ChannelIndex][$this->ItemIndex][
"EnclosureType"].
"' />");
174 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex][
"Source"])
175 && ($this->Items[$this->ChannelIndex][$this->ItemIndex][
"Source"] != NULL))
177 FTOut(
"<source url='".$this->Items[$this->ChannelIndex][$this->ItemIndex][
"SourceUrl"].
"'>");
178 FTOut($this->Items[$this->ChannelIndex][$this->ItemIndex][
"Source"]);
186 # close channel element
190 # print closing elements
195 # ---- PRIVATE INTERFACE -------------------------------------------------
210 private function IsChannelElementSet($VarName)
212 return (isset($this->Channels[$this->ChannelIndex][$VarName])
213 && $this->Channels[$this->ChannelIndex][$VarName] != NULL
214 && strlen($this->Channels[$this->ChannelIndex][$VarName]));
222 private function IsItemElementSet($VarName)
224 return (isset($this->Items[$this->ChannelIndex][$this->ItemIndex][$VarName])
225 && $this->Items[$this->ChannelIndex][$this->ItemIndex][$VarName] != NULL);
233 private function PrintChannelElement($VarName, $TagName)
235 # only print channel elements if set
236 if (!$this->IsChannelElementSet($VarName))
241 $InnerText = $this->EscapeInnerText(
242 $this->Channels[$this->ChannelIndex][$VarName]);
244 FTOut(
"<${TagName}>".$InnerText.
"</${TagName}>");
252 private function PrintItemElement($VarName, $TagName)
254 # only print elements that are set
255 if (!$this->IsItemElementSet($VarName))
260 $InnerText = $this->EscapeInnerText(
261 $this->Items[$this->ChannelIndex][$this->ItemIndex][$VarName]);
263 FTOut(
"<${TagName}>".$InnerText.
"</${TagName}>");
273 private function FormatDate($Value)
275 return date(
"D, j M Y H:i:s O", strtotime($Value));
284 private function EscapeInnerText($Text)
286 # remove control characters
287 $Intermediate = preg_replace(
"/[\\x00-\\x1F]+/",
"", $Text);
289 # escape XML special characters for PHP version < 5.2.3
290 if (version_compare(phpversion(),
"5.2.3",
"<"))
292 $Intermediate = htmlspecialchars(
293 $Intermediate, ENT_QUOTES, $this->Encoding);
296 # escape XML special characters for PHP version >= 5.2.3
299 $Intermediate = htmlspecialchars(
300 $Intermediate, ENT_QUOTES, $this->Encoding, FALSE);
303 # map named entities to their hex references
304 $Replacements = array(
308 """ =>
""",
309 "'" =>
"'");
311 # replace named entities with hex references for compatibility as
312 # specified by the RSS spec/best practices
313 $Intermediate = str_replace(
314 array_keys($Replacements),
315 array_values($Replacements),
318 return $Intermediate;
323 # (FTOut == Formatted Tag Output)
324 function FTOut($String, $NewIndent = NULL)
330 # decrease indent if string contains end tag and does not start with begin tag
331 if (preg_match(
"/<\/[A-Za-z0-9]+>/", $String) && !preg_match(
"/^<[^\/]+/", $String)) { $Indent--; }
333 # reset indent if value is supplied
334 if ($NewIndent != NULL) { $Indent = $NewIndent; }
338 0, ($Indent * $IndentSize)).$String.
"\n");
340 # inrease indent if string starts with begin tag and does not contain end tag
341 if (preg_match(
"/^<[^\/]+/", $String)
342 && !preg_match(
"/<\/[A-Za-z0-9]+>/", $String)
343 && !preg_match(
"/\/>$/", $String))