-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleRSS.class.php
More file actions
216 lines (188 loc) · 5.57 KB
/
Copy pathsimpleRSS.class.php
File metadata and controls
216 lines (188 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/*
* Class: simpleRSS
* Purpose: to build RSS (V0.91+) channel files from provided textual inputs
* Author: Alexey Kulikov - alex@pvl.at, alex@inses.biz
* Further Notes:
* 1) RSS 0.92 specs are to be found here - http://backend.userland.com/rss092, http://backend.userland.com/rss
* Note: you can enforse RSS2.0 by adding additional elements
* 2) All date-times in RSS conform to the Date and Time Specification of RFC 822.
*
* Example Usage:
* <code>
* $myRSS = new simpleRSS( "Alex Says",
* "http://www.pvl.at",
* "Fall on your knees and pray the big lord",
* array("ttl"=>60,"lastBuildDate"=>"Tue, 25 Mar 2003 13:13:31 GMT"));
*
* $myRSS->addItem("Hello","http://www.pvl.at","Hell is here");
* $myRSS->addItem("Goodbye","http://www.pvl.at","Heaven is here");
*
* $myRSS->create("myFeed.xml");
* </code>
**/
class simpleRSS{
//required channel data
var $version = "2.0"; //RSS Version for output
var $title; //title of the channel
var $link; //link to the channel
var $description; //description of the channel
//optional Channel Data
var $optional = array();
//items
//@access private
var $items = array();
/**
* simpleRSS::simpleRSS() - constructor
*
* @param $title (string)
* @param $link (string)
* @param $description (string)
* @param $optional (array)
* @return void
* @access public
*/
function simpleRSS($title,$link,$description,$optional=""){
$this->title = $title;
$this->link = $link;
$this->description = $description;
if(is_array($optional)){
$this->optional = $optional;
}
//flush data
$this->items = array();
}
public function setOptions($opt)
{
$this->optional = array_merge($this->optional, $opt);
}
/**
* simpleRSS::addItem() - adds an Item to end of the feeder
*
* @param $title (string)
* @param $link (string)
* @param $description (string)
* @param $optional (array)
* @return (void)
* @access public
*/
function addItem($title,$link,$description,$optional=""){
$item = array(
"title" => $title,
"link" => $link,
"description" => $description
);
//RSS2.0 upgrade if needed
if(is_array($optional)){
$item = array_merge($item,$optional);
}
$this->items[] = $item;
}
/**
* simpleRSS::create() - creates an RSS XML file at the specified location
*
* @param $fileName (string)
* @return (bool)
* @access public
*/
function create($fileName,$path=null,$charset=CHARSET){
$channel = array(
"title" => strip_tags($this->title),
"link" => $this->link,
"description" => strip_tags($this->description)
);
if(is_array($this->optional)){
$channel = array_merge($channel, $this->optional);
}
//prepare output
$out = "<?xml version=\"1.0\" encoding=\"".$charset."\"?>\n<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n<channel>\n" .
$this->_parse($channel) .
$this->_parse($this->items) .
"\n</channel>" .
"\n</rss>";
//create RSS feed file
$new_file = @fopen($path.$fileName, "w");
if($new_file){
fputs($new_file, $out);
fclose($new_file);
//rename($path.$tempFile,$path.$fileName);
@chmod($path.$fileName,0777);
return true;
}else{
return false;
}
}
function printXML($charset=CHARSET){
$channel = array(
"title" => strip_tags($this->title),
"link" => $this->link,
"description" => strip_tags($this->description)
);
if(is_array($this->optional)){
$channel = array_merge($channel, $this->optional);
}
//prepare output
echo "<?xml version=\"1.0\" encoding=\"".$charset."\"?>\n<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n<channel>\n" .
$this->_parse($channel) .
$this->_parse($this->items) .
"\n</channel>" .
"\n</rss>";
}
/**
* simpleRSS::getItemCount() - return number of items in object
*
* @return (int)
* @access public
*/
function getItemCount(){
return count($this->items);
}
/**
* simpleRSS::getItems() - return the items of the feeder
*
* @return (array)
* @access public
*/
function getItems(){
return $this->items;
}
########## end of public access ##########
/**
* simpleRSS::_parse() - recursive function to create XML from associative arrays
*
* @param $toParse
* @return (string)
* @access private
*/
function _parse($toParse){
$out = '';
while(list($key,$val) = each($toParse)){
//fix integer keys
if(is_int($key)){
$key = "item";
}
//check if this is an enclosure
if($key == 'enclosure'){
$out .= "<enclosure url=\"" . $val['url'] . "\" type=\"" . $val['type'] . "\" length=\"" . $val['length'] . "\" />";
continue; // go to next element
}else{
//open tag
$out .= "<" . $key . ">";
}
//check for subtags
if(is_array($val)){ //yes
$out .= $this->_parse($val);
}else{ //no
if(false && $key != "title"){
$out .= str_replace("&","&",htmlspecialchars($val));
}else{
$out .= htmlspecialchars($val);
}
//$out .= htmlentities($val);
}
//close tag
$out .= "</" . $key . ">\n";
}
return $out;
}
}