forked from netcommons/NetCommons2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParse.class.php
More file actions
executable file
·208 lines (186 loc) · 6.35 KB
/
Parse.class.php
File metadata and controls
executable file
·208 lines (186 loc) · 6.35 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* RSSパース処理コンポーネントクラス
*
* @package NetCommons Components
* @author Noriko Arai,Ryuji Masukawa
* @copyright 2006-2007 NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @project NetCommons Project, supported by National Institute of Informatics
* @access public
*/
class Rss_Components_Parse
{
/**
* @var エラー情報を保持
*
* @access private
*/
var $_errors = array();
/**
* コンストラクター
*
* @access public
*/
function Rss_Components_Parse()
{
}
/**
* RSSデータを解析し返す
*
* @param string $xml XML文字列
* @return string RSSデータ文字列
* @access public
*/
function &parse($xml, $encoding)
{
if (empty($xml)) {
return $xml;
}
$xml = trim(mb_convert_encoding($xml, $encoding, "auto"));
include_once MAPLE_DIR."/includes/pear/XML/Unserializer.php";
$options = array(
XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => 'parseAttributes',
XML_UNSERIALIZER_OPTION_ENCODING_SOURCE => $encoding
);
$unserializer = new XML_Unserializer($options);
$unserializer->unserialize($xml);
$xmlArray = $unserializer->getUnserializedData();
if (empty($xmlArray)) return $xmlArray;
if (gettype($xmlArray) === 'object' && strtolower(get_class($xmlArray)) == "pear_error") {
$container =& DIContainerFactory::getContainer();
$actionChain =& $container->getComponent("ActionChain");
$errorList =& $actionChain->getCurErrorList();
$filterChain =& $container->getComponent("FilterChain");
$smartyAssign =& $filterChain->getFilterByName("SmartyAssign");
$errorList->add(null, $smartyAssign->getLang("rss_parse_error"));
$xmlArray = false;
return $xmlArray;
}
if (stristr(substr($xml, 0, 255), "xmlns=\"http://www.w3.org/2005/Atom\"") ||
stristr(substr($xml, 0, 255), "xmlns=\'http://www.w3.org/2005/Atom\'") ||
stristr(substr($xml, 0, 255), "http://purl.org/atom/")) {
$atomArray["channel"] = array(
"title" => !is_array($xmlArray["title"]) ? $xmlArray["title"] : $xmlArray["title"]["_content"],
"link" => !empty($xmlArray["link"]["href"]) ? $xmlArray["link"]["href"] : (!empty($xmlArray["link"][0]["href"]) ? $xmlArray["link"][0]["href"] : ""),
"lastbuilddate" => !empty($xmlArray["updated"]) ? $xmlArray["updated"] : $xmlArray["modified"],
"id" => !empty($xmlArray["id"]) ? $xmlArray["id"] : "",
"generator" => !empty($xmlArray["generator"]) ? $xmlArray["generator"] : "",
);
if (isset($xmlArray["entry"]["title"])) {
$entry = $xmlArray["entry"];
unset($xmlArray["entry"]);
$xmlArray["entry"][0] = $entry;
}
foreach ($xmlArray["entry"] as $entry ) {
if (!empty($entry["content"])) {
$description = !is_array($entry["content"]) ? $entry["content"] : $entry["content"]["_content"];
} elseif (!empty($entry["summary"])) {
$description = !is_array($entry["summary"]) ? $entry["summary"] : $entry["summary"]["_content"];
} else {
$description = "";
}
$atomArray["item"][] = array(
"title" => !is_array($entry["title"]) ? $entry["title"] : $entry["title"]["_content"],
"link" => $entry["link"]["href"],
"id" => $entry["id"],
"pubdate" => !empty($entry["updated"]) ? $entry["updated"] : (!empty($entry["modified"]) ? $entry["modified"] : ""),
"description" => $description
) ;
}
$xmlArray = $atomArray;
}
// 「作成」をセット
if (empty($xmlArray["channel"]["generator"])) {
if (!empty($xmlArray["channel"]["dc:creator"])) {
$xmlArray["channel"]["generator"] = $xmlArray["channel"]["dc:creator"];
}
}
// 「最終更新時刻」をセット
if (empty($xmlArray["channel"]["lastbuilddate"])) {
if (!empty($xmlArray["channel"]["dc:date"])) {
$xmlArray["channel"]["lastbuilddate"] = $xmlArray["channel"]["dc:date"];
}
}
//itemがchannelの内にある場合とchannelの外にある場合がある
if (isset($xmlArray["channel"]["item"])) {
$xmlArray["item"] = $xmlArray["channel"]["item"];
}
$pubdateNone = false;
$itemsExtracted = array();
if (empty($xmlArray["item"])) { /* item が0の場合 */
$item_count = 0;
return $xmlArray;
} else if (!empty($xmlArray["item"]["title"])) { /* item が1つの場合 */
$items["0"] = $xmlArray["item"];
$item_count = 1;
} else { /* item が2つ以上の場合 */
$items = $xmlArray["item"];
$item_count = count($items);
}
for ($key=0; $key<$item_count; $key=$key+1) {
if (empty($items[$key]["pubdate"])) {
if (empty($items[$key]["dc:date"])) {
if (empty($items[$key]["pubDate"])) {
$items[$key]["pubdate"] = "" ;
$pubdateNone = true ;
} else {
$items[$key]["pubdate"] = $items[$key]["pubDate"];
}
} else {
$items[$key]["pubdate"] = $items[$key]["dc:date"];
}
}
array_walk($items[$key], array($this, "stripTags"));
if (!empty($items[$key]["description"])) {
$items[$key]["description"] = nl2br(preg_replace('/(\n{2,})/s', "\n", $items[$key]["description"]));
}
$items[$key]["link"] = str_replace('"', """, $items[$key]["link"]);
$itemsExtracted[] = $items[$key];
}
if (!$pubdateNone) {
//usort($itemsExtracted, create_function('$a,$b', 'return $a["pubdate"] < $b["pubdate"] ? 1 : -1 ;'));
}
$xmlArray["item"] = $itemsExtracted;
return $xmlArray;
}
/**
* Sets error messages
*
* @param $error string an error message
*/
function setErrors($error)
{
$this->errors[] = trim($error);
}
/**
* Gets all the error messages
*
* @param $ashtml bool return as html?
* @return mixed エラー配列 or エラー文字列
*/
function &getErrors($ashtml = true)
{
if (!$ashtml) {
return $this->errors;
} else {
$ret = "";
foreach ($this->errors as $error) {
$ret .= $error. "<br />\n";
}
return $ret;
}
}
/**
* HTMLタグを取り除く
*
* @param $value mixed 取り除く値
*/
function stripTags(&$value)
{
if (!is_string($value)) return;
$value = strip_tags($value, "<br>");
}
}
?>