-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRSSParser.php
More file actions
266 lines (219 loc) · 6.06 KB
/
RSSParser.php
File metadata and controls
266 lines (219 loc) · 6.06 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
* This class is written based entirely on the work found below
* www.techbytes.co.in/blogs/2006/01/15/consuming-rss-with-php-the-simple-way/
* All credit should be given to the original author
*
* Example:
$this->load->library('rssparser');
$this->rssparser->set_feed_url('http://example.com/feed');
$this->rssparser->set_cache_life(30);
$rss = $this->rssparser->getFeed(6); // Get six items from the feed
// Using a callback function to parse addictional XML fields
$this->load->library('rssparser', array($this, 'parseFile')); // parseFile method of current class
function parseFile($data, $item)
{
$data['summary'] = (string)$item->summary;
return $data;
}
*/
class RSSParser {
var $feed_uri = NULL; // Feed URI
var $data = FALSE; // Associative array containing all the feed items
var $channel_data = array(); // Store RSS Channel Data in an array
var $feed_unavailable; // Boolean variable which indicates whether an RSS feed was unavailable
var $cache_life = 0; // Cache lifetime
var $cache_dir = './application/cache/'; // Cache directory
var $write_cache_flag = FALSE; // Flag to write to cache - defaulted to false
var $callback = FALSE; // Callback to read custom data
public $isSecure = FALSE;
function RSSParser($callback = FALSE)
{
if ($callback)
{
$this->callback = $callback;
}
$this->current_feed['title'] = '';
$this->current_feed['description'] = '';
$this->current_feed['link'] = '';
}
// --------------------------------------------------------------------
function parse()
{
// Are we caching?
if ($this->cache_life != 0)
{
$filename = $this->cache_dir.'rss_Parse_'.md5($this->feed_uri);
// Is there a cache file ?
if (file_exists($filename))
{
// Has it expired?
$timedif = (time() - filemtime($filename));
if ($timedif < ( $this->cache_life * 60))
{
$rawFeed = file_get_contents($filename);
}
else
{
// So raise the falg
$this->write_cache_flag = true;
}
}
else
{
// Raise the flag to write the cache
$this->write_cache_flag = true;
}
}
// Reset
$this->current_feed['title'] = '';
$this->current_feed['description'] = '';
$this->current_feed['link'] = '';
$this->data = array();
$this->channel_data = array();
// Parse the document
if (!isset($rawFeed))
{
$rawFeed = file_get_contents($this->feed_uri);
}
$xml = new SimpleXmlElement($rawFeed);
if ($xml->channel)
{
// Assign the channel data
$this->channel_data['title'] = $xml->channel->title;
$this->channel_data['description'] = $xml->channel->description;
// Build the item array
foreach ($xml->channel->item as $item)
{
$data = array();
$data['title'] = (string)$item->title;
$data['description'] = (string)$item->description;
$data['pubDate'] = (string)$item->pubDate;
$data['link'] = (string)$item->link;
$dc = $item->children('http://purl.org/dc/elements/1.1/');
$data['author'] = (string)$dc->creator;
if ($this->callback)
{
$data = call_user_func($this->callback, $data, $item);
}
$this->data[] = $data;
}
}
else
{
// Assign the channel data
$this->channel_data['title'] = $xml->title;
$this->channel_data['description'] = $xml->subtitle;
// Build the item array
foreach ($xml->entry as $item)
{
$data = array();
$data['id'] = (string)$item->id;
$data['title'] = (string)$item->title;
$data['description'] = (string)$item->content;
$data['pubDate'] = (string)$item->published;
$data['link'] = (string)$item->link['href'];
$dc = $item->children('http://purl.org/dc/elements/1.1/');
$data['author'] = (string)$dc->creator;
if ($this->callback)
{
$data = call_user_func($this->callback, $data, $item);
}
$this->data[] = $data;
}
}
// Do we need to write the cache file?
if ($this->write_cache_flag)
{
if (!$fp = @fopen($filename, 'wb'))
{
echo "RSSParser error";
log_message('error', "Unable to write cache file: ".$filename);
return;
}
flock($fp, LOCK_EX);
if($this->isSecure){
fwrite($fp, str_replace("http://", "https://", $rawFeed));
}
else{
fwrite($fp, $rawFeed);
}
flock($fp, LOCK_UN);
fclose($fp);
}
return TRUE;
}
// --------------------------------------------------------------------
function set_cache_life($period = NULL)
{
$this->cache_life = $period;
}
// --------------------------------------------------------------------
function set_feed_url($url = NULL)
{
$this->feed_uri = $url;
}
// --------------------------------------------------------------------
function set_secure_content($isSecure = FALSE)
{
$this->isSecure = $isSecure;
return $this;
}
// --------------------------------------------------------------------
/* Return the feeds one at a time: when there are no more feeds return false
* @param No of items to return from the feed
* @return Associative array of items
*/
function getFeed($num)
{
if (!$this->data)
{
$this->parse();
}
$c = 0;
$return = array();
foreach ($this->data AS $item)
{
$return[] = $item;
$c++;
if ($c == $num)
{
break;
}
}
return $return;
}
// --------------------------------------------------------------------
/* Return channel data for the feed */
function & getChannelData()
{
$flag = false;
if (!empty($this->channel_data))
{
return $this->channel_data;
}
else
{
return $flag;
}
}
// --------------------------------------------------------------------
/* Were we unable to retreive the feeds ? */
function errorInResponse()
{
return $this->feed_unavailable;
}
// --------------------------------------------------------------------
/* Initialize the feed data */
function clear()
{
$this->feed_uri = NULL;
$this->data = FALSE;
$this->channel_data = array();
$this->cache_life = 0;
$this->callback = FALSE;
return $this;
}
}
/* End of file RSSParser.php */
/* Location: ./application/libraries/RSSParser.php */