-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFeeds.php
329 lines (273 loc) · 7.8 KB
/
Feeds.php
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* RSS for PHP - small and easy-to-use library for consuming an RSS Feed
*
* @copyright Copyright (c) 2008 David Grudl
* @license New BSD License
* @version 1.2
*/
class Feed
{
/** @var int */
public static $cacheExpire = '1 day';
/** @var string */
public static $cacheDir;
/** @var SimpleXMLElement */
protected $xml;
/**
* Loads RSS or Atom feed.
* @param string
* @param string
* @param string
* @return Feed
* @throws FeedException
*/
public static function load($url, $user = NULL, $pass = NULL)
{
$xml = self::loadXml($url, $user, $pass);
if ($xml->channel) {
return self::fromRss($xml);
} else {
return self::fromAtom($xml);
}
}
/**
* Loads RSS feed.
* @param string RSS feed URL
* @param string optional user name
* @param string optional password
* @return Feed
* @throws FeedException
*/
public static function loadRss($url, $user = NULL, $pass = NULL)
{
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.0');
return self::fromRss(self::loadXml($url, $user, $pass));
}
/**
* Loads Atom feed.
* @param string Atom feed URL
* @param string optional user name
* @param string optional password
* @return Feed
* @throws FeedException
*/
public static function loadAtom($url, $user = NULL, $pass = NULL)
{
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.0');
return self::fromAtom(self::loadXml($url, $user, $pass));
}
public static function fromRss(SimpleXMLElement $xml)
{
if (!$xml->channel) {
throw new FeedException('Invalid feed.');
}
self::adjustNamespaces($xml);
//foreach ($xml->channel as $channel) {
//if ($xml->channel) {
$channel = $xml->channel;
self::adjustNamespaces($channel);
if (isset($channel->{'now:title'})) {
$channel->nowTitle = (string)$channel->{'now:title'};
}
if (isset($channel->{'now:link'})) {
$channel->nowLink = (string)$channel->{'now:link'};
}
if (isset($channel->{'now:content'})) {
$channel->nowContent = (string)$channel->{'now:content'};
}
if (isset($channel->{'now:markdown'})) {
$channel->nowMarkdown = (string)$channel->{'now:markdown'};
}
if (isset($channel->{'now:timestamp'})) {
$channel->nowTimestamp = (string)$channel->{'now:timestamp'};
}
//}
foreach ($xml->channel->item as $item) {
// converts namespaces to dotted tags
self::adjustNamespaces($item);
// generate 'timestamp' tag
if (isset($item->{'dc:date'})) {
$item->timestamp = strtotime($item->{'dc:date'});
} elseif (isset($item->pubDate)) {
$item->timestamp = strtotime($item->pubDate);
}
// check for markdown
if (isset($item->{'source:markdown'})) {
$item->markdown = $item->{'source:markdown'};
}
}
$feed = new self;
$feed->xml = $xml->channel;
return $feed;
}
public static function fromAtom(SimpleXMLElement $xml)
{
if (!in_array('http://www.w3.org/2005/Atom', $xml->getDocNamespaces(), TRUE)
&& !in_array('http://purl.org/atom/ns#', $xml->getDocNamespaces(), TRUE)
) {
throw new FeedException('Invalid feed.');
}
// generate 'timestamp' tag
foreach ($xml->entry as $entry) {
$entry->timestamp = strtotime($entry->updated);
}
$feed = new self;
$feed->xml = $xml;
return $feed;
}
/**
* Returns property value. Do not call directly.
* @param string tag name
* @return SimpleXMLElement
*/
public function __get($name)
{
return $this->xml->{$name};
}
/**
* Sets value of a property. Do not call directly.
* @param string property name
* @param mixed property value
* @return void
*/
public function __set($name, $value)
{
throw new Exception("Cannot assign to a read-only property '$name'.");
}
/**
* Converts a SimpleXMLElement into an array.
* @param SimpleXMLElement
* @return array
*/
public function toArray(SimpleXMLElement $xml = NULL)
{
if ($xml === NULL) {
$xml = $this->xml;
}
if (!$xml->children()) {
return (string) $xml;
}
$arr = array();
foreach ($xml->children() as $tag => $child) {
if (count($xml->$tag) === 1) {
$arr[$tag] = $this->toArray($child);
} else {
$arr[$tag][] = $this->toArray($child);
}
}
return $arr;
}
/**
* Load XML from cache or HTTP.
* @param string
* @param string
* @param string
* @return SimpleXMLElement
* @throws FeedException
*/
private static function loadXml($url, $user, $pass)
{
//ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.0');
$e = self::$cacheExpire;
$cacheFile = self::$cacheDir . '/feed.' . md5(serialize(func_get_args())) . '.xml';
if (self::$cacheDir
&& (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e))
&& $data = @file_get_contents($cacheFile)
) {
// ok
} elseif ($data = trim(self::httpRequest($url, $user, $pass))) {
if (self::$cacheDir) {
file_put_contents($cacheFile, $data);
}
} elseif (self::$cacheDir && $data = @file_get_contents($cacheFile)) {
// ok
} else {
throw new FeedException('Cannot load feed.');
}
return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR);
}
/**
* Process HTTP request.
* @param string
* @param string
* @param string
* @return string|FALSE
* @throws FeedException
*/
private static function httpRequest($url, $user, $pass)
{
if (extension_loaded('curl')) {
$options = array(
CURLOPT_TCP_FASTOPEN => TRUE,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.0',
CURLOPT_ENCODING => '',
CURLOPT_HEADER => FALSE,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 20,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
);
$port = parse_url($url, PHP_URL_PORT);
if (!empty($port)) {
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$url = $scheme.'://'.$host.$path;
array_push($options,'CURLOPT_PORT => ' .$port);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt_array($curl, $options);
/*
curl_setopt($curl, CURLOPT_TCP_FASTOPEN, TRUE);
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($port)) {
curl_setopt($curl, CURLOPT_PORT, $port);
}
curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.0");
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($curl, CURLOPT_ENCODING, '');
if ($user !== NULL || $pass !== NULL) {
curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
}
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
curl_setopt($curl, CURLOPT_FILETIME, true);
if (!ini_get('open_basedir')) {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // sometime is useful :)
}
*/
$result = curl_exec($curl);
return curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200
? $result
: FALSE;
} elseif ($user === NULL && $pass === NULL) {
return file_get_contents($url);
} else {
throw new FeedException('PHP extension CURL is not loaded.');
}
}
/**
* Generates better accessible namespaced tags.
* @param SimpleXMLElement
* @return void
*/
private static function adjustNamespaces($el)
{
foreach ($el->getNamespaces(TRUE) as $prefix => $ns) {
$children = $el->children($ns);
foreach ($children as $tag => $content) {
$el->{$prefix . ':' . $tag} = $content;
}
}
}
}
/**
* An exception generated by Feed.
*/
class FeedException extends Exception
{
}