/
feed.class.php
177 lines (144 loc) · 4.58 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
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
<?php
/**
* @file feed.class.php
* Abstract class which encapsulates a content feed behind an object interface.
*
* @section License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* 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
*
* @author Copyright © 2009-2012 Daniel Swanson <danij@dengine.net>
*/
includeGuard('Feed');
require_once(DIR_CLASSES.'/visual.interface.php');
require_once(DIR_EXTERNAL.'/magpierss/rss_fetch.inc');
class Feed implements Visual, Iterator, Countable
{
public static $name = 'pages';
private static $displayOptions = 0;
private $maxItems;
private $feedUri;
private $feed;
private $position;
private $title = 'Untitled';
private $labelSpanClass = NULL;
// Callback to make when generating HTML for a feed item.
private $func_genElementHTML = NULL;
public function __construct($feedURL, $maxItems=5)
{
$this->_feedUri = $feedURL;
$this->_maxItems = intval($maxItems);
$this->_feed = fetch_rss($this->_feedUri);
$this->_feedFormat = 'RSS';
$this->_position = 0;
}
public function setTitle($title, $labelSpanClass=NULL)
{
$this->_title = "$title";
if(!is_null($labelSpanClass))
$this->_labelSpanClass = "$labelSpanClass";
}
public function setGenerateElementHTMLCallback($funcName)
{
$funcName = "$funcName";
if(!is_callable($funcName)) return FALSE;
$this->func_genElementHTML = $funcName;
return TRUE;
}
/**
* Implements Visual.
*/
public function displayOptions()
{
return $this->_displayOptions;
}
private function generateElementHTML(&$item)
{
if(!is_array($item))
throw new Exception('Received invalid item, array expected.');
if(!is_null($this->func_genElementHTML))
{
return call_user_func($this->func_genElementHTML, $item);
}
$html = '<a href="'. preg_replace('/(&)/', '&', $item['link'])
.'" title="'. date("m/d/y", $item['date_timestamp']) .' - '. htmlspecialchars($item['title'])
.'">'. htmlspecialchars($item['title']) .'</a>';
return $html;
}
public function generateHTML()
{
if(count($this) > 0)
{
?><a href="<?php echo preg_replace('/(&)/', '&', $this->_feedUri); ?>" class="link-rss" title="<?php echo htmlspecialchars($this->_title); ?>"><span class="hidden"><?php echo htmlspecialchars($this->_feedFormat); ?></span></a><?php
if(!is_null($this->_labelSpanClass))
{
?> <span class="<?php echo $this->_labelSpanClass; ?>"><?php
}
echo htmlspecialchars($this->_title);
if(!is_null($this->_labelSpanClass))
{
?></span><?php
}
?><ul><?php
$n = (integer) 0;
foreach($this as $item)
{
$elementHtml = $this->generateElementHTML($item);
?><li><?php echo $elementHtml; ?></li><?php
if(++$n >= $this->_maxItems) break;
}
}
?></ul><?php
}
public function url()
{
return $this->_feedUri;
}
/**
* Implements Countable.
*/
public function count()
{
if(is_object($this->_feed) && is_array($this->_feed->items))
return sizeof($this->_feed->items);
return 0;
}
/**
* Implements Iterator.
*/
public function rewind()
{
reset($this->_feed->items);
$this->_position = key($this->_feed->items);
}
public function current()
{
return $this->_feed->items[$this->_position];
}
public function key()
{
return $this->_position;
}
public function next()
{
next($this->_feed->items);
$this->_position = key($this->_feed->items);
}
public function valid()
{
return isset($this->_feed->items[$this->_position]);
}
}