/
feed.class.php
116 lines (98 loc) · 2.83 KB
/
feed.class.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
<?php
/** @file feed.class.php Abstract feed.
*
* Encapsulates a content feed with object API and interface semantics.
*
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
includeGuard('Feed');
require_once(DIR_EXTERNAL.'/magpierss/rss_fetch.inc');
class FeedItem
{
public $author;
public $timestamp; // unix
public $title;
public $link;
public $description;
}
class Feed implements Iterator, Countable
{
private $feedFormat;
private $feedUri;
private $items;
private $position;
public function __construct($uri, $format = 'RSS')
{
$this->feedUri = (string)$uri;
$this->feedFormat = $format;
$this->items = array();
$this->position = 0;
$feed = fetch_rss($this->feedUri);
if(!$feed || $feed->ERROR)
return;
foreach($feed->items as &$rawItem)
{
$newItem = new FeedItem();
$newItem->author = (string)$rawItem['author'];
$newItem->timestamp = $rawItem['date_timestamp'];
$newItem->title = (string)$rawItem['title'];
$newItem->link = (string)$rawItem['link'];
$newItem->description = (string)$rawItem['description'];
$this->items[] = $newItem;
}
}
public function uri()
{
return $this->feedUri;
}
public function format()
{
return $this->feedFormat;
}
/**
* Implements Countable.
*/
public function count()
{
return count($this->items);
}
/**
* Implements Iterator.
*/
public function rewind()
{
reset($this->items);
$this->position = key($this->items);
}
public function current()
{
return $this->items[$this->position];
}
public function key()
{
return $this->position;
}
public function next()
{
next($this->items);
$this->position = key($this->items);
}
public function valid()
{
return isset($this->items[$this->position]);
}
}