1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace XoopsModules\Xoopsheadline; |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
* You may not change or alter any portion of this comment or credits |
9
|
|
|
* of supporting developers from this source code or any supporting source code |
10
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
//use think\console\command\Help; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
21
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
22
|
|
|
* @author XOOPS Development Team, Kazumi Ono (AKA onokazu) |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
require_once XOOPS_ROOT_PATH . '/class/template.php'; |
27
|
|
|
|
28
|
|
|
$helper = Helper::getInstance(); |
29
|
|
|
$helper->loadLanguage('main'); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class HeadlineRenderer |
33
|
|
|
*/ |
34
|
|
|
class HeadlineRenderer |
35
|
|
|
{ |
36
|
|
|
// holds reference to Headline class object |
37
|
|
|
protected $headline; |
38
|
|
|
protected $tpl; |
39
|
|
|
protected $feed; |
40
|
|
|
protected $block; |
41
|
|
|
protected $errors = []; |
42
|
|
|
// RSS2 SAX parser |
43
|
|
|
protected $parser; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* HeadlineRenderer constructor. |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Headline $headline) |
49
|
|
|
{ |
50
|
|
|
$this->headline = $headline; |
51
|
|
|
$this->tpl = new \XoopsTpl(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int|bool |
56
|
|
|
*/ |
57
|
|
|
public function updateCache() |
58
|
|
|
{ |
59
|
|
|
$helper = Helper::getInstance(); |
60
|
|
|
/** |
61
|
|
|
* Update cache - first try using fopen and then cURL |
62
|
|
|
*/ |
63
|
|
|
$retval = false; |
64
|
|
|
if ($fp = @\fopen($this->headline->getVar('headline_rssurl'), 'r')) { // successfully openned file using fopen |
65
|
|
|
$data = ''; |
66
|
|
|
while (!\feof($fp)) { |
67
|
|
|
$data .= \fgets($fp, 4096); |
68
|
|
|
} |
69
|
|
|
\fclose($fp); |
70
|
|
|
$this->headline->setVar('headline_xml', $this->convertToUtf8($data)); |
71
|
|
|
$this->headline->setVar('headline_updated', \time()); |
72
|
|
|
$headlineHandler = $helper->getHandler('Headline'); |
73
|
|
|
if (null !== $headlineHandler) { |
74
|
|
|
$retval = $headlineHandler->insert($this->headline); |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
// failed open using fopen, now try cURL |
78
|
|
|
$ch = \curl_init($this->headline->getVar('headline_rssurl')); |
79
|
|
|
if (\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true)) { |
80
|
|
|
if ($data = \curl_exec($ch)) { |
81
|
|
|
\curl_close($ch); |
82
|
|
|
$this->headline->setVar('headline_xml', $this->convertToUtf8($data)); |
|
|
|
|
83
|
|
|
$this->headline->setVar('headline_updated', \time()); |
84
|
|
|
$headlineHandler = $helper->getHandler('Headline'); |
85
|
|
|
if (null !== $headlineHandler) { |
86
|
|
|
$retval = $headlineHandler->insert($this->headline); |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
|
|
\curl_close($ch); |
90
|
|
|
$errmsg = \sprintf(\_MD_XOOPSHEADLINE_NOTOPEN, $this->headline->getVar('headline_rssurl')); |
|
|
|
|
91
|
|
|
$this->setErrors($errmsg); |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
$this->setErrors(\_MD_XOOPSHEADLINE_BADOPT); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $retval; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function renderFeed(bool $force_update = false): bool |
102
|
|
|
{ |
103
|
|
|
$retval = false; |
104
|
|
|
if ($force_update || $this->headline->cacheExpired()) { |
105
|
|
|
if (!$this->updateCache()) { |
106
|
|
|
return $retval; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
if ($this->_parse()) { |
110
|
|
|
$this->tpl->clear_all_assign(); |
111
|
|
|
$this->tpl->assign('xoops_url', XOOPS_URL); |
112
|
|
|
$channel_data = $this->parser->getChannelData(); |
113
|
|
|
\array_walk($channel_data, [$this, 'convertFromUtf8']); |
114
|
|
|
$this->tpl->assign_by_ref('channel', $channel_data); |
115
|
|
|
if (1 == $this->headline->getVar('headline_mainimg')) { |
116
|
|
|
$image_data = $this->parser->getImageData(); |
117
|
|
|
\array_walk($image_data, [$this, 'convertFromUtf8']); |
118
|
|
|
$max_width = 256; |
119
|
|
|
$max_height = 92; |
120
|
|
|
if (!isset($image_data['height']) || !isset($image_data['width'])) { |
121
|
|
|
$image_size = @\getimagesize($image_data['url']); |
122
|
|
|
if ($image_size) { |
123
|
|
|
$image_data['width'] = $image_size[0]; |
124
|
|
|
$image_data['height'] = $image_size[1]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
if (\array_key_exists('height', $image_data) && \array_key_exists('width', $image_data) |
128
|
|
|
&& ($image_data['width'] > 0)) { |
129
|
|
|
$width_ratio = $image_data['width'] / $max_width; |
130
|
|
|
$height_ratio = $image_data['height'] / $max_height; |
131
|
|
|
$scale = \max($width_ratio, $height_ratio); |
132
|
|
|
if ($scale > 1) { |
133
|
|
|
$image_data['width'] = (int)($image_data['width'] / $scale); |
134
|
|
|
$image_data['height'] = (int)($image_data['height'] / $scale); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
$this->tpl->assign_by_ref('image', $image_data); |
138
|
|
|
} |
139
|
|
|
if (1 == $this->headline->getVar('headline_mainfull')) { |
140
|
|
|
$this->tpl->assign('show_full', true); |
141
|
|
|
} else { |
142
|
|
|
$this->tpl->assign('show_full', false); |
143
|
|
|
} |
144
|
|
|
$items = $this->parser->getItems(); |
145
|
|
|
$count = \count($items); |
146
|
|
|
$max = ($count > $this->headline->getVar('headline_mainmax')) ? $this->headline->getVar('headline_mainmax') : $count; |
147
|
|
|
for ($i = 0; $i < $max; ++$i) { |
148
|
|
|
\array_walk($items[$i], [$this, 'convertFromUtf8']); |
149
|
|
|
$this->tpl->append_by_ref('items', $items[$i]); |
150
|
|
|
} |
151
|
|
|
$this->tpl->assign( |
152
|
|
|
[ |
153
|
|
|
'lang_lastbuild' => \_MD_XOOPSHEADLINE_LASTBUILD, |
154
|
|
|
'lang_language' => \_MD_XOOPSHEADLINE_LANGUAGE, |
155
|
|
|
'lang_description' => \_MD_XOOPSHEADLINE_DESCRIPTION, |
156
|
|
|
'lang_webmaster' => \_MD_XOOPSHEADLINE_WEBMASTER, |
157
|
|
|
'lang_category' => \_MD_XOOPSHEADLINE_CATEGORY, |
158
|
|
|
'lang_generator' => \_MD_XOOPSHEADLINE_GENERATOR, |
159
|
|
|
'lang_title' => \_MD_XOOPSHEADLINE_TITLE, |
160
|
|
|
'lang_pubdate' => \_MD_XOOPSHEADLINE_PUBDATE, |
161
|
|
|
// 'lang_description2' => _MD_XOOPSHEADLINE_DESCRIPTION2, |
162
|
|
|
'lang_more' => _MORE, |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
$this->feed = $this->tpl->fetch('db:xoopsheadline_feed.tpl'); |
166
|
|
|
$retval = true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $retval; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function renderBlock(bool $force_update = false): bool |
173
|
|
|
{ |
174
|
|
|
$retval = false; |
175
|
|
|
if ($force_update || $this->headline->cacheExpired()) { |
176
|
|
|
if (!$this->updateCache()) { |
177
|
|
|
return $retval; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
if ($this->_parse()) { |
181
|
|
|
$this->tpl->clear_all_assign(); |
182
|
|
|
$this->tpl->assign('xoops_url', XOOPS_URL); |
183
|
|
|
$channel_data = $this->parser->getChannelData(); |
184
|
|
|
\array_walk($channel_data, [$this, 'convertFromUtf8']); |
185
|
|
|
$this->tpl->assign_by_ref('channel', $channel_data); |
186
|
|
|
if (1 == $this->headline->getVar('headline_blockimg')) { |
187
|
|
|
$image_data = $this->parser->getImageData(); |
188
|
|
|
\array_walk($image_data, [$this, 'convertFromUtf8']); |
189
|
|
|
$this->tpl->assign_by_ref('image', $image_data); |
190
|
|
|
} |
191
|
|
|
$items = $this->parser->getItems(); |
192
|
|
|
$count = \count($items); |
193
|
|
|
$max = ($count > $this->headline->getVar('headline_blockmax')) ? $this->headline->getVar('headline_blockmax') : $count; |
194
|
|
|
for ($i = 0; $i < $max; ++$i) { |
195
|
|
|
\array_walk($items[$i], [$this, 'convertFromUtf8']); |
196
|
|
|
$this->tpl->append_by_ref('items', $items[$i]); |
197
|
|
|
} |
198
|
|
|
$this->tpl->assign( |
199
|
|
|
[ |
200
|
|
|
'site_name' => $this->headline->getVar('headline_name'), |
201
|
|
|
'site_url' => $this->headline->getVar('headline_url'), |
202
|
|
|
'site_id' => $this->headline->getVar('headline_id'), |
203
|
|
|
] |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$this->block = $this->tpl->fetch('db:blocks/headline_block.tpl'); |
207
|
|
|
$retval = true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $retval; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function &_parse(): bool |
214
|
|
|
{ |
215
|
|
|
$retval = true; |
216
|
|
|
if (!isset($this->parser)) { |
217
|
|
|
require_once XOOPS_ROOT_PATH . '/class/xml/rss/xmlrss2parser.php'; |
218
|
|
|
$temp = $this->headline->getVar('headline_xml'); |
219
|
|
|
$this->parser = new \XoopsXmlRss2Parser($temp); |
220
|
|
|
switch ($this->headline->getVar('headline_encoding')) { |
221
|
|
|
case 'utf-8': |
222
|
|
|
$this->parser->useUtfEncoding(); |
223
|
|
|
break; |
224
|
|
|
case 'us-ascii': |
225
|
|
|
$this->parser->useAsciiEncoding(); |
226
|
|
|
break; |
227
|
|
|
default: |
228
|
|
|
$this->parser->useIsoEncoding(); |
229
|
|
|
break; |
230
|
|
|
} |
231
|
|
|
$result = $this->parser->parse(); |
232
|
|
|
if (!$result) { |
233
|
|
|
$this->setErrors($this->parser->getErrors(false)); |
|
|
|
|
234
|
|
|
unset($this->parser); |
235
|
|
|
$retval = false; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $retval; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public function &getFeed() |
246
|
|
|
{ |
247
|
|
|
return $this->feed; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return mixed |
252
|
|
|
*/ |
253
|
|
|
public function &getBlock() |
254
|
|
|
{ |
255
|
|
|
return $this->block; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
protected function setErrors(string $err): void |
260
|
|
|
{ |
261
|
|
|
$this->errors[] = $err; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return array|string |
266
|
|
|
*/ |
267
|
|
|
public function &getErrors(bool $ashtml = true) |
268
|
|
|
{ |
269
|
|
|
if ($ashtml) { |
270
|
|
|
$retval = ''; |
271
|
|
|
if (\count($this->errors) > 0) { |
272
|
|
|
foreach ($this->errors as $error) { |
273
|
|
|
$retval .= $error . '<br>'; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} else { |
277
|
|
|
$retval = $this->errors; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $retval; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// abstract |
284
|
|
|
// overide this method in /language/your_language/headlinerenderer.php |
285
|
|
|
// this method is called by the array_walk function |
286
|
|
|
// return void |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param $value |
290
|
|
|
* @param $key |
291
|
|
|
*/ |
292
|
|
|
public function convertFromUtf8(&$value, $key): void |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// abstract |
297
|
|
|
// overide this method in /language/your_language/headlinerenderer.php |
298
|
|
|
// return string |
299
|
|
|
|
300
|
|
|
public function &convertToUtf8(string &$xmlfile): string |
301
|
|
|
{ |
302
|
|
|
if ('iso-8859-1' === \mb_strtolower($this->headline->getVar('headline_encoding'))) { |
303
|
|
|
$xmlfile = utf8_encode($xmlfile); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $xmlfile; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|