1
|
|
|
<?php |
2
|
|
|
// |
3
|
|
|
// ------------------------------------------------------------------------ // |
4
|
|
|
// This program is free software; you can redistribute it and/or modify // |
5
|
|
|
// it under the terms of the GNU General Public License as published by // |
6
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
7
|
|
|
// (at your option) any later version. // |
8
|
|
|
// // |
9
|
|
|
// You may not change or alter any portion of this comment or credits // |
10
|
|
|
// of supporting developers from this source code or any supporting // |
11
|
|
|
// source code which is considered copyrighted (c) material of the // |
12
|
|
|
// original comment or credit authors. // |
13
|
|
|
// // |
14
|
|
|
// This program is distributed in the hope that it will be useful, // |
15
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
16
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
17
|
|
|
// GNU General Public License for more details. // |
18
|
|
|
// // |
19
|
|
|
// You should have received a copy of the GNU General Public License // |
20
|
|
|
// along with this program; if not, write to the Free Software // |
21
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
22
|
|
|
// ------------------------------------------------------------------------ // |
23
|
|
|
// Author: phppp (D.J., [email protected]) // |
24
|
|
|
// URL: http://xoops.org // |
25
|
|
|
// Project: Article Project // |
26
|
|
|
// ------------------------------------------------------------------------ // |
27
|
|
|
/** |
28
|
|
|
* @package module::blogline |
29
|
|
|
* @copyright copyright © 2005 XoopsForge.com |
30
|
|
|
*/ |
31
|
|
|
global $msg; |
32
|
|
|
|
33
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['moddirname'] . '/class/magpie.inc.php'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* XmlParser |
37
|
|
|
* |
38
|
|
|
* @author D.J. (phppp) |
39
|
|
|
* @copyright copyright © 2005 XoopsForge.com |
40
|
|
|
* @package module::article |
41
|
|
|
* |
42
|
|
|
* {@link MagpieRSS} |
43
|
|
|
**/ |
44
|
|
|
class xmlparser extends MagpieRSS |
45
|
|
|
{ |
46
|
|
|
public $content; |
47
|
|
|
public $charset_in; |
48
|
|
|
public $charset_out; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set up XML parser, parse source, and return populated RSS object.. |
52
|
|
|
* |
53
|
|
|
* @param string $content string containing the RSS to be parsed |
54
|
|
|
* |
55
|
|
|
* |
56
|
|
|
* @param string $input_charset |
57
|
|
|
* @param null|string $output_charset |
58
|
|
|
* @param array $tags |
59
|
|
|
* @internal param string $output_encoding output the parsed RSS in this character |
60
|
|
|
* set defaults to ISO-8859-1 as this is PHP's |
61
|
|
|
* default. |
62
|
|
|
* |
63
|
|
|
* @internal param string $input_encoding the character set of the incoming RSS source. |
64
|
|
|
* Leave blank and Magpie will try to figure it |
65
|
|
|
* out. |
66
|
|
|
*/ |
67
|
|
|
public function __construct($content, $input_charset, $output_charset = _CHARSET, $tags = array()) { |
68
|
|
|
if (!in_array(strtoupper($input_charset), array('UTF-8', 'US-ASCII', 'ISO-8859-1'))) { |
69
|
|
|
$content = XoopsLocal::convert_encoding($content, 'UTF-8', $input_charset); |
70
|
|
|
$content = preg_replace('/(<\?xml.*encoding=[\'"])(.*?)([\'"].*\?>)/m', '$1UTF-8$3', $content); |
71
|
|
|
$input_charset = 'UTF-8'; |
72
|
|
|
} |
73
|
|
|
$this->content = $content; |
74
|
|
|
$this->charset_in = $input_charset; |
75
|
|
|
$this->charset_out = $output_charset; |
76
|
|
|
|
77
|
|
|
/* TODO: parse specified tags only */ |
78
|
|
|
parent::__construct($content, $input_charset, $input_charset, false); |
79
|
|
|
|
80
|
|
|
//xoops_message($this); |
81
|
|
|
unset($this->content); |
82
|
|
|
$this->encoding_convert($tags); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool|string |
87
|
|
|
*/ |
88
|
|
|
public function is_atom() { |
89
|
|
|
if ($this->feed_type == ATOM) { |
90
|
|
|
$this->feed_version = empty($this->feed_version) ? '0.3' : $this->feed_version; |
91
|
|
|
|
92
|
|
|
return $this->feed_version; |
93
|
|
|
} else { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function normalize() { |
99
|
|
|
if ($this->is_atom()): |
|
|
|
|
100
|
|
|
if (empty($this->channel['tagline'])) { |
101
|
|
|
/* ATOM */ |
102
|
|
|
$this->channel['tagline'] = @$this->channel['subtitle']; |
103
|
|
|
unset($this->channel['subtitle']); |
104
|
|
|
} |
105
|
|
|
for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) { |
106
|
|
|
// ATOM time |
107
|
|
|
if ($date = @$this->items[$i]['modified']) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
if (empty($date)) { |
111
|
|
|
$date = @$this->items[$i]['updated']; |
112
|
|
|
} |
113
|
|
|
if (empty($date)) { |
114
|
|
|
$date = @$this->items[$i]['issued']; |
115
|
|
|
} |
116
|
|
|
if (empty($date)) { |
117
|
|
|
$date = @$this->items[$i]['created']; |
118
|
|
|
} |
119
|
|
|
if (empty($date)) { |
120
|
|
|
$date = @$this->items[$i]['created']; |
121
|
|
|
} |
122
|
|
|
$this->items[$i]['modified'] = $date; |
123
|
|
|
} |
124
|
|
|
elseif ($this->is_rss() !== '1.0'): |
125
|
|
|
for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) { |
126
|
|
|
if ($date = @$this->items[$i]['pubdate']) { |
|
|
|
|
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
$this->items[$i]['pubdate'] = @$this->items[$i]['dc']['date']; |
130
|
|
|
} |
131
|
|
|
endif; |
132
|
|
|
parent::normalize(); |
133
|
|
|
/* ATOM */ |
134
|
|
|
if (empty($this->channel['language']) && !empty($this->channel['dc']['language'])) { |
135
|
|
|
$this->channel['language'] = $this->channel['dc']['language']; |
136
|
|
|
unset($this->channel['dc']['language']); |
137
|
|
|
} |
138
|
|
View Code Duplication |
if (empty($this->channel['language']) |
|
|
|
|
139
|
|
|
&& preg_match('/<link.*hreflang=[\'"](.*?)[\'"].*?>/m', $this->content, $match) |
140
|
|
|
) { |
141
|
|
|
$this->channel['language'] = $match[1]; |
142
|
|
|
} |
143
|
|
View Code Duplication |
if (empty($this->channel['language']) |
|
|
|
|
144
|
|
|
&& preg_match('/<feed.*xml:lang=[\'"](.*?)[\'"].*?>/m', $this->content, $match) |
145
|
|
|
) { |
146
|
|
|
$this->channel['language'] = $match[1]; |
147
|
|
|
} |
148
|
|
|
/* remove to avoid redundant encoding conversion */ |
149
|
|
|
if (!empty($this->channel['tagline'])) { |
150
|
|
|
unset($this->channel['tagline']); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) { |
154
|
|
|
if ($date_timestamp = @$this->items[$i]['date_timestamp']) { |
|
|
|
|
155
|
|
|
continue; |
156
|
|
|
} |
157
|
|
|
if ($date_timestamp = @$this->items[$i]['pubdate']) { |
158
|
|
|
$this->items[$i]['date_timestamp'] = $date_timestamp; |
159
|
|
|
} elseif ($date_timestamp = @$this->items[$i]['dc']['date']) { |
160
|
|
|
$this->items[$i]['date_timestamp'] = $date_timestamp; |
161
|
|
|
} else { |
162
|
|
|
$this->items[$i]['date_timestamp'] = time(); |
163
|
|
|
} |
164
|
|
|
if (!is_numeric($this->items[$i]['date_timestamp'])) { |
165
|
|
|
if ($date = parse_w3cdtf($this->items[$i]['date_timestamp'])) { |
166
|
|
|
$this->items[$i]['date_timestamp'] = $date; |
167
|
|
|
} elseif ($date = strtotime($this->items[$i]['date_timestamp'])) { |
168
|
|
|
$this->items[$i]['date_timestamp'] = $date; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/* remove to avoid redundant encoding conversion */ |
173
|
|
|
if (isset($this->items[$i]['summary'])) { |
174
|
|
|
unset($this->items[$i]['summary']); |
175
|
|
|
} |
176
|
|
|
if (isset($this->items[$i]['atom_content'])) { |
177
|
|
|
unset($this->items[$i]['atom_content']); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $tags |
186
|
|
|
*/ |
187
|
|
|
public function encoding_convert($tags = array()) { |
188
|
|
|
if (empty($tags) || in_array('channel', $tags)) { |
189
|
|
|
$this->channel = $this->_encoding($this->channel); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
if (empty($tags) || in_array('items', $tags)) { |
192
|
|
|
$this->items = $this->_encoding($this->items); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param $val |
198
|
|
|
* @return array|mixed|string |
199
|
|
|
*/ |
200
|
|
View Code Duplication |
public function _encoding($val) { |
|
|
|
|
201
|
|
|
if (is_array($val)) { |
202
|
|
|
foreach (array_keys($val) as $key) { |
203
|
|
|
$val[$key] = $this->_encoding($val[$key]); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
|
|
$val = XoopsLocal::convert_encoding($val, $this->charset_out, $this->charset_in); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $val; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: