|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package SimplePortal |
|
5
|
|
|
* |
|
6
|
|
|
* @author SimplePortal Team |
|
7
|
|
|
* @copyright 2015-2021 SimplePortal Team |
|
8
|
|
|
* @license BSD 3-clause |
|
9
|
|
|
* @version 1.0.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* RSS Block, Displays rss feed in a block. |
|
15
|
|
|
* |
|
16
|
|
|
* @param mixed[] $parameters |
|
17
|
|
|
* 'url' => url of the feed |
|
18
|
|
|
* 'show_title' => Show the feed title |
|
19
|
|
|
* 'show_content' => Show the content of the feed |
|
20
|
|
|
* 'show_date' => Show the date of the feed item |
|
21
|
|
|
* 'strip_preserve' => preserve tags |
|
22
|
|
|
* 'count' => number of items to show |
|
23
|
|
|
* 'limit' => number of characters of content to show |
|
24
|
|
|
* @param int $id - not used in this block |
|
25
|
|
|
* @param boolean $return_parameters if true returns the configuration options for the block |
|
26
|
|
|
*/ |
|
27
|
|
|
class Rss_Feed_Block extends SP_Abstract_Block |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor, used to define block parameters |
|
31
|
|
|
* |
|
32
|
|
|
* @param Database|null $db |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($db = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->block_parameters = array( |
|
37
|
|
|
'url' => 'text', |
|
38
|
|
|
'show_title' => 'check', |
|
39
|
|
|
'show_content' => 'check', |
|
40
|
|
|
'show_date' => 'check', |
|
41
|
|
|
'strip_preserve' => 'text', |
|
42
|
|
|
'count' => 'int', |
|
43
|
|
|
'limit' => 'int', |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
parent::__construct($db); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Initializes a block for use. |
|
51
|
|
|
* |
|
52
|
|
|
* - Called from portal.subs as part of the sportal_load_blocks process |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed[] $parameters |
|
55
|
|
|
* @param int $id |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setup($parameters, $id) |
|
58
|
|
|
{ |
|
59
|
|
|
global $txt; |
|
60
|
|
|
|
|
61
|
|
|
$feed = !empty($parameters['url']) ? un_htmlspecialchars($parameters['url']) : ''; |
|
62
|
|
|
$this->data['show_title'] = !empty($parameters['show_title']); |
|
63
|
|
|
$this->data['show_content'] = !empty($parameters['show_content']); |
|
64
|
|
|
$this->data['show_date'] = !empty($parameters['show_date']); |
|
65
|
|
|
$strip_preserve = !empty($parameters['strip_preserve']) ? $parameters['strip_preserve'] : 'br'; |
|
66
|
|
|
$strip_preserve = preg_match_all('~[A-Za-z0-9]+~', $strip_preserve, $match) ? $match[0] : array(); |
|
67
|
|
|
$count = !empty($parameters['count']) ? (int) $parameters['count'] : 5; |
|
68
|
|
|
$limit = !empty($parameters['limit']) ? (int) $parameters['limit'] : 0; |
|
69
|
|
|
|
|
70
|
|
|
// Need a feed name to load it |
|
71
|
|
|
if (empty($feed)) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->data['error_msg'] = $txt['error_sp_invalid_feed']; |
|
74
|
|
|
$this->setTemplate('template_sp_rssFeed_error'); |
|
75
|
|
|
|
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$rss = array(); |
|
80
|
|
|
|
|
81
|
|
|
require_once(SUBSDIR . '/Package.subs.php'); |
|
82
|
|
|
$data = fetch_web_data($feed); |
|
83
|
|
|
$data_save = $data; |
|
84
|
|
|
|
|
85
|
|
|
// Convert it to UTF8 if we can and its not already |
|
86
|
|
|
preg_match('~encoding="([^"]*)"~', $data, $charset); |
|
87
|
|
|
if (!empty($charset[1]) && $charset != 'UTF-8') |
|
88
|
|
|
{ |
|
89
|
|
|
// Use iconv if its available |
|
90
|
|
|
if (function_exists('iconv')) |
|
91
|
|
|
{ |
|
92
|
|
|
$data = @iconv($charset[1], 'UTF-8//TRANSLIT//IGNORE', $data); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// No iconv or a false response from it |
|
96
|
|
|
if (!function_exists('iconv') || ($data == false)) |
|
97
|
|
|
{ |
|
98
|
|
|
// PHP (some 5.4 versions) mishandles //TRANSLIT//IGNORE and returns false: see https://bugs.php.net/bug.php?id=61484 |
|
99
|
|
|
if ($data == false) |
|
100
|
|
|
{ |
|
101
|
|
|
$data = $data_save; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (function_exists('mb_convert_encoding')) |
|
105
|
|
|
{ |
|
106
|
|
|
// Replace unknown characters with a space |
|
107
|
|
|
@ini_set('mbstring.substitute_character', '32'); |
|
108
|
|
|
$data = @mb_convert_encoding($data, 'UTF-8', $charset[1]); |
|
109
|
|
|
} |
|
110
|
|
|
elseif (function_exists('recode_string')) |
|
111
|
|
|
{ |
|
112
|
|
|
$data = @recode_string($charset[1] . '..' . 'UTF-8', $data); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$data = str_replace(array("\n", "\r", "\t"), '', $data); |
|
118
|
|
|
$data = preg_replace_callback('~<\!\[CDATA\[(.+?)\]\]>~u', function($m) { |
|
119
|
|
|
return "#cdata_escape_encode#" . Util::htmlspecialchars($m[1]); |
|
120
|
|
|
}, $data); |
|
121
|
|
|
|
|
122
|
|
|
// Find all the feed items |
|
123
|
|
|
preg_match_all('~<item>(.+?)</item>~', $data, $items); |
|
124
|
|
|
foreach ($items[1] as $item_id => $item) |
|
125
|
|
|
{ |
|
126
|
|
|
if ($item_id === $count) |
|
127
|
|
|
{ |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
preg_match_all('~<([A-Za-z]+)>(.+?)</\\1>~', $item, $match); |
|
132
|
|
|
|
|
133
|
|
|
foreach (array_keys($match[0]) as $tag_id) |
|
134
|
|
|
{ |
|
135
|
|
|
if (Util::strpos($match[2][$tag_id], '#cdata_escape_encode#') === 0) |
|
136
|
|
|
{ |
|
137
|
|
|
$match[2][$tag_id] = stripslashes(un_htmlspecialchars(Util::substr($match[2][$tag_id], 21))); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$rss[$item_id][strtolower($match[1][$tag_id])] = un_htmlspecialchars($match[2][$tag_id]); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Nothing, say its invalid |
|
145
|
|
|
if (empty($rss)) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->data['error_msg'] = $txt['error_sp_invalid_feed']; |
|
148
|
|
|
$this->setTemplate('template_sp_rssFeed_error'); |
|
149
|
|
|
|
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Add all the items to an array |
|
154
|
|
|
$this->data['items'] = array(); |
|
155
|
|
|
foreach ($rss as $item) |
|
156
|
|
|
{ |
|
157
|
|
|
$item['title'] = isset($item['title']) ? strip_tags($item['title']) : ''; |
|
158
|
|
|
$item['description'] = isset($item['description']) ? strip_tags($item['description'], empty($strip_preserve) ? '' : '<' . implode('><', $strip_preserve) . '>') : ''; |
|
159
|
|
|
|
|
160
|
|
|
$this->data['items'][] = array( |
|
161
|
|
|
'title' => $item['title'], |
|
162
|
|
|
'href' => $item['link'], |
|
163
|
|
|
'link' => $item['title'] === '' ? '' : ($item['link'] === '' ? $item['title'] : '<a href="' . $item['link'] . '" target="_blank" class="new_win">' . $item['title'] . '</a>'), |
|
164
|
|
|
'content' => $limit > 0 ? Util::shorten_text($item['description'], $limit, true) : $item['description'], |
|
165
|
|
|
'date' => !empty($item['pubdate']) ? standardTime(strtotime($item['pubdate']), '%d %B') : '', |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// No items in the feed |
|
170
|
|
|
if (empty($this->data['items'])) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->data['error_msg'] = $txt['error_sp_invalid_feed']; |
|
173
|
|
|
$this->setTemplate('template_sp_rssFeed_error'); |
|
174
|
|
|
|
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
else |
|
178
|
|
|
{ |
|
179
|
|
|
$this->data['items'][count($this->data['items']) - 1]['is_last'] = true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$this->setTemplate('template_sp_rssFeed'); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Error template for this block |
|
188
|
|
|
* |
|
189
|
|
|
* @param mixed[] $data |
|
190
|
|
|
*/ |
|
191
|
|
|
function template_sp_rssFeed_error($data) |
|
192
|
|
|
{ |
|
193
|
|
|
echo ' |
|
194
|
|
|
', $data['error_msg']; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Main template for this block |
|
199
|
|
|
* |
|
200
|
|
|
* @param mixed[] $data |
|
201
|
|
|
*/ |
|
202
|
|
|
function template_sp_rssFeed($data) |
|
203
|
|
|
{ |
|
204
|
|
|
if ($data['show_content']) |
|
205
|
|
|
{ |
|
206
|
|
|
echo ' |
|
207
|
|
|
<div class="sp_rss_flow"> |
|
208
|
|
|
<ul class="sp_list">'; |
|
209
|
|
|
|
|
210
|
|
|
foreach ($data['items'] as $item) |
|
211
|
|
|
{ |
|
212
|
|
|
if ($data['show_title'] && !empty($item['link'])) |
|
213
|
|
|
{ |
|
214
|
|
|
echo ' |
|
215
|
|
|
<li ', sp_embed_class('post', '', 'sp_list_top'), '> |
|
216
|
|
|
<strong>', $item['link'], '</strong>', ($data['show_date'] && !empty($item['date']) ? ' - ' . $item['date'] : ''), ' |
|
217
|
|
|
</li>'; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
echo ' |
|
221
|
|
|
<li', empty($item['is_last']) ? ' class="sp_list_divider"' : '', '>', $item['content'], '</li>'; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
echo ' |
|
225
|
|
|
</ul> |
|
226
|
|
|
</div>'; |
|
227
|
|
|
} |
|
228
|
|
|
else |
|
229
|
|
|
{ |
|
230
|
|
|
echo ' |
|
231
|
|
|
<ul class="sp_list">'; |
|
232
|
|
|
|
|
233
|
|
|
foreach ($data['items'] as $item) |
|
234
|
|
|
{ |
|
235
|
|
|
echo ' |
|
236
|
|
|
<li ', sp_embed_class('dot_feed'), '> ', $item['link'], ($data['show_date'] && !empty($item['date']) ? ' - ' . $item['date'] : ''), '</li>'; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
echo ' |
|
240
|
|
|
</ul>'; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|