00001 <?PHP 00002 00003 # 00004 # Axis--RSS.php 00005 # An Object to Support RSS 0.92 (Rich Site Summary) Output 00006 # 00007 # Copyright 2002 Axis Data 00008 # This code is free software that can be used or redistributed under the 00009 # terms of Version 2 of the GNU General Public License, as published by the 00010 # Free Software Foundation (http://www.fsf.org). 00011 # 00012 # Author: Edward Almasy (almasy@axisdata.com) 00013 # 00014 # Part of the AxisPHP library v1.2.5 00015 # For more information see http://www.axisdata.com/AxisPHP/ 00016 # 00017 00018 00019 class RSS { 00020 00021 # ---- PUBLIC INTERFACE -------------------------------------------------- 00022 00023 function RSS() 00024 { 00025 $this->ChannelCount = -1; 00026 } 00027 00028 # required channel values 00029 function AddChannel($Title, $Link, $Description) 00030 { 00031 $this->ChannelCount++; 00032 $this->ItemCounts[$this->ChannelCount] = -1; 00033 $this->Channels[$this->ChannelCount]["Title"] = $Title; 00034 $this->Channels[$this->ChannelCount]["Link"] = $Link; 00035 $this->Channels[$this->ChannelCount]["Description"] = $Description; 00036 } 00037 function SetImage($Url, $Title, $Link, 00038 $Height = NULL, $Width = NULL, $Description = NULL) 00039 { 00040 $this->Channels[$this->ChannelCount]["ImageUrl"] = $Url; 00041 $this->Channels[$this->ChannelCount]["ImageTitle"] = $Title; 00042 $this->Channels[$this->ChannelCount]["ImageLink"] = $Link; 00043 $this->Channels[$this->ChannelCount]["ImageHeight"] = $Height; 00044 $this->Channels[$this->ChannelCount]["ImageWidth"] = $Width; 00045 $this->Channels[$this->ChannelCount]["ImageDescription"] = $Description; 00046 } 00047 00048 # optional channel values 00049 function SetLanguage($Value) { $this->Channels[$this->ChannelCount]["Language"] = $Value; } 00050 function SetCopyright($Value) { $this->Channels[$this->ChannelCount]["Copyright"] = $Value; } 00051 function SetManagingEditor($Value) { $this->Channels[$this->ChannelCount]["ManagingEditor"] = $Value; } 00052 function SetWebmaster($Value) { $this->Channels[$this->ChannelCount]["Webmaster"] = $Value; } 00053 function SetPicsRating($Value) { $this->Channels[$this->ChannelCount]["PicsRating"] = $Value; } 00054 function SetPublicationDate($Value) { $this->Channels[$this->ChannelCount]["PublicationDate"] = $this->FormatDate($Value); } 00055 function SetLastChangeDate($Value) { $this->Channels[$this->ChannelCount]["LastChangeDate"] = $this->FormatDate($Value); } 00056 function SetTextInput($Title, $Description, $Name) 00057 { 00058 $this->Channels[$this->ChannelCount]["TextInputTitle"] = $Title; 00059 $this->Channels[$this->ChannelCount]["TextInputDescription"] = $Description; 00060 $this->Channels[$this->ChannelCount]["TextInputName"] = $Name; 00061 } 00062 function SetSkipTimes($Days, $Hours) 00063 { 00064 # ??? 00065 } 00066 function SetCloud($Domain, $Port, $Path, $Procedure, $Protocol) 00067 { 00068 # ??? 00069 } 00070 00071 # add item to channel 00072 function AddItem($Title = NULL, $Link = NULL, $Description = NULL, $Date = NULL) 00073 { 00074 $this->ItemCounts[$this->ChannelCount]++; 00075 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["Title"] = $Title; 00076 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["Link"] = $Link; 00077 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["Description"] = $Description; 00078 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["Date"] = $this->FormatDate($Date); 00079 } 00080 function AddItemCategory($Category, $Url) 00081 { 00082 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["Category"] = $Category; 00083 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["CategoryUrl"] = $Url; 00084 } 00085 function AddItemEnclosure($Url, $Length, $Type) 00086 { 00087 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["EnclosureUrl"] = $Url; 00088 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["EnclosureLength"] = $Length; 00089 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["EnclosureType"] = $Type; 00090 } 00091 function AddItemSource($Source, $Url) 00092 { 00093 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["Source"] = $Source; 00094 $this->Items[$this->ChannelCount][$this->ItemCounts[$this->ChannelCount]]["SourceUrl"] = $Url; 00095 } 00096 00097 # write out and RSS page 00098 function PrintRSS() 00099 { 00100 # print opening elements 00101 header("Content-type: text/xml"); 00102 FTOut("<?xml version='1.0'?>"); 00103 FTOut("<rss version='0.92'>", 0); 00104 00105 # for each channel 00106 for ($this->ChannelIndex = 0; $this->ChannelIndex <= $this->ChannelCount; $this->ChannelIndex++) 00107 { 00108 # open channel element 00109 FTOut("<channel>"); 00110 00111 # print required channel elements 00112 $this->PrintChannelElement("Title", "title"); 00113 $this->PrintChannelElement("Link", "link"); 00114 $this->PrintChannelElement("Description", "description"); 00115 00116 # print image element (url, title, link required) 00117 FTOut("<image>"); 00118 $this->PrintChannelElement("ImageUrl", "url"); 00119 $this->PrintChannelElement("ImageTitle", "title"); 00120 $this->PrintChannelElement("ImageLink", "link"); 00121 $this->PrintChannelElement("ImageWidth", "width"); 00122 $this->PrintChannelElement("ImageHeight", "height"); 00123 $this->PrintChannelElement("ImageDescription", "description"); 00124 FTOut("</image>"); 00125 00126 # print optional channel elements 00127 $this->PrintChannelElement("Language", "language"); 00128 $this->PrintChannelElement("Copyright", "copyright"); 00129 $this->PrintChannelElement("ManagingEditor", "managingEditor"); 00130 $this->PrintChannelElement("Webmaster", "webMaster"); 00131 $this->PrintChannelElement("PicsRating", "rating"); 00132 $this->PrintChannelElement("PublicationDate", "pubDate"); 00133 $this->PrintChannelElement("LastChangeDate", "lastBuildDate"); 00134 # ??? STILL TO DO: SkipDays, SkipHours, Cloud 00135 FTOut("<docs>http://backend.userland.com/rss092</docs>"); 00136 00137 # for each item in this channel 00138 for ($this->ItemIndex = 0; $this->ItemIndex <= $this->ItemCounts[$this->ChannelCount]; $this->ItemIndex++) 00139 { 00140 # open item element 00141 FTOut("<item>"); 00142 00143 # print item elements 00144 $this->PrintItemElement("Title", "title"); 00145 $this->PrintItemElement("Link", "link"); 00146 $this->PrintItemElement("Description", "description"); 00147 $this->PrintItemElement("Date", "pubDate"); 00148 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex]["Category"]) 00149 && ($this->Items[$this->ChannelIndex][$this->ItemIndex]["Category"] != NULL)) 00150 { 00151 FTOut("<category url='".$this->Items[$this->ChannelIndex][$this->ItemIndex]["CategoryUrl"]."'>"); 00152 FTOut($this->Items[$this->ChannelIndex][$this->ItemIndex]["Category"]); 00153 FTOut("</category>"); 00154 } 00155 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex]["EnclosureUrl"]) 00156 && ($this->Items[$this->ChannelIndex][$this->ItemIndex]["EnclosureUrl"] != NULL)) 00157 { 00158 FTOut("<enclosure " 00159 ."url='".$this->Items[$this->ChannelIndex][$this->ItemIndex]["EnclosureUrl"]."' " 00160 ."length='".$this->Items[$this->ChannelIndex][$this->ItemIndex]["EnclosureLength"]."' " 00161 ."type='".$this->Items[$this->ChannelIndex][$this->ItemIndex]["EnclosureType"]."' />"); 00162 } 00163 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex]["Source"]) 00164 && ($this->Items[$this->ChannelIndex][$this->ItemIndex]["Source"] != NULL)) 00165 { 00166 FTOut("<source url='".$this->Items[$this->ChannelIndex][$this->ItemIndex]["SourceUrl"]."'>"); 00167 FTOut($this->Items[$this->ChannelIndex][$this->ItemIndex]["Source"]); 00168 FTOut("</source>"); 00169 } 00170 00171 # close item element 00172 FTOut("</item>"); 00173 } 00174 00175 # close channel element 00176 FTOut("</channel>"); 00177 } 00178 00179 # print closing elements 00180 FTOut("</rss>"); 00181 } 00182 00183 00184 # ---- PRIVATE INTERFACE ------------------------------------------------- 00185 00186 var $Channels; 00187 var $Items; 00188 var $ChannelCount; 00189 var $ItemCounts; 00190 var $ChannelIndex; 00191 var $ItemIndex; 00192 00193 function PrintChannelElement($VarName, $TagName) 00194 { 00195 if (isset($this->Channels[$this->ChannelIndex][$VarName]) 00196 && ($this->Channels[$this->ChannelIndex][$VarName] != NULL) 00197 && (strlen($this->Channels[$this->ChannelIndex][$VarName]))) 00198 { 00199 FTOut("<${TagName}>" 00200 .utf8_encode(htmlspecialchars(preg_replace("/[\\x00-\\x1F]+/", "", $this->Channels[$this->ChannelIndex][$VarName]))) 00201 ."</${TagName}>"); 00202 } 00203 } 00204 function PrintItemElement($VarName, $TagName) 00205 { 00206 if (isset($this->Items[$this->ChannelIndex][$this->ItemIndex][$VarName]) 00207 && ($this->Items[$this->ChannelIndex][$this->ItemIndex][$VarName] != NULL)) 00208 { 00209 FTOut("<${TagName}>" 00210 .utf8_encode(htmlspecialchars(preg_replace("/[\\x00-\\x1F]+/", "", $this->Items[$this->ChannelIndex][$this->ItemIndex][$VarName]))) 00211 ."</${TagName}>"); 00212 } 00213 } 00214 00215 function FormatDate($DateValue) 00216 { 00217 # convert date to timestamp 00218 $TimeStamp = strtotime($DateValue); 00219 00220 # convert timestamp to properly-formatted date 00221 $DateString = date("r", $TimeStamp); 00222 00223 # return properly-formatted date to caller 00224 return $DateString; 00225 } 00226 } 00227 00228 # (FTOut == Formatted Tag Output) 00229 function FTOut($String, $NewIndent = NULL) 00230 { 00231 static $Indent = 0; 00232 00233 $IndentSize = 4; 00234 00235 # decrease indent if string contains end tag and does not start with begin tag 00236 if (preg_match("/<\/[A-Za-z0-9]+>/", $String) && !preg_match("/^<[^\/]+/", $String)) { $Indent--; } 00237 00238 # reset indent if value is supplied 00239 if ($NewIndent != NULL) { $Indent = $NewIndent; } 00240 00241 # print string 00242 print(substr(" ", 00243 0, ($Indent * $IndentSize)).$String."\n"); 00244 00245 # inrease indent if string starts with begin tag and does not contain end tag 00246 if (preg_match("/^<[^\/]+/", $String) 00247 && !preg_match("/<\/[A-Za-z0-9]+>/", $String) 00248 && !preg_match("/\/>$/", $String)) 00249 { 00250 $Indent++; 00251 } 00252 } 00253 00254 00255 ?>